blob: 43f45e366606ecbbbee378ba248f00b9bfcbaa40 [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;
Marius Vladc114fd62021-08-26 16:37:24 +0300191
192 struct wl_list link; /** shell::seat_list */
Giulio Camuffo5085a752013-03-25 21:42:45 +0100193};
194
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800195
Alex Wubd3354b2012-04-17 17:20:49 +0800196static struct desktop_shell *
197shell_surface_get_shell(struct shell_surface *shsurf);
198
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500199static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200200set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
201
202static void
Derek Foreman74de4692015-07-15 13:00:39 -0500203surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500204
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300205static void
206shell_fade_startup(struct desktop_shell *shell);
207
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700208static void
209shell_fade(struct desktop_shell *shell, enum fade_type type);
210
Philip Withnallbecb77e2013-11-25 18:01:30 +0000211static struct shell_seat *
212get_shell_seat(struct weston_seat *seat);
213
Jonny Lambd73c6942014-08-20 15:53:20 +0200214static void
215get_output_panel_size(struct desktop_shell *shell,
216 struct weston_output *output,
217 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700218
Philip Withnall648a4dd2013-11-25 18:01:44 +0000219static void
220shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
221
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222static int
223shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
224{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300225 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200226 struct weston_desktop_surface *desktop_surface =
227 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300228
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200229 t = weston_desktop_surface_get_title(desktop_surface);
230 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300231
232 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200233 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300234 t ? " '" : "", t ?: "", t ? "'" : "",
235 c ? " of " : "", c ?: "");
236}
237
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500238static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400239destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300240{
241 struct shell_grab *grab;
242
243 grab = container_of(listener, struct shell_grab,
244 shsurf_destroy_listener);
245
246 grab->shsurf = NULL;
247}
248
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800249struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500250get_default_view(struct weston_surface *surface)
251{
252 struct shell_surface *shsurf;
253 struct weston_view *view;
254
255 if (!surface || wl_list_empty(&surface->views))
256 return NULL;
257
258 shsurf = get_shell_surface(surface);
259 if (shsurf)
260 return shsurf->view;
261
262 wl_list_for_each(view, &surface->views, surface_link)
263 if (weston_view_is_mapped(view))
264 return view;
265
266 return container_of(surface->views.next, struct weston_view, surface_link);
267}
268
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400271 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400273 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800274 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300275{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300276 struct desktop_shell *shell = shsurf->shell;
277
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200278 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400279
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280 grab->grab.interface = interface;
281 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400282 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500283 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400284 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300285
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700286 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400287 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800289 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400290 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 weston_pointer_set_focus(pointer,
292 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400293 wl_fixed_from_int(0),
294 wl_fixed_from_int(0));
295 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296}
297
Jonny Lambd73c6942014-08-20 15:53:20 +0200298static void
Quentin Glidic581df062016-06-23 18:55:19 +0200299get_panel_size(struct desktop_shell *shell,
300 struct weston_view *view,
301 int *width,
302 int *height)
303{
304 float x1, y1;
305 float x2, y2;
306 weston_view_to_global_float(view, 0, 0, &x1, &y1);
307 weston_view_to_global_float(view,
308 view->surface->width,
309 view->surface->height,
310 &x2, &y2);
311 *width = (int)(x2 - x1);
312 *height = (int)(y2 - y1);
313}
314
315static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200316get_output_panel_size(struct desktop_shell *shell,
317 struct weston_output *output,
318 int *width,
319 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700320{
321 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200322
323 *width = 0;
324 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700325
326 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200327 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700328
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300329 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200330 if (view->surface->output == output) {
331 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200332 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700333 }
334 }
335
Jonny Lambd73c6942014-08-20 15:53:20 +0200336 /* the correct view wasn't found */
337}
338
Leandro Ribeiro0c59e922020-01-24 17:53:44 -0300339void
Quentin Glidicfff39812016-08-15 10:56:52 +0200340get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200341 struct weston_output *output,
342 pixman_rectangle32_t *area)
343{
344 int32_t panel_width = 0, panel_height = 0;
345
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200346 if (!output) {
347 area->x = 0;
348 area->y = 0;
349 area->width = 0;
350 area->height = 0;
351
352 return;
353 }
354
Derek Foremanf814c5d2015-04-15 12:23:54 -0500355 area->x = output->x;
356 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200357
358 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200359 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800360 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200361 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500362 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000363 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800364 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200365 area->width = output->width;
366 area->height = output->height - panel_height;
367 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800368 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500369 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000370 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800371 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200372 area->width = output->width - panel_width;
373 area->height = output->height;
374 break;
375 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700376}
377
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400378static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300379shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300380{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700381 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400382 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700383 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400384
385 if (grab->shsurf->resize_edges) {
386 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400387 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700388 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300389
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700390 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391}
392
393static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700394shell_touch_grab_start(struct shell_touch_grab *grab,
395 const struct weston_touch_grab_interface *interface,
396 struct shell_surface *shsurf,
397 struct weston_touch *touch)
398{
399 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800400
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200401 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700402
Rusty Lynch1084da52013-08-15 09:10:08 -0700403 grab->grab.interface = interface;
404 grab->shsurf = shsurf;
405 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
406 wl_signal_add(&shsurf->destroy_signal,
407 &grab->shsurf_destroy_listener);
408
409 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700410 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700411
412 weston_touch_start_grab(touch, &grab->grab);
413 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500414 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700416}
417
418static void
419shell_touch_grab_end(struct shell_touch_grab *grab)
420{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700421 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700422 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700423 grab->shsurf->grabbed = 0;
424 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700425
426 weston_touch_end_grab(grab->touch);
427}
428
429static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500430center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800431 struct weston_output *output);
432
Daniel Stone496ca172012-05-30 16:31:42 +0100433static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300434get_modifier(char *modifier)
435{
436 if (!modifier)
437 return MODIFIER_SUPER;
438
439 if (!strcmp("ctrl", modifier))
440 return MODIFIER_CTRL;
441 else if (!strcmp("alt", modifier))
442 return MODIFIER_ALT;
443 else if (!strcmp("super", modifier))
444 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000445 else if (!strcmp("none", modifier))
446 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300447 else
448 return MODIFIER_SUPER;
449}
450
Juan Zhaoe10d2792012-04-25 19:09:52 +0800451static enum animation_type
452get_animation_type(char *animation)
453{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800454 if (!animation)
455 return ANIMATION_NONE;
456
Juan Zhaoe10d2792012-04-25 19:09:52 +0800457 if (!strcmp("zoom", animation))
458 return ANIMATION_ZOOM;
459 else if (!strcmp("fade", animation))
460 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100461 else if (!strcmp("dim-layer", animation))
462 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800463 else
464 return ANIMATION_NONE;
465}
466
Alex Wu4539b082012-03-01 12:57:46 +0800467static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400468shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200469{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500471 char *s, *client;
Daniel Stone51d995a2019-11-26 00:14:24 +0000472 bool allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200473
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300474 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400475 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200476 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000477 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500478 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100479 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000480
481 weston_config_section_get_bool(section,
482 "allow-zap", &allow_zap, true);
483 shell->allow_zap = allow_zap;
484
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100485 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400486 "binding-modifier", &s, "super");
487 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200488 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800489
490 weston_config_section_get_string(section,
491 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000492 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800493 free(s);
494
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400495 weston_config_section_get_string(section, "animation", &s, "none");
496 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200497 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200498 weston_config_section_get_string(section, "close-animation", &s, "fade");
499 shell->win_close_animation_type = get_animation_type(s);
500 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700501 weston_config_section_get_string(section,
502 "startup-animation", &s, "fade");
503 shell->startup_animation_type = get_animation_type(s);
504 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700505 if (shell->startup_animation_type == ANIMATION_ZOOM)
506 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100507 weston_config_section_get_string(section, "focus-animation", &s, "none");
508 shell->focus_animation_type = get_animation_type(s);
509 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400510 weston_config_section_get_uint(section, "num-workspaces",
511 &shell->workspaces.num,
512 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200513}
514
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800515struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516get_default_output(struct weston_compositor *compositor)
517{
Armin Krezović10b06182016-06-23 11:59:30 +0200518 if (wl_list_empty(&compositor->output_list))
519 return NULL;
520
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100521 return container_of(compositor->output_list.next,
522 struct weston_output, link);
523}
524
Pekka Paalanen8274d902014-08-06 19:36:51 +0300525static int
526focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
527{
528 return snprintf(buf, len, "focus highlight effect for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -0700529 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +0300530}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100531
532/* no-op func for checking focus surface */
533static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200534focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535{
536}
537
538static struct focus_surface *
539get_focus_surface(struct weston_surface *surface)
540{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200541 if (surface->committed == focus_surface_committed)
542 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100543 else
544 return NULL;
545}
546
547static bool
548is_focus_surface (struct weston_surface *es)
549{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200550 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100551}
552
553static bool
554is_focus_view (struct weston_view *view)
555{
556 return is_focus_surface (view->surface);
557}
558
559static struct focus_surface *
560create_focus_surface(struct weston_compositor *ec,
561 struct weston_output *output)
562{
563 struct focus_surface *fsurf = NULL;
564 struct weston_surface *surface = NULL;
565
566 fsurf = malloc(sizeof *fsurf);
567 if (!fsurf)
568 return NULL;
569
570 fsurf->surface = weston_surface_create(ec);
571 surface = fsurf->surface;
572 if (surface == NULL) {
573 free(fsurf);
574 return NULL;
575 }
576
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200577 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100578 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200579 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200580 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300581 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100582
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800583 fsurf->view = weston_view_create(surface);
584 if (fsurf->view == NULL) {
585 weston_surface_destroy(surface);
586 free(fsurf);
587 return NULL;
588 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200589 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200590 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100591
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600592 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600593 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100594 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
595 pixman_region32_fini(&surface->opaque);
596 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
597 output->width, output->height);
598 pixman_region32_fini(&surface->input);
599 pixman_region32_init(&surface->input);
600
601 wl_list_init(&fsurf->workspace_transform.link);
602
603 return fsurf;
604}
605
606static void
607focus_surface_destroy(struct focus_surface *fsurf)
608{
609 weston_surface_destroy(fsurf->surface);
610 free(fsurf);
611}
612
613static void
614focus_animation_done(struct weston_view_animation *animation, void *data)
615{
616 struct workspace *ws = data;
617
618 ws->focus_animation = NULL;
619}
620
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200621static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200622focus_state_destroy(struct focus_state *state)
623{
624 wl_list_remove(&state->seat_destroy_listener.link);
625 wl_list_remove(&state->surface_destroy_listener.link);
626 free(state);
627}
628
629static void
630focus_state_seat_destroy(struct wl_listener *listener, void *data)
631{
632 struct focus_state *state = container_of(listener,
633 struct focus_state,
634 seat_destroy_listener);
635
636 wl_list_remove(&state->link);
637 focus_state_destroy(state);
638}
639
640static void
641focus_state_surface_destroy(struct wl_listener *listener, void *data)
642{
643 struct focus_state *state = container_of(listener,
644 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400645 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200646 struct weston_surface *main_surface;
647 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649
Pekka Paalanen01388e22013-04-25 13:57:44 +0300650 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
651
Kristian Høgsberge3778222012-07-31 17:29:30 -0400652 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300653 wl_list_for_each(view,
654 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500655 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100657 if (is_focus_view(view))
658 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200659 if (!get_shell_surface(view->surface))
660 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400661
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200662 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400663 break;
664 }
665
Pekka Paalanen01388e22013-04-25 13:57:44 +0300666 /* if the focus was a sub-surface, activate its main surface */
667 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200668 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300669
Kristian Høgsberge3778222012-07-31 17:29:30 -0400670 if (next) {
Greg V15d3d302018-12-07 01:33:34 +0300671 if (state->keyboard_focus) {
672 wl_list_remove(&state->surface_destroy_listener.link);
673 wl_list_init(&state->surface_destroy_listener.link);
674 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100675 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200676 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200677 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400678 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200679 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680 if (state->ws->focus_animation)
681 weston_view_animation_destroy(state->ws->focus_animation);
682
683 state->ws->focus_animation = weston_fade_run(
684 state->ws->fsurf_front->view,
685 state->ws->fsurf_front->view->alpha, 0.0, 300,
686 focus_animation_done, state->ws);
687 }
688
Kristian Høgsberge3778222012-07-31 17:29:30 -0400689 wl_list_remove(&state->link);
690 focus_state_destroy(state);
691 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200692}
693
694static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200695focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
696 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200697{
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200699
700 state = malloc(sizeof *state);
701 if (state == NULL)
702 return NULL;
703
Quentin Glidicfff39812016-08-15 10:56:52 +0200704 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400706 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200707 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400708 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200709
710 state->seat_destroy_listener.notify = focus_state_seat_destroy;
711 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400712 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200713 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400714 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
716 return state;
717}
718
Jonas Ådahl8538b222012-08-29 22:13:03 +0200719static struct focus_state *
720ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
721{
722 struct workspace *ws = get_current_workspace(shell);
723 struct focus_state *state;
724
725 wl_list_for_each(state, &ws->focus_list, link)
726 if (state->seat == seat)
727 break;
728
729 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200730 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200731
732 return state;
733}
734
Jonas Ådahl04769742012-06-13 00:01:24 +0200735static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800736focus_state_set_focus(struct focus_state *state,
737 struct weston_surface *surface)
738{
739 if (state->keyboard_focus) {
740 wl_list_remove(&state->surface_destroy_listener.link);
741 wl_list_init(&state->surface_destroy_listener.link);
742 }
743
744 state->keyboard_focus = surface;
745 if (surface)
746 wl_signal_add(&surface->destroy_signal,
747 &state->surface_destroy_listener);
748}
749
750static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400751restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200752{
753 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400754 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100755 struct wl_list pending_seat_list;
756 struct weston_seat *seat, *next_seat;
757
758 /* Temporarily steal the list of seats so that we can keep
759 * track of the seats we've already processed */
760 wl_list_init(&pending_seat_list);
761 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
762 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200763
764 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500765 struct weston_keyboard *keyboard =
766 weston_seat_get_keyboard(state->seat);
767
Neil Roberts4237f502014-04-09 16:33:31 +0100768 wl_list_remove(&state->seat->link);
769 wl_list_insert(&shell->compositor->seat_list,
770 &state->seat->link);
771
Derek Foreman1281a362015-07-31 16:55:32 -0500772 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800773 continue;
774
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400775 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200778 }
Neil Roberts4237f502014-04-09 16:33:31 +0100779
780 /* For any remaining seats that we don't have a focus state
781 * for we'll reset the keyboard focus to NULL */
782 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500783 struct weston_keyboard *keyboard =
784 weston_seat_get_keyboard(seat);
785
Neil Roberts4237f502014-04-09 16:33:31 +0100786 wl_list_insert(&shell->compositor->seat_list, &seat->link);
787
Derek Foreman1281a362015-07-31 16:55:32 -0500788 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100789 continue;
790
Derek Foreman1281a362015-07-31 16:55:32 -0500791 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100792 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200793}
794
795static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
797 struct weston_seat *seat)
798{
Derek Foreman1281a362015-07-31 16:55:32 -0500799 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200800 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200801
802 wl_list_for_each(state, &ws->focus_list, link) {
803 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500804 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805 return;
806 }
807 }
808}
809
810static void
811drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
812 struct weston_surface *surface)
813{
814 struct focus_state *state;
815
816 wl_list_for_each(state, &ws->focus_list, link)
817 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800818 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819}
820
821static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100822animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
823 struct weston_view *from, struct weston_view *to)
824{
825 struct weston_output *output;
826 bool focus_surface_created = false;
827
828 /* FIXME: Only support dim animation using two layers */
829 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
830 return;
831
832 output = get_default_output(shell->compositor);
833 if (ws->fsurf_front == NULL && (from || to)) {
834 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800835 if (ws->fsurf_front == NULL)
836 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800838
839 ws->fsurf_back = create_focus_surface(shell->compositor, output);
840 if (ws->fsurf_back == NULL) {
841 focus_surface_destroy(ws->fsurf_front);
842 return;
843 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800845
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 focus_surface_created = true;
847 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
849 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 }
851
852 if (ws->focus_animation) {
853 weston_view_animation_destroy(ws->focus_animation);
854 ws->focus_animation = NULL;
855 }
856
857 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300858 weston_layer_entry_insert(&to->layer_link,
859 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&ws->layer.view_list,
862 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863
864 if (focus_surface_created) {
865 ws->focus_animation = weston_fade_run(
866 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200867 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 focus_animation_done, ws);
869 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300870 weston_layer_entry_insert(&from->layer_link,
871 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 ws->focus_animation = weston_stable_fade_run(
873 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200874 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100875 focus_animation_done, ws);
876 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300877 weston_layer_entry_insert(&ws->layer.view_list,
878 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100879 ws->focus_animation = weston_stable_fade_run(
880 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200881 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 focus_animation_done, ws);
883 }
884}
885
886static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887workspace_destroy(struct workspace *ws)
888{
Jonas Ådahl04769742012-06-13 00:01:24 +0200889 struct focus_state *state, *next;
890
891 wl_list_for_each_safe(state, next, &ws->focus_list, link)
892 focus_state_destroy(state);
893
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100894 if (ws->fsurf_front)
895 focus_surface_destroy(ws->fsurf_front);
896 if (ws->fsurf_back)
897 focus_surface_destroy(ws->fsurf_back);
898
Pekka Paalanen4bb326b2021-05-14 14:37:46 +0300899 weston_layer_fini(&ws->layer);
900
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200901 free(ws);
902}
903
Jonas Ådahl04769742012-06-13 00:01:24 +0200904static void
905seat_destroyed(struct wl_listener *listener, void *data)
906{
907 struct weston_seat *seat = data;
908 struct focus_state *state, *next;
909 struct workspace *ws = container_of(listener,
910 struct workspace,
911 seat_destroyed_listener);
912
913 wl_list_for_each_safe(state, next, &ws->focus_list, link)
914 if (state->seat == seat)
915 wl_list_remove(&state->link);
916}
917
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100919workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200920{
921 struct workspace *ws = malloc(sizeof *ws);
922 if (ws == NULL)
923 return NULL;
924
Quentin Glidic82681572016-12-17 13:40:51 +0100925 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200926
Jonas Ådahl04769742012-06-13 00:01:24 +0200927 wl_list_init(&ws->focus_list);
928 wl_list_init(&ws->seat_destroyed_listener.link);
929 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100930 ws->fsurf_front = NULL;
931 ws->fsurf_back = NULL;
932 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200933
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200934 return ws;
935}
936
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937static int
938workspace_is_empty(struct workspace *ws)
939{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300940 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941}
942
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200943static struct workspace *
944get_workspace(struct desktop_shell *shell, unsigned int index)
945{
946 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200947 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200948 pws += index;
949 return *pws;
950}
951
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800952struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200953get_current_workspace(struct desktop_shell *shell)
954{
955 return get_workspace(shell, shell->workspaces.current);
956}
957
958static void
959activate_workspace(struct desktop_shell *shell, unsigned int index)
960{
961 struct workspace *ws;
962
963 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100964 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200965
966 shell->workspaces.current = index;
967}
968
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200969static unsigned int
970get_output_height(struct weston_output *output)
971{
972 return abs(output->region.extents.y1 - output->region.extents.y2);
973}
974
Greg Vf57774e2018-07-24 23:21:55 +0300975static struct weston_transform *
976view_get_transform(struct weston_view *view)
977{
978 struct focus_surface *fsurf = NULL;
979 struct shell_surface *shsurf = NULL;
980
981 if (is_focus_view(view)) {
982 fsurf = get_focus_surface(view->surface);
983 return &fsurf->workspace_transform;
984 }
985
986 shsurf = get_shell_surface(view->surface);
987 if (shsurf)
988 return &shsurf->workspace_transform;
989
990 return NULL;
991}
992
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995{
Greg Vf57774e2018-07-24 23:21:55 +0300996 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997
Greg Vf57774e2018-07-24 23:21:55 +0300998 if (!transform)
999 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 weston_matrix_init(&transform->matrix);
1006 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009}
1010
1011static void
1012workspace_translate_out(struct workspace *ws, double fraction)
1013{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 unsigned int height;
1016 double d;
1017
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001018 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020 d = height * fraction;
1021
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001022 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001023 }
1024}
1025
1026static void
1027workspace_translate_in(struct workspace *ws, double fraction)
1028{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030 unsigned int height;
1031 double d;
1032
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001033 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035
1036 if (fraction > 0)
1037 d = -(height - height * fraction);
1038 else
1039 d = height + height * fraction;
1040
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001041 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001042 }
1043}
1044
1045static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046reverse_workspace_change_animation(struct desktop_shell *shell,
1047 unsigned int index,
1048 struct workspace *from,
1049 struct workspace *to)
1050{
1051 shell->workspaces.current = index;
1052
1053 shell->workspaces.anim_to = to;
1054 shell->workspaces.anim_from = from;
1055 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001056 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057
Quentin Glidic82681572016-12-17 13:40:51 +01001058 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1059 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1060
Scott Moreau4272e632012-08-13 09:58:41 -06001061 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062}
1063
1064static void
1065workspace_deactivate_transforms(struct workspace *ws)
1066{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001068 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001070 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001071 transform = view_get_transform(view);
1072 if (!transform)
1073 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001074
1075 if (!wl_list_empty(&transform->link)) {
1076 wl_list_remove(&transform->link);
1077 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001078 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080 }
1081}
1082
1083static void
1084finish_workspace_change_animation(struct desktop_shell *shell,
1085 struct workspace *from,
1086 struct workspace *to)
1087{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001088 struct weston_view *view;
1089
Scott Moreau4272e632012-08-13 09:58:41 -06001090 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001092 /* Views that extend past the bottom of the output are still
1093 * visible after the workspace animation ends but before its layer
1094 * is hidden. In that case, we need to damage below those views so
1095 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001096 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001097 weston_view_damage_below(view);
1098
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099 wl_list_remove(&shell->workspaces.animation.link);
1100 workspace_deactivate_transforms(from);
1101 workspace_deactivate_transforms(to);
1102 shell->workspaces.anim_to = NULL;
1103
Quentin Glidic82681572016-12-17 13:40:51 +01001104 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105}
1106
1107static void
1108animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001109 struct weston_output *output,
1110 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111{
1112 struct desktop_shell *shell =
1113 container_of(animation, struct desktop_shell,
1114 workspaces.animation);
1115 struct workspace *from = shell->workspaces.anim_from;
1116 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001117 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001118 double x, y;
1119
1120 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1121 finish_workspace_change_animation(shell, from, to);
1122 return;
1123 }
1124
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001125 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001127 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001129 timespec_add_msec(&shell->workspaces.anim_timestamp,
1130 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001131 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001132 -(asin(1.0 - shell->workspaces.anim_current) *
1133 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1134 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135 }
1136
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001137 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001138
1139 /*
1140 * x = [0, π/2]
1141 * y(x) = sin(x)
1142 */
1143 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1144 y = sin(x);
1145
1146 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001147 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148
1149 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1150 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1151 shell->workspaces.anim_current = y;
1152
Scott Moreau4272e632012-08-13 09:58:41 -06001153 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001155 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001156 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157}
1158
1159static void
1160animate_workspace_change(struct desktop_shell *shell,
1161 unsigned int index,
1162 struct workspace *from,
1163 struct workspace *to)
1164{
1165 struct weston_output *output;
1166
1167 int dir;
1168
1169 if (index > shell->workspaces.current)
1170 dir = -1;
1171 else
1172 dir = 1;
1173
1174 shell->workspaces.current = index;
1175
1176 shell->workspaces.anim_dir = dir;
1177 shell->workspaces.anim_from = from;
1178 shell->workspaces.anim_to = to;
1179 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001180 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181
1182 output = container_of(shell->compositor->output_list.next,
1183 struct weston_output, link);
1184 wl_list_insert(&output->animation_list,
1185 &shell->workspaces.animation.link);
1186
Quentin Glidic82681572016-12-17 13:40:51 +01001187 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1188 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001189
1190 workspace_translate_in(to, 0);
1191
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001192 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001193
Scott Moreau4272e632012-08-13 09:58:41 -06001194 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195}
1196
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001197static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198update_workspace(struct desktop_shell *shell, unsigned int index,
1199 struct workspace *from, struct workspace *to)
1200{
1201 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001202 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1203 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204}
1205
1206static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207change_workspace(struct desktop_shell *shell, unsigned int index)
1208{
1209 struct workspace *from;
1210 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001211 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001212
1213 if (index == shell->workspaces.current)
1214 return;
1215
1216 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001217 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218 return;
1219
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001220 from = get_current_workspace(shell);
1221 to = get_workspace(shell, index);
1222
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001223 if (shell->workspaces.anim_from == to &&
1224 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001225 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001226 reverse_workspace_change_animation(shell, index, from, to);
1227 return;
1228 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001229
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001230 if (shell->workspaces.anim_to != NULL)
1231 finish_workspace_change_animation(shell,
1232 shell->workspaces.anim_from,
1233 shell->workspaces.anim_to);
1234
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001235 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001236
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001237 if (shell->focus_animation_type != ANIMATION_NONE) {
1238 wl_list_for_each(state, &from->focus_list, link)
1239 if (state->keyboard_focus)
1240 animate_focus_change(shell, from,
1241 get_default_view(state->keyboard_focus), NULL);
1242
1243 wl_list_for_each(state, &to->focus_list, link)
1244 if (state->keyboard_focus)
1245 animate_focus_change(shell, to,
1246 NULL, get_default_view(state->keyboard_focus));
1247 }
1248
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249 if (workspace_is_empty(to) && workspace_is_empty(from))
1250 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001251 else
1252 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001253}
1254
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255static bool
1256workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1257{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001258 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001259 struct wl_list *e;
1260
1261 if (wl_list_empty(list))
1262 return false;
1263
1264 e = list->next;
1265
1266 if (e->next != list)
1267 return false;
1268
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001269 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270}
1271
1272static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001273surface_keyboard_focus_lost(struct weston_surface *surface)
1274{
1275 struct weston_compositor *compositor = surface->compositor;
1276 struct weston_seat *seat;
1277 struct weston_surface *focus;
1278
1279 wl_list_for_each(seat, &compositor->seat_list, link) {
1280 struct weston_keyboard *keyboard =
1281 weston_seat_get_keyboard(seat);
1282
1283 if (!keyboard)
1284 continue;
1285
1286 focus = weston_surface_get_main_surface(keyboard->focus);
1287 if (focus == surface)
1288 weston_keyboard_set_focus(keyboard, NULL);
1289 }
1290}
1291
1292static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001294 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 unsigned int index)
1296{
Derek Foreman1281a362015-07-31 16:55:32 -05001297 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001298 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 struct shell_surface *shsurf;
1301 struct workspace *from;
1302 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001303 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304
Derek Foreman1281a362015-07-31 16:55:32 -05001305 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 view = get_default_view(surface);
1307 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001308 index == shell->workspaces.current ||
1309 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001310 return;
1311
1312 from = get_current_workspace(shell);
1313 to = get_workspace(shell, index);
1314
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001315 weston_layer_entry_remove(&view->layer_link);
1316 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001317
Philip Withnall659163d2013-11-25 18:01:36 +00001318 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001319 if (shsurf != NULL)
1320 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001321
Jonas Ådahle9d22502012-08-29 22:13:01 +02001322 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001323 drop_focus_state(shell, from, surface);
1324
1325 if (shell->workspaces.anim_from == to &&
1326 shell->workspaces.anim_to == from) {
1327 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001329 return;
1330 }
1331
1332 if (shell->workspaces.anim_to != NULL)
1333 finish_workspace_change_animation(shell,
1334 shell->workspaces.anim_from,
1335 shell->workspaces.anim_to);
1336
1337 if (workspace_is_empty(from) &&
1338 workspace_has_only(to, surface))
1339 update_workspace(shell, index, from, to);
1340 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001341 if (shsurf != NULL &&
1342 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001343 wl_list_insert(&shell->workspaces.anim_sticky_list,
1344 &shsurf->workspace_transform.link);
1345
1346 animate_workspace_change(shell, index, from, to);
1347 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001348
Jonas Ådahl8538b222012-08-29 22:13:03 +02001349 state = ensure_focus_state(shell, seat);
1350 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001351 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352}
1353
1354static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001355touch_move_grab_down(struct weston_touch_grab *grab,
1356 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001357 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001358{
1359}
1360
1361static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001362touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1363 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001364{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001365 struct weston_touch_move_grab *move =
1366 (struct weston_touch_move_grab *) container_of(
1367 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001368
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001369 if (touch_id == 0)
1370 move->active = 0;
1371
Jonas Ådahl9484b692013-12-02 22:05:03 +01001372 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001373 shell_touch_grab_end(&move->base);
1374 free(move);
1375 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001376}
1377
1378static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001379touch_move_grab_motion(struct weston_touch_grab *grab,
1380 const struct timespec *time, int touch_id,
1381 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001382{
1383 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1384 struct shell_surface *shsurf = move->base.shsurf;
1385 struct weston_surface *es;
1386 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1387 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1388
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001389 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001390 return;
1391
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001392 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001393
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001394 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001395
1396 weston_compositor_schedule_repaint(es->compositor);
1397}
1398
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001399static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001400touch_move_grab_frame(struct weston_touch_grab *grab)
1401{
1402}
1403
1404static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001405touch_move_grab_cancel(struct weston_touch_grab *grab)
1406{
1407 struct weston_touch_move_grab *move =
1408 (struct weston_touch_move_grab *) container_of(
1409 grab, struct shell_touch_grab, grab);
1410
1411 shell_touch_grab_end(&move->base);
1412 free(move);
1413}
1414
Rusty Lynch1084da52013-08-15 09:10:08 -07001415static const struct weston_touch_grab_interface touch_move_grab_interface = {
1416 touch_move_grab_down,
1417 touch_move_grab_up,
1418 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001419 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001420 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001421};
1422
1423static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001424surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001425{
1426 struct weston_touch_move_grab *move;
1427
1428 if (!shsurf)
1429 return -1;
1430
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001431 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1432 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001433 return 0;
1434
1435 move = malloc(sizeof *move);
1436 if (!move)
1437 return -1;
1438
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001439 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001441 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001443 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001444
1445 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001446 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001447
1448 return 0;
1449}
1450
1451static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001452noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001453{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001454}
1455
1456static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001457noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001458 const struct timespec *time,
1459 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001460{
1461}
1462
1463static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001464noop_grab_axis_source(struct weston_pointer_grab *grab,
1465 uint32_t source)
1466{
1467}
1468
1469static void
1470noop_grab_frame(struct weston_pointer_grab *grab)
1471{
1472}
1473
1474static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1476{
1477 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001478 struct weston_surface *surface =
1479 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001480 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001481 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001482 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001483 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001485
1486 x = wl_fixed_to_int(pointer->x + move->dx);
1487 y = wl_fixed_to_int(pointer->y + move->dy);
1488
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001489 if (shsurf->shell->panel_position ==
1490 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001491 get_output_work_area(shsurf->shell, surface->output, &area);
1492 geometry =
1493 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001494
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001495 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001496 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001497 y = area.y + safety - geometry.height
1498 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001499
1500 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001501 y + geometry.y < area.y)
1502 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001503 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001504
1505 *cx = x;
1506 *cy = y;
1507}
1508
1509static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001510move_grab_motion(struct weston_pointer_grab *grab,
1511 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001512 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001515 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001517 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001518 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001519
Jonas Ådahld2510102014-10-05 21:39:14 +02001520 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001521 if (!shsurf)
1522 return;
1523
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001524 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1525
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001526 constrain_position(move, &cx, &cy);
1527
1528 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001530 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531}
1532
1533static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001534move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001535 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001537 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1538 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001539 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001540 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001541
Daniel Stone4dbadb12012-05-30 16:31:51 +01001542 if (pointer->button_count == 0 &&
1543 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001544 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001545 free(grab);
1546 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001547}
1548
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001549static void
1550move_grab_cancel(struct weston_pointer_grab *grab)
1551{
1552 struct shell_grab *shell_grab =
1553 container_of(grab, struct shell_grab, grab);
1554
1555 shell_grab_end(shell_grab);
1556 free(grab);
1557}
1558
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001559static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001560 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001561 move_grab_motion,
1562 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001563 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001564 noop_grab_axis_source,
1565 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001566 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001567};
1568
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001570surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001571 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572{
1573 struct weston_move_grab *move;
1574
1575 if (!shsurf)
1576 return -1;
1577
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001578 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001579 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1580 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 return 0;
1582
1583 move = malloc(sizeof *move);
1584 if (!move)
1585 return -1;
1586
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001588 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001590 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001591 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592
1593 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001594 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595
1596 return 0;
1597}
1598
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599struct weston_resize_grab {
1600 struct shell_grab base;
1601 uint32_t edges;
1602 int32_t width, height;
1603};
1604
1605static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001606resize_grab_motion(struct weston_pointer_grab *grab,
1607 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001608 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001609{
1610 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001611 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612 struct shell_surface *shsurf = resize->base.shsurf;
1613 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001614 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001615 wl_fixed_t from_x, from_y;
1616 wl_fixed_t to_x, to_y;
1617
Jonas Ådahld2510102014-10-05 21:39:14 +02001618 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001619
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001620 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 return;
1622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 weston_view_from_global_fixed(shsurf->view,
1624 pointer->grab_x, pointer->grab_y,
1625 &from_x, &from_y);
1626 weston_view_from_global_fixed(shsurf->view,
1627 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001628
1629 width = resize->width;
1630 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1631 width += wl_fixed_to_int(from_x - to_x);
1632 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1633 width += wl_fixed_to_int(to_x - from_x);
1634 }
1635
1636 height = resize->height;
1637 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1638 height += wl_fixed_to_int(from_y - to_y);
1639 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1640 height += wl_fixed_to_int(to_y - from_y);
1641 }
1642
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001643 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1644 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1645
1646 min_size.width = MAX(1, min_size.width);
1647 min_size.height = MAX(1, min_size.height);
1648
1649 if (width < min_size.width)
1650 width = min_size.width;
1651 else if (max_size.width > 0 && width > max_size.width)
1652 width = max_size.width;
1653 if (height < min_size.height)
1654 height = min_size.height;
1655 else if (max_size.width > 0 && width > max_size.width)
1656 width = max_size.width;
1657 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658}
1659
1660static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001661resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001662 const struct timespec *time,
1663 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664{
1665 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001666 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001667 enum wl_pointer_button_state state = state_w;
1668
1669 if (pointer->button_count == 0 &&
1670 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001671 if (resize->base.shsurf != NULL) {
1672 struct weston_desktop_surface *desktop_surface =
1673 resize->base.shsurf->desktop_surface;
1674 weston_desktop_surface_set_resizing(desktop_surface,
1675 false);
1676 }
1677
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678 shell_grab_end(&resize->base);
1679 free(grab);
1680 }
1681}
1682
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001683static void
1684resize_grab_cancel(struct weston_pointer_grab *grab)
1685{
1686 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1687
Greg Vec3f7792018-11-08 00:24:56 +03001688 if (resize->base.shsurf != NULL) {
1689 struct weston_desktop_surface *desktop_surface =
1690 resize->base.shsurf->desktop_surface;
1691 weston_desktop_surface_set_resizing(desktop_surface, false);
1692 }
1693
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001694 shell_grab_end(&resize->base);
1695 free(grab);
1696}
1697
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 noop_grab_focus,
1700 resize_grab_motion,
1701 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001702 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001703 noop_grab_axis_source,
1704 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001705 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706};
1707
Giulio Camuffob8366642013-04-25 13:57:46 +03001708/*
1709 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001710 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001711static void
1712surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1713 int32_t *y, int32_t *w, int32_t *h) {
1714 pixman_region32_t region;
1715 pixman_box32_t *box;
1716 struct weston_subsurface *subsurface;
1717
1718 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 surface->width,
1720 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001721
1722 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1723 pixman_region32_union_rect(&region, &region,
1724 subsurface->position.x,
1725 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 subsurface->surface->width,
1727 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001728 }
1729
1730 box = pixman_region32_extents(&region);
1731 if (x)
1732 *x = box->x1;
1733 if (y)
1734 *y = box->y1;
1735 if (w)
1736 *w = box->x2 - box->x1;
1737 if (h)
1738 *h = box->y2 - box->y1;
1739
1740 pixman_region32_fini(&region);
1741}
1742
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743static int
1744surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001745 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746{
1747 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001748 const unsigned resize_topbottom =
1749 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1750 const unsigned resize_leftright =
1751 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1752 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001753 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001755 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001756 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1757 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758 return 0;
1759
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001760 /* Check for invalid edge combinations. */
1761 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1762 (edges & resize_topbottom) == resize_topbottom ||
1763 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764 return 0;
1765
1766 resize = malloc(sizeof *resize);
1767 if (!resize)
1768 return -1;
1769
1770 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001772 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1773 resize->width = geometry.width;
1774 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001775
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001776 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001777 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001779 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001780
1781 return 0;
1782}
1783
1784static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001785busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001786{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001788 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001789 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001791 wl_fixed_t sx, sy;
1792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 view = weston_compositor_pick_view(pointer->seat->compositor,
1794 pointer->x, pointer->y,
1795 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001796 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001797
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001798 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001799 shell_grab_end(grab);
1800 free(grab);
1801 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001802}
1803
1804static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001805busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1806 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001807 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001808{
Jonas Ådahld2510102014-10-05 21:39:14 +02001809 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001813busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001814 const struct timespec *time,
1815 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001817 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001818 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001819 struct weston_pointer *pointer = grab->grab.pointer;
1820 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001821
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001822 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001823 activate(shsurf->shell, shsurf->view, seat,
1824 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001825 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001826 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001827 activate(shsurf->shell, shsurf->view, seat,
1828 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001829 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001830 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831}
1832
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001833static void
1834busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1835{
1836 struct shell_grab *grab = (struct shell_grab *) base;
1837
1838 shell_grab_end(grab);
1839 free(grab);
1840}
1841
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001842static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001843 busy_cursor_grab_focus,
1844 busy_cursor_grab_motion,
1845 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001846 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001847 noop_grab_axis_source,
1848 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001849 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001850};
1851
1852static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001853handle_pointer_focus(struct wl_listener *listener, void *data)
1854{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001855 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001857 struct shell_surface *shsurf;
1858 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861 return;
1862
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001863 shsurf = get_shell_surface(view->surface);
1864 if (!shsurf)
1865 return;
1866
1867 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1868
1869 if (shsurf->unresponsive)
1870 set_busy_cursor(shsurf, pointer);
1871 else
1872 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001873}
1874
1875static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001876shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1877{
1878 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001879 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001880}
1881
1882static void
1883shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1884{
1885 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001886 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001887}
1888
1889static void
1890handle_keyboard_focus(struct wl_listener *listener, void *data)
1891{
1892 struct weston_keyboard *keyboard = data;
1893 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1894
1895 if (seat->focused_surface) {
1896 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1897 if (shsurf)
1898 shell_surface_lose_keyboard_focus(shsurf);
1899 }
1900
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001901 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001902
1903 if (seat->focused_surface) {
1904 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1905 if (shsurf)
1906 shell_surface_gain_keyboard_focus(shsurf);
1907 }
1908}
1909
Philip Withnall07926d92013-11-25 18:01:40 +00001910/* The surface will be inserted into the list immediately after the link
1911 * returned by this function (i.e. will be stacked immediately above the
1912 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001913static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001914shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1915{
1916 struct workspace *ws;
1917
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001918 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1919 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001920 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001921 }
1922
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001923 /* Move the surface to a normal workspace layer so that surfaces
1924 * which were previously fullscreen or transient are no longer
1925 * rendered on top. */
1926 ws = get_current_workspace(shsurf->shell);
1927 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001928}
1929
Philip Withnall648a4dd2013-11-25 18:01:44 +00001930static void
1931shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1932{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001933 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001934}
1935
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001936/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001937 * geometry to ensure the changes are redrawn.
1938 *
1939 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1940 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001941static void
1942shell_surface_update_layer(struct shell_surface *shsurf)
1943{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001944 struct weston_surface *surface =
1945 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001946 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001947
1948 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1949
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001950 if (new_layer_link == NULL)
1951 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001952 if (new_layer_link == &shsurf->view->layer_link)
1953 return;
1954
1955 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001956 weston_layer_entry_remove(&shsurf->view->layer_link);
1957 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001958 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001959 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001960
1961 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001962}
1963
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001964static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001965notify_output_destroy(struct wl_listener *listener, void *data)
1966{
1967 struct shell_surface *shsurf =
1968 container_of(listener,
1969 struct shell_surface, output_destroy_listener);
1970
1971 shsurf->output = NULL;
1972 shsurf->output_destroy_listener.notify = NULL;
1973}
1974
1975static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001976shell_surface_set_output(struct shell_surface *shsurf,
1977 struct weston_output *output)
1978{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001979 struct weston_surface *es =
1980 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001981
1982 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001983 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001984 if (output)
1985 shsurf->output = output;
1986 else if (es->output)
1987 shsurf->output = es->output;
1988 else
1989 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001990
1991 if (shsurf->output_destroy_listener.notify) {
1992 wl_list_remove(&shsurf->output_destroy_listener.link);
1993 shsurf->output_destroy_listener.notify = NULL;
1994 }
1995
1996 if (!shsurf->output)
1997 return;
1998
1999 shsurf->output_destroy_listener.notify = notify_output_destroy;
2000 wl_signal_add(&shsurf->output->destroy_signal,
2001 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00002002}
2003
2004static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002005weston_view_set_initial_position(struct weston_view *view,
2006 struct desktop_shell *shell);
2007
2008static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002009unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002010{
Philip Withnallf85fe842013-11-25 18:01:37 +00002011 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08002012 wl_list_remove(&shsurf->fullscreen.transform.link);
2013 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002014
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 if (shsurf->fullscreen.black_view)
2016 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2017 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002018
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002019 if (shsurf->saved_position_valid)
2020 weston_view_set_position(shsurf->view,
2021 shsurf->saved_x, shsurf->saved_y);
2022 else
2023 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002024 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002025
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002026 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002028 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002029 shsurf->saved_rotation_valid = false;
2030 }
Alex Wu4539b082012-03-01 12:57:46 +08002031}
2032
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002033static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002034unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002035{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002036 struct weston_surface *surface =
2037 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2038
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002039 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002040 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002041
2042 if (shsurf->saved_position_valid)
2043 weston_view_set_position(shsurf->view,
2044 shsurf->saved_x, shsurf->saved_y);
2045 else
2046 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002047 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002048
2049 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 wl_list_insert(&shsurf->view->geometry.transformation_list,
2051 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052 shsurf->saved_rotation_valid = false;
2053 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002054}
2055
Philip Withnallbecb77e2013-11-25 18:01:30 +00002056static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002057set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002058{
2059 struct shell_surface *shsurf;
2060 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002061 struct weston_view *view;
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002062 struct weston_subsurface *subsurface;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002063
2064 view = get_default_view(surface);
2065 if (!view)
2066 return;
2067
2068 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2069
2070 shsurf = get_shell_surface(surface);
2071 current_ws = get_current_workspace(shsurf->shell);
2072
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002073 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002074 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002075
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002076 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002077 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002078
2079 shell_surface_update_child_surface_layers(shsurf);
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002080
2081 weston_view_damage_below(shsurf->view);
2082 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
2083 wl_list_for_each(view, &subsurface->surface->views, surface_link)
2084 weston_view_damage_below(view);
2085 }
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002086}
2087
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002088
Tiago Vignattibe143262012-04-16 17:31:41 +03002089static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002090shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002091{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002092 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002093}
2094
Pekka Paalanen8274d902014-08-06 19:36:51 +03002095static int
2096black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2097{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002098 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002099 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002100 int n;
2101 int rem;
2102 int ret;
2103
2104 n = snprintf(buf, len, "black background surface for ");
2105 if (n < 0)
2106 return n;
2107
2108 rem = (int)len - n;
2109 if (rem < 0)
2110 rem = 0;
2111
2112 if (fs_surface->get_label)
2113 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2114 else
2115 ret = snprintf(buf + n, rem, "<unknown>");
2116
2117 if (ret < 0)
2118 return n;
2119
2120 return n + ret;
2121}
2122
Alex Wu21858432012-04-01 20:13:08 +08002123static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002124black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002125
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002127create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002128 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002129 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002130{
2131 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002133
2134 surface = weston_surface_create(ec);
2135 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002136 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002137 return NULL;
2138 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139 view = weston_view_create(surface);
2140 if (surface == NULL) {
2141 weston_log("no memory\n");
2142 weston_surface_destroy(surface);
2143 return NULL;
2144 }
Alex Wu4539b082012-03-01 12:57:46 +08002145
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002146 surface->committed = black_surface_committed;
2147 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002148 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002149 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002150 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002151 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002152 pixman_region32_fini(&surface->input);
2153 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002154
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002155 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002156 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157
2158 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002159}
2160
Philip Withnalled8f1a92013-11-25 18:01:43 +00002161static void
2162shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2163{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002164 struct weston_surface *surface =
2165 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002166 struct weston_output *output = shsurf->fullscreen_output;
2167
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002168 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002169
2170 if (!shsurf->fullscreen.black_view)
2171 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002172 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002173 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002174 output->x, output->y,
2175 output->width,
2176 output->height);
2177
2178 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002179 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2180 weston_layer_entry_insert(&shsurf->view->layer_link,
2181 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002182 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002183 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002184
Armin Krezović4663aca2016-06-30 06:04:29 +02002185 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002186 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002187}
2188
Alex Wu4539b082012-03-01 12:57:46 +08002189/* Create black surface and append it to the associated fullscreen surface.
2190 * Handle size dismatch and positioning according to the method. */
2191static void
2192shell_configure_fullscreen(struct shell_surface *shsurf)
2193{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002194 struct weston_surface *surface =
2195 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002196 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002197
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002198 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002199 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002200 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2201 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002202
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002203 if (!shsurf->fullscreen_output) {
2204 /* If there is no output, there's not much we can do.
2205 * Position the window somewhere, whatever. */
2206 weston_view_set_position(shsurf->view, 0, 0);
2207 return;
2208 }
2209
Philip Withnalled8f1a92013-11-25 18:01:43 +00002210 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002211
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002212 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002213 &surf_width, &surf_height);
2214
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002215 if (surface->buffer_ref.buffer)
2216 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002217}
2218
Alex Wu4539b082012-03-01 12:57:46 +08002219static void
2220shell_map_fullscreen(struct shell_surface *shsurf)
2221{
Alex Wubd3354b2012-04-17 17:20:49 +08002222 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002223}
2224
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002225static struct weston_output *
2226get_focused_output(struct weston_compositor *compositor)
2227{
2228 struct weston_seat *seat;
2229 struct weston_output *output = NULL;
2230
2231 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002232 struct weston_touch *touch = weston_seat_get_touch(seat);
2233 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2234 struct weston_keyboard *keyboard =
2235 weston_seat_get_keyboard(seat);
2236
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002237 /* Priority has touch focus, then pointer and
2238 * then keyboard focus. We should probably have
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01002239 * three for loops and check first for touch,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002240 * then for pointer, etc. but unless somebody has some
2241 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002242 if (touch && touch->focus)
2243 output = touch->focus->output;
2244 else if (pointer && pointer->focus)
2245 output = pointer->focus->output;
2246 else if (keyboard && keyboard->focus)
2247 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002248
2249 if (output)
2250 break;
2251 }
2252
2253 return output;
2254}
2255
2256static void
Marius Vladc114fd62021-08-26 16:37:24 +03002257desktop_shell_destroy_seat(struct shell_seat *shseat)
2258{
Marius Vlada7392c82021-08-26 16:38:43 +03002259 wl_list_remove(&shseat->keyboard_focus_listener.link);
2260 wl_list_remove(&shseat->caps_changed_listener.link);
2261 wl_list_remove(&shseat->pointer_focus_listener.link);
Marius Vladc114fd62021-08-26 16:37:24 +03002262 wl_list_remove(&shseat->seat_destroy_listener.link);
2263
2264 wl_list_remove(&shseat->link);
2265 free(shseat);
2266}
2267
2268static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002269destroy_shell_seat(struct wl_listener *listener, void *data)
2270{
2271 struct shell_seat *shseat =
2272 container_of(listener,
2273 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002274
Marius Vladc114fd62021-08-26 16:37:24 +03002275 desktop_shell_destroy_seat(shseat);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002276}
2277
Jason Ekstrand024177c2014-04-21 19:42:58 -05002278static void
2279shell_seat_caps_changed(struct wl_listener *listener, void *data)
2280{
Derek Foreman1281a362015-07-31 16:55:32 -05002281 struct weston_keyboard *keyboard;
2282 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002283 struct shell_seat *seat;
2284
2285 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002286 keyboard = weston_seat_get_keyboard(seat->seat);
2287 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002288
Derek Foreman1281a362015-07-31 16:55:32 -05002289 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002290 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002291 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002292 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002293 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002294 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002295 wl_list_init(&seat->keyboard_focus_listener.link);
2296 }
2297
Derek Foreman1281a362015-07-31 16:55:32 -05002298 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002299 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002300 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002301 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002302 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002303 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002304 wl_list_init(&seat->pointer_focus_listener.link);
2305 }
2306}
2307
Giulio Camuffo5085a752013-03-25 21:42:45 +01002308static struct shell_seat *
Marius Vladc114fd62021-08-26 16:37:24 +03002309create_shell_seat(struct desktop_shell *shell, struct weston_seat *seat)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002310{
2311 struct shell_seat *shseat;
2312
2313 shseat = calloc(1, sizeof *shseat);
2314 if (!shseat) {
2315 weston_log("no memory to allocate shell seat\n");
2316 return NULL;
2317 }
2318
2319 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002320
2321 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2322 wl_signal_add(&seat->destroy_signal,
2323 &shseat->seat_destroy_listener);
2324
Jason Ekstrand024177c2014-04-21 19:42:58 -05002325 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2326 wl_list_init(&shseat->keyboard_focus_listener.link);
2327
2328 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2329 wl_list_init(&shseat->pointer_focus_listener.link);
2330
2331 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2332 wl_signal_add(&seat->updated_caps_signal,
2333 &shseat->caps_changed_listener);
2334 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2335
Marius Vladc114fd62021-08-26 16:37:24 +03002336 wl_list_insert(&shell->seat_list, &shseat->link);
2337
Giulio Camuffo5085a752013-03-25 21:42:45 +01002338 return shseat;
2339}
2340
2341static struct shell_seat *
2342get_shell_seat(struct weston_seat *seat)
2343{
2344 struct wl_listener *listener;
2345
2346 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002347 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002348
2349 return container_of(listener,
2350 struct shell_seat, seat_destroy_listener);
2351}
2352
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002353static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002354fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002355{
2356 struct shell_surface *shsurf = data;
2357
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002358 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002359
2360 if (shsurf->output_destroy_listener.notify) {
2361 wl_list_remove(&shsurf->output_destroy_listener.link);
2362 shsurf->output_destroy_listener.notify = NULL;
2363 }
2364
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002365 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002366}
2367
2368static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002369fade_out_done(struct weston_view_animation *animation, void *data)
2370{
2371 struct shell_surface *shsurf = data;
2372 struct wl_event_loop *loop;
2373
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002374 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002375
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002376 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002377 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002378 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002379 }
2380}
2381
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002382struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002383get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002384{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002385 if (weston_surface_is_desktop_surface(surface)) {
2386 struct weston_desktop_surface *desktop_surface =
2387 weston_surface_get_desktop_surface(surface);
2388 return weston_desktop_surface_get_user_data(desktop_surface);
2389 }
2390 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002391}
2392
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002393/*
2394 * libweston-desktop
2395 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002396
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002397static void
2398desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2399 void *shell)
2400{
2401 struct weston_desktop_client *client =
2402 weston_desktop_surface_get_client(desktop_surface);
2403 struct wl_client *wl_client =
2404 weston_desktop_client_get_client(client);
2405 struct weston_view *view;
2406 struct shell_surface *shsurf;
2407 struct weston_surface *surface =
2408 weston_desktop_surface_get_surface(desktop_surface);
2409
2410 view = weston_desktop_surface_create_view(desktop_surface);
2411 if (!view)
2412 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002413
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002414 shsurf = calloc(1, sizeof *shsurf);
2415 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 if (wl_client)
2417 wl_client_post_no_memory(wl_client);
2418 else
2419 weston_log("no memory to allocate shell surface\n");
2420 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002421 }
2422
Pekka Paalanen8274d902014-08-06 19:36:51 +03002423 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002424
Tiago Vignattibc052c92012-04-19 16:18:18 +03002425 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002426 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002427 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002428 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 shsurf->desktop_surface = desktop_surface;
2430 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002431 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002432 wl_list_init(&shsurf->fullscreen.transform.link);
2433
Semi Malinen99f8c082018-05-02 11:10:32 +02002434 shell_surface_set_output(
2435 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002436
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002437 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002438
Pekka Paalanen460099f2012-01-20 16:48:25 +02002439 /* empty when not in use */
2440 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002441 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002442
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002443 wl_list_init(&shsurf->workspace_transform.link);
2444
Stefan Agnera8da2082019-06-23 14:53:59 +02002445 /*
2446 * initialize list as well as link. The latter allows to use
2447 * wl_list_remove() even when this surface is not in another list.
2448 */
2449 wl_list_init(&shsurf->children_list);
2450 wl_list_init(&shsurf->children_link);
2451
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2453 weston_desktop_surface_set_activated(desktop_surface,
2454 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002455}
2456
Tiago Vignattibc052c92012-04-19 16:18:18 +03002457static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002458desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2459 void *shell)
2460{
2461 struct shell_surface *shsurf =
2462 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002463 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002464 struct weston_surface *surface =
2465 weston_desktop_surface_get_surface(desktop_surface);
2466
2467 if (!shsurf)
2468 return;
2469
Stefan Agnera8da2082019-06-23 14:53:59 +02002470 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2471 wl_list_remove(&shsurf_child->children_link);
2472 wl_list_init(&shsurf_child->children_link);
2473 }
2474 wl_list_remove(&shsurf->children_link);
2475
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002476 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2477
2478 if (shsurf->fullscreen.black_view)
2479 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2480
2481 weston_surface_set_label_func(surface, NULL);
2482 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2483 shsurf->desktop_surface = NULL;
2484
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002485 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002486 if (weston_surface_is_mapped(surface) &&
2487 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002488
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002489 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2490 pixman_region32_fini(&surface->pending.input);
2491 pixman_region32_init(&surface->pending.input);
2492 pixman_region32_fini(&surface->input);
2493 pixman_region32_init(&surface->input);
2494 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2495 fade_out_done, shsurf);
2496 return;
2497 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002498 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002499 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002501
Marius Vladc30cb292021-08-02 15:47:16 +03002502 weston_view_destroy(shsurf->view);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002503
2504 if (shsurf->output_destroy_listener.notify) {
2505 wl_list_remove(&shsurf->output_destroy_listener.link);
2506 shsurf->output_destroy_listener.notify = NULL;
2507 }
2508
2509 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002510}
2511
2512static void
2513set_maximized_position(struct desktop_shell *shell,
2514 struct shell_surface *shsurf)
2515{
2516 pixman_rectangle32_t area;
2517 struct weston_geometry geometry;
2518
2519 get_output_work_area(shell, shsurf->output, &area);
2520 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2521
2522 weston_view_set_position(shsurf->view,
2523 area.x - geometry.x,
2524 area.y - geometry.y);
2525}
2526
2527static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002528set_position_from_xwayland(struct shell_surface *shsurf)
2529{
2530 struct weston_geometry geometry;
2531 float x;
2532 float y;
2533
2534 assert(shsurf->xwayland.is_set);
2535
2536 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2537 x = shsurf->xwayland.x - geometry.x;
2538 y = shsurf->xwayland.y - geometry.y;
2539
2540 weston_view_set_position(shsurf->view, x, y);
2541
2542#ifdef WM_DEBUG
2543 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2544 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2545 geometry.x, geometry.y, x, y);
2546#endif
2547}
2548
2549static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002550map(struct desktop_shell *shell, struct shell_surface *shsurf,
2551 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002552{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002553 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002554 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2555 struct weston_compositor *compositor = shell->compositor;
2556 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002557
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002559 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002560 center_on_output(shsurf->view, shsurf->fullscreen_output);
2561 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002562 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002563 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002564 } else if (shsurf->xwayland.is_set) {
2565 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002566 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002567 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002568 }
2569
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002570 /* Surface stacking order, see also activate(). */
2571 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002572
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002573 weston_view_update_transform(shsurf->view);
2574 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002575 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002576 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002577 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002578 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002579
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002580 if (!shell->locked) {
2581 wl_list_for_each(seat, &compositor->seat_list, link)
2582 activate(shell, shsurf->view, seat,
2583 WESTON_ACTIVATE_FLAG_CONFIGURE);
2584 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002585
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002586 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587 switch (shell->win_animation_type) {
2588 case ANIMATION_FADE:
2589 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2590 break;
2591 case ANIMATION_ZOOM:
2592 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2593 break;
2594 case ANIMATION_NONE:
2595 default:
2596 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002597 }
2598 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002599}
2600
2601static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002602desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2603 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002604{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002605 struct shell_surface *shsurf =
2606 weston_desktop_surface_get_user_data(desktop_surface);
2607 struct weston_surface *surface =
2608 weston_desktop_surface_get_surface(desktop_surface);
2609 struct weston_view *view = shsurf->view;
2610 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002611 bool was_fullscreen;
2612 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002613
2614 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002615 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002616
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002617 was_fullscreen = shsurf->state.fullscreen;
2618 was_maximized = shsurf->state.maximized;
2619
2620 shsurf->state.fullscreen =
2621 weston_desktop_surface_get_fullscreen(desktop_surface);
2622 shsurf->state.maximized =
2623 weston_desktop_surface_get_maximized(desktop_surface);
2624
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002625 if (!weston_surface_is_mapped(surface)) {
2626 map(shell, shsurf, sx, sy);
2627 surface->is_mapped = true;
2628 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2629 ++surface->ref_count;
2630 return;
2631 }
2632
2633 if (sx == 0 && sy == 0 &&
2634 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002635 shsurf->last_height == surface->height &&
2636 was_fullscreen == shsurf->state.fullscreen &&
2637 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002638 return;
2639
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002640 if (was_fullscreen)
2641 unset_fullscreen(shsurf);
2642 if (was_maximized)
2643 unset_maximized(shsurf);
2644
Quentin Glidic920cf042016-08-16 14:26:20 +02002645 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2646 !shsurf->saved_position_valid) {
2647 shsurf->saved_x = shsurf->view->geometry.x;
2648 shsurf->saved_y = shsurf->view->geometry.y;
2649 shsurf->saved_position_valid = true;
2650
2651 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2652 wl_list_remove(&shsurf->rotation.transform.link);
2653 wl_list_init(&shsurf->rotation.transform.link);
2654 weston_view_geometry_dirty(shsurf->view);
2655 shsurf->saved_rotation_valid = true;
2656 }
2657 }
2658
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002659 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002660 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002661 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002662 set_maximized_position(shell, shsurf);
2663 surface->output = shsurf->output;
2664 } else {
2665 float from_x, from_y;
2666 float to_x, to_y;
2667 float x, y;
2668
2669 if (shsurf->resize_edges) {
2670 sx = 0;
2671 sy = 0;
2672 }
2673
2674 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2675 sx = shsurf->last_width - surface->width;
2676 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2677 sy = shsurf->last_height - surface->height;
2678
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002679 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2680 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2681 x = shsurf->view->geometry.x + to_x - from_x;
2682 y = shsurf->view->geometry.y + to_y - from_y;
2683
2684 weston_view_set_position(shsurf->view, x, y);
2685 }
2686
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002687 shsurf->last_width = surface->width;
2688 shsurf->last_height = surface->height;
2689
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690 /* XXX: would a fullscreen surface need the same handling? */
2691 if (surface->output) {
2692 wl_list_for_each(view, &surface->views, surface_link)
2693 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002694 }
2695}
2696
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002697static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002698get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2699{
2700 struct desktop_shell *shell;
2701 pixman_rectangle32_t area;
2702
2703 shell = shell_surface_get_shell(shsurf);
2704 get_output_work_area(shell, shsurf->output, &area);
2705
2706 *width = area.width;
2707 *height = area.height;
2708}
2709
2710static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2712 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002713{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002714 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2715 struct weston_surface *surface =
2716 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2717 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002718
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002719 if (fullscreen) {
2720 /* handle clients launching in fullscreen */
2721 if (output == NULL && !weston_surface_is_mapped(surface)) {
2722 /* Set the output to the one that has focus currently. */
2723 output = get_focused_output(surface->compositor);
2724 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002725
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002726 shell_surface_set_output(shsurf, output);
2727 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002728
Marius Vlad6ebda362020-03-28 00:23:14 +02002729 if (shsurf->output) {
2730 width = shsurf->output->width;
2731 height = shsurf->output->height;
2732 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002733 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2734 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002735 }
2736 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2737 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002738}
2739
2740static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002741desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2742 struct weston_seat *seat, uint32_t serial, void *shell)
2743{
2744 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2745 struct weston_touch *touch = weston_seat_get_touch(seat);
2746 struct shell_surface *shsurf =
2747 weston_desktop_surface_get_user_data(desktop_surface);
2748 struct weston_surface *surface =
2749 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2750 struct wl_resource *resource = surface->resource;
2751 struct weston_surface *focus;
2752
2753 if (pointer &&
2754 pointer->focus &&
2755 pointer->button_count > 0 &&
2756 pointer->grab_serial == serial) {
2757 focus = weston_surface_get_main_surface(pointer->focus->surface);
2758 if ((focus == surface) &&
2759 (surface_move(shsurf, pointer, true) < 0))
2760 wl_resource_post_no_memory(resource);
2761 } else if (touch &&
2762 touch->focus &&
2763 touch->grab_serial == serial) {
2764 focus = weston_surface_get_main_surface(touch->focus->surface);
2765 if ((focus == surface) &&
2766 (surface_touch_move(shsurf, touch) < 0))
2767 wl_resource_post_no_memory(resource);
2768 }
2769}
2770
2771static void
2772desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2773 struct weston_seat *seat, uint32_t serial,
2774 enum weston_desktop_surface_edge edges, void *shell)
2775{
2776 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2777 struct shell_surface *shsurf =
2778 weston_desktop_surface_get_user_data(desktop_surface);
2779 struct weston_surface *surface =
2780 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2781 struct wl_resource *resource = surface->resource;
2782 struct weston_surface *focus;
2783
2784 if (!pointer ||
2785 pointer->button_count == 0 ||
2786 pointer->grab_serial != serial ||
2787 pointer->focus == NULL)
2788 return;
2789
2790 focus = weston_surface_get_main_surface(pointer->focus->surface);
2791 if (focus != surface)
2792 return;
2793
2794 if (surface_resize(shsurf, pointer, edges) < 0)
2795 wl_resource_post_no_memory(resource);
2796}
2797
2798static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002799desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2800 struct weston_desktop_surface *parent,
2801 void *shell)
2802{
Marius Vlada27658e2020-01-17 12:32:55 +02002803 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002804 struct shell_surface *shsurf =
2805 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002806
Marius Vlada27658e2020-01-17 12:32:55 +02002807 /* unlink any potential child */
2808 wl_list_remove(&shsurf->children_link);
2809
2810 if (parent) {
2811 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2812 wl_list_insert(shsurf_parent->children_list.prev,
2813 &shsurf->children_link);
2814 } else {
2815 wl_list_init(&shsurf->children_link);
2816 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002817}
2818
2819static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002820desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2821 bool fullscreen,
2822 struct weston_output *output, void *shell)
2823{
2824 struct shell_surface *shsurf =
2825 weston_desktop_surface_get_user_data(desktop_surface);
2826
2827 set_fullscreen(shsurf, fullscreen, output);
2828}
2829
2830static void
2831set_maximized(struct shell_surface *shsurf, bool maximized)
2832{
2833 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2834 struct weston_surface *surface =
2835 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2836 int32_t width = 0, height = 0;
2837
2838 if (maximized) {
2839 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002840
2841 if (!weston_surface_is_mapped(surface))
2842 output = get_focused_output(surface->compositor);
2843 else
2844 output = surface->output;
2845
2846 shell_surface_set_output(shsurf, output);
2847
Derek Foreman927d9e22017-10-06 14:37:44 -05002848 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002849 }
2850 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2851 weston_desktop_surface_set_size(desktop_surface, width, height);
2852}
2853
2854static void
2855desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2856 bool maximized, void *shell)
2857{
2858 struct shell_surface *shsurf =
2859 weston_desktop_surface_get_user_data(desktop_surface);
2860
2861 set_maximized(shsurf, maximized);
2862}
2863
2864static void
2865desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2866 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002867{
2868 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002869 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002870
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002871 /* apply compositor's own minimization logic (hide) */
2872 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002873}
2874
Rafael Antognollie2a34552013-12-03 15:35:45 -02002875static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002876set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002877{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002878 struct shell_grab *grab;
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 return;
2882
2883 grab = malloc(sizeof *grab);
2884 if (!grab)
2885 return;
2886
2887 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2888 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2889 /* Mark the shsurf as ungrabbed so that button binding is able
2890 * to move it. */
2891 shsurf->grabbed = 0;
2892}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002893
2894static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002895end_busy_cursor(struct weston_compositor *compositor,
2896 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002897{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002898 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002899 struct shell_grab *grab;
2900 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002901
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002902 wl_list_for_each(seat, &compositor->seat_list, link) {
2903 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2904 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002905
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002906 if (!pointer)
2907 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002908
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002909 if (pointer->grab->interface != &busy_cursor_grab_interface)
2910 continue;
2911
2912 grab = (struct shell_grab *) pointer->grab;
2913 shsurf = grab->shsurf;
2914 if (!shsurf)
2915 continue;
2916
2917 grab_client =
2918 weston_desktop_surface_get_client(shsurf->desktop_surface);
2919 if (grab_client == desktop_client) {
2920 shell_grab_end(grab);
2921 free(grab);
2922 }
2923 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002924}
2925
2926static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002927desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2928 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002929{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002930 struct shell_surface *shsurf =
2931 weston_desktop_surface_get_user_data(desktop_surface);
2932 bool *unresponsive = user_data;
2933
2934 shsurf->unresponsive = *unresponsive;
2935}
2936
2937static void
2938desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2939 void *shell_)
2940{
2941 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002942 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002943 struct weston_seat *seat;
2944 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002945
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002946 weston_desktop_client_for_each_surface(desktop_client,
2947 desktop_surface_set_unresponsive,
2948 &unresponsive);
2949
2950
2951 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2952 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2953 struct weston_desktop_client *grab_client;
2954
2955 if (!pointer || !pointer->focus)
2956 continue;
2957
2958 shsurf = get_shell_surface(pointer->focus->surface);
2959 if (!shsurf)
2960 continue;
2961
2962 grab_client =
2963 weston_desktop_surface_get_client(shsurf->desktop_surface);
2964 if (grab_client == desktop_client)
2965 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002966 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002967}
2968
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002969static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002970desktop_surface_pong(struct weston_desktop_client *desktop_client,
2971 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002972{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002973 struct desktop_shell *shell = shell_;
2974 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002975
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002976 weston_desktop_client_for_each_surface(desktop_client,
2977 desktop_surface_set_unresponsive,
2978 &unresponsive);
2979 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002980}
2981
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002982static void
2983desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2984 int32_t x, int32_t y, void *shell_)
2985{
2986 struct shell_surface *shsurf =
2987 weston_desktop_surface_get_user_data(surface);
2988
2989 shsurf->xwayland.x = x;
2990 shsurf->xwayland.y = y;
2991 shsurf->xwayland.is_set = true;
2992}
2993
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002994static const struct weston_desktop_api shell_desktop_api = {
2995 .struct_size = sizeof(struct weston_desktop_api),
2996 .surface_added = desktop_surface_added,
2997 .surface_removed = desktop_surface_removed,
2998 .committed = desktop_surface_committed,
2999 .move = desktop_surface_move,
3000 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02003001 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003002 .fullscreen_requested = desktop_surface_fullscreen_requested,
3003 .maximized_requested = desktop_surface_maximized_requested,
3004 .minimized_requested = desktop_surface_minimized_requested,
3005 .ping_timeout = desktop_surface_ping_timeout,
3006 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02003007 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003008};
3009
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003010/* ************************ *
3011 * end of libweston-desktop *
3012 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04003013static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02003014configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003016 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003017
Semi Malinene7a52fb2018-04-26 11:08:10 +02003018 if (!ev->output)
3019 return;
3020
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003021 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003022 if (v->output == ev->output && v != ev) {
3023 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003024 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003025 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003026 }
3027 }
3028
Quentin Glidice8bf9592016-06-23 18:55:20 +02003029 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003030 ev->surface->is_mapped = true;
3031 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003032
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003033 if (wl_list_empty(&ev->layer_link.link)) {
3034 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003035 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003036 }
3037}
3038
David Fort50d962f2016-06-09 17:55:52 +02003039
3040static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003041find_shell_output_from_weston_output(struct desktop_shell *shell,
3042 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003043{
3044 struct shell_output *shell_output;
3045
3046 wl_list_for_each(shell_output, &shell->output_list, link) {
3047 if (shell_output->output == output)
3048 return shell_output;
3049 }
3050
3051 return NULL;
3052}
3053
Pekka Paalanen8274d902014-08-06 19:36:51 +03003054static int
3055background_get_label(struct weston_surface *surface, char *buf, size_t len)
3056{
3057 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003058 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003059}
3060
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003061static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003062background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003063{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003064 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003066
Jason Ekstranda7af7042013-10-12 22:38:11 -05003067 view = container_of(es->views.next, struct weston_view, surface_link);
3068
Quentin Glidice8bf9592016-06-23 18:55:20 +02003069 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003070}
3071
3072static void
David Fort50d962f2016-06-09 17:55:52 +02003073handle_background_surface_destroy(struct wl_listener *listener, void *data)
3074{
3075 struct shell_output *output =
3076 container_of(listener, struct shell_output, background_surface_listener);
3077
3078 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003079 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003080 output->background_surface = NULL;
3081}
3082
3083static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003084desktop_shell_set_background(struct wl_client *client,
3085 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003086 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003087 struct wl_resource *surface_resource)
3088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003089 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003090 struct weston_surface *surface =
3091 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003092 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003093 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003094
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003095 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003096 wl_resource_post_error(surface_resource,
3097 WL_DISPLAY_ERROR_INVALID_OBJECT,
3098 "surface role already assigned");
3099 return;
3100 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003101
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3103 weston_view_destroy(view);
3104 view = weston_view_create(surface);
3105
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003106 surface->committed = background_committed;
3107 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003108 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003109 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003110 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003111
3112 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003113 if (sh_output->background_surface) {
3114 /* The output already has a background, tell our helper
3115 * there is no need for another one. */
3116 weston_desktop_shell_send_configure(resource, 0,
3117 surface_resource,
3118 0, 0);
3119 } else {
3120 weston_desktop_shell_send_configure(resource, 0,
3121 surface_resource,
3122 surface->output->width,
3123 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003124
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003125 sh_output->background_surface = surface;
3126
3127 sh_output->background_surface_listener.notify =
3128 handle_background_surface_destroy;
3129 wl_signal_add(&surface->destroy_signal,
3130 &sh_output->background_surface_listener);
3131 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003132}
3133
Pekka Paalanen8274d902014-08-06 19:36:51 +03003134static int
3135panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3136{
3137 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003138 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003139}
3140
Kristian Høgsberg75840622011-09-06 13:48:16 -04003141static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003142panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003143{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003144 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003145 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003146 int width, height;
3147 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003148
Jason Ekstranda7af7042013-10-12 22:38:11 -05003149 view = container_of(es->views.next, struct weston_view, surface_link);
3150
Quentin Glidice8bf9592016-06-23 18:55:20 +02003151 get_panel_size(shell, view, &width, &height);
3152 switch (shell->panel_position) {
3153 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3154 break;
3155 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3156 y = view->output->height - height;
3157 break;
3158 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3159 break;
3160 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3161 x = view->output->width - width;
3162 break;
3163 }
3164
3165 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003166}
3167
3168static void
David Fort50d962f2016-06-09 17:55:52 +02003169handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3170{
3171 struct shell_output *output =
3172 container_of(listener, struct shell_output, panel_surface_listener);
3173
3174 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003175 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003176 output->panel_surface = NULL;
3177}
3178
3179
3180static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003181desktop_shell_set_panel(struct wl_client *client,
3182 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003183 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003184 struct wl_resource *surface_resource)
3185{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003186 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003187 struct weston_surface *surface =
3188 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003189 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003190 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003191
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003192 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003193 wl_resource_post_error(surface_resource,
3194 WL_DISPLAY_ERROR_INVALID_OBJECT,
3195 "surface role already assigned");
3196 return;
3197 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003198
Jason Ekstranda7af7042013-10-12 22:38:11 -05003199 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3200 weston_view_destroy(view);
3201 view = weston_view_create(surface);
3202
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003203 surface->committed = panel_committed;
3204 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003205 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003206 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003207 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003208
3209 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003210 if (sh_output->panel_surface) {
3211 /* The output already has a panel, tell our helper
3212 * there is no need for another one. */
3213 weston_desktop_shell_send_configure(resource, 0,
3214 surface_resource,
3215 0, 0);
3216 } else {
3217 weston_desktop_shell_send_configure(resource, 0,
3218 surface_resource,
3219 surface->output->width,
3220 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003221
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003222 sh_output->panel_surface = surface;
3223
3224 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3225 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3226 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003227}
3228
Pekka Paalanen8274d902014-08-06 19:36:51 +03003229static int
3230lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3231{
3232 return snprintf(buf, len, "lock window");
3233}
3234
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003235static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003236lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003237{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003238 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003239 struct weston_view *view;
3240
3241 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003242
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003243 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003244 return;
3245
Jason Ekstranda7af7042013-10-12 22:38:11 -05003246 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003247
3248 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003249 weston_layer_entry_insert(&shell->lock_layer.view_list,
3250 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003252 surface->is_mapped = true;
3253 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003254 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003255 }
3256}
3257
3258static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003259handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003260{
Tiago Vignattibe143262012-04-16 17:31:41 +03003261 struct desktop_shell *shell =
3262 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003263
Martin Minarik6d118362012-06-07 18:01:59 +02003264 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003265 shell->lock_surface = NULL;
3266}
3267
3268static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003269desktop_shell_set_lock_surface(struct wl_client *client,
3270 struct wl_resource *resource,
3271 struct wl_resource *surface_resource)
3272{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003273 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003274 struct weston_surface *surface =
3275 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003276
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003277 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003278
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003279 if (!shell->locked)
3280 return;
3281
Pekka Paalanen98262232011-12-01 10:42:22 +02003282 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003283
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003284 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003285 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003286 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003287
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003288 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003289 surface->committed = lock_surface_committed;
3290 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003291 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003292}
3293
3294static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003295resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003296{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003297 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003298
Quentin Glidic82681572016-12-17 13:40:51 +01003299 weston_layer_unset_position(&shell->lock_layer);
3300
3301 if (shell->showing_input_panels)
3302 weston_layer_set_position(&shell->input_panel_layer,
3303 WESTON_LAYER_POSITION_TOP_UI);
3304 weston_layer_set_position(&shell->fullscreen_layer,
3305 WESTON_LAYER_POSITION_FULLSCREEN);
3306 weston_layer_set_position(&shell->panel_layer,
3307 WESTON_LAYER_POSITION_UI);
3308 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003309
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003310 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003311
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003312 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003313 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003314 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003315}
3316
3317static void
3318desktop_shell_unlock(struct wl_client *client,
3319 struct wl_resource *resource)
3320{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003321 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003322
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003323 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003324
3325 if (shell->locked)
3326 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003327}
3328
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003329static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003330desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003331 struct wl_resource *resource,
3332 struct wl_resource *surface_resource)
3333{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003334 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003335
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003336 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003337 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003338}
3339
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003340static void
3341desktop_shell_desktop_ready(struct wl_client *client,
3342 struct wl_resource *resource)
3343{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003344 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003345
3346 shell_fade_startup(shell);
3347}
3348
Jonny Lamb765760d2014-08-20 15:53:19 +02003349static void
3350desktop_shell_set_panel_position(struct wl_client *client,
3351 struct wl_resource *resource,
3352 uint32_t position)
3353{
3354 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3355
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003356 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3357 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3358 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3359 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003360 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003361 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003362 "bad position argument");
3363 return;
3364 }
3365
3366 shell->panel_position = position;
3367}
3368
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003369static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003370 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003371 desktop_shell_set_panel,
3372 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003373 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003374 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003375 desktop_shell_desktop_ready,
3376 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003377};
3378
3379static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003380move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003381 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003382{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003383 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003384 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003385 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003386
Derek Foreman8ae2db52015-07-15 13:00:36 -05003387 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003388 return;
3389
Derek Foreman8ae2db52015-07-15 13:00:36 -05003390 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003391
Pekka Paalanen01388e22013-04-25 13:57:44 +03003392 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003393 if (surface == NULL)
3394 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003395
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003396 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003397 if (shsurf == NULL ||
3398 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3399 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003400 return;
3401
Derek Foreman794fa0e2015-07-15 13:00:38 -05003402 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003403}
3404
3405static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003406maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003407 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003408{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003409 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003410 struct weston_surface *surface;
3411 struct shell_surface *shsurf;
3412
3413 surface = weston_surface_get_main_surface(focus);
3414 if (surface == NULL)
3415 return;
3416
3417 shsurf = get_shell_surface(surface);
3418 if (shsurf == NULL)
3419 return;
3420
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003421 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003422}
3423
3424static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003425fullscreen_binding(struct weston_keyboard *keyboard,
3426 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003427{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003428 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003429 struct weston_surface *surface;
3430 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003431 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003432
3433 surface = weston_surface_get_main_surface(focus);
3434 if (surface == NULL)
3435 return;
3436
3437 shsurf = get_shell_surface(surface);
3438 if (shsurf == NULL)
3439 return;
3440
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003441 fullscreen =
3442 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003443
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003444 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003445}
3446
3447static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003448touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003449{
Derek Foreman362656b2014-09-04 10:23:05 -05003450 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003451 struct weston_surface *surface;
3452 struct shell_surface *shsurf;
3453
Derek Foreman8ae2db52015-07-15 13:00:36 -05003454 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003455 return;
3456
Derek Foreman8ae2db52015-07-15 13:00:36 -05003457 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003458 surface = weston_surface_get_main_surface(focus);
3459 if (surface == NULL)
3460 return;
3461
3462 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003463 if (shsurf == NULL ||
3464 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3465 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003466 return;
3467
Derek Foremanb7674ae2015-07-15 13:00:37 -05003468 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003469}
3470
3471static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003472resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003473 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003474{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003475 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003476 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003477 uint32_t edges = 0;
3478 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003479 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003480
Derek Foreman8ae2db52015-07-15 13:00:36 -05003481 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003482 return;
3483
Derek Foreman8ae2db52015-07-15 13:00:36 -05003484 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003485
Pekka Paalanen01388e22013-04-25 13:57:44 +03003486 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003487 if (surface == NULL)
3488 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003489
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003490 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003491 if (shsurf == NULL ||
3492 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3493 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003494 return;
3495
Jason Ekstranda7af7042013-10-12 22:38:11 -05003496 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003497 wl_fixed_to_int(pointer->grab_x),
3498 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003499 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003500
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003501 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003502 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003503 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003504 edges |= 0;
3505 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003506 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003507
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003508 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003509 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003510 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003511 edges |= 0;
3512 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003513 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003514
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003515 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003516}
3517
3518static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003519surface_opacity_binding(struct weston_pointer *pointer,
3520 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003521 struct weston_pointer_axis_event *event,
3522 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003523{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003524 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003525 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003526 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003527 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003528
Pekka Paalanen01388e22013-04-25 13:57:44 +03003529 /* XXX: broken for windows containing sub-surfaces */
3530 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003531 if (surface == NULL)
3532 return;
3533
3534 shsurf = get_shell_surface(surface);
3535 if (!shsurf)
3536 return;
3537
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003538 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003539
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 if (shsurf->view->alpha > 1.0)
3541 shsurf->view->alpha = 1.0;
3542 if (shsurf->view->alpha < step)
3543 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003544
Jason Ekstranda7af7042013-10-12 22:38:11 -05003545 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003546 weston_surface_damage(surface);
3547}
3548
3549static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003550do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3551 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003552{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003553 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003554 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003555 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003556 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003557
Derek Foreman1281a362015-07-31 16:55:32 -05003558 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003559 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3560 return;
3561 }
3562
Scott Moreauccbf29d2012-02-22 14:21:41 -07003563 wl_list_for_each(output, &compositor->output_list, link) {
3564 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003565 wl_fixed_to_double(pointer->x),
3566 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003567 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003568 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003569 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003570 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003571 increment = -output->zoom.increment;
3572 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003573 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003574 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003575 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003576 else
3577 increment = 0;
3578
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003579 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003580
Scott Moreaue6603982012-06-11 13:07:51 -06003581 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003582 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003583 else if (output->zoom.level > output->zoom.max_level)
3584 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003585
3586 if (!output->zoom.active) {
3587 if (output->zoom.level <= 0.0)
3588 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003589 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003590 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003591
Scott Moreaue6603982012-06-11 13:07:51 -06003592 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003593
Jason Ekstranda7af7042013-10-12 22:38:11 -05003594 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003595 }
3596 }
3597}
3598
Scott Moreauccbf29d2012-02-22 14:21:41 -07003599static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003600zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003601 struct weston_pointer_axis_event *event,
3602 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003603{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003604 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003605}
3606
3607static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003608zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003609 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003610{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003611 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003612}
3613
3614static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003615terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003616 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003617{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003618 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003619
Pekka Paalanen052032d2019-02-06 10:44:37 +02003620 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003621}
3622
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003623static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003624rotate_grab_motion(struct weston_pointer_grab *grab,
3625 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003626 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003627{
3628 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003629 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003630 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003631 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003632 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003633 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003634
Jonas Ådahld2510102014-10-05 21:39:14 +02003635 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003636
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003637 if (!shsurf)
3638 return;
3639
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003640 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3641
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003642 cx = 0.5f * surface->width;
3643 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003644
Daniel Stone37816df2012-05-16 18:45:18 +01003645 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3646 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003647 r = sqrtf(dx * dx + dy * dy);
3648
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003649 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003651
3652 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003654 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003655
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003656 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003657 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003658
3659 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003660 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003661 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003662 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003663 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003664
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003665 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003667 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003668 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003669 wl_list_init(&shsurf->rotation.transform.link);
3670 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003671 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003672 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003673
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003674 /* We need to adjust the position of the surface
3675 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003676 cposx = shsurf->view->geometry.x + cx;
3677 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003678 dposx = rotate->center.x - cposx;
3679 dposy = rotate->center.y - cposy;
3680 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003681 weston_view_set_position(shsurf->view,
3682 shsurf->view->geometry.x + dposx,
3683 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003684 }
3685
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003686 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003687 * lazily applies the damage due to rotation update.
3688 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003689 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003690}
3691
3692static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003693rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003694 const struct timespec *time,
3695 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003696{
3697 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003698 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003699 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003700 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003701 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702
Daniel Stone4dbadb12012-05-30 16:31:51 +01003703 if (pointer->button_count == 0 &&
3704 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003705 if (shsurf)
3706 weston_matrix_multiply(&shsurf->rotation.rotation,
3707 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003708 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709 free(rotate);
3710 }
3711}
3712
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003713static void
3714rotate_grab_cancel(struct weston_pointer_grab *grab)
3715{
3716 struct rotate_grab *rotate =
3717 container_of(grab, struct rotate_grab, base.grab);
3718
3719 shell_grab_end(&rotate->base);
3720 free(rotate);
3721}
3722
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003723static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003724 noop_grab_focus,
3725 rotate_grab_motion,
3726 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003727 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003728 noop_grab_axis_source,
3729 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003730 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003731};
3732
3733static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003734surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003735{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003736 struct weston_surface *surface =
3737 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003738 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003739 float dx, dy;
3740 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003741
Pekka Paalanen460099f2012-01-20 16:48:25 +02003742 rotate = malloc(sizeof *rotate);
3743 if (!rotate)
3744 return;
3745
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003746 weston_view_to_global_float(shsurf->view,
3747 surface->width * 0.5f,
3748 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003749 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003750
Derek Foreman74de4692015-07-15 13:00:39 -05003751 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3752 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003753 r = sqrtf(dx * dx + dy * dy);
3754 if (r > 20.0f) {
3755 struct weston_matrix inverse;
3756
3757 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003758 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003759 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003760
3761 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003762 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003763 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003764 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003765 weston_matrix_init(&rotate->rotation);
3766 }
3767
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003768 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003769 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003770}
3771
3772static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003773rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3774 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003775{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003776 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003777 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003778 struct shell_surface *surface;
3779
Derek Foreman8ae2db52015-07-15 13:00:36 -05003780 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003781 return;
3782
Derek Foreman8ae2db52015-07-15 13:00:36 -05003783 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003784
Pekka Paalanen01388e22013-04-25 13:57:44 +03003785 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003786 if (base_surface == NULL)
3787 return;
3788
3789 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003790 if (surface == NULL ||
3791 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3792 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003793 return;
3794
Derek Foreman74de4692015-07-15 13:00:39 -05003795 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003796}
3797
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003798/* Move all fullscreen layers down to the current workspace and hide their
3799 * black views. The surfaces' state is set to both fullscreen and lowered,
3800 * and this is reversed when such a surface is re-configured, see
3801 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3802 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003803 * lowering_output = NULL - Lower on all outputs, else only lower on the
3804 * specified output.
3805 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003806 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003807 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003808void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003809lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003810 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003811{
3812 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003814
3815 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003816 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003817 &shell->fullscreen_layer.view_list.link,
3818 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003819 struct shell_surface *shsurf = get_shell_surface(view->surface);
3820
3821 if (!shsurf)
3822 continue;
3823
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003824 /* Only lower surfaces which have lowering_output as their fullscreen
3825 * output, unless a NULL output asks for lowering on all outputs.
3826 */
3827 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3828 continue;
3829
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003830 /* We can have a non-fullscreen popup for a fullscreen surface
3831 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003832 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003833 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003834 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3835 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003836 weston_view_damage_below(shsurf->fullscreen.black_view);
3837
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003838 }
3839
3840 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003841 weston_layer_entry_remove(&view->layer_link);
3842 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003843 weston_view_damage_below(view);
3844 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003845
3846 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003847 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003848}
3849
Stefan Agnera8da2082019-06-23 14:53:59 +02003850static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3851{
3852 struct shell_surface *shsurf_child;
3853
3854 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3855 if (weston_view_is_mapped(shsurf_child->view))
3856 return shsurf_child;
3857 }
3858
3859 return NULL;
3860}
3861
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003862void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003863activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003864 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003865{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003866 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003867 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003868 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003869 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003870 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003871 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003872
Pekka Paalanen01388e22013-04-25 13:57:44 +03003873 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003874 shsurf = get_shell_surface(main_surface);
3875 assert(shsurf);
3876
Stefan Agnera8da2082019-06-23 14:53:59 +02003877 shsurf_child = get_last_child(shsurf);
3878 if (shsurf_child) {
3879 /* Activate last xdg child instead of parent. */
3880 activate(shell, shsurf_child->view, seat, flags);
3881 return;
3882 }
3883
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003884 /* Only demote fullscreen surfaces on the output of activated shsurf.
3885 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003886 if (shsurf->output)
3887 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003888
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003889 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003890
Jonas Ådahl8538b222012-08-29 22:13:03 +02003891 state = ensure_focus_state(shell, seat);
3892 if (state == NULL)
3893 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003894
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003895 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003896 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003897
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003898 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3899 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003900 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003901
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003902 /* Update the surface’s layer. This brings it to the top of the stacking
3903 * order as appropriate. */
3904 shell_surface_update_layer(shsurf);
3905
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003906 if (shell->focus_animation_type != ANIMATION_NONE) {
3907 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003908 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003909 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003910}
3911
Alex Wu21858432012-04-01 20:13:08 +08003912/* no-op func for checking black surface */
3913static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003914black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003915{
3916}
3917
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003918static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003919is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003920{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003921 struct weston_surface *surface = view->surface;
3922
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003923 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003924 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003925 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003926 return true;
3927 }
3928 return false;
3929}
3930
Kristian Høgsberg75840622011-09-06 13:48:16 -04003931static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003932activate_binding(struct weston_seat *seat,
3933 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003934 struct weston_view *focus_view,
3935 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003936{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003937 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003938 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003939
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003940 if (!focus_view)
3941 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003942
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003943 if (is_black_surface_view(focus_view, &main_view))
3944 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003945
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003946 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003947 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003948 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003949
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003950 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003951}
3952
3953static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003954click_to_activate_binding(struct weston_pointer *pointer,
3955 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003956 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003957{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003958 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003959 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003960 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003961 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003962
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003963 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003964 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003965 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003966}
3967
3968static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003969touch_to_activate_binding(struct weston_touch *touch,
3970 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003971 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003972{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003973 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003974 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003975 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003976 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003977
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003978 activate_binding(touch->seat, data, touch->focus,
3979 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003980}
3981
3982static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003983unfocus_all_seats(struct desktop_shell *shell)
3984{
3985 struct weston_seat *seat, *next;
3986
3987 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003988 struct weston_keyboard *keyboard =
3989 weston_seat_get_keyboard(seat);
3990
3991 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003992 continue;
3993
Derek Foreman1281a362015-07-31 16:55:32 -05003994 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003995 }
3996}
3997
3998static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003999lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004000{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004001 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004002
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004003 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004004 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004005 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004006 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004007
4008 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004009
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004010 /* Hide all surfaces by removing the fullscreen, panel and
4011 * toplevel layers. This way nothing else can show or receive
4012 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004013
Quentin Glidic82681572016-12-17 13:40:51 +01004014 weston_layer_unset_position(&shell->panel_layer);
4015 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004016 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01004017 weston_layer_unset_position(&shell->input_panel_layer);
4018 weston_layer_unset_position(&ws->layer);
4019
4020 weston_layer_set_position(&shell->lock_layer,
4021 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004022
Derek Foreman004b4a12015-07-20 16:28:13 -05004023 weston_compositor_sleep(shell->compositor);
4024
Neil Robertsb4a91702014-04-09 16:33:32 +01004025 /* Remove the keyboard focus on all seats. This will be
4026 * restored to the workspace's saved state via
4027 * restore_focus_state when the compositor is unlocked */
4028 unfocus_all_seats(shell);
4029
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004030 /* TODO: disable bindings that should not work while locked. */
4031
4032 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004033}
4034
4035static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004036unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004037{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004038 struct wl_resource *shell_resource;
4039
Pekka Paalanend81c2162011-11-16 13:47:34 +02004040 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004041 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004042 return;
4043 }
4044
4045 /* If desktop-shell client has gone away, unlock immediately. */
4046 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004047 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004048 return;
4049 }
4050
4051 if (shell->prepare_event_sent)
4052 return;
4053
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004054 shell_resource = shell->child.desktop_shell;
4055 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004056 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004057}
4058
4059static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004060shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004061{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004062 struct shell_output *shell_output = data;
4063 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004064
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004065 shell_output->fade.animation = NULL;
4066 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004067 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004068 weston_surface_destroy(shell_output->fade.view->surface);
4069 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004070 break;
4071 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004072 lock(shell);
4073 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004074 default:
4075 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004076 }
4077}
4078
Jason Ekstranda7af7042013-10-12 22:38:11 -05004079static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004080shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004081{
4082 struct weston_compositor *compositor = shell->compositor;
4083 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004084 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004085
4086 surface = weston_surface_create(compositor);
4087 if (!surface)
4088 return NULL;
4089
Jason Ekstranda7af7042013-10-12 22:38:11 -05004090 view = weston_view_create(surface);
4091 if (!view) {
4092 weston_surface_destroy(surface);
4093 return NULL;
4094 }
4095
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004096 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4097 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004098 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004099 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4100 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004101 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004102 surface->is_mapped = true;
4103 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004104
Jason Ekstranda7af7042013-10-12 22:38:11 -05004105 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004106}
4107
4108static void
4109shell_fade(struct desktop_shell *shell, enum fade_type type)
4110{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004111 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004112 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004113
4114 switch (type) {
4115 case FADE_IN:
4116 tint = 0.0;
4117 break;
4118 case FADE_OUT:
4119 tint = 1.0;
4120 break;
4121 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004122 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004123 return;
4124 }
4125
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004126 /* Create a separate fade surface for each output */
4127 wl_list_for_each(shell_output, &shell->output_list, link) {
4128 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004129
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004130 if (shell_output->fade.view == NULL) {
4131 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4132 if (!shell_output->fade.view)
4133 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004134
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004135 shell_output->fade.view->alpha = 1.0 - tint;
4136 weston_view_update_transform(shell_output->fade.view);
4137 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004138
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004139 if (shell_output->fade.view->output == NULL) {
4140 /* If the black view gets a NULL output, we lost the
4141 * last output and we'll just cancel the fade. This
4142 * happens when you close the last window under the
4143 * X11 or Wayland backends. */
4144 shell->locked = false;
4145 weston_surface_destroy(shell_output->fade.view->surface);
4146 shell_output->fade.view = NULL;
4147 } else if (shell_output->fade.animation) {
4148 weston_fade_update(shell_output->fade.animation, tint);
4149 } else {
4150 shell_output->fade.animation =
4151 weston_fade_run(shell_output->fade.view,
4152 1.0 - tint, tint, 300.0,
4153 shell_fade_done_for_output, shell_output);
4154 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004155 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004156}
4157
4158static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004159do_shell_fade_startup(void *data)
4160{
4161 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004162 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004163
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004164 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004165 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004166 } else {
4167 weston_log("desktop shell: "
4168 "unexpected fade-in animation type %d\n",
4169 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004170 wl_list_for_each(shell_output, &shell->output_list, link) {
4171 weston_surface_destroy(shell_output->fade.view->surface);
4172 shell_output->fade.view = NULL;
4173 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004174 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004175}
4176
4177static void
4178shell_fade_startup(struct desktop_shell *shell)
4179{
4180 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004181 struct shell_output *shell_output;
4182 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004183
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004184 wl_list_for_each(shell_output, &shell->output_list, link) {
4185 if (!shell_output->fade.startup_timer)
4186 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004187
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004188 wl_event_source_remove(shell_output->fade.startup_timer);
4189 shell_output->fade.startup_timer = NULL;
4190 has_fade = true;
4191 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004192
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004193 if (has_fade) {
4194 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4195 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4196 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004197}
4198
4199static int
4200fade_startup_timeout(void *data)
4201{
4202 struct desktop_shell *shell = data;
4203
4204 shell_fade_startup(shell);
4205 return 0;
4206}
4207
4208static void
4209shell_fade_init(struct desktop_shell *shell)
4210{
4211 /* Make compositor output all black, and wait for the desktop-shell
4212 * client to signal it is ready, then fade in. The timer triggers a
4213 * fade-in, in case the desktop-shell client takes too long.
4214 */
4215
4216 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004217 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004218
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004219 if (shell->startup_animation_type == ANIMATION_NONE)
4220 return;
4221
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004222 wl_list_for_each(shell_output, &shell->output_list, link) {
4223 if (shell_output->fade.view != NULL) {
4224 weston_log("%s: warning: fade surface already exists\n",
4225 __func__);
4226 continue;
4227 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004228
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004229 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4230 if (!shell_output->fade.view)
4231 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004232
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004233 weston_view_update_transform(shell_output->fade.view);
4234 weston_surface_damage(shell_output->fade.view->surface);
4235
4236 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4237 shell_output->fade.startup_timer =
4238 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4239 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4240 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004241}
4242
4243static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004244idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004245{
4246 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004247 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004248
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004249 struct weston_seat *seat;
4250
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004251 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4252 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004253
4254 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004255 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004256}
4257
4258static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004259wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004260{
4261 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004262 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004263
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004264 unlock(shell);
4265}
4266
4267static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004268transform_handler(struct wl_listener *listener, void *data)
4269{
4270 struct weston_surface *surface = data;
4271 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004272 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004273 int x, y;
4274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004275 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004276 return;
4277
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004278 api = shsurf->shell->xwayland_surface_api;
4279 if (!api) {
4280 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4281 shsurf->shell->xwayland_surface_api = api;
4282 }
4283
4284 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004285 return;
4286
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004287 if (!weston_view_is_mapped(shsurf->view))
4288 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004289
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004290 x = shsurf->view->geometry.x;
4291 y = shsurf->view->geometry.y;
4292
4293 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004294}
4295
4296static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004297center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004298{
Giulio Camuffob8366642013-04-25 13:57:46 +03004299 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004300 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004301
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004302 if (!output) {
4303 weston_view_set_position(view, 0, 0);
4304 return;
4305 }
4306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004308
4309 x = output->x + (output->width - width) / 2 - surf_x / 2;
4310 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004311
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004312 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004313}
4314
4315static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004316weston_view_set_initial_position(struct weston_view *view,
4317 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004318{
4319 struct weston_compositor *compositor = shell->compositor;
4320 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004321 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004322 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004323 struct weston_output *output, *target_output = NULL;
4324 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004325 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004326
4327 /* As a heuristic place the new window on the same output as the
4328 * pointer. Falling back to the output containing 0, 0.
4329 *
4330 * TODO: Do something clever for touch too?
4331 */
4332 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004333 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4334
4335 if (pointer) {
4336 ix = wl_fixed_to_int(pointer->x);
4337 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004338 break;
4339 }
4340 }
4341
4342 wl_list_for_each(output, &compositor->output_list, link) {
4343 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4344 target_output = output;
4345 break;
4346 }
4347 }
4348
4349 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 weston_view_set_position(view, 10 + random() % 400,
4351 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004352 return;
4353 }
4354
4355 /* Valid range within output where the surface will still be onscreen.
4356 * If this is negative it means that the surface is bigger than
4357 * output.
4358 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004359 get_output_work_area(shell, target_output, &area);
4360
Derek Foremanf814c5d2015-04-15 12:23:54 -05004361 x = area.x;
4362 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004363 range_x = area.width - view->surface->width;
4364 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004365
Rob Bradford4cb88c72012-08-13 15:18:44 +01004366 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004367 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004368
4369 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004370 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004371
Jason Ekstranda7af7042013-10-12 22:38:11 -05004372 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004373}
4374
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004375static bool
4376check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4377{
4378 struct timespec now;
4379
4380 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4381 return false;
4382
4383 /*
4384 * If the shell helper client dies before the session has been
4385 * up for roughly 30 seconds, better just make Weston shut down,
4386 * because the user likely has no way to interact with the desktop
4387 * anyway.
4388 */
4389 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4390 weston_log("Error: %s apparently cannot run at all.\n",
4391 shell->client);
4392 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004393 weston_compositor_exit_with_code(shell->compositor,
4394 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004395
4396 return true;
4397 }
4398
4399 return false;
4400}
4401
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004402static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004403
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004404static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004405respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004406{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004407 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004408
4409 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004410 weston_compositor_get_time(&time);
4411 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004412 shell->child.deathstamp = time;
4413 shell->child.deathcount = 0;
4414 }
4415
4416 shell->child.deathcount++;
4417 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004418 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004419 return;
4420 }
4421
Pekka Paalanen826dc142014-08-27 12:11:53 +03004422 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004423 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004424}
4425
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004426static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004427desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4428{
4429 struct desktop_shell *shell;
4430
4431 shell = container_of(listener, struct desktop_shell,
4432 child.client_destroy_listener);
4433
Pekka Paalanen826dc142014-08-27 12:11:53 +03004434 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004435 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004436 /*
4437 * unbind_desktop_shell() will reset shell->child.desktop_shell
4438 * before the respawned process has a chance to create a new
4439 * desktop_shell object, because we are being called from the
4440 * wl_client destructor which destroys all wl_resources before
4441 * returning.
4442 */
4443
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004444 if (!check_desktop_shell_crash_too_early(shell))
4445 respawn_desktop_shell_process(shell);
4446
Pekka Paalanen826dc142014-08-27 12:11:53 +03004447 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004448}
4449
4450static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004451launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004452{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004453 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004454
Pekka Paalanen826dc142014-08-27 12:11:53 +03004455 shell->child.client = weston_client_start(shell->compositor,
4456 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004457
Arnaud Vrac42631192014-08-25 20:56:46 +02004458 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004459 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004460 return;
4461 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004462
4463 shell->child.client_destroy_listener.notify =
4464 desktop_shell_client_destroy;
4465 wl_client_add_destroy_listener(shell->child.client,
4466 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004467}
4468
4469static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004470unbind_desktop_shell(struct wl_resource *resource)
4471{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004472 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004473
4474 if (shell->locked)
4475 resume_desktop(shell);
4476
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004477 shell->child.desktop_shell = NULL;
4478 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004479}
4480
4481static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004482bind_desktop_shell(struct wl_client *client,
4483 void *data, uint32_t version, uint32_t id)
4484{
Tiago Vignattibe143262012-04-16 17:31:41 +03004485 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004486 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004487
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004488 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4489 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004490
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004491 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004492 wl_resource_set_implementation(resource,
4493 &desktop_shell_implementation,
4494 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004495 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004496 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004497 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004498
4499 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4500 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004501}
4502
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004504 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004505 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004506 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004507 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004508 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004509};
4510
4511static void
4512switcher_next(struct switcher *switcher)
4513{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004514 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004515 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004516 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004517 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004518
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004519 /* temporary re-display minimized surfaces */
4520 struct weston_view *tmp;
4521 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004522 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4523 weston_layer_entry_remove(&view->layer_link);
4524 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004525 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4526 *minimized = view;
4527 }
4528
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004529 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004530 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004531 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004532 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004533 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004534 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004535 next = view;
4536 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 view->alpha = 0.25;
4538 weston_view_geometry_dirty(view);
4539 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004540 }
Alex Wu1659daa2012-04-01 20:13:09 +08004541
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004542 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543 view->alpha = 0.25;
4544 weston_view_geometry_dirty(view);
4545 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004546 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004547 }
4548
4549 if (next == NULL)
4550 next = first;
4551
Alex Wu07b26062012-03-12 16:06:01 +08004552 if (next == NULL)
4553 return;
4554
Kristian Høgsberg07045392012-02-19 18:52:44 -05004555 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004556 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004557
4558 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004559 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004561
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004562 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004563 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004564 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004565}
4566
4567static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004568switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004569{
4570 struct switcher *switcher =
4571 container_of(listener, struct switcher, listener);
4572
4573 switcher_next(switcher);
4574}
4575
4576static void
Daniel Stone351eb612012-05-31 15:27:47 -04004577switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004578{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004580 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004581 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004582
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004583 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004584 if (is_focus_view(view))
4585 continue;
4586
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587 view->alpha = 1.0;
4588 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004589 }
4590
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004591 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004592 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004593 keyboard->seat,
4594 WESTON_ACTIVATE_FLAG_CONFIGURE);
4595 }
4596
Kristian Høgsberg07045392012-02-19 18:52:44 -05004597 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004598 weston_keyboard_end_grab(keyboard);
4599 if (keyboard->input_method_resource)
4600 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004601
4602 /* re-hide surfaces that were temporary shown during the switch */
4603 struct weston_view **minimized;
4604 wl_array_for_each(minimized, &switcher->minimized_array) {
4605 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004606 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004607 weston_layer_entry_remove(&(*minimized)->layer_link);
4608 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004609 weston_view_damage_below(*minimized);
4610 }
4611 }
4612 wl_array_release(&switcher->minimized_array);
4613
Kristian Høgsberg07045392012-02-19 18:52:44 -05004614 free(switcher);
4615}
4616
4617static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004618switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004619 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004620{
4621 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004622 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004623
Daniel Stonec9785ea2012-05-30 16:31:52 +01004624 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004625 switcher_next(switcher);
4626}
4627
4628static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004629switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004630 uint32_t mods_depressed, uint32_t mods_latched,
4631 uint32_t mods_locked, uint32_t group)
4632{
4633 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004634 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004635
Daniel Stone351eb612012-05-31 15:27:47 -04004636 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4637 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004638}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004639
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004640static void
4641switcher_cancel(struct weston_keyboard_grab *grab)
4642{
4643 struct switcher *switcher = container_of(grab, struct switcher, grab);
4644
4645 switcher_destroy(switcher);
4646}
4647
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004648static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004649 switcher_key,
4650 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004651 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004652};
4653
4654static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004655switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004656 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004657{
Tiago Vignattibe143262012-04-16 17:31:41 +03004658 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004659 struct switcher *switcher;
4660
4661 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004662 if (!switcher)
4663 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004664
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004665 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004666 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004667 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004668 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004669 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004670
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004671 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004672 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004673 weston_keyboard_start_grab(keyboard, &switcher->grab);
4674 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004675 switcher_next(switcher);
4676}
4677
Pekka Paalanen3c647232011-12-22 13:43:43 +02004678static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004679backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004680 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004681{
4682 struct weston_compositor *compositor = data;
4683 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004684 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004685
4686 /* TODO: we're limiting to simple use cases, where we assume just
4687 * control on the primary display. We'd have to extend later if we
4688 * ever get support for setting backlights on random desktop LCD
4689 * panels though */
4690 output = get_default_output(compositor);
4691 if (!output)
4692 return;
4693
4694 if (!output->set_backlight)
4695 return;
4696
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004697 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4698 backlight_new = output->backlight_current - 25;
4699 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4700 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004701
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004702 if (backlight_new < 5)
4703 backlight_new = 5;
4704 if (backlight_new > 255)
4705 backlight_new = 255;
4706
4707 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004708 output->set_backlight(output, output->backlight_current);
4709}
4710
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004711static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004712force_kill_binding(struct weston_keyboard *keyboard,
4713 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004714{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004715 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004716 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004717 struct desktop_shell *shell = data;
4718 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004719 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004720
Derek Foreman8ae2db52015-07-15 13:00:36 -05004721 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004722 if (!focus_surface)
4723 return;
4724
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004725 wl_signal_emit(&compositor->kill_signal, focus_surface);
4726
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004727 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004728 wl_client_get_credentials(client, &pid, NULL, NULL);
4729
4730 /* Skip clients that we launched ourselves (the credentials of
4731 * the socketpair is ours) */
4732 if (pid == getpid())
4733 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004734
Daniel Stone325fc2d2012-05-30 16:31:58 +01004735 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004736}
4737
4738static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004739workspace_up_binding(struct weston_keyboard *keyboard,
4740 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004741{
4742 struct desktop_shell *shell = data;
4743 unsigned int new_index = shell->workspaces.current;
4744
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004745 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004746 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004747 if (new_index != 0)
4748 new_index--;
4749
4750 change_workspace(shell, new_index);
4751}
4752
4753static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004754workspace_down_binding(struct weston_keyboard *keyboard,
4755 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004756{
4757 struct desktop_shell *shell = data;
4758 unsigned int new_index = shell->workspaces.current;
4759
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004760 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004761 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004762 if (new_index < shell->workspaces.num - 1)
4763 new_index++;
4764
4765 change_workspace(shell, new_index);
4766}
4767
4768static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004769workspace_f_binding(struct weston_keyboard *keyboard,
4770 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004771{
4772 struct desktop_shell *shell = data;
4773 unsigned int new_index;
4774
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004775 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004776 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004777 new_index = key - KEY_F1;
4778 if (new_index >= shell->workspaces.num)
4779 new_index = shell->workspaces.num - 1;
4780
4781 change_workspace(shell, new_index);
4782}
4783
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004784static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004785workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004786 const struct timespec *time, uint32_t key,
4787 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004788{
4789 struct desktop_shell *shell = data;
4790 unsigned int new_index = shell->workspaces.current;
4791
4792 if (shell->locked)
4793 return;
4794
4795 if (new_index != 0)
4796 new_index--;
4797
Derek Foreman8ae2db52015-07-15 13:00:36 -05004798 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004799}
4800
4801static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004802workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004803 const struct timespec *time, uint32_t key,
4804 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004805{
4806 struct desktop_shell *shell = data;
4807 unsigned int new_index = shell->workspaces.current;
4808
4809 if (shell->locked)
4810 return;
4811
4812 if (new_index < shell->workspaces.num - 1)
4813 new_index++;
4814
Derek Foreman8ae2db52015-07-15 13:00:36 -05004815 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004816}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004817
4818static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304819shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004820{
4821 struct weston_output *output, *first_output;
4822 struct weston_compositor *ec = view->surface->compositor;
4823 struct shell_surface *shsurf;
4824 float x, y;
4825 int visible;
4826
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304827 if (wl_list_empty(&ec->output_list))
4828 return;
4829
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004830 x = view->geometry.x;
4831 y = view->geometry.y;
4832
4833 /* At this point the destroyed output is not in the list anymore.
4834 * If the view is still visible somewhere, we leave where it is,
4835 * otherwise, move it to the first output. */
4836 visible = 0;
4837 wl_list_for_each(output, &ec->output_list, link) {
4838 if (pixman_region32_contains_point(&output->region,
4839 x, y, NULL)) {
4840 visible = 1;
4841 break;
4842 }
4843 }
4844
4845 if (!visible) {
4846 first_output = container_of(ec->output_list.next,
4847 struct weston_output, link);
4848
4849 x = first_output->x + first_output->width / 4;
4850 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004851
4852 weston_view_set_position(view, x, y);
4853 } else {
4854 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004855 }
4856
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004857
4858 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004859 if (!shsurf)
4860 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004861
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004862 shsurf->saved_position_valid = false;
4863 set_maximized(shsurf, false);
4864 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004865}
4866
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004867void
4868shell_for_each_layer(struct desktop_shell *shell,
4869 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004870{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004871 struct workspace **ws;
4872
4873 func(shell, &shell->fullscreen_layer, data);
4874 func(shell, &shell->panel_layer, data);
4875 func(shell, &shell->background_layer, data);
4876 func(shell, &shell->lock_layer, data);
4877 func(shell, &shell->input_panel_layer, data);
4878
4879 wl_array_for_each(ws, &shell->workspaces.array)
4880 func(shell, &(*ws)->layer, data);
4881}
4882
4883static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304884shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004885 struct weston_layer *layer,
4886 void *data)
4887{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004888 struct weston_view *view;
4889
Marius Vladea40d6d2018-02-08 18:46:42 +02004890 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304891 shell_reposition_view_on_output_change(view);
4892
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004893}
4894
4895static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004896shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004897{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004898 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004899
Harish Krupodc3edc02019-04-20 17:50:18 +05304900 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004901
Pekka Paalanen719ca872021-05-24 16:53:40 +03004902 if (shell_output->fade.animation) {
4903 weston_view_animation_destroy(shell_output->fade.animation);
4904 shell_output->fade.animation = NULL;
4905 }
4906
4907 if (shell_output->fade.view) {
4908 /* destroys the view as well */
4909 weston_surface_destroy(shell_output->fade.view->surface);
4910 }
4911
4912 if (shell_output->fade.startup_timer)
4913 wl_event_source_remove(shell_output->fade.startup_timer);
4914
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004915 if (shell_output->panel_surface)
4916 wl_list_remove(&shell_output->panel_surface_listener.link);
4917 if (shell_output->background_surface)
4918 wl_list_remove(&shell_output->background_surface_listener.link);
4919 wl_list_remove(&shell_output->destroy_listener.link);
4920 wl_list_remove(&shell_output->link);
4921 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004922}
4923
4924static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004925handle_output_destroy(struct wl_listener *listener, void *data)
4926{
4927 struct shell_output *shell_output =
4928 container_of(listener, struct shell_output, destroy_listener);
4929
4930 shell_output_destroy(shell_output);
4931}
4932
4933static void
David Fort50d962f2016-06-09 17:55:52 +02004934shell_resize_surface_to_output(struct desktop_shell *shell,
4935 struct weston_surface *surface,
4936 const struct weston_output *output)
4937{
4938 if (!surface)
4939 return;
4940
4941 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4942 surface->resource,
4943 output->width,
4944 output->height);
4945}
4946
4947
4948static void
4949handle_output_resized(struct wl_listener *listener, void *data)
4950{
4951 struct desktop_shell *shell =
4952 container_of(listener, struct desktop_shell, resized_listener);
4953 struct weston_output *output = (struct weston_output *)data;
4954 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4955
4956 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4957 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4958}
4959
4960static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004961create_shell_output(struct desktop_shell *shell,
4962 struct weston_output *output)
4963{
4964 struct shell_output *shell_output;
4965
4966 shell_output = zalloc(sizeof *shell_output);
4967 if (shell_output == NULL)
4968 return;
4969
4970 shell_output->output = output;
4971 shell_output->shell = shell;
4972 shell_output->destroy_listener.notify = handle_output_destroy;
4973 wl_signal_add(&output->destroy_signal,
4974 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004975 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304976
4977 if (wl_list_length(&shell->output_list) == 1)
4978 shell_for_each_layer(shell,
4979 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004980}
4981
4982static void
4983handle_output_create(struct wl_listener *listener, void *data)
4984{
4985 struct desktop_shell *shell =
4986 container_of(listener, struct desktop_shell, output_create_listener);
4987 struct weston_output *output = (struct weston_output *)data;
4988
4989 create_shell_output(shell, output);
4990}
4991
4992static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004993handle_output_move_layer(struct desktop_shell *shell,
4994 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004995{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004996 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004997 struct weston_view *view;
4998 float x, y;
4999
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005000 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005001 if (view->output != output)
5002 continue;
5003
5004 x = view->geometry.x + output->move_x;
5005 y = view->geometry.y + output->move_y;
5006 weston_view_set_position(view, x, y);
5007 }
5008}
5009
5010static void
5011handle_output_move(struct wl_listener *listener, void *data)
5012{
5013 struct desktop_shell *shell;
5014
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005015 shell = container_of(listener, struct desktop_shell,
5016 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005017
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005018 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005019}
5020
5021static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005022setup_output_destroy_handler(struct weston_compositor *ec,
5023 struct desktop_shell *shell)
5024{
5025 struct weston_output *output;
5026
5027 wl_list_init(&shell->output_list);
5028 wl_list_for_each(output, &ec->output_list, link)
5029 create_shell_output(shell, output);
5030
5031 shell->output_create_listener.notify = handle_output_create;
5032 wl_signal_add(&ec->output_created_signal,
5033 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005034
5035 shell->output_move_listener.notify = handle_output_move;
5036 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005037}
5038
5039static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005040shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005041{
Tiago Vignattibe143262012-04-16 17:31:41 +03005042 struct desktop_shell *shell =
5043 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005044 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005045 struct shell_output *shell_output, *tmp;
Marius Vladc114fd62021-08-26 16:37:24 +03005046 struct shell_seat *shseat, *shseat_next;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005047
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005048 /* Force state to unlocked so we don't try to fade */
5049 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005050
5051 if (shell->child.client) {
5052 /* disable respawn */
5053 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005054 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005055 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005056
Harsha M Mb8b2c722018-08-07 19:05:02 +05305057 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005058 wl_list_remove(&shell->idle_listener.link);
5059 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005060 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005061
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005062 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005063 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005064
Pekka Paalanen719ca872021-05-24 16:53:40 +03005065 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
5066 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08005067
5068 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005069 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005070 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005071
Marius Vladc114fd62021-08-26 16:37:24 +03005072 wl_list_for_each_safe(shseat, shseat_next, &shell->seat_list, link)
5073 desktop_shell_destroy_seat(shseat);
5074
Pekka Paalanenabd72922021-05-24 14:42:00 +03005075 weston_desktop_destroy(shell->desktop);
5076
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005077 wl_array_for_each(ws, &shell->workspaces.array)
5078 workspace_destroy(*ws);
5079 wl_array_release(&shell->workspaces.array);
5080
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03005081 weston_layer_fini(&shell->fullscreen_layer);
5082 weston_layer_fini(&shell->panel_layer);
5083 weston_layer_fini(&shell->background_layer);
5084 weston_layer_fini(&shell->lock_layer);
5085 weston_layer_fini(&shell->input_panel_layer);
5086 weston_layer_fini(&shell->minimized_layer);
5087
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005088 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005089 free(shell);
5090}
5091
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005092static void
5093shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5094{
5095 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005096 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005097
Bob Ham744e6532016-01-12 10:21:48 +00005098 if (shell->allow_zap)
5099 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5100 MODIFIER_CTRL | MODIFIER_ALT,
5101 terminate_binding, ec);
5102
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005103 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005104 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5105 click_to_activate_binding,
5106 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005107 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5108 click_to_activate_binding,
5109 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005110 weston_compositor_add_touch_binding(ec, 0,
5111 touch_to_activate_binding,
5112 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005113 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5114 backlight_binding, ec);
5115 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5116 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005117
5118 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005119 if (shell->exposay_modifier)
5120 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5121 exposay_binding, shell);
5122
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005123 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005124 if (!mod)
5125 return;
5126
Ian Ray13404c42017-09-18 15:22:01 +03005127 /* This binding is not configurable, but is only enabled if there is a
5128 * valid binding modifier. */
5129 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5130 MODIFIER_SUPER | MODIFIER_ALT,
5131 surface_opacity_binding, NULL);
5132
Ian Rayab7c0b62017-09-18 15:22:00 +03005133 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5134 mod, zoom_axis_binding,
5135 NULL);
5136
Daniel Stone325fc2d2012-05-30 16:31:58 +01005137 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5138 zoom_key_binding, NULL);
5139 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5140 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005141 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5142 maximize_binding, NULL);
5143 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5144 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005145 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5146 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005147 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005148 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005149 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005150 weston_compositor_add_button_binding(ec, BTN_LEFT,
5151 mod | MODIFIER_SHIFT,
5152 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005153
5154 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005155 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005156 rotate_binding, NULL);
5157
Daniel Stone325fc2d2012-05-30 16:31:58 +01005158 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5159 shell);
5160 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5161 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005162 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5163 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005164 weston_compositor_add_key_binding(ec, KEY_K, mod,
5165 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005166 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5167 workspace_up_binding, shell);
5168 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5169 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005170 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5171 workspace_move_surface_up_binding,
5172 shell);
5173 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5174 workspace_move_surface_down_binding,
5175 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005176
5177 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5178 if (shell->workspaces.num > 1) {
5179 num_workspace_bindings = shell->workspaces.num;
5180 if (num_workspace_bindings > 6)
5181 num_workspace_bindings = 6;
5182 for (i = 0; i < num_workspace_bindings; i++)
5183 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5184 workspace_f_binding,
5185 shell);
5186 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005187
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005188 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005189}
5190
Jason Ekstrand024177c2014-04-21 19:42:58 -05005191static void
5192handle_seat_created(struct wl_listener *listener, void *data)
5193{
5194 struct weston_seat *seat = data;
Marius Vladc114fd62021-08-26 16:37:24 +03005195 struct desktop_shell *shell =
5196 container_of(listener, struct desktop_shell, seat_create_listener);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005197
Marius Vladc114fd62021-08-26 16:37:24 +03005198 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005199}
5200
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005201WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005202wet_shell_init(struct weston_compositor *ec,
5203 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005204{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005205 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005206 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005207 struct workspace **pws;
5208 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005209 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005210
Peter Huttererf3d62272013-08-08 11:57:05 +10005211 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005212 if (shell == NULL)
5213 return -1;
5214
Kristian Høgsberg75840622011-09-06 13:48:16 -04005215 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005216
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005217 if (!weston_compositor_add_destroy_listener_once(ec,
5218 &shell->destroy_listener,
5219 shell_destroy)) {
5220 free(shell);
5221 return 0;
5222 }
5223
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005224 shell->idle_listener.notify = idle_handler;
5225 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5226 shell->wake_listener.notify = wake_handler;
5227 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005228 shell->transform_listener.notify = transform_handler;
5229 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005230
Quentin Glidic82681572016-12-17 13:40:51 +01005231 weston_layer_init(&shell->fullscreen_layer, ec);
5232 weston_layer_init(&shell->panel_layer, ec);
5233 weston_layer_init(&shell->background_layer, ec);
5234 weston_layer_init(&shell->lock_layer, ec);
5235 weston_layer_init(&shell->input_panel_layer, ec);
5236
5237 weston_layer_set_position(&shell->fullscreen_layer,
5238 WESTON_LAYER_POSITION_FULLSCREEN);
5239 weston_layer_set_position(&shell->panel_layer,
5240 WESTON_LAYER_POSITION_UI);
5241 weston_layer_set_position(&shell->background_layer,
5242 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005243
5244 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005245 wl_list_init(&shell->workspaces.client_list);
Marius Vladc114fd62021-08-26 16:37:24 +03005246 wl_list_init(&shell->seat_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005247
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005248 if (input_panel_setup(shell) < 0)
5249 return -1;
5250
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005251 shell->text_backend = text_backend_init(ec);
5252 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005253 return -1;
5254
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005255 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005256
Daniel Stonedf8133b2013-11-19 11:37:14 +01005257 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5258 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5259
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005260 for (i = 0; i < shell->workspaces.num; i++) {
5261 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5262 if (pws == NULL)
5263 return -1;
5264
Quentin Glidic82681572016-12-17 13:40:51 +01005265 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005266 if (*pws == NULL)
5267 return -1;
5268 }
5269 activate_workspace(shell, 0);
5270
Quentin Glidic82681572016-12-17 13:40:51 +01005271 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005272
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005273 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005274 wl_list_init(&shell->workspaces.animation.link);
5275 shell->workspaces.animation.frame = animate_workspace_change_frame;
5276
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005277 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5278 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005279 return -1;
5280
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005281 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005282 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005283 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005284 return -1;
5285
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005286 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005287
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005288 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005289
Xiong Zhang6b481422013-10-23 13:58:32 +08005290 setup_output_destroy_handler(ec, shell);
5291
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005292 loop = wl_display_get_event_loop(ec->wl_display);
5293 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005294
Jason Ekstrand024177c2014-04-21 19:42:58 -05005295 wl_list_for_each(seat, &ec->seat_list, link)
Marius Vladc114fd62021-08-26 16:37:24 +03005296 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005297 shell->seat_create_listener.notify = handle_seat_created;
5298 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005299
David Fort50d962f2016-06-09 17:55:52 +02005300 shell->resized_listener.notify = handle_output_resized;
5301 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5302
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005303 screenshooter_create(ec);
5304
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005305 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005306
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005307 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005308
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005309 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5310
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005311 return 0;
5312}