blob: 13b7d4734d0ac2e2453e19c8b602ab8303caf5cd [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
Marius Vladab39e1d2021-03-05 21:40:22 +02001876shell_surface_deactivate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001877{
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
Marius Vladab39e1d2021-03-05 21:40:22 +02001883shell_surface_activate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001884{
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)
Marius Vladab39e1d2021-03-05 21:40:22 +02001898 shell_surface_deactivate(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001899 }
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)
Marius Vladab39e1d2021-03-05 21:40:22 +02001906 shell_surface_activate(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001907 }
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);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002453}
2454
Tiago Vignattibc052c92012-04-19 16:18:18 +03002455static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002456desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2457 void *shell)
2458{
2459 struct shell_surface *shsurf =
2460 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002461 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002462 struct weston_surface *surface =
2463 weston_desktop_surface_get_surface(desktop_surface);
2464
2465 if (!shsurf)
2466 return;
2467
Stefan Agnera8da2082019-06-23 14:53:59 +02002468 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2469 wl_list_remove(&shsurf_child->children_link);
2470 wl_list_init(&shsurf_child->children_link);
2471 }
2472 wl_list_remove(&shsurf->children_link);
2473
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2475
2476 if (shsurf->fullscreen.black_view)
2477 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2478
2479 weston_surface_set_label_func(surface, NULL);
2480 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2481 shsurf->desktop_surface = NULL;
2482
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002483 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002484 if (weston_surface_is_mapped(surface) &&
2485 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002486
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002487 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2488 pixman_region32_fini(&surface->pending.input);
2489 pixman_region32_init(&surface->pending.input);
2490 pixman_region32_fini(&surface->input);
2491 pixman_region32_init(&surface->input);
2492 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2493 fade_out_done, shsurf);
2494 return;
2495 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002496 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002497 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002498 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002499
Marius Vladc30cb292021-08-02 15:47:16 +03002500 weston_view_destroy(shsurf->view);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002501
2502 if (shsurf->output_destroy_listener.notify) {
2503 wl_list_remove(&shsurf->output_destroy_listener.link);
2504 shsurf->output_destroy_listener.notify = NULL;
2505 }
2506
2507 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002508}
2509
2510static void
2511set_maximized_position(struct desktop_shell *shell,
2512 struct shell_surface *shsurf)
2513{
2514 pixman_rectangle32_t area;
2515 struct weston_geometry geometry;
2516
2517 get_output_work_area(shell, shsurf->output, &area);
2518 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2519
2520 weston_view_set_position(shsurf->view,
2521 area.x - geometry.x,
2522 area.y - geometry.y);
2523}
2524
2525static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002526set_position_from_xwayland(struct shell_surface *shsurf)
2527{
2528 struct weston_geometry geometry;
2529 float x;
2530 float y;
2531
2532 assert(shsurf->xwayland.is_set);
2533
2534 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2535 x = shsurf->xwayland.x - geometry.x;
2536 y = shsurf->xwayland.y - geometry.y;
2537
2538 weston_view_set_position(shsurf->view, x, y);
2539
2540#ifdef WM_DEBUG
2541 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2542 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2543 geometry.x, geometry.y, x, y);
2544#endif
2545}
2546
2547static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002548map(struct desktop_shell *shell, struct shell_surface *shsurf,
2549 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002550{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002551 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2553 struct weston_compositor *compositor = shell->compositor;
2554 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002555
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002556 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002557 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558 center_on_output(shsurf->view, shsurf->fullscreen_output);
2559 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002560 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002562 } else if (shsurf->xwayland.is_set) {
2563 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002564 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002565 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002566 }
2567
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002568 /* Surface stacking order, see also activate(). */
2569 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002570
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002571 weston_view_update_transform(shsurf->view);
2572 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002573 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002574 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002575 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002576 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002577
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002578 if (!shell->locked) {
2579 wl_list_for_each(seat, &compositor->seat_list, link)
2580 activate(shell, shsurf->view, seat,
2581 WESTON_ACTIVATE_FLAG_CONFIGURE);
2582 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002583
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002584 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 switch (shell->win_animation_type) {
2586 case ANIMATION_FADE:
2587 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2588 break;
2589 case ANIMATION_ZOOM:
2590 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2591 break;
2592 case ANIMATION_NONE:
2593 default:
2594 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002595 }
2596 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002597}
2598
2599static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002600desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2601 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002602{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002603 struct shell_surface *shsurf =
2604 weston_desktop_surface_get_user_data(desktop_surface);
2605 struct weston_surface *surface =
2606 weston_desktop_surface_get_surface(desktop_surface);
2607 struct weston_view *view = shsurf->view;
2608 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002609 bool was_fullscreen;
2610 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002611
2612 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002613 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002614
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002615 was_fullscreen = shsurf->state.fullscreen;
2616 was_maximized = shsurf->state.maximized;
2617
2618 shsurf->state.fullscreen =
2619 weston_desktop_surface_get_fullscreen(desktop_surface);
2620 shsurf->state.maximized =
2621 weston_desktop_surface_get_maximized(desktop_surface);
2622
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002623 if (!weston_surface_is_mapped(surface)) {
2624 map(shell, shsurf, sx, sy);
2625 surface->is_mapped = true;
2626 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2627 ++surface->ref_count;
2628 return;
2629 }
2630
2631 if (sx == 0 && sy == 0 &&
2632 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002633 shsurf->last_height == surface->height &&
2634 was_fullscreen == shsurf->state.fullscreen &&
2635 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002636 return;
2637
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002638 if (was_fullscreen)
2639 unset_fullscreen(shsurf);
2640 if (was_maximized)
2641 unset_maximized(shsurf);
2642
Quentin Glidic920cf042016-08-16 14:26:20 +02002643 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2644 !shsurf->saved_position_valid) {
2645 shsurf->saved_x = shsurf->view->geometry.x;
2646 shsurf->saved_y = shsurf->view->geometry.y;
2647 shsurf->saved_position_valid = true;
2648
2649 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2650 wl_list_remove(&shsurf->rotation.transform.link);
2651 wl_list_init(&shsurf->rotation.transform.link);
2652 weston_view_geometry_dirty(shsurf->view);
2653 shsurf->saved_rotation_valid = true;
2654 }
2655 }
2656
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002657 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002658 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002659 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002660 set_maximized_position(shell, shsurf);
2661 surface->output = shsurf->output;
2662 } else {
2663 float from_x, from_y;
2664 float to_x, to_y;
2665 float x, y;
2666
2667 if (shsurf->resize_edges) {
2668 sx = 0;
2669 sy = 0;
2670 }
2671
2672 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2673 sx = shsurf->last_width - surface->width;
2674 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2675 sy = shsurf->last_height - surface->height;
2676
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002677 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2678 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2679 x = shsurf->view->geometry.x + to_x - from_x;
2680 y = shsurf->view->geometry.y + to_y - from_y;
2681
2682 weston_view_set_position(shsurf->view, x, y);
2683 }
2684
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002685 shsurf->last_width = surface->width;
2686 shsurf->last_height = surface->height;
2687
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002688 /* XXX: would a fullscreen surface need the same handling? */
2689 if (surface->output) {
2690 wl_list_for_each(view, &surface->views, surface_link)
2691 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002692 }
2693}
2694
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002695static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002696get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2697{
2698 struct desktop_shell *shell;
2699 pixman_rectangle32_t area;
2700
2701 shell = shell_surface_get_shell(shsurf);
2702 get_output_work_area(shell, shsurf->output, &area);
2703
2704 *width = area.width;
2705 *height = area.height;
2706}
2707
2708static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002709set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2710 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002711{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002712 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2713 struct weston_surface *surface =
2714 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2715 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002716
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002717 if (fullscreen) {
2718 /* handle clients launching in fullscreen */
2719 if (output == NULL && !weston_surface_is_mapped(surface)) {
2720 /* Set the output to the one that has focus currently. */
2721 output = get_focused_output(surface->compositor);
2722 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002723
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002724 shell_surface_set_output(shsurf, output);
2725 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002726
Marius Vlad6ebda362020-03-28 00:23:14 +02002727 if (shsurf->output) {
2728 width = shsurf->output->width;
2729 height = shsurf->output->height;
2730 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002731 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2732 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002733 }
2734 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2735 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002736}
2737
2738static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002739desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2740 struct weston_seat *seat, uint32_t serial, void *shell)
2741{
2742 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2743 struct weston_touch *touch = weston_seat_get_touch(seat);
2744 struct shell_surface *shsurf =
2745 weston_desktop_surface_get_user_data(desktop_surface);
2746 struct weston_surface *surface =
2747 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2748 struct wl_resource *resource = surface->resource;
2749 struct weston_surface *focus;
2750
2751 if (pointer &&
2752 pointer->focus &&
2753 pointer->button_count > 0 &&
2754 pointer->grab_serial == serial) {
2755 focus = weston_surface_get_main_surface(pointer->focus->surface);
2756 if ((focus == surface) &&
2757 (surface_move(shsurf, pointer, true) < 0))
2758 wl_resource_post_no_memory(resource);
2759 } else if (touch &&
2760 touch->focus &&
2761 touch->grab_serial == serial) {
2762 focus = weston_surface_get_main_surface(touch->focus->surface);
2763 if ((focus == surface) &&
2764 (surface_touch_move(shsurf, touch) < 0))
2765 wl_resource_post_no_memory(resource);
2766 }
2767}
2768
2769static void
2770desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2771 struct weston_seat *seat, uint32_t serial,
2772 enum weston_desktop_surface_edge edges, void *shell)
2773{
2774 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2775 struct shell_surface *shsurf =
2776 weston_desktop_surface_get_user_data(desktop_surface);
2777 struct weston_surface *surface =
2778 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2779 struct wl_resource *resource = surface->resource;
2780 struct weston_surface *focus;
2781
2782 if (!pointer ||
2783 pointer->button_count == 0 ||
2784 pointer->grab_serial != serial ||
2785 pointer->focus == NULL)
2786 return;
2787
2788 focus = weston_surface_get_main_surface(pointer->focus->surface);
2789 if (focus != surface)
2790 return;
2791
2792 if (surface_resize(shsurf, pointer, edges) < 0)
2793 wl_resource_post_no_memory(resource);
2794}
2795
2796static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002797desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2798 struct weston_desktop_surface *parent,
2799 void *shell)
2800{
Marius Vlada27658e2020-01-17 12:32:55 +02002801 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002802 struct shell_surface *shsurf =
2803 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002804
Marius Vlada27658e2020-01-17 12:32:55 +02002805 /* unlink any potential child */
2806 wl_list_remove(&shsurf->children_link);
2807
2808 if (parent) {
2809 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2810 wl_list_insert(shsurf_parent->children_list.prev,
2811 &shsurf->children_link);
2812 } else {
2813 wl_list_init(&shsurf->children_link);
2814 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002815}
2816
2817static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002818desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2819 bool fullscreen,
2820 struct weston_output *output, void *shell)
2821{
2822 struct shell_surface *shsurf =
2823 weston_desktop_surface_get_user_data(desktop_surface);
2824
2825 set_fullscreen(shsurf, fullscreen, output);
2826}
2827
2828static void
2829set_maximized(struct shell_surface *shsurf, bool maximized)
2830{
2831 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2832 struct weston_surface *surface =
2833 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2834 int32_t width = 0, height = 0;
2835
2836 if (maximized) {
2837 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002838
2839 if (!weston_surface_is_mapped(surface))
2840 output = get_focused_output(surface->compositor);
2841 else
2842 output = surface->output;
2843
2844 shell_surface_set_output(shsurf, output);
2845
Derek Foreman927d9e22017-10-06 14:37:44 -05002846 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847 }
2848 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2849 weston_desktop_surface_set_size(desktop_surface, width, height);
2850}
2851
2852static void
2853desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2854 bool maximized, void *shell)
2855{
2856 struct shell_surface *shsurf =
2857 weston_desktop_surface_get_user_data(desktop_surface);
2858
2859 set_maximized(shsurf, maximized);
2860}
2861
2862static void
2863desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2864 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002865{
2866 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002867 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002868
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002869 /* apply compositor's own minimization logic (hide) */
2870 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002871}
2872
Rafael Antognollie2a34552013-12-03 15:35:45 -02002873static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002874set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002875{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002876 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002877
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002878 if (pointer->grab->interface == &busy_cursor_grab_interface)
2879 return;
2880
2881 grab = malloc(sizeof *grab);
2882 if (!grab)
2883 return;
2884
2885 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2886 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2887 /* Mark the shsurf as ungrabbed so that button binding is able
2888 * to move it. */
2889 shsurf->grabbed = 0;
2890}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002891
2892static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002893end_busy_cursor(struct weston_compositor *compositor,
2894 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002895{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002896 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002897 struct shell_grab *grab;
2898 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002899
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002900 wl_list_for_each(seat, &compositor->seat_list, link) {
2901 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2902 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002903
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002904 if (!pointer)
2905 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002906
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002907 if (pointer->grab->interface != &busy_cursor_grab_interface)
2908 continue;
2909
2910 grab = (struct shell_grab *) pointer->grab;
2911 shsurf = grab->shsurf;
2912 if (!shsurf)
2913 continue;
2914
2915 grab_client =
2916 weston_desktop_surface_get_client(shsurf->desktop_surface);
2917 if (grab_client == desktop_client) {
2918 shell_grab_end(grab);
2919 free(grab);
2920 }
2921 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002922}
2923
2924static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002925desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2926 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002927{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002928 struct shell_surface *shsurf =
2929 weston_desktop_surface_get_user_data(desktop_surface);
2930 bool *unresponsive = user_data;
2931
2932 shsurf->unresponsive = *unresponsive;
2933}
2934
2935static void
2936desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2937 void *shell_)
2938{
2939 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002940 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002941 struct weston_seat *seat;
2942 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002943
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002944 weston_desktop_client_for_each_surface(desktop_client,
2945 desktop_surface_set_unresponsive,
2946 &unresponsive);
2947
2948
2949 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2950 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2951 struct weston_desktop_client *grab_client;
2952
2953 if (!pointer || !pointer->focus)
2954 continue;
2955
2956 shsurf = get_shell_surface(pointer->focus->surface);
2957 if (!shsurf)
2958 continue;
2959
2960 grab_client =
2961 weston_desktop_surface_get_client(shsurf->desktop_surface);
2962 if (grab_client == desktop_client)
2963 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002964 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002965}
2966
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002967static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002968desktop_surface_pong(struct weston_desktop_client *desktop_client,
2969 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002970{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002971 struct desktop_shell *shell = shell_;
2972 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002973
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002974 weston_desktop_client_for_each_surface(desktop_client,
2975 desktop_surface_set_unresponsive,
2976 &unresponsive);
2977 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002978}
2979
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002980static void
2981desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2982 int32_t x, int32_t y, void *shell_)
2983{
2984 struct shell_surface *shsurf =
2985 weston_desktop_surface_get_user_data(surface);
2986
2987 shsurf->xwayland.x = x;
2988 shsurf->xwayland.y = y;
2989 shsurf->xwayland.is_set = true;
2990}
2991
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002992static const struct weston_desktop_api shell_desktop_api = {
2993 .struct_size = sizeof(struct weston_desktop_api),
2994 .surface_added = desktop_surface_added,
2995 .surface_removed = desktop_surface_removed,
2996 .committed = desktop_surface_committed,
2997 .move = desktop_surface_move,
2998 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002999 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003000 .fullscreen_requested = desktop_surface_fullscreen_requested,
3001 .maximized_requested = desktop_surface_maximized_requested,
3002 .minimized_requested = desktop_surface_minimized_requested,
3003 .ping_timeout = desktop_surface_ping_timeout,
3004 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02003005 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003006};
3007
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003008/* ************************ *
3009 * end of libweston-desktop *
3010 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04003011static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02003012configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003013{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003014 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003015
Semi Malinene7a52fb2018-04-26 11:08:10 +02003016 if (!ev->output)
3017 return;
3018
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003019 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 if (v->output == ev->output && v != ev) {
3021 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003022 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003023 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003024 }
3025 }
3026
Quentin Glidice8bf9592016-06-23 18:55:20 +02003027 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003028 ev->surface->is_mapped = true;
3029 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003030
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003031 if (wl_list_empty(&ev->layer_link.link)) {
3032 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003033 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003034 }
3035}
3036
David Fort50d962f2016-06-09 17:55:52 +02003037
3038static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003039find_shell_output_from_weston_output(struct desktop_shell *shell,
3040 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003041{
3042 struct shell_output *shell_output;
3043
3044 wl_list_for_each(shell_output, &shell->output_list, link) {
3045 if (shell_output->output == output)
3046 return shell_output;
3047 }
3048
3049 return NULL;
3050}
3051
Pekka Paalanen8274d902014-08-06 19:36:51 +03003052static int
3053background_get_label(struct weston_surface *surface, char *buf, size_t len)
3054{
3055 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003056 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003057}
3058
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003059static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003060background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003061{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003062 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003063 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003064
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 view = container_of(es->views.next, struct weston_view, surface_link);
3066
Quentin Glidice8bf9592016-06-23 18:55:20 +02003067 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003068}
3069
3070static void
David Fort50d962f2016-06-09 17:55:52 +02003071handle_background_surface_destroy(struct wl_listener *listener, void *data)
3072{
3073 struct shell_output *output =
3074 container_of(listener, struct shell_output, background_surface_listener);
3075
3076 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003077 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003078 output->background_surface = NULL;
3079}
3080
3081static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003082desktop_shell_set_background(struct wl_client *client,
3083 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003084 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003085 struct wl_resource *surface_resource)
3086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003088 struct weston_surface *surface =
3089 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003090 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003091 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003092
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003093 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003094 wl_resource_post_error(surface_resource,
3095 WL_DISPLAY_ERROR_INVALID_OBJECT,
3096 "surface role already assigned");
3097 return;
3098 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003099
Jason Ekstranda7af7042013-10-12 22:38:11 -05003100 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3101 weston_view_destroy(view);
3102 view = weston_view_create(surface);
3103
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003104 surface->committed = background_committed;
3105 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003106 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003107 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003108 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003109
3110 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003111 if (sh_output->background_surface) {
3112 /* The output already has a background, tell our helper
3113 * there is no need for another one. */
3114 weston_desktop_shell_send_configure(resource, 0,
3115 surface_resource,
3116 0, 0);
3117 } else {
3118 weston_desktop_shell_send_configure(resource, 0,
3119 surface_resource,
3120 surface->output->width,
3121 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003122
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003123 sh_output->background_surface = surface;
3124
3125 sh_output->background_surface_listener.notify =
3126 handle_background_surface_destroy;
3127 wl_signal_add(&surface->destroy_signal,
3128 &sh_output->background_surface_listener);
3129 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003130}
3131
Pekka Paalanen8274d902014-08-06 19:36:51 +03003132static int
3133panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3134{
3135 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003136 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003137}
3138
Kristian Høgsberg75840622011-09-06 13:48:16 -04003139static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003140panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003141{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003142 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003143 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003144 int width, height;
3145 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003146
Jason Ekstranda7af7042013-10-12 22:38:11 -05003147 view = container_of(es->views.next, struct weston_view, surface_link);
3148
Quentin Glidice8bf9592016-06-23 18:55:20 +02003149 get_panel_size(shell, view, &width, &height);
3150 switch (shell->panel_position) {
3151 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3152 break;
3153 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3154 y = view->output->height - height;
3155 break;
3156 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3157 break;
3158 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3159 x = view->output->width - width;
3160 break;
3161 }
3162
3163 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003164}
3165
3166static void
David Fort50d962f2016-06-09 17:55:52 +02003167handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3168{
3169 struct shell_output *output =
3170 container_of(listener, struct shell_output, panel_surface_listener);
3171
3172 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003173 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003174 output->panel_surface = NULL;
3175}
3176
3177
3178static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003179desktop_shell_set_panel(struct wl_client *client,
3180 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003181 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003182 struct wl_resource *surface_resource)
3183{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003184 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003185 struct weston_surface *surface =
3186 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003187 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003188 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003189
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003190 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003191 wl_resource_post_error(surface_resource,
3192 WL_DISPLAY_ERROR_INVALID_OBJECT,
3193 "surface role already assigned");
3194 return;
3195 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003196
Jason Ekstranda7af7042013-10-12 22:38:11 -05003197 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3198 weston_view_destroy(view);
3199 view = weston_view_create(surface);
3200
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003201 surface->committed = panel_committed;
3202 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003203 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003204 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003205 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003206
3207 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003208 if (sh_output->panel_surface) {
3209 /* The output already has a panel, tell our helper
3210 * there is no need for another one. */
3211 weston_desktop_shell_send_configure(resource, 0,
3212 surface_resource,
3213 0, 0);
3214 } else {
3215 weston_desktop_shell_send_configure(resource, 0,
3216 surface_resource,
3217 surface->output->width,
3218 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003219
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003220 sh_output->panel_surface = surface;
3221
3222 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3223 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3224 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003225}
3226
Pekka Paalanen8274d902014-08-06 19:36:51 +03003227static int
3228lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3229{
3230 return snprintf(buf, len, "lock window");
3231}
3232
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003233static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003234lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003235{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003236 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 struct weston_view *view;
3238
3239 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003240
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003241 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003242 return;
3243
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003245
3246 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003247 weston_layer_entry_insert(&shell->lock_layer.view_list,
3248 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003250 surface->is_mapped = true;
3251 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003252 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003253 }
3254}
3255
3256static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003257handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003258{
Tiago Vignattibe143262012-04-16 17:31:41 +03003259 struct desktop_shell *shell =
3260 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003261
Martin Minarik6d118362012-06-07 18:01:59 +02003262 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003263 shell->lock_surface = NULL;
3264}
3265
3266static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003267desktop_shell_set_lock_surface(struct wl_client *client,
3268 struct wl_resource *resource,
3269 struct wl_resource *surface_resource)
3270{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003271 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003272 struct weston_surface *surface =
3273 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003274
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003275 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003276
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003277 if (!shell->locked)
3278 return;
3279
Pekka Paalanen98262232011-12-01 10:42:22 +02003280 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003281
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003282 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003283 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003284 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003285
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003286 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003287 surface->committed = lock_surface_committed;
3288 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003289 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003290}
3291
3292static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003293resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003294{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003295 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003296
Quentin Glidic82681572016-12-17 13:40:51 +01003297 weston_layer_unset_position(&shell->lock_layer);
3298
3299 if (shell->showing_input_panels)
3300 weston_layer_set_position(&shell->input_panel_layer,
3301 WESTON_LAYER_POSITION_TOP_UI);
3302 weston_layer_set_position(&shell->fullscreen_layer,
3303 WESTON_LAYER_POSITION_FULLSCREEN);
3304 weston_layer_set_position(&shell->panel_layer,
3305 WESTON_LAYER_POSITION_UI);
3306 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003307
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003308 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003309
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003310 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003311 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003312 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003313}
3314
3315static void
3316desktop_shell_unlock(struct wl_client *client,
3317 struct wl_resource *resource)
3318{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003319 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003320
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003321 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003322
3323 if (shell->locked)
3324 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003325}
3326
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003327static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003328desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003329 struct wl_resource *resource,
3330 struct wl_resource *surface_resource)
3331{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003332 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003333
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003334 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003335 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003336}
3337
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003338static void
3339desktop_shell_desktop_ready(struct wl_client *client,
3340 struct wl_resource *resource)
3341{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003342 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003343
3344 shell_fade_startup(shell);
3345}
3346
Jonny Lamb765760d2014-08-20 15:53:19 +02003347static void
3348desktop_shell_set_panel_position(struct wl_client *client,
3349 struct wl_resource *resource,
3350 uint32_t position)
3351{
3352 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3353
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003354 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3355 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3356 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3357 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003358 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003359 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003360 "bad position argument");
3361 return;
3362 }
3363
3364 shell->panel_position = position;
3365}
3366
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003367static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003368 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003369 desktop_shell_set_panel,
3370 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003371 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003372 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003373 desktop_shell_desktop_ready,
3374 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003375};
3376
3377static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003378move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003379 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003380{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003381 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003382 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003383 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003384
Derek Foreman8ae2db52015-07-15 13:00:36 -05003385 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003386 return;
3387
Derek Foreman8ae2db52015-07-15 13:00:36 -05003388 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003389
Pekka Paalanen01388e22013-04-25 13:57:44 +03003390 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003391 if (surface == NULL)
3392 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003393
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003394 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003395 if (shsurf == NULL ||
3396 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3397 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003398 return;
3399
Derek Foreman794fa0e2015-07-15 13:00:38 -05003400 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003401}
3402
3403static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003404maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003405 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003406{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003407 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003408 struct weston_surface *surface;
3409 struct shell_surface *shsurf;
3410
3411 surface = weston_surface_get_main_surface(focus);
3412 if (surface == NULL)
3413 return;
3414
3415 shsurf = get_shell_surface(surface);
3416 if (shsurf == NULL)
3417 return;
3418
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003419 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003420}
3421
3422static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003423fullscreen_binding(struct weston_keyboard *keyboard,
3424 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003425{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003426 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003427 struct weston_surface *surface;
3428 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003429 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003430
3431 surface = weston_surface_get_main_surface(focus);
3432 if (surface == NULL)
3433 return;
3434
3435 shsurf = get_shell_surface(surface);
3436 if (shsurf == NULL)
3437 return;
3438
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003439 fullscreen =
3440 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003441
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003442 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003443}
3444
3445static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003446touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003447{
Derek Foreman362656b2014-09-04 10:23:05 -05003448 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003449 struct weston_surface *surface;
3450 struct shell_surface *shsurf;
3451
Derek Foreman8ae2db52015-07-15 13:00:36 -05003452 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003453 return;
3454
Derek Foreman8ae2db52015-07-15 13:00:36 -05003455 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003456 surface = weston_surface_get_main_surface(focus);
3457 if (surface == NULL)
3458 return;
3459
3460 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003461 if (shsurf == NULL ||
3462 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3463 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003464 return;
3465
Derek Foremanb7674ae2015-07-15 13:00:37 -05003466 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003467}
3468
3469static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003470resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003471 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003472{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003473 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003474 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003475 uint32_t edges = 0;
3476 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003477 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003478
Derek Foreman8ae2db52015-07-15 13:00:36 -05003479 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003480 return;
3481
Derek Foreman8ae2db52015-07-15 13:00:36 -05003482 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003483
Pekka Paalanen01388e22013-04-25 13:57:44 +03003484 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003485 if (surface == NULL)
3486 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003487
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003488 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003489 if (shsurf == NULL ||
3490 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3491 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003492 return;
3493
Jason Ekstranda7af7042013-10-12 22:38:11 -05003494 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003495 wl_fixed_to_int(pointer->grab_x),
3496 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003497 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003498
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003499 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003500 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003501 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003502 edges |= 0;
3503 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003504 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003505
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003506 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003507 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003508 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003509 edges |= 0;
3510 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003511 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003512
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003513 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003514}
3515
3516static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003517surface_opacity_binding(struct weston_pointer *pointer,
3518 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003519 struct weston_pointer_axis_event *event,
3520 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003521{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003522 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003523 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003524 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003525 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003526
Pekka Paalanen01388e22013-04-25 13:57:44 +03003527 /* XXX: broken for windows containing sub-surfaces */
3528 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003529 if (surface == NULL)
3530 return;
3531
3532 shsurf = get_shell_surface(surface);
3533 if (!shsurf)
3534 return;
3535
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003536 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 if (shsurf->view->alpha > 1.0)
3539 shsurf->view->alpha = 1.0;
3540 if (shsurf->view->alpha < step)
3541 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003542
Jason Ekstranda7af7042013-10-12 22:38:11 -05003543 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003544 weston_surface_damage(surface);
3545}
3546
3547static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003548do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3549 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003550{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003551 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003552 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003553 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003554 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003555
Derek Foreman1281a362015-07-31 16:55:32 -05003556 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003557 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3558 return;
3559 }
3560
Scott Moreauccbf29d2012-02-22 14:21:41 -07003561 wl_list_for_each(output, &compositor->output_list, link) {
3562 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003563 wl_fixed_to_double(pointer->x),
3564 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003565 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003566 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003567 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003568 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003569 increment = -output->zoom.increment;
3570 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003571 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003572 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003573 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003574 else
3575 increment = 0;
3576
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003577 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003578
Scott Moreaue6603982012-06-11 13:07:51 -06003579 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003580 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003581 else if (output->zoom.level > output->zoom.max_level)
3582 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003583
3584 if (!output->zoom.active) {
3585 if (output->zoom.level <= 0.0)
3586 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003587 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003588 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003589
Scott Moreaue6603982012-06-11 13:07:51 -06003590 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003591
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003593 }
3594 }
3595}
3596
Scott Moreauccbf29d2012-02-22 14:21:41 -07003597static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003598zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003599 struct weston_pointer_axis_event *event,
3600 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003601{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003602 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003603}
3604
3605static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003606zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003607 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003608{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003609 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003610}
3611
3612static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003613terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003614 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003615{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003616 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003617
Pekka Paalanen052032d2019-02-06 10:44:37 +02003618 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003619}
3620
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003621static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003622rotate_grab_motion(struct weston_pointer_grab *grab,
3623 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003624 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625{
3626 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003627 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003628 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003629 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003630 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003631 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003632
Jonas Ådahld2510102014-10-05 21:39:14 +02003633 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003634
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003635 if (!shsurf)
3636 return;
3637
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003638 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3639
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003640 cx = 0.5f * surface->width;
3641 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642
Daniel Stone37816df2012-05-16 18:45:18 +01003643 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3644 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003645 r = sqrtf(dx * dx + dy * dy);
3646
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003647 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003649
3650 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003651 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003652 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003654 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003655 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003656
3657 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003658 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003659 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003660 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003661 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003662
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003663 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003664 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003665 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003667 wl_list_init(&shsurf->rotation.transform.link);
3668 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003669 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003670 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003671
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003672 /* We need to adjust the position of the surface
3673 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 cposx = shsurf->view->geometry.x + cx;
3675 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003676 dposx = rotate->center.x - cposx;
3677 dposy = rotate->center.y - cposy;
3678 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679 weston_view_set_position(shsurf->view,
3680 shsurf->view->geometry.x + dposx,
3681 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003682 }
3683
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003684 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003685 * lazily applies the damage due to rotation update.
3686 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003687 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003688}
3689
3690static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003691rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003692 const struct timespec *time,
3693 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003694{
3695 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003696 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003697 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003698 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003699 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003700
Daniel Stone4dbadb12012-05-30 16:31:51 +01003701 if (pointer->button_count == 0 &&
3702 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003703 if (shsurf)
3704 weston_matrix_multiply(&shsurf->rotation.rotation,
3705 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003706 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003707 free(rotate);
3708 }
3709}
3710
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003711static void
3712rotate_grab_cancel(struct weston_pointer_grab *grab)
3713{
3714 struct rotate_grab *rotate =
3715 container_of(grab, struct rotate_grab, base.grab);
3716
3717 shell_grab_end(&rotate->base);
3718 free(rotate);
3719}
3720
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003721static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003722 noop_grab_focus,
3723 rotate_grab_motion,
3724 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003725 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003726 noop_grab_axis_source,
3727 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003728 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003729};
3730
3731static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003732surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003733{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003734 struct weston_surface *surface =
3735 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003736 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003737 float dx, dy;
3738 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003739
Pekka Paalanen460099f2012-01-20 16:48:25 +02003740 rotate = malloc(sizeof *rotate);
3741 if (!rotate)
3742 return;
3743
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003744 weston_view_to_global_float(shsurf->view,
3745 surface->width * 0.5f,
3746 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003747 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003748
Derek Foreman74de4692015-07-15 13:00:39 -05003749 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3750 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003751 r = sqrtf(dx * dx + dy * dy);
3752 if (r > 20.0f) {
3753 struct weston_matrix inverse;
3754
3755 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003756 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003757 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003758
3759 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003760 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003761 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003762 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003763 weston_matrix_init(&rotate->rotation);
3764 }
3765
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003766 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003767 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003768}
3769
3770static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003771rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3772 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003773{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003774 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003775 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003776 struct shell_surface *surface;
3777
Derek Foreman8ae2db52015-07-15 13:00:36 -05003778 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003779 return;
3780
Derek Foreman8ae2db52015-07-15 13:00:36 -05003781 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003782
Pekka Paalanen01388e22013-04-25 13:57:44 +03003783 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003784 if (base_surface == NULL)
3785 return;
3786
3787 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003788 if (surface == NULL ||
3789 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3790 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003791 return;
3792
Derek Foreman74de4692015-07-15 13:00:39 -05003793 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003794}
3795
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003796/* Move all fullscreen layers down to the current workspace and hide their
3797 * black views. The surfaces' state is set to both fullscreen and lowered,
3798 * and this is reversed when such a surface is re-configured, see
3799 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3800 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003801 * lowering_output = NULL - Lower on all outputs, else only lower on the
3802 * specified output.
3803 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003804 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003805 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003806void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003807lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003808 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003809{
3810 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003812
3813 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003814 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003815 &shell->fullscreen_layer.view_list.link,
3816 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003817 struct shell_surface *shsurf = get_shell_surface(view->surface);
3818
3819 if (!shsurf)
3820 continue;
3821
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003822 /* Only lower surfaces which have lowering_output as their fullscreen
3823 * output, unless a NULL output asks for lowering on all outputs.
3824 */
3825 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3826 continue;
3827
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003828 /* We can have a non-fullscreen popup for a fullscreen surface
3829 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003830 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003831 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003832 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3833 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003834 weston_view_damage_below(shsurf->fullscreen.black_view);
3835
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003836 }
3837
3838 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003839 weston_layer_entry_remove(&view->layer_link);
3840 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003841 weston_view_damage_below(view);
3842 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003843
3844 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003845 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003846}
3847
Stefan Agnera8da2082019-06-23 14:53:59 +02003848static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3849{
3850 struct shell_surface *shsurf_child;
3851
3852 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3853 if (weston_view_is_mapped(shsurf_child->view))
3854 return shsurf_child;
3855 }
3856
3857 return NULL;
3858}
3859
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003860void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003861activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003862 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003863{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003864 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003865 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003866 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003867 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003868 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003869 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003870
Pekka Paalanen01388e22013-04-25 13:57:44 +03003871 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003872 shsurf = get_shell_surface(main_surface);
3873 assert(shsurf);
3874
Stefan Agnera8da2082019-06-23 14:53:59 +02003875 shsurf_child = get_last_child(shsurf);
3876 if (shsurf_child) {
3877 /* Activate last xdg child instead of parent. */
3878 activate(shell, shsurf_child->view, seat, flags);
3879 return;
3880 }
3881
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003882 /* Only demote fullscreen surfaces on the output of activated shsurf.
3883 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003884 if (shsurf->output)
3885 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003886
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003887 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003888
Jonas Ådahl8538b222012-08-29 22:13:03 +02003889 state = ensure_focus_state(shell, seat);
3890 if (state == NULL)
3891 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003892
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003893 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003894 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003895
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003896 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3897 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003898 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003899
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003900 /* Update the surface’s layer. This brings it to the top of the stacking
3901 * order as appropriate. */
3902 shell_surface_update_layer(shsurf);
3903
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003904 if (shell->focus_animation_type != ANIMATION_NONE) {
3905 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003906 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003907 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003908}
3909
Alex Wu21858432012-04-01 20:13:08 +08003910/* no-op func for checking black surface */
3911static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003912black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003913{
3914}
3915
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003916static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003917is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003918{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003919 struct weston_surface *surface = view->surface;
3920
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003921 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003922 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003923 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003924 return true;
3925 }
3926 return false;
3927}
3928
Kristian Høgsberg75840622011-09-06 13:48:16 -04003929static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003930activate_binding(struct weston_seat *seat,
3931 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003932 struct weston_view *focus_view,
3933 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003934{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003935 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003936 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003937
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003938 if (!focus_view)
3939 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003940
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003941 if (is_black_surface_view(focus_view, &main_view))
3942 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003943
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003944 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003945 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003946 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003947
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003948 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003949}
3950
3951static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003952click_to_activate_binding(struct weston_pointer *pointer,
3953 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003954 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003955{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003956 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003957 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003958 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003959 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003960
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003961 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003962 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003963 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003964}
3965
3966static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003967touch_to_activate_binding(struct weston_touch *touch,
3968 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003969 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003970{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003971 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003972 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003973 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003974 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003975
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003976 activate_binding(touch->seat, data, touch->focus,
3977 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003978}
3979
3980static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003981unfocus_all_seats(struct desktop_shell *shell)
3982{
3983 struct weston_seat *seat, *next;
3984
3985 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003986 struct weston_keyboard *keyboard =
3987 weston_seat_get_keyboard(seat);
3988
3989 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003990 continue;
3991
Derek Foreman1281a362015-07-31 16:55:32 -05003992 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003993 }
3994}
3995
3996static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003997lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003998{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003999 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004000
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004001 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004002 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004003 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004004 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004005
4006 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004007
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004008 /* Hide all surfaces by removing the fullscreen, panel and
4009 * toplevel layers. This way nothing else can show or receive
4010 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004011
Quentin Glidic82681572016-12-17 13:40:51 +01004012 weston_layer_unset_position(&shell->panel_layer);
4013 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004014 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01004015 weston_layer_unset_position(&shell->input_panel_layer);
4016 weston_layer_unset_position(&ws->layer);
4017
4018 weston_layer_set_position(&shell->lock_layer,
4019 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004020
Derek Foreman004b4a12015-07-20 16:28:13 -05004021 weston_compositor_sleep(shell->compositor);
4022
Neil Robertsb4a91702014-04-09 16:33:32 +01004023 /* Remove the keyboard focus on all seats. This will be
4024 * restored to the workspace's saved state via
4025 * restore_focus_state when the compositor is unlocked */
4026 unfocus_all_seats(shell);
4027
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004028 /* TODO: disable bindings that should not work while locked. */
4029
4030 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004031}
4032
4033static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004034unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004035{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004036 struct wl_resource *shell_resource;
4037
Pekka Paalanend81c2162011-11-16 13:47:34 +02004038 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004039 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004040 return;
4041 }
4042
4043 /* If desktop-shell client has gone away, unlock immediately. */
4044 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004045 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004046 return;
4047 }
4048
4049 if (shell->prepare_event_sent)
4050 return;
4051
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004052 shell_resource = shell->child.desktop_shell;
4053 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004054 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004055}
4056
4057static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004058shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004059{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004060 struct shell_output *shell_output = data;
4061 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004062
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004063 shell_output->fade.animation = NULL;
4064 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004065 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004066 weston_surface_destroy(shell_output->fade.view->surface);
4067 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004068 break;
4069 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004070 lock(shell);
4071 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004072 default:
4073 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004074 }
4075}
4076
Jason Ekstranda7af7042013-10-12 22:38:11 -05004077static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004078shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004079{
4080 struct weston_compositor *compositor = shell->compositor;
4081 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004082 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004083
4084 surface = weston_surface_create(compositor);
4085 if (!surface)
4086 return NULL;
4087
Jason Ekstranda7af7042013-10-12 22:38:11 -05004088 view = weston_view_create(surface);
4089 if (!view) {
4090 weston_surface_destroy(surface);
4091 return NULL;
4092 }
4093
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004094 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4095 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004096 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004097 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4098 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004099 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004100 surface->is_mapped = true;
4101 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004102
Jason Ekstranda7af7042013-10-12 22:38:11 -05004103 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004104}
4105
4106static void
4107shell_fade(struct desktop_shell *shell, enum fade_type type)
4108{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004109 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004110 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004111
4112 switch (type) {
4113 case FADE_IN:
4114 tint = 0.0;
4115 break;
4116 case FADE_OUT:
4117 tint = 1.0;
4118 break;
4119 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004120 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004121 return;
4122 }
4123
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004124 /* Create a separate fade surface for each output */
4125 wl_list_for_each(shell_output, &shell->output_list, link) {
4126 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004127
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004128 if (shell_output->fade.view == NULL) {
4129 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4130 if (!shell_output->fade.view)
4131 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004132
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004133 shell_output->fade.view->alpha = 1.0 - tint;
4134 weston_view_update_transform(shell_output->fade.view);
4135 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004136
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004137 if (shell_output->fade.view->output == NULL) {
4138 /* If the black view gets a NULL output, we lost the
4139 * last output and we'll just cancel the fade. This
4140 * happens when you close the last window under the
4141 * X11 or Wayland backends. */
4142 shell->locked = false;
4143 weston_surface_destroy(shell_output->fade.view->surface);
4144 shell_output->fade.view = NULL;
4145 } else if (shell_output->fade.animation) {
4146 weston_fade_update(shell_output->fade.animation, tint);
4147 } else {
4148 shell_output->fade.animation =
4149 weston_fade_run(shell_output->fade.view,
4150 1.0 - tint, tint, 300.0,
4151 shell_fade_done_for_output, shell_output);
4152 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004153 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004154}
4155
4156static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004157do_shell_fade_startup(void *data)
4158{
4159 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004160 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004161
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004162 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004163 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004164 } else {
4165 weston_log("desktop shell: "
4166 "unexpected fade-in animation type %d\n",
4167 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004168 wl_list_for_each(shell_output, &shell->output_list, link) {
4169 weston_surface_destroy(shell_output->fade.view->surface);
4170 shell_output->fade.view = NULL;
4171 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004172 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004173}
4174
4175static void
4176shell_fade_startup(struct desktop_shell *shell)
4177{
4178 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004179 struct shell_output *shell_output;
4180 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004181
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004182 wl_list_for_each(shell_output, &shell->output_list, link) {
4183 if (!shell_output->fade.startup_timer)
4184 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004185
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004186 wl_event_source_remove(shell_output->fade.startup_timer);
4187 shell_output->fade.startup_timer = NULL;
4188 has_fade = true;
4189 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004190
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004191 if (has_fade) {
4192 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4193 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4194 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004195}
4196
4197static int
4198fade_startup_timeout(void *data)
4199{
4200 struct desktop_shell *shell = data;
4201
4202 shell_fade_startup(shell);
4203 return 0;
4204}
4205
4206static void
4207shell_fade_init(struct desktop_shell *shell)
4208{
4209 /* Make compositor output all black, and wait for the desktop-shell
4210 * client to signal it is ready, then fade in. The timer triggers a
4211 * fade-in, in case the desktop-shell client takes too long.
4212 */
4213
4214 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004215 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004216
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004217 if (shell->startup_animation_type == ANIMATION_NONE)
4218 return;
4219
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004220 wl_list_for_each(shell_output, &shell->output_list, link) {
4221 if (shell_output->fade.view != NULL) {
4222 weston_log("%s: warning: fade surface already exists\n",
4223 __func__);
4224 continue;
4225 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004226
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004227 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4228 if (!shell_output->fade.view)
4229 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004230
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004231 weston_view_update_transform(shell_output->fade.view);
4232 weston_surface_damage(shell_output->fade.view->surface);
4233
4234 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4235 shell_output->fade.startup_timer =
4236 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4237 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4238 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004239}
4240
4241static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004242idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004243{
4244 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004245 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004246
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004247 struct weston_seat *seat;
4248
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004249 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4250 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004251
4252 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004253 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004254}
4255
4256static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004257wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004258{
4259 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004260 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004261
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004262 unlock(shell);
4263}
4264
4265static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004266transform_handler(struct wl_listener *listener, void *data)
4267{
4268 struct weston_surface *surface = data;
4269 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004270 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004271 int x, y;
4272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004273 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004274 return;
4275
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004276 api = shsurf->shell->xwayland_surface_api;
4277 if (!api) {
4278 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4279 shsurf->shell->xwayland_surface_api = api;
4280 }
4281
4282 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004283 return;
4284
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004285 if (!weston_view_is_mapped(shsurf->view))
4286 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004287
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004288 x = shsurf->view->geometry.x;
4289 y = shsurf->view->geometry.y;
4290
4291 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004292}
4293
4294static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004296{
Giulio Camuffob8366642013-04-25 13:57:46 +03004297 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004298 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004299
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004300 if (!output) {
4301 weston_view_set_position(view, 0, 0);
4302 return;
4303 }
4304
Jason Ekstranda7af7042013-10-12 22:38:11 -05004305 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004306
4307 x = output->x + (output->width - width) / 2 - surf_x / 2;
4308 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004309
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004310 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004311}
4312
4313static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004314weston_view_set_initial_position(struct weston_view *view,
4315 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004316{
4317 struct weston_compositor *compositor = shell->compositor;
4318 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004319 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004320 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004321 struct weston_output *output, *target_output = NULL;
4322 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004323 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004324
4325 /* As a heuristic place the new window on the same output as the
4326 * pointer. Falling back to the output containing 0, 0.
4327 *
4328 * TODO: Do something clever for touch too?
4329 */
4330 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004331 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4332
4333 if (pointer) {
4334 ix = wl_fixed_to_int(pointer->x);
4335 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004336 break;
4337 }
4338 }
4339
4340 wl_list_for_each(output, &compositor->output_list, link) {
4341 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4342 target_output = output;
4343 break;
4344 }
4345 }
4346
4347 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 weston_view_set_position(view, 10 + random() % 400,
4349 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004350 return;
4351 }
4352
4353 /* Valid range within output where the surface will still be onscreen.
4354 * If this is negative it means that the surface is bigger than
4355 * output.
4356 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004357 get_output_work_area(shell, target_output, &area);
4358
Derek Foremanf814c5d2015-04-15 12:23:54 -05004359 x = area.x;
4360 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004361 range_x = area.width - view->surface->width;
4362 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004363
Rob Bradford4cb88c72012-08-13 15:18:44 +01004364 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004365 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004366
4367 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004368 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004369
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004371}
4372
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004373static bool
4374check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4375{
4376 struct timespec now;
4377
4378 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4379 return false;
4380
4381 /*
4382 * If the shell helper client dies before the session has been
4383 * up for roughly 30 seconds, better just make Weston shut down,
4384 * because the user likely has no way to interact with the desktop
4385 * anyway.
4386 */
4387 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4388 weston_log("Error: %s apparently cannot run at all.\n",
4389 shell->client);
4390 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004391 weston_compositor_exit_with_code(shell->compositor,
4392 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004393
4394 return true;
4395 }
4396
4397 return false;
4398}
4399
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004400static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004401
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004402static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004403respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004404{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004405 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004406
4407 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004408 weston_compositor_get_time(&time);
4409 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004410 shell->child.deathstamp = time;
4411 shell->child.deathcount = 0;
4412 }
4413
4414 shell->child.deathcount++;
4415 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004416 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004417 return;
4418 }
4419
Pekka Paalanen826dc142014-08-27 12:11:53 +03004420 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004421 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004422}
4423
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004424static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004425desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4426{
4427 struct desktop_shell *shell;
4428
4429 shell = container_of(listener, struct desktop_shell,
4430 child.client_destroy_listener);
4431
Pekka Paalanen826dc142014-08-27 12:11:53 +03004432 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004433 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004434 /*
4435 * unbind_desktop_shell() will reset shell->child.desktop_shell
4436 * before the respawned process has a chance to create a new
4437 * desktop_shell object, because we are being called from the
4438 * wl_client destructor which destroys all wl_resources before
4439 * returning.
4440 */
4441
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004442 if (!check_desktop_shell_crash_too_early(shell))
4443 respawn_desktop_shell_process(shell);
4444
Pekka Paalanen826dc142014-08-27 12:11:53 +03004445 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004446}
4447
4448static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004449launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004450{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004451 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004452
Pekka Paalanen826dc142014-08-27 12:11:53 +03004453 shell->child.client = weston_client_start(shell->compositor,
4454 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004455
Arnaud Vrac42631192014-08-25 20:56:46 +02004456 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004457 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004458 return;
4459 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004460
4461 shell->child.client_destroy_listener.notify =
4462 desktop_shell_client_destroy;
4463 wl_client_add_destroy_listener(shell->child.client,
4464 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004465}
4466
4467static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004468unbind_desktop_shell(struct wl_resource *resource)
4469{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004470 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004471
4472 if (shell->locked)
4473 resume_desktop(shell);
4474
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004475 shell->child.desktop_shell = NULL;
4476 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004477}
4478
4479static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004480bind_desktop_shell(struct wl_client *client,
4481 void *data, uint32_t version, uint32_t id)
4482{
Tiago Vignattibe143262012-04-16 17:31:41 +03004483 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004484 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004485
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004486 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4487 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004488
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004489 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004490 wl_resource_set_implementation(resource,
4491 &desktop_shell_implementation,
4492 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004493 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004494 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004495 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004496
4497 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4498 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004499}
4500
Kristian Høgsberg07045392012-02-19 18:52:44 -05004501struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004502 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004503 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004504 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004505 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004506 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004507};
4508
4509static void
4510switcher_next(struct switcher *switcher)
4511{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004512 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004513 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004514 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004515 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004516
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004517 /* temporary re-display minimized surfaces */
4518 struct weston_view *tmp;
4519 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004520 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4521 weston_layer_entry_remove(&view->layer_link);
4522 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004523 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4524 *minimized = view;
4525 }
4526
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004527 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004528 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004529 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004530 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004531 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004532 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004533 next = view;
4534 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 view->alpha = 0.25;
4536 weston_view_geometry_dirty(view);
4537 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004538 }
Alex Wu1659daa2012-04-01 20:13:09 +08004539
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004540 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 view->alpha = 0.25;
4542 weston_view_geometry_dirty(view);
4543 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004544 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545 }
4546
4547 if (next == NULL)
4548 next = first;
4549
Alex Wu07b26062012-03-12 16:06:01 +08004550 if (next == NULL)
4551 return;
4552
Kristian Høgsberg07045392012-02-19 18:52:44 -05004553 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004554 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004555
4556 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004557 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004559
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004560 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004561 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004562 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563}
4564
4565static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004566switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004567{
4568 struct switcher *switcher =
4569 container_of(listener, struct switcher, listener);
4570
4571 switcher_next(switcher);
4572}
4573
4574static void
Daniel Stone351eb612012-05-31 15:27:47 -04004575switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004576{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004577 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004578 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004579 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004580
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004581 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004582 if (is_focus_view(view))
4583 continue;
4584
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585 view->alpha = 1.0;
4586 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004587 }
4588
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004589 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004590 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004591 keyboard->seat,
4592 WESTON_ACTIVATE_FLAG_CONFIGURE);
4593 }
4594
Kristian Høgsberg07045392012-02-19 18:52:44 -05004595 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004596 weston_keyboard_end_grab(keyboard);
4597 if (keyboard->input_method_resource)
4598 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004599
4600 /* re-hide surfaces that were temporary shown during the switch */
4601 struct weston_view **minimized;
4602 wl_array_for_each(minimized, &switcher->minimized_array) {
4603 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004604 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004605 weston_layer_entry_remove(&(*minimized)->layer_link);
4606 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004607 weston_view_damage_below(*minimized);
4608 }
4609 }
4610 wl_array_release(&switcher->minimized_array);
4611
Kristian Høgsberg07045392012-02-19 18:52:44 -05004612 free(switcher);
4613}
4614
4615static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004616switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004617 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004618{
4619 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004620 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004621
Daniel Stonec9785ea2012-05-30 16:31:52 +01004622 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004623 switcher_next(switcher);
4624}
4625
4626static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004627switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004628 uint32_t mods_depressed, uint32_t mods_latched,
4629 uint32_t mods_locked, uint32_t group)
4630{
4631 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004632 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004633
Daniel Stone351eb612012-05-31 15:27:47 -04004634 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4635 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004636}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004637
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004638static void
4639switcher_cancel(struct weston_keyboard_grab *grab)
4640{
4641 struct switcher *switcher = container_of(grab, struct switcher, grab);
4642
4643 switcher_destroy(switcher);
4644}
4645
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004646static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004647 switcher_key,
4648 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004649 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004650};
4651
4652static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004653switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004654 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004655{
Tiago Vignattibe143262012-04-16 17:31:41 +03004656 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004657 struct switcher *switcher;
4658
4659 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004660 if (!switcher)
4661 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004662
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004663 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004664 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004665 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004666 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004667 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004668
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004669 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004670 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004671 weston_keyboard_start_grab(keyboard, &switcher->grab);
4672 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004673 switcher_next(switcher);
4674}
4675
Pekka Paalanen3c647232011-12-22 13:43:43 +02004676static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004677backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004678 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004679{
4680 struct weston_compositor *compositor = data;
4681 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004682 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004683
4684 /* TODO: we're limiting to simple use cases, where we assume just
4685 * control on the primary display. We'd have to extend later if we
4686 * ever get support for setting backlights on random desktop LCD
4687 * panels though */
4688 output = get_default_output(compositor);
4689 if (!output)
4690 return;
4691
4692 if (!output->set_backlight)
4693 return;
4694
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004695 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4696 backlight_new = output->backlight_current - 25;
4697 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4698 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004699
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004700 if (backlight_new < 5)
4701 backlight_new = 5;
4702 if (backlight_new > 255)
4703 backlight_new = 255;
4704
4705 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004706 output->set_backlight(output, output->backlight_current);
4707}
4708
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004709static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004710force_kill_binding(struct weston_keyboard *keyboard,
4711 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004712{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004713 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004714 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004715 struct desktop_shell *shell = data;
4716 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004717 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004718
Derek Foreman8ae2db52015-07-15 13:00:36 -05004719 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004720 if (!focus_surface)
4721 return;
4722
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004723 wl_signal_emit(&compositor->kill_signal, focus_surface);
4724
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004725 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004726 wl_client_get_credentials(client, &pid, NULL, NULL);
4727
4728 /* Skip clients that we launched ourselves (the credentials of
4729 * the socketpair is ours) */
4730 if (pid == getpid())
4731 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004732
Daniel Stone325fc2d2012-05-30 16:31:58 +01004733 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004734}
4735
4736static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004737workspace_up_binding(struct weston_keyboard *keyboard,
4738 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004739{
4740 struct desktop_shell *shell = data;
4741 unsigned int new_index = shell->workspaces.current;
4742
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004743 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004744 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004745 if (new_index != 0)
4746 new_index--;
4747
4748 change_workspace(shell, new_index);
4749}
4750
4751static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004752workspace_down_binding(struct weston_keyboard *keyboard,
4753 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004754{
4755 struct desktop_shell *shell = data;
4756 unsigned int new_index = shell->workspaces.current;
4757
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004758 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004759 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004760 if (new_index < shell->workspaces.num - 1)
4761 new_index++;
4762
4763 change_workspace(shell, new_index);
4764}
4765
4766static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004767workspace_f_binding(struct weston_keyboard *keyboard,
4768 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004769{
4770 struct desktop_shell *shell = data;
4771 unsigned int new_index;
4772
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004773 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004774 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004775 new_index = key - KEY_F1;
4776 if (new_index >= shell->workspaces.num)
4777 new_index = shell->workspaces.num - 1;
4778
4779 change_workspace(shell, new_index);
4780}
4781
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004782static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004783workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004784 const struct timespec *time, uint32_t key,
4785 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004786{
4787 struct desktop_shell *shell = data;
4788 unsigned int new_index = shell->workspaces.current;
4789
4790 if (shell->locked)
4791 return;
4792
4793 if (new_index != 0)
4794 new_index--;
4795
Derek Foreman8ae2db52015-07-15 13:00:36 -05004796 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004797}
4798
4799static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004800workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004801 const struct timespec *time, uint32_t key,
4802 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004803{
4804 struct desktop_shell *shell = data;
4805 unsigned int new_index = shell->workspaces.current;
4806
4807 if (shell->locked)
4808 return;
4809
4810 if (new_index < shell->workspaces.num - 1)
4811 new_index++;
4812
Derek Foreman8ae2db52015-07-15 13:00:36 -05004813 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004814}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004815
4816static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304817shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004818{
4819 struct weston_output *output, *first_output;
4820 struct weston_compositor *ec = view->surface->compositor;
4821 struct shell_surface *shsurf;
4822 float x, y;
4823 int visible;
4824
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304825 if (wl_list_empty(&ec->output_list))
4826 return;
4827
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004828 x = view->geometry.x;
4829 y = view->geometry.y;
4830
4831 /* At this point the destroyed output is not in the list anymore.
4832 * If the view is still visible somewhere, we leave where it is,
4833 * otherwise, move it to the first output. */
4834 visible = 0;
4835 wl_list_for_each(output, &ec->output_list, link) {
4836 if (pixman_region32_contains_point(&output->region,
4837 x, y, NULL)) {
4838 visible = 1;
4839 break;
4840 }
4841 }
4842
4843 if (!visible) {
4844 first_output = container_of(ec->output_list.next,
4845 struct weston_output, link);
4846
4847 x = first_output->x + first_output->width / 4;
4848 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004849
4850 weston_view_set_position(view, x, y);
4851 } else {
4852 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004853 }
4854
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004855
4856 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004857 if (!shsurf)
4858 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004859
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004860 shsurf->saved_position_valid = false;
4861 set_maximized(shsurf, false);
4862 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004863}
4864
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004865void
4866shell_for_each_layer(struct desktop_shell *shell,
4867 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004868{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004869 struct workspace **ws;
4870
4871 func(shell, &shell->fullscreen_layer, data);
4872 func(shell, &shell->panel_layer, data);
4873 func(shell, &shell->background_layer, data);
4874 func(shell, &shell->lock_layer, data);
4875 func(shell, &shell->input_panel_layer, data);
4876
4877 wl_array_for_each(ws, &shell->workspaces.array)
4878 func(shell, &(*ws)->layer, data);
4879}
4880
4881static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304882shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004883 struct weston_layer *layer,
4884 void *data)
4885{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004886 struct weston_view *view;
4887
Marius Vladea40d6d2018-02-08 18:46:42 +02004888 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304889 shell_reposition_view_on_output_change(view);
4890
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004891}
4892
4893static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004894shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004895{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004896 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004897
Harish Krupodc3edc02019-04-20 17:50:18 +05304898 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004899
Pekka Paalanen719ca872021-05-24 16:53:40 +03004900 if (shell_output->fade.animation) {
4901 weston_view_animation_destroy(shell_output->fade.animation);
4902 shell_output->fade.animation = NULL;
4903 }
4904
4905 if (shell_output->fade.view) {
4906 /* destroys the view as well */
4907 weston_surface_destroy(shell_output->fade.view->surface);
4908 }
4909
4910 if (shell_output->fade.startup_timer)
4911 wl_event_source_remove(shell_output->fade.startup_timer);
4912
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004913 if (shell_output->panel_surface)
4914 wl_list_remove(&shell_output->panel_surface_listener.link);
4915 if (shell_output->background_surface)
4916 wl_list_remove(&shell_output->background_surface_listener.link);
4917 wl_list_remove(&shell_output->destroy_listener.link);
4918 wl_list_remove(&shell_output->link);
4919 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004920}
4921
4922static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004923handle_output_destroy(struct wl_listener *listener, void *data)
4924{
4925 struct shell_output *shell_output =
4926 container_of(listener, struct shell_output, destroy_listener);
4927
4928 shell_output_destroy(shell_output);
4929}
4930
4931static void
David Fort50d962f2016-06-09 17:55:52 +02004932shell_resize_surface_to_output(struct desktop_shell *shell,
4933 struct weston_surface *surface,
4934 const struct weston_output *output)
4935{
4936 if (!surface)
4937 return;
4938
4939 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4940 surface->resource,
4941 output->width,
4942 output->height);
4943}
4944
4945
4946static void
4947handle_output_resized(struct wl_listener *listener, void *data)
4948{
4949 struct desktop_shell *shell =
4950 container_of(listener, struct desktop_shell, resized_listener);
4951 struct weston_output *output = (struct weston_output *)data;
4952 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4953
4954 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4955 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4956}
4957
4958static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004959create_shell_output(struct desktop_shell *shell,
4960 struct weston_output *output)
4961{
4962 struct shell_output *shell_output;
4963
4964 shell_output = zalloc(sizeof *shell_output);
4965 if (shell_output == NULL)
4966 return;
4967
4968 shell_output->output = output;
4969 shell_output->shell = shell;
4970 shell_output->destroy_listener.notify = handle_output_destroy;
4971 wl_signal_add(&output->destroy_signal,
4972 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004973 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304974
4975 if (wl_list_length(&shell->output_list) == 1)
4976 shell_for_each_layer(shell,
4977 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004978}
4979
4980static void
4981handle_output_create(struct wl_listener *listener, void *data)
4982{
4983 struct desktop_shell *shell =
4984 container_of(listener, struct desktop_shell, output_create_listener);
4985 struct weston_output *output = (struct weston_output *)data;
4986
4987 create_shell_output(shell, output);
4988}
4989
4990static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004991handle_output_move_layer(struct desktop_shell *shell,
4992 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004993{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004994 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004995 struct weston_view *view;
4996 float x, y;
4997
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004998 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004999 if (view->output != output)
5000 continue;
5001
5002 x = view->geometry.x + output->move_x;
5003 y = view->geometry.y + output->move_y;
5004 weston_view_set_position(view, x, y);
5005 }
5006}
5007
5008static void
5009handle_output_move(struct wl_listener *listener, void *data)
5010{
5011 struct desktop_shell *shell;
5012
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005013 shell = container_of(listener, struct desktop_shell,
5014 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005015
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005016 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005017}
5018
5019static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005020setup_output_destroy_handler(struct weston_compositor *ec,
5021 struct desktop_shell *shell)
5022{
5023 struct weston_output *output;
5024
5025 wl_list_init(&shell->output_list);
5026 wl_list_for_each(output, &ec->output_list, link)
5027 create_shell_output(shell, output);
5028
5029 shell->output_create_listener.notify = handle_output_create;
5030 wl_signal_add(&ec->output_created_signal,
5031 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005032
5033 shell->output_move_listener.notify = handle_output_move;
5034 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005035}
5036
5037static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005038shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005039{
Tiago Vignattibe143262012-04-16 17:31:41 +03005040 struct desktop_shell *shell =
5041 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005042 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005043 struct shell_output *shell_output, *tmp;
Marius Vladc114fd62021-08-26 16:37:24 +03005044 struct shell_seat *shseat, *shseat_next;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005045
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005046 /* Force state to unlocked so we don't try to fade */
5047 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005048
5049 if (shell->child.client) {
5050 /* disable respawn */
5051 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005052 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005053 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005054
Harsha M Mb8b2c722018-08-07 19:05:02 +05305055 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005056 wl_list_remove(&shell->idle_listener.link);
5057 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005058 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005059
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005060 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005061 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005062
Pekka Paalanen719ca872021-05-24 16:53:40 +03005063 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
5064 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08005065
5066 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005067 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005068 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005069
Marius Vladc114fd62021-08-26 16:37:24 +03005070 wl_list_for_each_safe(shseat, shseat_next, &shell->seat_list, link)
5071 desktop_shell_destroy_seat(shseat);
5072
Pekka Paalanenabd72922021-05-24 14:42:00 +03005073 weston_desktop_destroy(shell->desktop);
5074
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005075 wl_array_for_each(ws, &shell->workspaces.array)
5076 workspace_destroy(*ws);
5077 wl_array_release(&shell->workspaces.array);
5078
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03005079 weston_layer_fini(&shell->fullscreen_layer);
5080 weston_layer_fini(&shell->panel_layer);
5081 weston_layer_fini(&shell->background_layer);
5082 weston_layer_fini(&shell->lock_layer);
5083 weston_layer_fini(&shell->input_panel_layer);
5084 weston_layer_fini(&shell->minimized_layer);
5085
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005086 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005087 free(shell);
5088}
5089
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005090static void
5091shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5092{
5093 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005094 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005095
Bob Ham744e6532016-01-12 10:21:48 +00005096 if (shell->allow_zap)
5097 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5098 MODIFIER_CTRL | MODIFIER_ALT,
5099 terminate_binding, ec);
5100
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005101 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005102 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5103 click_to_activate_binding,
5104 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005105 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5106 click_to_activate_binding,
5107 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005108 weston_compositor_add_touch_binding(ec, 0,
5109 touch_to_activate_binding,
5110 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005111 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5112 backlight_binding, ec);
5113 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5114 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005115
5116 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005117 if (shell->exposay_modifier)
5118 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5119 exposay_binding, shell);
5120
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005121 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005122 if (!mod)
5123 return;
5124
Ian Ray13404c42017-09-18 15:22:01 +03005125 /* This binding is not configurable, but is only enabled if there is a
5126 * valid binding modifier. */
5127 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5128 MODIFIER_SUPER | MODIFIER_ALT,
5129 surface_opacity_binding, NULL);
5130
Ian Rayab7c0b62017-09-18 15:22:00 +03005131 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5132 mod, zoom_axis_binding,
5133 NULL);
5134
Daniel Stone325fc2d2012-05-30 16:31:58 +01005135 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5136 zoom_key_binding, NULL);
5137 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5138 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005139 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5140 maximize_binding, NULL);
5141 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5142 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005143 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5144 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005145 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005146 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005147 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005148 weston_compositor_add_button_binding(ec, BTN_LEFT,
5149 mod | MODIFIER_SHIFT,
5150 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005151
5152 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005153 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005154 rotate_binding, NULL);
5155
Daniel Stone325fc2d2012-05-30 16:31:58 +01005156 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5157 shell);
5158 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5159 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005160 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5161 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005162 weston_compositor_add_key_binding(ec, KEY_K, mod,
5163 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005164 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5165 workspace_up_binding, shell);
5166 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5167 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005168 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5169 workspace_move_surface_up_binding,
5170 shell);
5171 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5172 workspace_move_surface_down_binding,
5173 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005174
5175 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5176 if (shell->workspaces.num > 1) {
5177 num_workspace_bindings = shell->workspaces.num;
5178 if (num_workspace_bindings > 6)
5179 num_workspace_bindings = 6;
5180 for (i = 0; i < num_workspace_bindings; i++)
5181 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5182 workspace_f_binding,
5183 shell);
5184 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005185
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005186 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005187}
5188
Jason Ekstrand024177c2014-04-21 19:42:58 -05005189static void
5190handle_seat_created(struct wl_listener *listener, void *data)
5191{
5192 struct weston_seat *seat = data;
Marius Vladc114fd62021-08-26 16:37:24 +03005193 struct desktop_shell *shell =
5194 container_of(listener, struct desktop_shell, seat_create_listener);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005195
Marius Vladc114fd62021-08-26 16:37:24 +03005196 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005197}
5198
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005199WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005200wet_shell_init(struct weston_compositor *ec,
5201 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005202{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005203 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005204 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005205 struct workspace **pws;
5206 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005207 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005208
Peter Huttererf3d62272013-08-08 11:57:05 +10005209 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005210 if (shell == NULL)
5211 return -1;
5212
Kristian Høgsberg75840622011-09-06 13:48:16 -04005213 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005214
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005215 if (!weston_compositor_add_destroy_listener_once(ec,
5216 &shell->destroy_listener,
5217 shell_destroy)) {
5218 free(shell);
5219 return 0;
5220 }
5221
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005222 shell->idle_listener.notify = idle_handler;
5223 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5224 shell->wake_listener.notify = wake_handler;
5225 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005226 shell->transform_listener.notify = transform_handler;
5227 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005228
Quentin Glidic82681572016-12-17 13:40:51 +01005229 weston_layer_init(&shell->fullscreen_layer, ec);
5230 weston_layer_init(&shell->panel_layer, ec);
5231 weston_layer_init(&shell->background_layer, ec);
5232 weston_layer_init(&shell->lock_layer, ec);
5233 weston_layer_init(&shell->input_panel_layer, ec);
5234
5235 weston_layer_set_position(&shell->fullscreen_layer,
5236 WESTON_LAYER_POSITION_FULLSCREEN);
5237 weston_layer_set_position(&shell->panel_layer,
5238 WESTON_LAYER_POSITION_UI);
5239 weston_layer_set_position(&shell->background_layer,
5240 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005241
5242 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005243 wl_list_init(&shell->workspaces.client_list);
Marius Vladc114fd62021-08-26 16:37:24 +03005244 wl_list_init(&shell->seat_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005245
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005246 if (input_panel_setup(shell) < 0)
5247 return -1;
5248
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005249 shell->text_backend = text_backend_init(ec);
5250 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005251 return -1;
5252
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005253 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005254
Daniel Stonedf8133b2013-11-19 11:37:14 +01005255 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5256 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5257
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005258 for (i = 0; i < shell->workspaces.num; i++) {
5259 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5260 if (pws == NULL)
5261 return -1;
5262
Quentin Glidic82681572016-12-17 13:40:51 +01005263 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005264 if (*pws == NULL)
5265 return -1;
5266 }
5267 activate_workspace(shell, 0);
5268
Quentin Glidic82681572016-12-17 13:40:51 +01005269 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005270
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005271 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005272 wl_list_init(&shell->workspaces.animation.link);
5273 shell->workspaces.animation.frame = animate_workspace_change_frame;
5274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005275 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5276 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005277 return -1;
5278
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005279 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005280 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005281 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005282 return -1;
5283
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005284 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005285
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005286 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005287
Xiong Zhang6b481422013-10-23 13:58:32 +08005288 setup_output_destroy_handler(ec, shell);
5289
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005290 loop = wl_display_get_event_loop(ec->wl_display);
5291 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005292
Jason Ekstrand024177c2014-04-21 19:42:58 -05005293 wl_list_for_each(seat, &ec->seat_list, link)
Marius Vladc114fd62021-08-26 16:37:24 +03005294 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005295 shell->seat_create_listener.notify = handle_seat_created;
5296 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005297
David Fort50d962f2016-06-09 17:55:52 +02005298 shell->resized_listener.notify = handle_output_resized;
5299 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5300
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005301 screenshooter_create(ec);
5302
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005303 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005304
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005305 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005306
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005307 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5308
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005309 return 0;
5310}