blob: e0ee506b192d64c14a3160a81ec6caa8e212b984 [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"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/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"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020045#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
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500107 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800108 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800109 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700110 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800111 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100112
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500113 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200114 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500115 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200116 } rotation;
117
118 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800119 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500120 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800121 } fullscreen;
122
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200123 struct weston_transform workspace_transform;
124
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500125 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500126 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200127 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200128
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700129 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200130 bool fullscreen;
131 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100132 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200133 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700134
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200135 struct {
136 bool is_set;
137 int32_t x;
138 int32_t y;
139 } xwayland;
140
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500141 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500142
143 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200144};
145
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300146struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400147 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300148 struct shell_surface *shsurf;
149 struct wl_listener shsurf_destroy_listener;
150};
151
Rusty Lynch1084da52013-08-15 09:10:08 -0700152struct shell_touch_grab {
153 struct weston_touch_grab grab;
154 struct shell_surface *shsurf;
155 struct wl_listener shsurf_destroy_listener;
156 struct weston_touch *touch;
157};
158
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300159struct weston_move_grab {
160 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100161 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500162 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500163};
164
Rusty Lynch1084da52013-08-15 09:10:08 -0700165struct weston_touch_move_grab {
166 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800167 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700168 wl_fixed_t dx, dy;
169};
170
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300172 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500173 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200175 float x;
176 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200177 } center;
178};
179
Giulio Camuffo5085a752013-03-25 21:42:45 +0100180struct shell_seat {
181 struct weston_seat *seat;
182 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500183 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184
Jason Ekstrand024177c2014-04-21 19:42:58 -0500185 struct wl_listener caps_changed_listener;
186 struct wl_listener pointer_focus_listener;
187 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100188};
189
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800190
Alex Wubd3354b2012-04-17 17:20:49 +0800191static struct desktop_shell *
192shell_surface_get_shell(struct shell_surface *shsurf);
193
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500194static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200195set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
196
197static void
Derek Foreman74de4692015-07-15 13:00:39 -0500198surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500199
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300200static void
201shell_fade_startup(struct desktop_shell *shell);
202
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700203static void
204shell_fade(struct desktop_shell *shell, enum fade_type type);
205
Philip Withnallbecb77e2013-11-25 18:01:30 +0000206static struct shell_seat *
207get_shell_seat(struct weston_seat *seat);
208
Jonny Lambd73c6942014-08-20 15:53:20 +0200209static void
210get_output_panel_size(struct desktop_shell *shell,
211 struct weston_output *output,
212 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700213
Philip Withnall648a4dd2013-11-25 18:01:44 +0000214static void
215shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
216
Pekka Paalanen8274d902014-08-06 19:36:51 +0300217static int
218shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
219{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200221 struct weston_desktop_surface *desktop_surface =
222 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200224 t = weston_desktop_surface_get_title(desktop_surface);
225 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226
227 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200228 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229 t ? " '" : "", t ?: "", t ? "'" : "",
230 c ? " of " : "", c ?: "");
231}
232
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500233static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400234destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235{
236 struct shell_grab *grab;
237
238 grab = container_of(listener, struct shell_grab,
239 shsurf_destroy_listener);
240
241 grab->shsurf = NULL;
242}
243
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800244struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500245get_default_view(struct weston_surface *surface)
246{
247 struct shell_surface *shsurf;
248 struct weston_view *view;
249
250 if (!surface || wl_list_empty(&surface->views))
251 return NULL;
252
253 shsurf = get_shell_surface(surface);
254 if (shsurf)
255 return shsurf->view;
256
257 wl_list_for_each(view, &surface->views, surface_link)
258 if (weston_view_is_mapped(view))
259 return view;
260
261 return container_of(surface->views.next, struct weston_view, surface_link);
262}
263
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300264static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300265shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400266 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400268 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800269 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300270{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300271 struct desktop_shell *shell = shsurf->shell;
272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200273 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400274
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300275 grab->grab.interface = interface;
276 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400277 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500278 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700281 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400282 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400283 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800284 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400285 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286 weston_pointer_set_focus(pointer,
287 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 wl_fixed_from_int(0),
289 wl_fixed_from_int(0));
290 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291}
292
Jonny Lambd73c6942014-08-20 15:53:20 +0200293static void
Quentin Glidic581df062016-06-23 18:55:19 +0200294get_panel_size(struct desktop_shell *shell,
295 struct weston_view *view,
296 int *width,
297 int *height)
298{
299 float x1, y1;
300 float x2, y2;
301 weston_view_to_global_float(view, 0, 0, &x1, &y1);
302 weston_view_to_global_float(view,
303 view->surface->width,
304 view->surface->height,
305 &x2, &y2);
306 *width = (int)(x2 - x1);
307 *height = (int)(y2 - y1);
308}
309
310static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200311get_output_panel_size(struct desktop_shell *shell,
312 struct weston_output *output,
313 int *width,
314 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700315{
316 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200317
318 *width = 0;
319 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700320
321 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200322 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700323
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300324 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200325 if (view->surface->output == output) {
326 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200327 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700328 }
329 }
330
Jonny Lambd73c6942014-08-20 15:53:20 +0200331 /* the correct view wasn't found */
332}
333
334static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200335get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200336 struct weston_output *output,
337 pixman_rectangle32_t *area)
338{
339 int32_t panel_width = 0, panel_height = 0;
340
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200341 if (!output) {
342 area->x = 0;
343 area->y = 0;
344 area->width = 0;
345 area->height = 0;
346
347 return;
348 }
349
Derek Foremanf814c5d2015-04-15 12:23:54 -0500350 area->x = output->x;
351 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200352
353 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200354 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800355 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200356 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000358 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 area->width = output->width;
361 area->height = output->height - panel_height;
362 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800363 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500364 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000365 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800366 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200367 area->width = output->width - panel_width;
368 area->height = output->height;
369 break;
370 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700371}
372
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300375{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400377 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400379
380 if (grab->shsurf->resize_edges) {
381 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400382 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700383 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300384
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700385 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386}
387
388static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700389shell_touch_grab_start(struct shell_touch_grab *grab,
390 const struct weston_touch_grab_interface *interface,
391 struct shell_surface *shsurf,
392 struct weston_touch *touch)
393{
394 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800395
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200396 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700397
Rusty Lynch1084da52013-08-15 09:10:08 -0700398 grab->grab.interface = interface;
399 grab->shsurf = shsurf;
400 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
401 wl_signal_add(&shsurf->destroy_signal,
402 &grab->shsurf_destroy_listener);
403
404 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700405 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700406
407 weston_touch_start_grab(touch, &grab->grab);
408 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500409 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700411}
412
413static void
414shell_touch_grab_end(struct shell_touch_grab *grab)
415{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700416 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700417 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700418 grab->shsurf->grabbed = 0;
419 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700420
421 weston_touch_end_grab(grab->touch);
422}
423
424static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800426 struct weston_output *output);
427
Daniel Stone496ca172012-05-30 16:31:42 +0100428static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300429get_modifier(char *modifier)
430{
431 if (!modifier)
432 return MODIFIER_SUPER;
433
434 if (!strcmp("ctrl", modifier))
435 return MODIFIER_CTRL;
436 else if (!strcmp("alt", modifier))
437 return MODIFIER_ALT;
438 else if (!strcmp("super", modifier))
439 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000440 else if (!strcmp("none", modifier))
441 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300442 else
443 return MODIFIER_SUPER;
444}
445
Juan Zhaoe10d2792012-04-25 19:09:52 +0800446static enum animation_type
447get_animation_type(char *animation)
448{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800449 if (!animation)
450 return ANIMATION_NONE;
451
Juan Zhaoe10d2792012-04-25 19:09:52 +0800452 if (!strcmp("zoom", animation))
453 return ANIMATION_ZOOM;
454 else if (!strcmp("fade", animation))
455 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100456 else if (!strcmp("dim-layer", animation))
457 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800458 else
459 return ANIMATION_NONE;
460}
461
Alex Wu4539b082012-03-01 12:57:46 +0800462static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400463shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200464{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400465 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500466 char *s, *client;
Bob Ham744e6532016-01-12 10:21:48 +0000467 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200468
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300469 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200471 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000472 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500473 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100474 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000475
476 weston_config_section_get_bool(section,
477 "allow-zap", &allow_zap, true);
478 shell->allow_zap = allow_zap;
479
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100480 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400481 "binding-modifier", &s, "super");
482 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200483 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800484
485 weston_config_section_get_string(section,
486 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000487 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800488 free(s);
489
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400490 weston_config_section_get_string(section, "animation", &s, "none");
491 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200492 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200493 weston_config_section_get_string(section, "close-animation", &s, "fade");
494 shell->win_close_animation_type = get_animation_type(s);
495 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700496 weston_config_section_get_string(section,
497 "startup-animation", &s, "fade");
498 shell->startup_animation_type = get_animation_type(s);
499 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700500 if (shell->startup_animation_type == ANIMATION_ZOOM)
501 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100502 weston_config_section_get_string(section, "focus-animation", &s, "none");
503 shell->focus_animation_type = get_animation_type(s);
504 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400505 weston_config_section_get_uint(section, "num-workspaces",
506 &shell->workspaces.num,
507 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200508}
509
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800510struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511get_default_output(struct weston_compositor *compositor)
512{
Armin Krezović10b06182016-06-23 11:59:30 +0200513 if (wl_list_empty(&compositor->output_list))
514 return NULL;
515
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516 return container_of(compositor->output_list.next,
517 struct weston_output, link);
518}
519
Pekka Paalanen8274d902014-08-06 19:36:51 +0300520static int
521focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
522{
523 return snprintf(buf, len, "focus highlight effect for output %s",
524 surface->output->name);
525}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100526
527/* no-op func for checking focus surface */
528static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200529focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530{
531}
532
533static struct focus_surface *
534get_focus_surface(struct weston_surface *surface)
535{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200536 if (surface->committed == focus_surface_committed)
537 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538 else
539 return NULL;
540}
541
542static bool
543is_focus_surface (struct weston_surface *es)
544{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200545 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100546}
547
548static bool
549is_focus_view (struct weston_view *view)
550{
551 return is_focus_surface (view->surface);
552}
553
554static struct focus_surface *
555create_focus_surface(struct weston_compositor *ec,
556 struct weston_output *output)
557{
558 struct focus_surface *fsurf = NULL;
559 struct weston_surface *surface = NULL;
560
561 fsurf = malloc(sizeof *fsurf);
562 if (!fsurf)
563 return NULL;
564
565 fsurf->surface = weston_surface_create(ec);
566 surface = fsurf->surface;
567 if (surface == NULL) {
568 free(fsurf);
569 return NULL;
570 }
571
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200572 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100573 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200574 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200575 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300576 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100577
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800578 fsurf->view = weston_view_create(surface);
579 if (fsurf->view == NULL) {
580 weston_surface_destroy(surface);
581 free(fsurf);
582 return NULL;
583 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200584 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200585 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600587 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600588 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
590 pixman_region32_fini(&surface->opaque);
591 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
592 output->width, output->height);
593 pixman_region32_fini(&surface->input);
594 pixman_region32_init(&surface->input);
595
596 wl_list_init(&fsurf->workspace_transform.link);
597
598 return fsurf;
599}
600
601static void
602focus_surface_destroy(struct focus_surface *fsurf)
603{
604 weston_surface_destroy(fsurf->surface);
605 free(fsurf);
606}
607
608static void
609focus_animation_done(struct weston_view_animation *animation, void *data)
610{
611 struct workspace *ws = data;
612
613 ws->focus_animation = NULL;
614}
615
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200616static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200617focus_state_destroy(struct focus_state *state)
618{
619 wl_list_remove(&state->seat_destroy_listener.link);
620 wl_list_remove(&state->surface_destroy_listener.link);
621 free(state);
622}
623
624static void
625focus_state_seat_destroy(struct wl_listener *listener, void *data)
626{
627 struct focus_state *state = container_of(listener,
628 struct focus_state,
629 seat_destroy_listener);
630
631 wl_list_remove(&state->link);
632 focus_state_destroy(state);
633}
634
635static void
636focus_state_surface_destroy(struct wl_listener *listener, void *data)
637{
638 struct focus_state *state = container_of(listener,
639 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400640 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200641 struct weston_surface *main_surface;
642 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200644
Pekka Paalanen01388e22013-04-25 13:57:44 +0300645 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
646
Kristian Høgsberge3778222012-07-31 17:29:30 -0400647 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300648 wl_list_for_each(view,
649 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500650 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400651 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100652 if (is_focus_view(view))
653 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200654 if (!get_shell_surface(view->surface))
655 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200657 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400658 break;
659 }
660
Pekka Paalanen01388e22013-04-25 13:57:44 +0300661 /* if the focus was a sub-surface, activate its main surface */
662 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200663 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300664
Kristian Høgsberge3778222012-07-31 17:29:30 -0400665 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100666 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200667 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200668 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400669 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200670 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100671 if (state->ws->focus_animation)
672 weston_view_animation_destroy(state->ws->focus_animation);
673
674 state->ws->focus_animation = weston_fade_run(
675 state->ws->fsurf_front->view,
676 state->ws->fsurf_front->view->alpha, 0.0, 300,
677 focus_animation_done, state->ws);
678 }
679
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 wl_list_remove(&state->link);
681 focus_state_destroy(state);
682 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200683}
684
685static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200686focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
687 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200688{
Jonas Ådahl04769742012-06-13 00:01:24 +0200689 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200690
691 state = malloc(sizeof *state);
692 if (state == NULL)
693 return NULL;
694
Quentin Glidicfff39812016-08-15 10:56:52 +0200695 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200700
701 state->seat_destroy_listener.notify = focus_state_seat_destroy;
702 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400703 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200704 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400705 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200706
707 return state;
708}
709
Jonas Ådahl8538b222012-08-29 22:13:03 +0200710static struct focus_state *
711ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
712{
713 struct workspace *ws = get_current_workspace(shell);
714 struct focus_state *state;
715
716 wl_list_for_each(state, &ws->focus_list, link)
717 if (state->seat == seat)
718 break;
719
720 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200721 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200722
723 return state;
724}
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800727focus_state_set_focus(struct focus_state *state,
728 struct weston_surface *surface)
729{
730 if (state->keyboard_focus) {
731 wl_list_remove(&state->surface_destroy_listener.link);
732 wl_list_init(&state->surface_destroy_listener.link);
733 }
734
735 state->keyboard_focus = surface;
736 if (surface)
737 wl_signal_add(&surface->destroy_signal,
738 &state->surface_destroy_listener);
739}
740
741static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400742restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200743{
744 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400745 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100746 struct wl_list pending_seat_list;
747 struct weston_seat *seat, *next_seat;
748
749 /* Temporarily steal the list of seats so that we can keep
750 * track of the seats we've already processed */
751 wl_list_init(&pending_seat_list);
752 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
753 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200754
755 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500756 struct weston_keyboard *keyboard =
757 weston_seat_get_keyboard(state->seat);
758
Neil Roberts4237f502014-04-09 16:33:31 +0100759 wl_list_remove(&state->seat->link);
760 wl_list_insert(&shell->compositor->seat_list,
761 &state->seat->link);
762
Derek Foreman1281a362015-07-31 16:55:32 -0500763 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800764 continue;
765
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200767
Derek Foreman1281a362015-07-31 16:55:32 -0500768 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200769 }
Neil Roberts4237f502014-04-09 16:33:31 +0100770
771 /* For any remaining seats that we don't have a focus state
772 * for we'll reset the keyboard focus to NULL */
773 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500774 struct weston_keyboard *keyboard =
775 weston_seat_get_keyboard(seat);
776
Neil Roberts4237f502014-04-09 16:33:31 +0100777 wl_list_insert(&shell->compositor->seat_list, &seat->link);
778
Derek Foreman1281a362015-07-31 16:55:32 -0500779 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100780 continue;
781
Derek Foreman1281a362015-07-31 16:55:32 -0500782 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100783 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200784}
785
786static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
788 struct weston_seat *seat)
789{
Derek Foreman1281a362015-07-31 16:55:32 -0500790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200792
793 wl_list_for_each(state, &ws->focus_list, link) {
794 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500795 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796 return;
797 }
798 }
799}
800
801static void
802drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
803 struct weston_surface *surface)
804{
805 struct focus_state *state;
806
807 wl_list_for_each(state, &ws->focus_list, link)
808 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800809 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200810}
811
812static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100813animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
814 struct weston_view *from, struct weston_view *to)
815{
816 struct weston_output *output;
817 bool focus_surface_created = false;
818
819 /* FIXME: Only support dim animation using two layers */
820 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
821 return;
822
823 output = get_default_output(shell->compositor);
824 if (ws->fsurf_front == NULL && (from || to)) {
825 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800826 if (ws->fsurf_front == NULL)
827 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800829
830 ws->fsurf_back = create_focus_surface(shell->compositor, output);
831 if (ws->fsurf_back == NULL) {
832 focus_surface_destroy(ws->fsurf_front);
833 return;
834 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800836
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 focus_surface_created = true;
838 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300839 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
840 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 }
842
843 if (ws->focus_animation) {
844 weston_view_animation_destroy(ws->focus_animation);
845 ws->focus_animation = NULL;
846 }
847
848 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300849 weston_layer_entry_insert(&to->layer_link,
850 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300852 weston_layer_entry_insert(&ws->layer.view_list,
853 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854
855 if (focus_surface_created) {
856 ws->focus_animation = weston_fade_run(
857 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200858 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 focus_animation_done, ws);
860 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&from->layer_link,
862 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->focus_animation = weston_stable_fade_run(
864 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200865 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 focus_animation_done, ws);
867 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300868 weston_layer_entry_insert(&ws->layer.view_list,
869 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 ws->focus_animation = weston_stable_fade_run(
871 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200872 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 focus_animation_done, ws);
874 }
875}
876
877static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200878workspace_destroy(struct workspace *ws)
879{
Jonas Ådahl04769742012-06-13 00:01:24 +0200880 struct focus_state *state, *next;
881
882 wl_list_for_each_safe(state, next, &ws->focus_list, link)
883 focus_state_destroy(state);
884
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100885 if (ws->fsurf_front)
886 focus_surface_destroy(ws->fsurf_front);
887 if (ws->fsurf_back)
888 focus_surface_destroy(ws->fsurf_back);
889
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200890 free(ws);
891}
892
Jonas Ådahl04769742012-06-13 00:01:24 +0200893static void
894seat_destroyed(struct wl_listener *listener, void *data)
895{
896 struct weston_seat *seat = data;
897 struct focus_state *state, *next;
898 struct workspace *ws = container_of(listener,
899 struct workspace,
900 seat_destroyed_listener);
901
902 wl_list_for_each_safe(state, next, &ws->focus_list, link)
903 if (state->seat == seat)
904 wl_list_remove(&state->link);
905}
906
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100908workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909{
910 struct workspace *ws = malloc(sizeof *ws);
911 if (ws == NULL)
912 return NULL;
913
Quentin Glidic82681572016-12-17 13:40:51 +0100914 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915
Jonas Ådahl04769742012-06-13 00:01:24 +0200916 wl_list_init(&ws->focus_list);
917 wl_list_init(&ws->seat_destroyed_listener.link);
918 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 ws->fsurf_front = NULL;
920 ws->fsurf_back = NULL;
921 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200922
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200923 return ws;
924}
925
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200926static int
927workspace_is_empty(struct workspace *ws)
928{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300929 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930}
931
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932static struct workspace *
933get_workspace(struct desktop_shell *shell, unsigned int index)
934{
935 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200936 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937 pws += index;
938 return *pws;
939}
940
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800941struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200942get_current_workspace(struct desktop_shell *shell)
943{
944 return get_workspace(shell, shell->workspaces.current);
945}
946
947static void
948activate_workspace(struct desktop_shell *shell, unsigned int index)
949{
950 struct workspace *ws;
951
952 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100953 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954
955 shell->workspaces.current = index;
956}
957
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958static unsigned int
959get_output_height(struct weston_output *output)
960{
961 return abs(output->region.extents.y1 - output->region.extents.y2);
962}
963
Greg Vf57774e2018-07-24 23:21:55 +0300964static struct weston_transform *
965view_get_transform(struct weston_view *view)
966{
967 struct focus_surface *fsurf = NULL;
968 struct shell_surface *shsurf = NULL;
969
970 if (is_focus_view(view)) {
971 fsurf = get_focus_surface(view->surface);
972 return &fsurf->workspace_transform;
973 }
974
975 shsurf = get_shell_surface(view->surface);
976 if (shsurf)
977 return &shsurf->workspace_transform;
978
979 return NULL;
980}
981
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984{
Greg Vf57774e2018-07-24 23:21:55 +0300985 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986
Greg Vf57774e2018-07-24 23:21:55 +0300987 if (!transform)
988 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994 weston_matrix_init(&transform->matrix);
995 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998}
999
1000static void
1001workspace_translate_out(struct workspace *ws, double fraction)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 unsigned int height;
1005 double d;
1006
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009 d = height * fraction;
1010
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 }
1013}
1014
1015static void
1016workspace_translate_in(struct workspace *ws, double fraction)
1017{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019 unsigned int height;
1020 double d;
1021
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001022 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024
1025 if (fraction > 0)
1026 d = -(height - height * fraction);
1027 else
1028 d = height + height * fraction;
1029
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001030 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031 }
1032}
1033
1034static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035reverse_workspace_change_animation(struct desktop_shell *shell,
1036 unsigned int index,
1037 struct workspace *from,
1038 struct workspace *to)
1039{
1040 shell->workspaces.current = index;
1041
1042 shell->workspaces.anim_to = to;
1043 shell->workspaces.anim_from = from;
1044 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001045 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046
Quentin Glidic82681572016-12-17 13:40:51 +01001047 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1048 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1049
Scott Moreau4272e632012-08-13 09:58:41 -06001050 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051}
1052
1053static void
1054workspace_deactivate_transforms(struct workspace *ws)
1055{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001057 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001059 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001060 transform = view_get_transform(view);
1061 if (!transform)
1062 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001063
1064 if (!wl_list_empty(&transform->link)) {
1065 wl_list_remove(&transform->link);
1066 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001067 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069 }
1070}
1071
1072static void
1073finish_workspace_change_animation(struct desktop_shell *shell,
1074 struct workspace *from,
1075 struct workspace *to)
1076{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001077 struct weston_view *view;
1078
Scott Moreau4272e632012-08-13 09:58:41 -06001079 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001081 /* Views that extend past the bottom of the output are still
1082 * visible after the workspace animation ends but before its layer
1083 * is hidden. In that case, we need to damage below those views so
1084 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001085 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001086 weston_view_damage_below(view);
1087
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 wl_list_remove(&shell->workspaces.animation.link);
1089 workspace_deactivate_transforms(from);
1090 workspace_deactivate_transforms(to);
1091 shell->workspaces.anim_to = NULL;
1092
Quentin Glidic82681572016-12-17 13:40:51 +01001093 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094}
1095
1096static void
1097animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001098 struct weston_output *output,
1099 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100{
1101 struct desktop_shell *shell =
1102 container_of(animation, struct desktop_shell,
1103 workspaces.animation);
1104 struct workspace *from = shell->workspaces.anim_from;
1105 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001106 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 double x, y;
1108
1109 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1110 finish_workspace_change_animation(shell, from, to);
1111 return;
1112 }
1113
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001114 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001118 timespec_add_msec(&shell->workspaces.anim_timestamp,
1119 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001121 -(asin(1.0 - shell->workspaces.anim_current) *
1122 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1123 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 }
1125
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001126 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 /*
1129 * x = [0, π/2]
1130 * y(x) = sin(x)
1131 */
1132 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1133 y = sin(x);
1134
1135 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001136 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137
1138 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1139 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1140 shell->workspaces.anim_current = y;
1141
Scott Moreau4272e632012-08-13 09:58:41 -06001142 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001144 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146}
1147
1148static void
1149animate_workspace_change(struct desktop_shell *shell,
1150 unsigned int index,
1151 struct workspace *from,
1152 struct workspace *to)
1153{
1154 struct weston_output *output;
1155
1156 int dir;
1157
1158 if (index > shell->workspaces.current)
1159 dir = -1;
1160 else
1161 dir = 1;
1162
1163 shell->workspaces.current = index;
1164
1165 shell->workspaces.anim_dir = dir;
1166 shell->workspaces.anim_from = from;
1167 shell->workspaces.anim_to = to;
1168 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001169 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170
1171 output = container_of(shell->compositor->output_list.next,
1172 struct weston_output, link);
1173 wl_list_insert(&output->animation_list,
1174 &shell->workspaces.animation.link);
1175
Quentin Glidic82681572016-12-17 13:40:51 +01001176 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1177 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178
1179 workspace_translate_in(to, 0);
1180
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001181 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001182
Scott Moreau4272e632012-08-13 09:58:41 -06001183 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184}
1185
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187update_workspace(struct desktop_shell *shell, unsigned int index,
1188 struct workspace *from, struct workspace *to)
1189{
1190 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001191 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1192 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193}
1194
1195static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001196change_workspace(struct desktop_shell *shell, unsigned int index)
1197{
1198 struct workspace *from;
1199 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001200 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
1202 if (index == shell->workspaces.current)
1203 return;
1204
1205 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001206 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207 return;
1208
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001209 from = get_current_workspace(shell);
1210 to = get_workspace(shell, index);
1211
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001212 if (shell->workspaces.anim_from == to &&
1213 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001215 reverse_workspace_change_animation(shell, index, from, to);
1216 return;
1217 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219 if (shell->workspaces.anim_to != NULL)
1220 finish_workspace_change_animation(shell,
1221 shell->workspaces.anim_from,
1222 shell->workspaces.anim_to);
1223
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001225
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001226 if (shell->focus_animation_type != ANIMATION_NONE) {
1227 wl_list_for_each(state, &from->focus_list, link)
1228 if (state->keyboard_focus)
1229 animate_focus_change(shell, from,
1230 get_default_view(state->keyboard_focus), NULL);
1231
1232 wl_list_for_each(state, &to->focus_list, link)
1233 if (state->keyboard_focus)
1234 animate_focus_change(shell, to,
1235 NULL, get_default_view(state->keyboard_focus));
1236 }
1237
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 if (workspace_is_empty(to) && workspace_is_empty(from))
1239 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240 else
1241 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001242}
1243
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244static bool
1245workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1246{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001247 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001248 struct wl_list *e;
1249
1250 if (wl_list_empty(list))
1251 return false;
1252
1253 e = list->next;
1254
1255 if (e->next != list)
1256 return false;
1257
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001258 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001259}
1260
1261static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001262surface_keyboard_focus_lost(struct weston_surface *surface)
1263{
1264 struct weston_compositor *compositor = surface->compositor;
1265 struct weston_seat *seat;
1266 struct weston_surface *focus;
1267
1268 wl_list_for_each(seat, &compositor->seat_list, link) {
1269 struct weston_keyboard *keyboard =
1270 weston_seat_get_keyboard(seat);
1271
1272 if (!keyboard)
1273 continue;
1274
1275 focus = weston_surface_get_main_surface(keyboard->focus);
1276 if (focus == surface)
1277 weston_keyboard_set_focus(keyboard, NULL);
1278 }
1279}
1280
1281static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001283 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284 unsigned int index)
1285{
Derek Foreman1281a362015-07-31 16:55:32 -05001286 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001287 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 struct shell_surface *shsurf;
1290 struct workspace *from;
1291 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001292 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293
Derek Foreman1281a362015-07-31 16:55:32 -05001294 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 view = get_default_view(surface);
1296 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001297 index == shell->workspaces.current ||
1298 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300
1301 from = get_current_workspace(shell);
1302 to = get_workspace(shell, index);
1303
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001304 weston_layer_entry_remove(&view->layer_link);
1305 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306
Philip Withnall659163d2013-11-25 18:01:36 +00001307 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001308 if (shsurf != NULL)
1309 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001310
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001312 drop_focus_state(shell, from, surface);
1313
1314 if (shell->workspaces.anim_from == to &&
1315 shell->workspaces.anim_to == from) {
1316 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 return;
1319 }
1320
1321 if (shell->workspaces.anim_to != NULL)
1322 finish_workspace_change_animation(shell,
1323 shell->workspaces.anim_from,
1324 shell->workspaces.anim_to);
1325
1326 if (workspace_is_empty(from) &&
1327 workspace_has_only(to, surface))
1328 update_workspace(shell, index, from, to);
1329 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001330 if (shsurf != NULL &&
1331 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 wl_list_insert(&shell->workspaces.anim_sticky_list,
1333 &shsurf->workspace_transform.link);
1334
1335 animate_workspace_change(shell, index, from, to);
1336 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Jonas Ådahl8538b222012-08-29 22:13:03 +02001338 state = ensure_focus_state(shell, seat);
1339 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001340 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001344touch_move_grab_down(struct weston_touch_grab *grab,
1345 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001346 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001347{
1348}
1349
1350static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001351touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1352 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001354 struct weston_touch_move_grab *move =
1355 (struct weston_touch_move_grab *) container_of(
1356 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001357
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001358 if (touch_id == 0)
1359 move->active = 0;
1360
Jonas Ådahl9484b692013-12-02 22:05:03 +01001361 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001362 shell_touch_grab_end(&move->base);
1363 free(move);
1364 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001365}
1366
1367static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001368touch_move_grab_motion(struct weston_touch_grab *grab,
1369 const struct timespec *time, int touch_id,
1370 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001371{
1372 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1373 struct shell_surface *shsurf = move->base.shsurf;
1374 struct weston_surface *es;
1375 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1376 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1377
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001378 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001379 return;
1380
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001381 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001383 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001384
1385 weston_compositor_schedule_repaint(es->compositor);
1386}
1387
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001388static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001389touch_move_grab_frame(struct weston_touch_grab *grab)
1390{
1391}
1392
1393static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394touch_move_grab_cancel(struct weston_touch_grab *grab)
1395{
1396 struct weston_touch_move_grab *move =
1397 (struct weston_touch_move_grab *) container_of(
1398 grab, struct shell_touch_grab, grab);
1399
1400 shell_touch_grab_end(&move->base);
1401 free(move);
1402}
1403
Rusty Lynch1084da52013-08-15 09:10:08 -07001404static const struct weston_touch_grab_interface touch_move_grab_interface = {
1405 touch_move_grab_down,
1406 touch_move_grab_up,
1407 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001408 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001409 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001410};
1411
1412static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001413surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001414{
1415 struct weston_touch_move_grab *move;
1416
1417 if (!shsurf)
1418 return -1;
1419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001420 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1421 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001422 return 0;
1423
1424 move = malloc(sizeof *move);
1425 if (!move)
1426 return -1;
1427
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001428 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001430 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001432 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001433
1434 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001435 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001436
1437 return 0;
1438}
1439
1440static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001441noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001442{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001443}
1444
1445static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001446noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001447 const struct timespec *time,
1448 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001449{
1450}
1451
1452static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001453noop_grab_axis_source(struct weston_pointer_grab *grab,
1454 uint32_t source)
1455{
1456}
1457
1458static void
1459noop_grab_frame(struct weston_pointer_grab *grab)
1460{
1461}
1462
1463static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001464constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1465{
1466 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 struct weston_surface *surface =
1468 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001469 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001470 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001471 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001472 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001474
1475 x = wl_fixed_to_int(pointer->x + move->dx);
1476 y = wl_fixed_to_int(pointer->y + move->dy);
1477
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001478 if (shsurf->shell->panel_position ==
1479 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001480 get_output_work_area(shsurf->shell, surface->output, &area);
1481 geometry =
1482 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001483
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001485 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001486 y = area.y + safety - geometry.height
1487 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001488
1489 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 y + geometry.y < area.y)
1491 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001492 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001493
1494 *cx = x;
1495 *cy = y;
1496}
1497
1498static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001499move_grab_motion(struct weston_pointer_grab *grab,
1500 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001501 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001504 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001505 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001506 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001507 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001508
Jonas Ådahld2510102014-10-05 21:39:14 +02001509 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001510 if (!shsurf)
1511 return;
1512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001513 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1514
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001515 constrain_position(move, &cx, &cy);
1516
1517 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001519 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520}
1521
1522static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001523move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001524 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001526 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1527 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001528 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001529 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001530
Daniel Stone4dbadb12012-05-30 16:31:51 +01001531 if (pointer->button_count == 0 &&
1532 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001533 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001534 free(grab);
1535 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536}
1537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538static void
1539move_grab_cancel(struct weston_pointer_grab *grab)
1540{
1541 struct shell_grab *shell_grab =
1542 container_of(grab, struct shell_grab, grab);
1543
1544 shell_grab_end(shell_grab);
1545 free(grab);
1546}
1547
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001548static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001549 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001550 move_grab_motion,
1551 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001552 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001553 noop_grab_axis_source,
1554 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001555 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001556};
1557
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001559surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001560 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561{
1562 struct weston_move_grab *move;
1563
1564 if (!shsurf)
1565 return -1;
1566
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001567 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001568 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1569 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 return 0;
1571
1572 move = malloc(sizeof *move);
1573 if (!move)
1574 return -1;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001577 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001579 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001580 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
1582 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001583 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 return 0;
1586}
1587
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588struct weston_resize_grab {
1589 struct shell_grab base;
1590 uint32_t edges;
1591 int32_t width, height;
1592};
1593
1594static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001595resize_grab_motion(struct weston_pointer_grab *grab,
1596 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001597 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 struct shell_surface *shsurf = resize->base.shsurf;
1602 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001603 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 wl_fixed_t from_x, from_y;
1605 wl_fixed_t to_x, to_y;
1606
Jonas Ådahld2510102014-10-05 21:39:14 +02001607 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001608
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001609 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 return;
1611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_view_from_global_fixed(shsurf->view,
1613 pointer->grab_x, pointer->grab_y,
1614 &from_x, &from_y);
1615 weston_view_from_global_fixed(shsurf->view,
1616 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617
1618 width = resize->width;
1619 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1620 width += wl_fixed_to_int(from_x - to_x);
1621 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1622 width += wl_fixed_to_int(to_x - from_x);
1623 }
1624
1625 height = resize->height;
1626 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1627 height += wl_fixed_to_int(from_y - to_y);
1628 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1629 height += wl_fixed_to_int(to_y - from_y);
1630 }
1631
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001632 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1633 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1634
1635 min_size.width = MAX(1, min_size.width);
1636 min_size.height = MAX(1, min_size.height);
1637
1638 if (width < min_size.width)
1639 width = min_size.width;
1640 else if (max_size.width > 0 && width > max_size.width)
1641 width = max_size.width;
1642 if (height < min_size.height)
1643 height = min_size.height;
1644 else if (max_size.width > 0 && width > max_size.width)
1645 width = max_size.width;
1646 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647}
1648
1649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001651 const struct timespec *time,
1652 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
1654 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001655 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 enum wl_pointer_button_state state = state_w;
1657
1658 if (pointer->button_count == 0 &&
1659 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001660 if (resize->base.shsurf != NULL) {
1661 struct weston_desktop_surface *desktop_surface =
1662 resize->base.shsurf->desktop_surface;
1663 weston_desktop_surface_set_resizing(desktop_surface,
1664 false);
1665 }
1666
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001667 shell_grab_end(&resize->base);
1668 free(grab);
1669 }
1670}
1671
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001672static void
1673resize_grab_cancel(struct weston_pointer_grab *grab)
1674{
1675 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1676
Greg Vec3f7792018-11-08 00:24:56 +03001677 if (resize->base.shsurf != NULL) {
1678 struct weston_desktop_surface *desktop_surface =
1679 resize->base.shsurf->desktop_surface;
1680 weston_desktop_surface_set_resizing(desktop_surface, false);
1681 }
1682
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001683 shell_grab_end(&resize->base);
1684 free(grab);
1685}
1686
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001687static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688 noop_grab_focus,
1689 resize_grab_motion,
1690 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001691 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001692 noop_grab_axis_source,
1693 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001694 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695};
1696
Giulio Camuffob8366642013-04-25 13:57:46 +03001697/*
1698 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001699 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001700static void
1701surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1702 int32_t *y, int32_t *w, int32_t *h) {
1703 pixman_region32_t region;
1704 pixman_box32_t *box;
1705 struct weston_subsurface *subsurface;
1706
1707 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 surface->width,
1709 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001710
1711 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1712 pixman_region32_union_rect(&region, &region,
1713 subsurface->position.x,
1714 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 subsurface->surface->width,
1716 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001717 }
1718
1719 box = pixman_region32_extents(&region);
1720 if (x)
1721 *x = box->x1;
1722 if (y)
1723 *y = box->y1;
1724 if (w)
1725 *w = box->x2 - box->x1;
1726 if (h)
1727 *h = box->y2 - box->y1;
1728
1729 pixman_region32_fini(&region);
1730}
1731
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732static int
1733surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001734 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735{
1736 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001737 const unsigned resize_topbottom =
1738 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1739 const unsigned resize_leftright =
1740 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1741 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001742 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001744 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001745 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1746 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747 return 0;
1748
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001749 /* Check for invalid edge combinations. */
1750 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1751 (edges & resize_topbottom) == resize_topbottom ||
1752 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753 return 0;
1754
1755 resize = malloc(sizeof *resize);
1756 if (!resize)
1757 return -1;
1758
1759 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001761 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1762 resize->width = geometry.width;
1763 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001764
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001765 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001766 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001768 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769
1770 return 0;
1771}
1772
1773static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001774busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001775{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001776 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001777 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001778 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001780 wl_fixed_t sx, sy;
1781
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782 view = weston_compositor_pick_view(pointer->seat->compositor,
1783 pointer->x, pointer->y,
1784 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001785 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001786
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001787 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001788 shell_grab_end(grab);
1789 free(grab);
1790 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001791}
1792
1793static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001794busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1795 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001796 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001797{
Jonas Ådahld2510102014-10-05 21:39:14 +02001798 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001799}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001800
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001802busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001803 const struct timespec *time,
1804 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001806 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001807 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001808 struct weston_pointer *pointer = grab->grab.pointer;
1809 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001810
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001811 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001812 activate(shsurf->shell, shsurf->view, seat,
1813 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001814 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001815 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001816 activate(shsurf->shell, shsurf->view, seat,
1817 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001818 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001819 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820}
1821
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001822static void
1823busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1824{
1825 struct shell_grab *grab = (struct shell_grab *) base;
1826
1827 shell_grab_end(grab);
1828 free(grab);
1829}
1830
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001831static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832 busy_cursor_grab_focus,
1833 busy_cursor_grab_motion,
1834 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001835 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001836 noop_grab_axis_source,
1837 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001838 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839};
1840
1841static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842handle_pointer_focus(struct wl_listener *listener, void *data)
1843{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001844 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001846 struct shell_surface *shsurf;
1847 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001850 return;
1851
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001852 shsurf = get_shell_surface(view->surface);
1853 if (!shsurf)
1854 return;
1855
1856 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1857
1858 if (shsurf->unresponsive)
1859 set_busy_cursor(shsurf, pointer);
1860 else
1861 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862}
1863
1864static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001865shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1866{
1867 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001868 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001869}
1870
1871static void
1872shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1873{
1874 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001876}
1877
1878static void
1879handle_keyboard_focus(struct wl_listener *listener, void *data)
1880{
1881 struct weston_keyboard *keyboard = data;
1882 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1883
1884 if (seat->focused_surface) {
1885 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1886 if (shsurf)
1887 shell_surface_lose_keyboard_focus(shsurf);
1888 }
1889
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001890 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001891
1892 if (seat->focused_surface) {
1893 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1894 if (shsurf)
1895 shell_surface_gain_keyboard_focus(shsurf);
1896 }
1897}
1898
Philip Withnall07926d92013-11-25 18:01:40 +00001899/* The surface will be inserted into the list immediately after the link
1900 * returned by this function (i.e. will be stacked immediately above the
1901 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001902static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001903shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1904{
1905 struct workspace *ws;
1906
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001907 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1908 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001909 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001910 }
1911
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001912 /* Move the surface to a normal workspace layer so that surfaces
1913 * which were previously fullscreen or transient are no longer
1914 * rendered on top. */
1915 ws = get_current_workspace(shsurf->shell);
1916 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001917}
1918
Philip Withnall648a4dd2013-11-25 18:01:44 +00001919static void
1920shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1921{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001922 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001923}
1924
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001925/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001926 * geometry to ensure the changes are redrawn.
1927 *
1928 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1929 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001930static void
1931shell_surface_update_layer(struct shell_surface *shsurf)
1932{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001933 struct weston_surface *surface =
1934 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001935 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001936
1937 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1938
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001939 if (new_layer_link == NULL)
1940 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001941 if (new_layer_link == &shsurf->view->layer_link)
1942 return;
1943
1944 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001945 weston_layer_entry_remove(&shsurf->view->layer_link);
1946 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001947 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001948 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001949
1950 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001951}
1952
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001953static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001954notify_output_destroy(struct wl_listener *listener, void *data)
1955{
1956 struct shell_surface *shsurf =
1957 container_of(listener,
1958 struct shell_surface, output_destroy_listener);
1959
1960 shsurf->output = NULL;
1961 shsurf->output_destroy_listener.notify = NULL;
1962}
1963
1964static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001965shell_surface_set_output(struct shell_surface *shsurf,
1966 struct weston_output *output)
1967{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001968 struct weston_surface *es =
1969 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001970
1971 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001972 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001973 if (output)
1974 shsurf->output = output;
1975 else if (es->output)
1976 shsurf->output = es->output;
1977 else
1978 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001979
1980 if (shsurf->output_destroy_listener.notify) {
1981 wl_list_remove(&shsurf->output_destroy_listener.link);
1982 shsurf->output_destroy_listener.notify = NULL;
1983 }
1984
1985 if (!shsurf->output)
1986 return;
1987
1988 shsurf->output_destroy_listener.notify = notify_output_destroy;
1989 wl_signal_add(&shsurf->output->destroy_signal,
1990 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001991}
1992
1993static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001994weston_view_set_initial_position(struct weston_view *view,
1995 struct desktop_shell *shell);
1996
1997static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001998unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001999{
Philip Withnallf85fe842013-11-25 18:01:37 +00002000 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08002001 wl_list_remove(&shsurf->fullscreen.transform.link);
2002 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002003
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 if (shsurf->fullscreen.black_view)
2005 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2006 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002007
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002008 if (shsurf->saved_position_valid)
2009 weston_view_set_position(shsurf->view,
2010 shsurf->saved_x, shsurf->saved_y);
2011 else
2012 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002013 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002014
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002015 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002017 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002018 shsurf->saved_rotation_valid = false;
2019 }
Alex Wu4539b082012-03-01 12:57:46 +08002020}
2021
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002022static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002023unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002024{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002025 struct weston_surface *surface =
2026 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2027
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002028 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002029 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002030
2031 if (shsurf->saved_position_valid)
2032 weston_view_set_position(shsurf->view,
2033 shsurf->saved_x, shsurf->saved_y);
2034 else
2035 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002036 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002037
2038 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 wl_list_insert(&shsurf->view->geometry.transformation_list,
2040 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002041 shsurf->saved_rotation_valid = false;
2042 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002043}
2044
Philip Withnallbecb77e2013-11-25 18:01:30 +00002045static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002046set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002047{
2048 struct shell_surface *shsurf;
2049 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002050 struct weston_view *view;
2051
2052 view = get_default_view(surface);
2053 if (!view)
2054 return;
2055
2056 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2057
2058 shsurf = get_shell_surface(surface);
2059 current_ws = get_current_workspace(shsurf->shell);
2060
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002061 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002062 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002063
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002064 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002065 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002066
2067 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002068 weston_view_damage_below(view);
2069}
2070
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002071
Tiago Vignattibe143262012-04-16 17:31:41 +03002072static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002073shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002074{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002075 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002076}
2077
Pekka Paalanen8274d902014-08-06 19:36:51 +03002078static int
2079black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2080{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002081 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002082 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002083 int n;
2084 int rem;
2085 int ret;
2086
2087 n = snprintf(buf, len, "black background surface for ");
2088 if (n < 0)
2089 return n;
2090
2091 rem = (int)len - n;
2092 if (rem < 0)
2093 rem = 0;
2094
2095 if (fs_surface->get_label)
2096 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2097 else
2098 ret = snprintf(buf + n, rem, "<unknown>");
2099
2100 if (ret < 0)
2101 return n;
2102
2103 return n + ret;
2104}
2105
Alex Wu21858432012-04-01 20:13:08 +08002106static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002107black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002108
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002110create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002111 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002112 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002113{
2114 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002116
2117 surface = weston_surface_create(ec);
2118 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002119 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002120 return NULL;
2121 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 view = weston_view_create(surface);
2123 if (surface == NULL) {
2124 weston_log("no memory\n");
2125 weston_surface_destroy(surface);
2126 return NULL;
2127 }
Alex Wu4539b082012-03-01 12:57:46 +08002128
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002129 surface->committed = black_surface_committed;
2130 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002131 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002132 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002133 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002134 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002135 pixman_region32_fini(&surface->input);
2136 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002137
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002138 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002139 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140
2141 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002142}
2143
Philip Withnalled8f1a92013-11-25 18:01:43 +00002144static void
2145shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2146{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002147 struct weston_surface *surface =
2148 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002149 struct weston_output *output = shsurf->fullscreen_output;
2150
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002151 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002152
2153 if (!shsurf->fullscreen.black_view)
2154 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002155 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002156 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002157 output->x, output->y,
2158 output->width,
2159 output->height);
2160
2161 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002162 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2163 weston_layer_entry_insert(&shsurf->view->layer_link,
2164 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002165 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002166 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002167
Armin Krezović4663aca2016-06-30 06:04:29 +02002168 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002169 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002170}
2171
Alex Wu4539b082012-03-01 12:57:46 +08002172/* Create black surface and append it to the associated fullscreen surface.
2173 * Handle size dismatch and positioning according to the method. */
2174static void
2175shell_configure_fullscreen(struct shell_surface *shsurf)
2176{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002177 struct weston_surface *surface =
2178 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002179 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002180
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002181 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002182 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002183 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2184 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002185
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002186 if (!shsurf->fullscreen_output) {
2187 /* If there is no output, there's not much we can do.
2188 * Position the window somewhere, whatever. */
2189 weston_view_set_position(shsurf->view, 0, 0);
2190 return;
2191 }
2192
Philip Withnalled8f1a92013-11-25 18:01:43 +00002193 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002194
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002195 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002196 &surf_width, &surf_height);
2197
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002198 if (surface->buffer_ref.buffer)
2199 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002200}
2201
Alex Wu4539b082012-03-01 12:57:46 +08002202static void
2203shell_map_fullscreen(struct shell_surface *shsurf)
2204{
Alex Wubd3354b2012-04-17 17:20:49 +08002205 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002206}
2207
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002208static struct weston_output *
2209get_focused_output(struct weston_compositor *compositor)
2210{
2211 struct weston_seat *seat;
2212 struct weston_output *output = NULL;
2213
2214 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002215 struct weston_touch *touch = weston_seat_get_touch(seat);
2216 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2217 struct weston_keyboard *keyboard =
2218 weston_seat_get_keyboard(seat);
2219
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002220 /* Priority has touch focus, then pointer and
2221 * then keyboard focus. We should probably have
2222 * three for loops and check frist for touch,
2223 * then for pointer, etc. but unless somebody has some
2224 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002225 if (touch && touch->focus)
2226 output = touch->focus->output;
2227 else if (pointer && pointer->focus)
2228 output = pointer->focus->output;
2229 else if (keyboard && keyboard->focus)
2230 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002231
2232 if (output)
2233 break;
2234 }
2235
2236 return output;
2237}
2238
2239static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002240destroy_shell_seat(struct wl_listener *listener, void *data)
2241{
2242 struct shell_seat *shseat =
2243 container_of(listener,
2244 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245
2246 wl_list_remove(&shseat->seat_destroy_listener.link);
2247 free(shseat);
2248}
2249
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250static void
2251shell_seat_caps_changed(struct wl_listener *listener, void *data)
2252{
Derek Foreman1281a362015-07-31 16:55:32 -05002253 struct weston_keyboard *keyboard;
2254 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002255 struct shell_seat *seat;
2256
2257 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002258 keyboard = weston_seat_get_keyboard(seat->seat);
2259 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002260
Derek Foreman1281a362015-07-31 16:55:32 -05002261 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002262 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002263 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002264 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002265 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002266 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002267 wl_list_init(&seat->keyboard_focus_listener.link);
2268 }
2269
Derek Foreman1281a362015-07-31 16:55:32 -05002270 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002271 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002272 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002273 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002274 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002275 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002276 wl_list_init(&seat->pointer_focus_listener.link);
2277 }
2278}
2279
Giulio Camuffo5085a752013-03-25 21:42:45 +01002280static struct shell_seat *
2281create_shell_seat(struct weston_seat *seat)
2282{
2283 struct shell_seat *shseat;
2284
2285 shseat = calloc(1, sizeof *shseat);
2286 if (!shseat) {
2287 weston_log("no memory to allocate shell seat\n");
2288 return NULL;
2289 }
2290
2291 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002292
2293 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2294 wl_signal_add(&seat->destroy_signal,
2295 &shseat->seat_destroy_listener);
2296
Jason Ekstrand024177c2014-04-21 19:42:58 -05002297 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2298 wl_list_init(&shseat->keyboard_focus_listener.link);
2299
2300 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2301 wl_list_init(&shseat->pointer_focus_listener.link);
2302
2303 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2304 wl_signal_add(&seat->updated_caps_signal,
2305 &shseat->caps_changed_listener);
2306 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2307
Giulio Camuffo5085a752013-03-25 21:42:45 +01002308 return shseat;
2309}
2310
2311static struct shell_seat *
2312get_shell_seat(struct weston_seat *seat)
2313{
2314 struct wl_listener *listener;
2315
2316 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002317 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002318
2319 return container_of(listener,
2320 struct shell_seat, seat_destroy_listener);
2321}
2322
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002323static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002324fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002325{
2326 struct shell_surface *shsurf = data;
2327
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002328 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002329
2330 if (shsurf->output_destroy_listener.notify) {
2331 wl_list_remove(&shsurf->output_destroy_listener.link);
2332 shsurf->output_destroy_listener.notify = NULL;
2333 }
2334
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002335 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002336}
2337
2338static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002339fade_out_done(struct weston_view_animation *animation, void *data)
2340{
2341 struct shell_surface *shsurf = data;
2342 struct wl_event_loop *loop;
2343
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002345
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002346 if (weston_view_is_mapped(shsurf->view)) {
2347 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002348 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002349 }
2350}
2351
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002352struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002353get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002354{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002355 if (weston_surface_is_desktop_surface(surface)) {
2356 struct weston_desktop_surface *desktop_surface =
2357 weston_surface_get_desktop_surface(surface);
2358 return weston_desktop_surface_get_user_data(desktop_surface);
2359 }
2360 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002361}
2362
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002363/*
2364 * libweston-desktop
2365 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002366
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367static void
2368desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2369 void *shell)
2370{
2371 struct weston_desktop_client *client =
2372 weston_desktop_surface_get_client(desktop_surface);
2373 struct wl_client *wl_client =
2374 weston_desktop_client_get_client(client);
2375 struct weston_view *view;
2376 struct shell_surface *shsurf;
2377 struct weston_surface *surface =
2378 weston_desktop_surface_get_surface(desktop_surface);
2379
2380 view = weston_desktop_surface_create_view(desktop_surface);
2381 if (!view)
2382 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002383
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002384 shsurf = calloc(1, sizeof *shsurf);
2385 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002386 if (wl_client)
2387 wl_client_post_no_memory(wl_client);
2388 else
2389 weston_log("no memory to allocate shell surface\n");
2390 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002391 }
2392
Pekka Paalanen8274d902014-08-06 19:36:51 +03002393 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002394
Tiago Vignattibc052c92012-04-19 16:18:18 +03002395 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002396 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002397 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002398 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002399 shsurf->desktop_surface = desktop_surface;
2400 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002402 wl_list_init(&shsurf->fullscreen.transform.link);
2403
Semi Malinen99f8c082018-05-02 11:10:32 +02002404 shell_surface_set_output(
2405 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002406
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002407 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002408
Pekka Paalanen460099f2012-01-20 16:48:25 +02002409 /* empty when not in use */
2410 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002411 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002412
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002413 wl_list_init(&shsurf->workspace_transform.link);
2414
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002415 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2416 weston_desktop_surface_set_activated(desktop_surface,
2417 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002418}
2419
Tiago Vignattibc052c92012-04-19 16:18:18 +03002420static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002421desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2422 void *shell)
2423{
2424 struct shell_surface *shsurf =
2425 weston_desktop_surface_get_user_data(desktop_surface);
2426 struct weston_surface *surface =
2427 weston_desktop_surface_get_surface(desktop_surface);
2428
2429 if (!shsurf)
2430 return;
2431
2432 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2433
2434 if (shsurf->fullscreen.black_view)
2435 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2436
2437 weston_surface_set_label_func(surface, NULL);
2438 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2439 shsurf->desktop_surface = NULL;
2440
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002441 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 if (weston_surface_is_mapped(surface) &&
2443 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2444 pixman_region32_fini(&surface->pending.input);
2445 pixman_region32_init(&surface->pending.input);
2446 pixman_region32_fini(&surface->input);
2447 pixman_region32_init(&surface->input);
2448 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2449 fade_out_done, shsurf);
2450 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002452
2453 if (shsurf->output_destroy_listener.notify) {
2454 wl_list_remove(&shsurf->output_destroy_listener.link);
2455 shsurf->output_destroy_listener.notify = NULL;
2456 }
2457
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002458 free(shsurf);
2459 }
2460}
2461
2462static void
2463set_maximized_position(struct desktop_shell *shell,
2464 struct shell_surface *shsurf)
2465{
2466 pixman_rectangle32_t area;
2467 struct weston_geometry geometry;
2468
2469 get_output_work_area(shell, shsurf->output, &area);
2470 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2471
2472 weston_view_set_position(shsurf->view,
2473 area.x - geometry.x,
2474 area.y - geometry.y);
2475}
2476
2477static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002478set_position_from_xwayland(struct shell_surface *shsurf)
2479{
2480 struct weston_geometry geometry;
2481 float x;
2482 float y;
2483
2484 assert(shsurf->xwayland.is_set);
2485
2486 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2487 x = shsurf->xwayland.x - geometry.x;
2488 y = shsurf->xwayland.y - geometry.y;
2489
2490 weston_view_set_position(shsurf->view, x, y);
2491
2492#ifdef WM_DEBUG
2493 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2494 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2495 geometry.x, geometry.y, x, y);
2496#endif
2497}
2498
2499static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500map(struct desktop_shell *shell, struct shell_surface *shsurf,
2501 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002502{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002503 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002504 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2505 struct weston_compositor *compositor = shell->compositor;
2506 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002507
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002508 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002509 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002510 center_on_output(shsurf->view, shsurf->fullscreen_output);
2511 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002512 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002513 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002514 } else if (shsurf->xwayland.is_set) {
2515 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002516 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002518 }
2519
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002520 /* Surface stacking order, see also activate(). */
2521 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002522
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002523 weston_view_update_transform(shsurf->view);
2524 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002525 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002526 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002527 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002528 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002529
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002530 if (!shell->locked) {
2531 wl_list_for_each(seat, &compositor->seat_list, link)
2532 activate(shell, shsurf->view, seat,
2533 WESTON_ACTIVATE_FLAG_CONFIGURE);
2534 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002535
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002536 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002537 switch (shell->win_animation_type) {
2538 case ANIMATION_FADE:
2539 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2540 break;
2541 case ANIMATION_ZOOM:
2542 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2543 break;
2544 case ANIMATION_NONE:
2545 default:
2546 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002547 }
2548 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002549}
2550
2551static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2553 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002554{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002555 struct shell_surface *shsurf =
2556 weston_desktop_surface_get_user_data(desktop_surface);
2557 struct weston_surface *surface =
2558 weston_desktop_surface_get_surface(desktop_surface);
2559 struct weston_view *view = shsurf->view;
2560 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002561 bool was_fullscreen;
2562 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002563
2564 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002565 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002566
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002567 was_fullscreen = shsurf->state.fullscreen;
2568 was_maximized = shsurf->state.maximized;
2569
2570 shsurf->state.fullscreen =
2571 weston_desktop_surface_get_fullscreen(desktop_surface);
2572 shsurf->state.maximized =
2573 weston_desktop_surface_get_maximized(desktop_surface);
2574
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002575 if (!weston_surface_is_mapped(surface)) {
2576 map(shell, shsurf, sx, sy);
2577 surface->is_mapped = true;
2578 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2579 ++surface->ref_count;
2580 return;
2581 }
2582
2583 if (sx == 0 && sy == 0 &&
2584 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002585 shsurf->last_height == surface->height &&
2586 was_fullscreen == shsurf->state.fullscreen &&
2587 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002588 return;
2589
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002590 if (was_fullscreen)
2591 unset_fullscreen(shsurf);
2592 if (was_maximized)
2593 unset_maximized(shsurf);
2594
Quentin Glidic920cf042016-08-16 14:26:20 +02002595 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2596 !shsurf->saved_position_valid) {
2597 shsurf->saved_x = shsurf->view->geometry.x;
2598 shsurf->saved_y = shsurf->view->geometry.y;
2599 shsurf->saved_position_valid = true;
2600
2601 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2602 wl_list_remove(&shsurf->rotation.transform.link);
2603 wl_list_init(&shsurf->rotation.transform.link);
2604 weston_view_geometry_dirty(shsurf->view);
2605 shsurf->saved_rotation_valid = true;
2606 }
2607 }
2608
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002609 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002610 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002611 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002612 set_maximized_position(shell, shsurf);
2613 surface->output = shsurf->output;
2614 } else {
2615 float from_x, from_y;
2616 float to_x, to_y;
2617 float x, y;
2618
2619 if (shsurf->resize_edges) {
2620 sx = 0;
2621 sy = 0;
2622 }
2623
2624 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2625 sx = shsurf->last_width - surface->width;
2626 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2627 sy = shsurf->last_height - surface->height;
2628
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002629 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2630 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2631 x = shsurf->view->geometry.x + to_x - from_x;
2632 y = shsurf->view->geometry.y + to_y - from_y;
2633
2634 weston_view_set_position(shsurf->view, x, y);
2635 }
2636
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002637 shsurf->last_width = surface->width;
2638 shsurf->last_height = surface->height;
2639
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002640 /* XXX: would a fullscreen surface need the same handling? */
2641 if (surface->output) {
2642 wl_list_for_each(view, &surface->views, surface_link)
2643 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002644 }
2645}
2646
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002647static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002648get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2649{
2650 struct desktop_shell *shell;
2651 pixman_rectangle32_t area;
2652
2653 shell = shell_surface_get_shell(shsurf);
2654 get_output_work_area(shell, shsurf->output, &area);
2655
2656 *width = area.width;
2657 *height = area.height;
2658}
2659
2660static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002661set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2662 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002663{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002664 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2665 struct weston_surface *surface =
2666 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2667 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002668
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002669 if (fullscreen) {
2670 /* handle clients launching in fullscreen */
2671 if (output == NULL && !weston_surface_is_mapped(surface)) {
2672 /* Set the output to the one that has focus currently. */
2673 output = get_focused_output(surface->compositor);
2674 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002675
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002676 shell_surface_set_output(shsurf, output);
2677 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002678
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002679 width = shsurf->output->width;
2680 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002681 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2682 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002683 }
2684 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2685 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002686}
2687
2688static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002689desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2690 struct weston_seat *seat, uint32_t serial, void *shell)
2691{
2692 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2693 struct weston_touch *touch = weston_seat_get_touch(seat);
2694 struct shell_surface *shsurf =
2695 weston_desktop_surface_get_user_data(desktop_surface);
2696 struct weston_surface *surface =
2697 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2698 struct wl_resource *resource = surface->resource;
2699 struct weston_surface *focus;
2700
2701 if (pointer &&
2702 pointer->focus &&
2703 pointer->button_count > 0 &&
2704 pointer->grab_serial == serial) {
2705 focus = weston_surface_get_main_surface(pointer->focus->surface);
2706 if ((focus == surface) &&
2707 (surface_move(shsurf, pointer, true) < 0))
2708 wl_resource_post_no_memory(resource);
2709 } else if (touch &&
2710 touch->focus &&
2711 touch->grab_serial == serial) {
2712 focus = weston_surface_get_main_surface(touch->focus->surface);
2713 if ((focus == surface) &&
2714 (surface_touch_move(shsurf, touch) < 0))
2715 wl_resource_post_no_memory(resource);
2716 }
2717}
2718
2719static void
2720desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2721 struct weston_seat *seat, uint32_t serial,
2722 enum weston_desktop_surface_edge edges, void *shell)
2723{
2724 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2725 struct shell_surface *shsurf =
2726 weston_desktop_surface_get_user_data(desktop_surface);
2727 struct weston_surface *surface =
2728 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2729 struct wl_resource *resource = surface->resource;
2730 struct weston_surface *focus;
2731
2732 if (!pointer ||
2733 pointer->button_count == 0 ||
2734 pointer->grab_serial != serial ||
2735 pointer->focus == NULL)
2736 return;
2737
2738 focus = weston_surface_get_main_surface(pointer->focus->surface);
2739 if (focus != surface)
2740 return;
2741
2742 if (surface_resize(shsurf, pointer, edges) < 0)
2743 wl_resource_post_no_memory(resource);
2744}
2745
2746static void
2747desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2748 bool fullscreen,
2749 struct weston_output *output, void *shell)
2750{
2751 struct shell_surface *shsurf =
2752 weston_desktop_surface_get_user_data(desktop_surface);
2753
2754 set_fullscreen(shsurf, fullscreen, output);
2755}
2756
2757static void
2758set_maximized(struct shell_surface *shsurf, bool maximized)
2759{
2760 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2761 struct weston_surface *surface =
2762 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2763 int32_t width = 0, height = 0;
2764
2765 if (maximized) {
2766 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002767
2768 if (!weston_surface_is_mapped(surface))
2769 output = get_focused_output(surface->compositor);
2770 else
2771 output = surface->output;
2772
2773 shell_surface_set_output(shsurf, output);
2774
Derek Foreman927d9e22017-10-06 14:37:44 -05002775 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002776 }
2777 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2778 weston_desktop_surface_set_size(desktop_surface, width, height);
2779}
2780
2781static void
2782desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2783 bool maximized, void *shell)
2784{
2785 struct shell_surface *shsurf =
2786 weston_desktop_surface_get_user_data(desktop_surface);
2787
2788 set_maximized(shsurf, maximized);
2789}
2790
2791static void
2792desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2793 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002794{
2795 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002796 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002797
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798 /* apply compositor's own minimization logic (hide) */
2799 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002800}
2801
Rafael Antognollie2a34552013-12-03 15:35:45 -02002802static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002803set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002804{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002805 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002806
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002807 if (pointer->grab->interface == &busy_cursor_grab_interface)
2808 return;
2809
2810 grab = malloc(sizeof *grab);
2811 if (!grab)
2812 return;
2813
2814 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2815 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2816 /* Mark the shsurf as ungrabbed so that button binding is able
2817 * to move it. */
2818 shsurf->grabbed = 0;
2819}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002820
2821static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822end_busy_cursor(struct weston_compositor *compositor,
2823 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002824{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002825 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002826 struct shell_grab *grab;
2827 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002828
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002829 wl_list_for_each(seat, &compositor->seat_list, link) {
2830 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2831 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002832
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002833 if (!pointer)
2834 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002835
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002836 if (pointer->grab->interface != &busy_cursor_grab_interface)
2837 continue;
2838
2839 grab = (struct shell_grab *) pointer->grab;
2840 shsurf = grab->shsurf;
2841 if (!shsurf)
2842 continue;
2843
2844 grab_client =
2845 weston_desktop_surface_get_client(shsurf->desktop_surface);
2846 if (grab_client == desktop_client) {
2847 shell_grab_end(grab);
2848 free(grab);
2849 }
2850 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002851}
2852
2853static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002854desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2855 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002856{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002857 struct shell_surface *shsurf =
2858 weston_desktop_surface_get_user_data(desktop_surface);
2859 bool *unresponsive = user_data;
2860
2861 shsurf->unresponsive = *unresponsive;
2862}
2863
2864static void
2865desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2866 void *shell_)
2867{
2868 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002869 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002870 struct weston_seat *seat;
2871 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002872
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002873 weston_desktop_client_for_each_surface(desktop_client,
2874 desktop_surface_set_unresponsive,
2875 &unresponsive);
2876
2877
2878 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2879 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2880 struct weston_desktop_client *grab_client;
2881
2882 if (!pointer || !pointer->focus)
2883 continue;
2884
2885 shsurf = get_shell_surface(pointer->focus->surface);
2886 if (!shsurf)
2887 continue;
2888
2889 grab_client =
2890 weston_desktop_surface_get_client(shsurf->desktop_surface);
2891 if (grab_client == desktop_client)
2892 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002893 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002894}
2895
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002896static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002897desktop_surface_pong(struct weston_desktop_client *desktop_client,
2898 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002899{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002900 struct desktop_shell *shell = shell_;
2901 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002902
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002903 weston_desktop_client_for_each_surface(desktop_client,
2904 desktop_surface_set_unresponsive,
2905 &unresponsive);
2906 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002907}
2908
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002909static void
2910desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2911 int32_t x, int32_t y, void *shell_)
2912{
2913 struct shell_surface *shsurf =
2914 weston_desktop_surface_get_user_data(surface);
2915
2916 shsurf->xwayland.x = x;
2917 shsurf->xwayland.y = y;
2918 shsurf->xwayland.is_set = true;
2919}
2920
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002921static const struct weston_desktop_api shell_desktop_api = {
2922 .struct_size = sizeof(struct weston_desktop_api),
2923 .surface_added = desktop_surface_added,
2924 .surface_removed = desktop_surface_removed,
2925 .committed = desktop_surface_committed,
2926 .move = desktop_surface_move,
2927 .resize = desktop_surface_resize,
2928 .fullscreen_requested = desktop_surface_fullscreen_requested,
2929 .maximized_requested = desktop_surface_maximized_requested,
2930 .minimized_requested = desktop_surface_minimized_requested,
2931 .ping_timeout = desktop_surface_ping_timeout,
2932 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002933 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002934};
2935
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002936/* ************************ *
2937 * end of libweston-desktop *
2938 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002939static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002940configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002943
Semi Malinene7a52fb2018-04-26 11:08:10 +02002944 if (!ev->output)
2945 return;
2946
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002947 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 if (v->output == ev->output && v != ev) {
2949 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002950 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002951 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002952 }
2953 }
2954
Quentin Glidice8bf9592016-06-23 18:55:20 +02002955 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002956 ev->surface->is_mapped = true;
2957 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002958
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002959 if (wl_list_empty(&ev->layer_link.link)) {
2960 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002961 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002962 }
2963}
2964
David Fort50d962f2016-06-09 17:55:52 +02002965
2966static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002967find_shell_output_from_weston_output(struct desktop_shell *shell,
2968 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002969{
2970 struct shell_output *shell_output;
2971
2972 wl_list_for_each(shell_output, &shell->output_list, link) {
2973 if (shell_output->output == output)
2974 return shell_output;
2975 }
2976
2977 return NULL;
2978}
2979
Pekka Paalanen8274d902014-08-06 19:36:51 +03002980static int
2981background_get_label(struct weston_surface *surface, char *buf, size_t len)
2982{
2983 return snprintf(buf, len, "background for output %s",
2984 surface->output->name);
2985}
2986
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002987static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002988background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002989{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002990 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002992
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993 view = container_of(es->views.next, struct weston_view, surface_link);
2994
Quentin Glidice8bf9592016-06-23 18:55:20 +02002995 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002996}
2997
2998static void
David Fort50d962f2016-06-09 17:55:52 +02002999handle_background_surface_destroy(struct wl_listener *listener, void *data)
3000{
3001 struct shell_output *output =
3002 container_of(listener, struct shell_output, background_surface_listener);
3003
3004 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003005 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003006 output->background_surface = NULL;
3007}
3008
3009static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003010desktop_shell_set_background(struct wl_client *client,
3011 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003012 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013 struct wl_resource *surface_resource)
3014{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003015 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003016 struct weston_surface *surface =
3017 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003018 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003020
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003021 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003022 wl_resource_post_error(surface_resource,
3023 WL_DISPLAY_ERROR_INVALID_OBJECT,
3024 "surface role already assigned");
3025 return;
3026 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003027
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3029 weston_view_destroy(view);
3030 view = weston_view_create(surface);
3031
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003032 surface->committed = background_committed;
3033 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003034 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003035 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003036 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003037
3038 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003039 if (sh_output->background_surface) {
3040 /* The output already has a background, tell our helper
3041 * there is no need for another one. */
3042 weston_desktop_shell_send_configure(resource, 0,
3043 surface_resource,
3044 0, 0);
3045 } else {
3046 weston_desktop_shell_send_configure(resource, 0,
3047 surface_resource,
3048 surface->output->width,
3049 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003050
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003051 sh_output->background_surface = surface;
3052
3053 sh_output->background_surface_listener.notify =
3054 handle_background_surface_destroy;
3055 wl_signal_add(&surface->destroy_signal,
3056 &sh_output->background_surface_listener);
3057 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003058}
3059
Pekka Paalanen8274d902014-08-06 19:36:51 +03003060static int
3061panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3062{
3063 return snprintf(buf, len, "panel for output %s",
3064 surface->output->name);
3065}
3066
Kristian Høgsberg75840622011-09-06 13:48:16 -04003067static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003068panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003069{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003070 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003071 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003072 int width, height;
3073 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003074
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 view = container_of(es->views.next, struct weston_view, surface_link);
3076
Quentin Glidice8bf9592016-06-23 18:55:20 +02003077 get_panel_size(shell, view, &width, &height);
3078 switch (shell->panel_position) {
3079 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3080 break;
3081 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3082 y = view->output->height - height;
3083 break;
3084 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3085 break;
3086 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3087 x = view->output->width - width;
3088 break;
3089 }
3090
3091 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003092}
3093
3094static void
David Fort50d962f2016-06-09 17:55:52 +02003095handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3096{
3097 struct shell_output *output =
3098 container_of(listener, struct shell_output, panel_surface_listener);
3099
3100 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003101 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003102 output->panel_surface = NULL;
3103}
3104
3105
3106static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003107desktop_shell_set_panel(struct wl_client *client,
3108 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003109 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003110 struct wl_resource *surface_resource)
3111{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003112 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003113 struct weston_surface *surface =
3114 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003116 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003117
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003118 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003119 wl_resource_post_error(surface_resource,
3120 WL_DISPLAY_ERROR_INVALID_OBJECT,
3121 "surface role already assigned");
3122 return;
3123 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003124
Jason Ekstranda7af7042013-10-12 22:38:11 -05003125 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3126 weston_view_destroy(view);
3127 view = weston_view_create(surface);
3128
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003129 surface->committed = panel_committed;
3130 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003131 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003132 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003133 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003134
3135 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003136 if (sh_output->panel_surface) {
3137 /* The output already has a panel, tell our helper
3138 * there is no need for another one. */
3139 weston_desktop_shell_send_configure(resource, 0,
3140 surface_resource,
3141 0, 0);
3142 } else {
3143 weston_desktop_shell_send_configure(resource, 0,
3144 surface_resource,
3145 surface->output->width,
3146 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003147
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003148 sh_output->panel_surface = surface;
3149
3150 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3151 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3152 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003153}
3154
Pekka Paalanen8274d902014-08-06 19:36:51 +03003155static int
3156lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3157{
3158 return snprintf(buf, len, "lock window");
3159}
3160
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003161static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003162lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003163{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003164 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 struct weston_view *view;
3166
3167 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003168
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003169 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003170 return;
3171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003173
3174 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003175 weston_layer_entry_insert(&shell->lock_layer.view_list,
3176 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003177 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003178 surface->is_mapped = true;
3179 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003180 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003181 }
3182}
3183
3184static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003185handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003186{
Tiago Vignattibe143262012-04-16 17:31:41 +03003187 struct desktop_shell *shell =
3188 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003189
Martin Minarik6d118362012-06-07 18:01:59 +02003190 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003191 shell->lock_surface = NULL;
3192}
3193
3194static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003195desktop_shell_set_lock_surface(struct wl_client *client,
3196 struct wl_resource *resource,
3197 struct wl_resource *surface_resource)
3198{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003199 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003200 struct weston_surface *surface =
3201 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003202
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003203 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003204
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003205 if (!shell->locked)
3206 return;
3207
Pekka Paalanen98262232011-12-01 10:42:22 +02003208 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003209
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003210 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003211 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003212 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003213
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003214 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003215 surface->committed = lock_surface_committed;
3216 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003217 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003218}
3219
3220static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003221resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003222{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003223 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003224
Quentin Glidic82681572016-12-17 13:40:51 +01003225 weston_layer_unset_position(&shell->lock_layer);
3226
3227 if (shell->showing_input_panels)
3228 weston_layer_set_position(&shell->input_panel_layer,
3229 WESTON_LAYER_POSITION_TOP_UI);
3230 weston_layer_set_position(&shell->fullscreen_layer,
3231 WESTON_LAYER_POSITION_FULLSCREEN);
3232 weston_layer_set_position(&shell->panel_layer,
3233 WESTON_LAYER_POSITION_UI);
3234 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003235
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003236 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003237
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003238 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003239 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003240 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003241}
3242
3243static void
3244desktop_shell_unlock(struct wl_client *client,
3245 struct wl_resource *resource)
3246{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003247 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003248
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003249 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003250
3251 if (shell->locked)
3252 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003253}
3254
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003255static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003256desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003257 struct wl_resource *resource,
3258 struct wl_resource *surface_resource)
3259{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003260 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003261
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003262 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003263 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003264}
3265
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003266static void
3267desktop_shell_desktop_ready(struct wl_client *client,
3268 struct wl_resource *resource)
3269{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003270 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003271
3272 shell_fade_startup(shell);
3273}
3274
Jonny Lamb765760d2014-08-20 15:53:19 +02003275static void
3276desktop_shell_set_panel_position(struct wl_client *client,
3277 struct wl_resource *resource,
3278 uint32_t position)
3279{
3280 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3281
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003282 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3283 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3284 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3285 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003286 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003287 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003288 "bad position argument");
3289 return;
3290 }
3291
3292 shell->panel_position = position;
3293}
3294
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003295static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003296 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003297 desktop_shell_set_panel,
3298 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003299 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003300 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003301 desktop_shell_desktop_ready,
3302 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003303};
3304
3305static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003306move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003307 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003308{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003309 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003310 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003311 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003312
Derek Foreman8ae2db52015-07-15 13:00:36 -05003313 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003314 return;
3315
Derek Foreman8ae2db52015-07-15 13:00:36 -05003316 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003317
Pekka Paalanen01388e22013-04-25 13:57:44 +03003318 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003319 if (surface == NULL)
3320 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003321
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003322 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003323 if (shsurf == NULL ||
3324 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3325 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003326 return;
3327
Derek Foreman794fa0e2015-07-15 13:00:38 -05003328 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003329}
3330
3331static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003332maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003333 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003334{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003335 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003336 struct weston_surface *surface;
3337 struct shell_surface *shsurf;
3338
3339 surface = weston_surface_get_main_surface(focus);
3340 if (surface == NULL)
3341 return;
3342
3343 shsurf = get_shell_surface(surface);
3344 if (shsurf == NULL)
3345 return;
3346
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003347 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003348}
3349
3350static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003351fullscreen_binding(struct weston_keyboard *keyboard,
3352 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003353{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003354 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003355 struct weston_surface *surface;
3356 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003357 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003358
3359 surface = weston_surface_get_main_surface(focus);
3360 if (surface == NULL)
3361 return;
3362
3363 shsurf = get_shell_surface(surface);
3364 if (shsurf == NULL)
3365 return;
3366
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003367 fullscreen =
3368 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003369
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003370 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003371}
3372
3373static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003374touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003375{
Derek Foreman362656b2014-09-04 10:23:05 -05003376 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003377 struct weston_surface *surface;
3378 struct shell_surface *shsurf;
3379
Derek Foreman8ae2db52015-07-15 13:00:36 -05003380 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003381 return;
3382
Derek Foreman8ae2db52015-07-15 13:00:36 -05003383 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003384 surface = weston_surface_get_main_surface(focus);
3385 if (surface == NULL)
3386 return;
3387
3388 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003389 if (shsurf == NULL ||
3390 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3391 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003392 return;
3393
Derek Foremanb7674ae2015-07-15 13:00:37 -05003394 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003395}
3396
3397static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003398resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003399 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003400{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003401 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003402 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003403 uint32_t edges = 0;
3404 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003405 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003406
Derek Foreman8ae2db52015-07-15 13:00:36 -05003407 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003408 return;
3409
Derek Foreman8ae2db52015-07-15 13:00:36 -05003410 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003411
Pekka Paalanen01388e22013-04-25 13:57:44 +03003412 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003413 if (surface == NULL)
3414 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003415
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003416 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003417 if (shsurf == NULL ||
3418 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3419 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003420 return;
3421
Jason Ekstranda7af7042013-10-12 22:38:11 -05003422 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003423 wl_fixed_to_int(pointer->grab_x),
3424 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003425 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003426
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003427 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003428 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003429 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003430 edges |= 0;
3431 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003432 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003433
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003434 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003435 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003436 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003437 edges |= 0;
3438 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003439 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003440
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003441 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003442}
3443
3444static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003445surface_opacity_binding(struct weston_pointer *pointer,
3446 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003447 struct weston_pointer_axis_event *event,
3448 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003449{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003450 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003451 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003452 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003453 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003454
Pekka Paalanen01388e22013-04-25 13:57:44 +03003455 /* XXX: broken for windows containing sub-surfaces */
3456 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003457 if (surface == NULL)
3458 return;
3459
3460 shsurf = get_shell_surface(surface);
3461 if (!shsurf)
3462 return;
3463
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003464 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003465
Jason Ekstranda7af7042013-10-12 22:38:11 -05003466 if (shsurf->view->alpha > 1.0)
3467 shsurf->view->alpha = 1.0;
3468 if (shsurf->view->alpha < step)
3469 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003470
Jason Ekstranda7af7042013-10-12 22:38:11 -05003471 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003472 weston_surface_damage(surface);
3473}
3474
3475static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003476do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3477 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003478{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003479 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003480 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003481 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003482 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003483
Derek Foreman1281a362015-07-31 16:55:32 -05003484 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003485 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3486 return;
3487 }
3488
Scott Moreauccbf29d2012-02-22 14:21:41 -07003489 wl_list_for_each(output, &compositor->output_list, link) {
3490 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003491 wl_fixed_to_double(pointer->x),
3492 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003493 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003494 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003495 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003496 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003497 increment = -output->zoom.increment;
3498 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003499 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003500 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003501 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003502 else
3503 increment = 0;
3504
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003505 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003506
Scott Moreaue6603982012-06-11 13:07:51 -06003507 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003508 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003509 else if (output->zoom.level > output->zoom.max_level)
3510 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003511
3512 if (!output->zoom.active) {
3513 if (output->zoom.level <= 0.0)
3514 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003515 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003516 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003517
Scott Moreaue6603982012-06-11 13:07:51 -06003518 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003519
Jason Ekstranda7af7042013-10-12 22:38:11 -05003520 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003521 }
3522 }
3523}
3524
Scott Moreauccbf29d2012-02-22 14:21:41 -07003525static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003526zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003527 struct weston_pointer_axis_event *event,
3528 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003529{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003530 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003531}
3532
3533static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003534zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003535 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003536{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003537 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003538}
3539
3540static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003541terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003542 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003543{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003544 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003545
Pekka Paalanen052032d2019-02-06 10:44:37 +02003546 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003547}
3548
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003549static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003550rotate_grab_motion(struct weston_pointer_grab *grab,
3551 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003552 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003553{
3554 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003555 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003556 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003557 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003558 struct weston_surface *surface =
3559 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003560 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003561
Jonas Ådahld2510102014-10-05 21:39:14 +02003562 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003563
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003564 if (!shsurf)
3565 return;
3566
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003567 cx = 0.5f * surface->width;
3568 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003569
Daniel Stone37816df2012-05-16 18:45:18 +01003570 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3571 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003572 r = sqrtf(dx * dx + dy * dy);
3573
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003574 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003575 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003576
3577 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003578 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003579 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003581 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003582 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003583
3584 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003585 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003586 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003587 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003588 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003589
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003590 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003591 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003592 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003593 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003594 wl_list_init(&shsurf->rotation.transform.link);
3595 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003596 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003597 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003598
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003599 /* We need to adjust the position of the surface
3600 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003601 cposx = shsurf->view->geometry.x + cx;
3602 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003603 dposx = rotate->center.x - cposx;
3604 dposy = rotate->center.y - cposy;
3605 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 weston_view_set_position(shsurf->view,
3607 shsurf->view->geometry.x + dposx,
3608 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003609 }
3610
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003611 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003612 * lazily applies the damage due to rotation update.
3613 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003614 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003615}
3616
3617static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003618rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003619 const struct timespec *time,
3620 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003621{
3622 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003623 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003624 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003625 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003626 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003627
Daniel Stone4dbadb12012-05-30 16:31:51 +01003628 if (pointer->button_count == 0 &&
3629 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003630 if (shsurf)
3631 weston_matrix_multiply(&shsurf->rotation.rotation,
3632 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003633 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003634 free(rotate);
3635 }
3636}
3637
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003638static void
3639rotate_grab_cancel(struct weston_pointer_grab *grab)
3640{
3641 struct rotate_grab *rotate =
3642 container_of(grab, struct rotate_grab, base.grab);
3643
3644 shell_grab_end(&rotate->base);
3645 free(rotate);
3646}
3647
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003648static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003649 noop_grab_focus,
3650 rotate_grab_motion,
3651 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003652 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003653 noop_grab_axis_source,
3654 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003655 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003656};
3657
3658static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003659surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003661 struct weston_surface *surface =
3662 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003663 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003664 float dx, dy;
3665 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666
Pekka Paalanen460099f2012-01-20 16:48:25 +02003667 rotate = malloc(sizeof *rotate);
3668 if (!rotate)
3669 return;
3670
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003671 weston_view_to_global_float(shsurf->view,
3672 surface->width * 0.5f,
3673 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003675
Derek Foreman74de4692015-07-15 13:00:39 -05003676 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3677 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003678 r = sqrtf(dx * dx + dy * dy);
3679 if (r > 20.0f) {
3680 struct weston_matrix inverse;
3681
3682 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003683 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003684 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003685
3686 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003687 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003688 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003689 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003690 weston_matrix_init(&rotate->rotation);
3691 }
3692
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003693 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003694 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003695}
3696
3697static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003698rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3699 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003700{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003701 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003702 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003703 struct shell_surface *surface;
3704
Derek Foreman8ae2db52015-07-15 13:00:36 -05003705 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003706 return;
3707
Derek Foreman8ae2db52015-07-15 13:00:36 -05003708 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003709
Pekka Paalanen01388e22013-04-25 13:57:44 +03003710 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003711 if (base_surface == NULL)
3712 return;
3713
3714 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003715 if (surface == NULL ||
3716 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3717 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003718 return;
3719
Derek Foreman74de4692015-07-15 13:00:39 -05003720 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003721}
3722
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003723/* Move all fullscreen layers down to the current workspace and hide their
3724 * black views. The surfaces' state is set to both fullscreen and lowered,
3725 * and this is reversed when such a surface is re-configured, see
3726 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3727 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003728 * lowering_output = NULL - Lower on all outputs, else only lower on the
3729 * specified output.
3730 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003731 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003732 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003733void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003734lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003735 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003736{
3737 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003738 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003739
3740 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003741 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003742 &shell->fullscreen_layer.view_list.link,
3743 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003744 struct shell_surface *shsurf = get_shell_surface(view->surface);
3745
3746 if (!shsurf)
3747 continue;
3748
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003749 /* Only lower surfaces which have lowering_output as their fullscreen
3750 * output, unless a NULL output asks for lowering on all outputs.
3751 */
3752 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3753 continue;
3754
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003755 /* We can have a non-fullscreen popup for a fullscreen surface
3756 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003757 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003758 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003759 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3760 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003761 weston_view_damage_below(shsurf->fullscreen.black_view);
3762
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003763 }
3764
3765 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003766 weston_layer_entry_remove(&view->layer_link);
3767 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003768 weston_view_damage_below(view);
3769 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003770
3771 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003772 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003773}
3774
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003775void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003776activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003777 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003779 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003780 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003781 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003782 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003783 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003784 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003785
Pekka Paalanen01388e22013-04-25 13:57:44 +03003786 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003787 shsurf = get_shell_surface(main_surface);
3788 assert(shsurf);
3789
3790 /* Only demote fullscreen surfaces on the output of activated shsurf.
3791 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003792 if (shsurf->output)
3793 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003794
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003795 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003796
Jonas Ådahl8538b222012-08-29 22:13:03 +02003797 state = ensure_focus_state(shell, seat);
3798 if (state == NULL)
3799 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003800
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003801 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003802 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003803
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003804 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3805 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003806 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003807
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003808 /* Update the surface’s layer. This brings it to the top of the stacking
3809 * order as appropriate. */
3810 shell_surface_update_layer(shsurf);
3811
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003812 if (shell->focus_animation_type != ANIMATION_NONE) {
3813 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003814 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003815 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003816}
3817
Alex Wu21858432012-04-01 20:13:08 +08003818/* no-op func for checking black surface */
3819static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003820black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003821{
3822}
3823
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003824static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003825is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003826{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003827 struct weston_surface *surface = view->surface;
3828
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003829 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003830 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003831 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003832 return true;
3833 }
3834 return false;
3835}
3836
Kristian Høgsberg75840622011-09-06 13:48:16 -04003837static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003838activate_binding(struct weston_seat *seat,
3839 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003840 struct weston_view *focus_view,
3841 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003842{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003843 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003844 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003845
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003846 if (!focus_view)
3847 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003848
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003849 if (is_black_surface_view(focus_view, &main_view))
3850 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003851
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003852 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003853 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003854 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003855
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003856 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003857}
3858
3859static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003860click_to_activate_binding(struct weston_pointer *pointer,
3861 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003862 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003863{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003864 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003865 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003866 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003867 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003868
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003869 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003870 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003871 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003872}
3873
3874static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003875touch_to_activate_binding(struct weston_touch *touch,
3876 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003877 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003878{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003879 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003880 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003881 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003882 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003883
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003884 activate_binding(touch->seat, data, touch->focus,
3885 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003886}
3887
3888static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003889unfocus_all_seats(struct desktop_shell *shell)
3890{
3891 struct weston_seat *seat, *next;
3892
3893 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003894 struct weston_keyboard *keyboard =
3895 weston_seat_get_keyboard(seat);
3896
3897 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003898 continue;
3899
Derek Foreman1281a362015-07-31 16:55:32 -05003900 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003901 }
3902}
3903
3904static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003905lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003906{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003907 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003908
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003909 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003910 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003911 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003912 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003913
3914 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003915
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003916 /* Hide all surfaces by removing the fullscreen, panel and
3917 * toplevel layers. This way nothing else can show or receive
3918 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003919
Quentin Glidic82681572016-12-17 13:40:51 +01003920 weston_layer_unset_position(&shell->panel_layer);
3921 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003922 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003923 weston_layer_unset_position(&shell->input_panel_layer);
3924 weston_layer_unset_position(&ws->layer);
3925
3926 weston_layer_set_position(&shell->lock_layer,
3927 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003928
Derek Foreman004b4a12015-07-20 16:28:13 -05003929 weston_compositor_sleep(shell->compositor);
3930
Neil Robertsb4a91702014-04-09 16:33:32 +01003931 /* Remove the keyboard focus on all seats. This will be
3932 * restored to the workspace's saved state via
3933 * restore_focus_state when the compositor is unlocked */
3934 unfocus_all_seats(shell);
3935
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003936 /* TODO: disable bindings that should not work while locked. */
3937
3938 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003939}
3940
3941static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003942unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003943{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003944 struct wl_resource *shell_resource;
3945
Pekka Paalanend81c2162011-11-16 13:47:34 +02003946 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003947 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003948 return;
3949 }
3950
3951 /* If desktop-shell client has gone away, unlock immediately. */
3952 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003953 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003954 return;
3955 }
3956
3957 if (shell->prepare_event_sent)
3958 return;
3959
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003960 shell_resource = shell->child.desktop_shell;
3961 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003962 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003963}
3964
3965static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003966shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003967{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003968 struct shell_output *shell_output = data;
3969 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003970
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003971 shell_output->fade.animation = NULL;
3972 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003973 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003974 weston_surface_destroy(shell_output->fade.view->surface);
3975 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003976 break;
3977 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003978 lock(shell);
3979 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003980 default:
3981 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982 }
3983}
3984
Jason Ekstranda7af7042013-10-12 22:38:11 -05003985static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003986shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003987{
3988 struct weston_compositor *compositor = shell->compositor;
3989 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003991
3992 surface = weston_surface_create(compositor);
3993 if (!surface)
3994 return NULL;
3995
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 view = weston_view_create(surface);
3997 if (!view) {
3998 weston_surface_destroy(surface);
3999 return NULL;
4000 }
4001
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004002 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4003 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004004 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004005 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4006 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004007 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004008 surface->is_mapped = true;
4009 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004010
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004012}
4013
4014static void
4015shell_fade(struct desktop_shell *shell, enum fade_type type)
4016{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004017 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004018 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004019
4020 switch (type) {
4021 case FADE_IN:
4022 tint = 0.0;
4023 break;
4024 case FADE_OUT:
4025 tint = 1.0;
4026 break;
4027 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004028 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004029 return;
4030 }
4031
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004032 /* Create a separate fade surface for each output */
4033 wl_list_for_each(shell_output, &shell->output_list, link) {
4034 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004035
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004036 if (shell_output->fade.view == NULL) {
4037 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4038 if (!shell_output->fade.view)
4039 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004040
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004041 shell_output->fade.view->alpha = 1.0 - tint;
4042 weston_view_update_transform(shell_output->fade.view);
4043 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004044
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004045 if (shell_output->fade.view->output == NULL) {
4046 /* If the black view gets a NULL output, we lost the
4047 * last output and we'll just cancel the fade. This
4048 * happens when you close the last window under the
4049 * X11 or Wayland backends. */
4050 shell->locked = false;
4051 weston_surface_destroy(shell_output->fade.view->surface);
4052 shell_output->fade.view = NULL;
4053 } else if (shell_output->fade.animation) {
4054 weston_fade_update(shell_output->fade.animation, tint);
4055 } else {
4056 shell_output->fade.animation =
4057 weston_fade_run(shell_output->fade.view,
4058 1.0 - tint, tint, 300.0,
4059 shell_fade_done_for_output, shell_output);
4060 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004061 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004062}
4063
4064static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004065do_shell_fade_startup(void *data)
4066{
4067 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004068 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004069
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004070 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004071 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004072 } else {
4073 weston_log("desktop shell: "
4074 "unexpected fade-in animation type %d\n",
4075 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004076 wl_list_for_each(shell_output, &shell->output_list, link) {
4077 weston_surface_destroy(shell_output->fade.view->surface);
4078 shell_output->fade.view = NULL;
4079 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004080 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004081}
4082
4083static void
4084shell_fade_startup(struct desktop_shell *shell)
4085{
4086 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004087 struct shell_output *shell_output;
4088 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004089
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004090 wl_list_for_each(shell_output, &shell->output_list, link) {
4091 if (!shell_output->fade.startup_timer)
4092 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004093
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004094 wl_event_source_remove(shell_output->fade.startup_timer);
4095 shell_output->fade.startup_timer = NULL;
4096 has_fade = true;
4097 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004098
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004099 if (has_fade) {
4100 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4101 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4102 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004103}
4104
4105static int
4106fade_startup_timeout(void *data)
4107{
4108 struct desktop_shell *shell = data;
4109
4110 shell_fade_startup(shell);
4111 return 0;
4112}
4113
4114static void
4115shell_fade_init(struct desktop_shell *shell)
4116{
4117 /* Make compositor output all black, and wait for the desktop-shell
4118 * client to signal it is ready, then fade in. The timer triggers a
4119 * fade-in, in case the desktop-shell client takes too long.
4120 */
4121
4122 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004123 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004124
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004125 if (shell->startup_animation_type == ANIMATION_NONE)
4126 return;
4127
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004128 wl_list_for_each(shell_output, &shell->output_list, link) {
4129 if (shell_output->fade.view != NULL) {
4130 weston_log("%s: warning: fade surface already exists\n",
4131 __func__);
4132 continue;
4133 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004134
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004135 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4136 if (!shell_output->fade.view)
4137 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004138
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004139 weston_view_update_transform(shell_output->fade.view);
4140 weston_surface_damage(shell_output->fade.view->surface);
4141
4142 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4143 shell_output->fade.startup_timer =
4144 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4145 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4146 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004147}
4148
4149static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004150idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004151{
4152 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004153 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004154
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004155 struct weston_seat *seat;
4156
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004157 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4158 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004159
4160 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004161 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004162}
4163
4164static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004165wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004166{
4167 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004168 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004169
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004170 unlock(shell);
4171}
4172
4173static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004174transform_handler(struct wl_listener *listener, void *data)
4175{
4176 struct weston_surface *surface = data;
4177 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004178 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004179 int x, y;
4180
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004181 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004182 return;
4183
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004184 api = shsurf->shell->xwayland_surface_api;
4185 if (!api) {
4186 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4187 shsurf->shell->xwayland_surface_api = api;
4188 }
4189
4190 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004191 return;
4192
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004193 if (!weston_view_is_mapped(shsurf->view))
4194 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004195
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004196 x = shsurf->view->geometry.x;
4197 y = shsurf->view->geometry.y;
4198
4199 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004200}
4201
4202static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004203center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004204{
Giulio Camuffob8366642013-04-25 13:57:46 +03004205 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004206 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004207
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004208 if (!output) {
4209 weston_view_set_position(view, 0, 0);
4210 return;
4211 }
4212
Jason Ekstranda7af7042013-10-12 22:38:11 -05004213 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004214
4215 x = output->x + (output->width - width) / 2 - surf_x / 2;
4216 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004217
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004218 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004219}
4220
4221static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004222weston_view_set_initial_position(struct weston_view *view,
4223 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004224{
4225 struct weston_compositor *compositor = shell->compositor;
4226 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004227 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004228 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004229 struct weston_output *output, *target_output = NULL;
4230 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004231 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004232
4233 /* As a heuristic place the new window on the same output as the
4234 * pointer. Falling back to the output containing 0, 0.
4235 *
4236 * TODO: Do something clever for touch too?
4237 */
4238 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004239 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4240
4241 if (pointer) {
4242 ix = wl_fixed_to_int(pointer->x);
4243 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004244 break;
4245 }
4246 }
4247
4248 wl_list_for_each(output, &compositor->output_list, link) {
4249 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4250 target_output = output;
4251 break;
4252 }
4253 }
4254
4255 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004256 weston_view_set_position(view, 10 + random() % 400,
4257 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004258 return;
4259 }
4260
4261 /* Valid range within output where the surface will still be onscreen.
4262 * If this is negative it means that the surface is bigger than
4263 * output.
4264 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004265 get_output_work_area(shell, target_output, &area);
4266
Derek Foremanf814c5d2015-04-15 12:23:54 -05004267 x = area.x;
4268 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004269 range_x = area.width - view->surface->width;
4270 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004271
Rob Bradford4cb88c72012-08-13 15:18:44 +01004272 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004273 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004274
4275 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004276 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004277
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004279}
4280
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004281static bool
4282check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4283{
4284 struct timespec now;
4285
4286 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4287 return false;
4288
4289 /*
4290 * If the shell helper client dies before the session has been
4291 * up for roughly 30 seconds, better just make Weston shut down,
4292 * because the user likely has no way to interact with the desktop
4293 * anyway.
4294 */
4295 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4296 weston_log("Error: %s apparently cannot run at all.\n",
4297 shell->client);
4298 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004299 weston_compositor_exit_with_code(shell->compositor,
4300 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004301
4302 return true;
4303 }
4304
4305 return false;
4306}
4307
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004308static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004309
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004310static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004311respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004312{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004313 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004314
4315 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004316 weston_compositor_get_time(&time);
4317 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004318 shell->child.deathstamp = time;
4319 shell->child.deathcount = 0;
4320 }
4321
4322 shell->child.deathcount++;
4323 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004324 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004325 return;
4326 }
4327
Pekka Paalanen826dc142014-08-27 12:11:53 +03004328 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004329 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004330}
4331
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004332static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004333desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4334{
4335 struct desktop_shell *shell;
4336
4337 shell = container_of(listener, struct desktop_shell,
4338 child.client_destroy_listener);
4339
Pekka Paalanen826dc142014-08-27 12:11:53 +03004340 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004341 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004342 /*
4343 * unbind_desktop_shell() will reset shell->child.desktop_shell
4344 * before the respawned process has a chance to create a new
4345 * desktop_shell object, because we are being called from the
4346 * wl_client destructor which destroys all wl_resources before
4347 * returning.
4348 */
4349
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004350 if (!check_desktop_shell_crash_too_early(shell))
4351 respawn_desktop_shell_process(shell);
4352
Pekka Paalanen826dc142014-08-27 12:11:53 +03004353 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004354}
4355
4356static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004357launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004358{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004359 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004360
Pekka Paalanen826dc142014-08-27 12:11:53 +03004361 shell->child.client = weston_client_start(shell->compositor,
4362 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004363
Arnaud Vrac42631192014-08-25 20:56:46 +02004364 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004365 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004366 return;
4367 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004368
4369 shell->child.client_destroy_listener.notify =
4370 desktop_shell_client_destroy;
4371 wl_client_add_destroy_listener(shell->child.client,
4372 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004373}
4374
4375static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004376unbind_desktop_shell(struct wl_resource *resource)
4377{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004378 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004379
4380 if (shell->locked)
4381 resume_desktop(shell);
4382
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004383 shell->child.desktop_shell = NULL;
4384 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004385}
4386
4387static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004388bind_desktop_shell(struct wl_client *client,
4389 void *data, uint32_t version, uint32_t id)
4390{
Tiago Vignattibe143262012-04-16 17:31:41 +03004391 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004392 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004393
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004394 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4395 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004396
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004397 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004398 wl_resource_set_implementation(resource,
4399 &desktop_shell_implementation,
4400 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004401 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004402 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004403 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004404
4405 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4406 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407}
4408
Kristian Høgsberg07045392012-02-19 18:52:44 -05004409struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004410 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004411 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004412 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004413 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004414 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004415};
4416
4417static void
4418switcher_next(struct switcher *switcher)
4419{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004421 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004422 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004423 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004424
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004425 /* temporary re-display minimized surfaces */
4426 struct weston_view *tmp;
4427 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004428 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4429 weston_layer_entry_remove(&view->layer_link);
4430 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004431 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4432 *minimized = view;
4433 }
4434
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004435 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004436 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004437 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004438 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004439 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004440 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004441 next = view;
4442 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 view->alpha = 0.25;
4444 weston_view_geometry_dirty(view);
4445 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004446 }
Alex Wu1659daa2012-04-01 20:13:09 +08004447
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004448 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 view->alpha = 0.25;
4450 weston_view_geometry_dirty(view);
4451 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004452 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004453 }
4454
4455 if (next == NULL)
4456 next = first;
4457
Alex Wu07b26062012-03-12 16:06:01 +08004458 if (next == NULL)
4459 return;
4460
Kristian Høgsberg07045392012-02-19 18:52:44 -05004461 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004462 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004463
4464 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004465 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004467
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004468 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004469 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004471}
4472
4473static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004474switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004475{
4476 struct switcher *switcher =
4477 container_of(listener, struct switcher, listener);
4478
4479 switcher_next(switcher);
4480}
4481
4482static void
Daniel Stone351eb612012-05-31 15:27:47 -04004483switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004484{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004485 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004486 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004487 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004489 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004490 if (is_focus_view(view))
4491 continue;
4492
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493 view->alpha = 1.0;
4494 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004495 }
4496
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004497 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004498 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004499 keyboard->seat,
4500 WESTON_ACTIVATE_FLAG_CONFIGURE);
4501 }
4502
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004504 weston_keyboard_end_grab(keyboard);
4505 if (keyboard->input_method_resource)
4506 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004507
4508 /* re-hide surfaces that were temporary shown during the switch */
4509 struct weston_view **minimized;
4510 wl_array_for_each(minimized, &switcher->minimized_array) {
4511 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004512 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004513 weston_layer_entry_remove(&(*minimized)->layer_link);
4514 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004515 weston_view_damage_below(*minimized);
4516 }
4517 }
4518 wl_array_release(&switcher->minimized_array);
4519
Kristian Høgsberg07045392012-02-19 18:52:44 -05004520 free(switcher);
4521}
4522
4523static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004524switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004525 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004526{
4527 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004528 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004529
Daniel Stonec9785ea2012-05-30 16:31:52 +01004530 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004531 switcher_next(switcher);
4532}
4533
4534static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004535switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004536 uint32_t mods_depressed, uint32_t mods_latched,
4537 uint32_t mods_locked, uint32_t group)
4538{
4539 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004540 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004541
Daniel Stone351eb612012-05-31 15:27:47 -04004542 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4543 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004544}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004546static void
4547switcher_cancel(struct weston_keyboard_grab *grab)
4548{
4549 struct switcher *switcher = container_of(grab, struct switcher, grab);
4550
4551 switcher_destroy(switcher);
4552}
4553
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004554static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004555 switcher_key,
4556 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004557 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004558};
4559
4560static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004561switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004562 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563{
Tiago Vignattibe143262012-04-16 17:31:41 +03004564 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004565 struct switcher *switcher;
4566
4567 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004568 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004569 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004570 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004571 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004572 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004573
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004574 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004575 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004576 weston_keyboard_start_grab(keyboard, &switcher->grab);
4577 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004578 switcher_next(switcher);
4579}
4580
Pekka Paalanen3c647232011-12-22 13:43:43 +02004581static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004582backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004583 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004584{
4585 struct weston_compositor *compositor = data;
4586 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004587 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004588
4589 /* TODO: we're limiting to simple use cases, where we assume just
4590 * control on the primary display. We'd have to extend later if we
4591 * ever get support for setting backlights on random desktop LCD
4592 * panels though */
4593 output = get_default_output(compositor);
4594 if (!output)
4595 return;
4596
4597 if (!output->set_backlight)
4598 return;
4599
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004600 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4601 backlight_new = output->backlight_current - 25;
4602 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4603 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004604
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004605 if (backlight_new < 5)
4606 backlight_new = 5;
4607 if (backlight_new > 255)
4608 backlight_new = 255;
4609
4610 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004611 output->set_backlight(output, output->backlight_current);
4612}
4613
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004614static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004615force_kill_binding(struct weston_keyboard *keyboard,
4616 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004617{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004618 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004619 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004620 struct desktop_shell *shell = data;
4621 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004622 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004623
Derek Foreman8ae2db52015-07-15 13:00:36 -05004624 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004625 if (!focus_surface)
4626 return;
4627
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004628 wl_signal_emit(&compositor->kill_signal, focus_surface);
4629
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004630 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004631 wl_client_get_credentials(client, &pid, NULL, NULL);
4632
4633 /* Skip clients that we launched ourselves (the credentials of
4634 * the socketpair is ours) */
4635 if (pid == getpid())
4636 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004637
Daniel Stone325fc2d2012-05-30 16:31:58 +01004638 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004639}
4640
4641static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004642workspace_up_binding(struct weston_keyboard *keyboard,
4643 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004644{
4645 struct desktop_shell *shell = data;
4646 unsigned int new_index = shell->workspaces.current;
4647
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004648 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004649 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004650 if (new_index != 0)
4651 new_index--;
4652
4653 change_workspace(shell, new_index);
4654}
4655
4656static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004657workspace_down_binding(struct weston_keyboard *keyboard,
4658 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004659{
4660 struct desktop_shell *shell = data;
4661 unsigned int new_index = shell->workspaces.current;
4662
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004663 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004664 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004665 if (new_index < shell->workspaces.num - 1)
4666 new_index++;
4667
4668 change_workspace(shell, new_index);
4669}
4670
4671static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004672workspace_f_binding(struct weston_keyboard *keyboard,
4673 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004674{
4675 struct desktop_shell *shell = data;
4676 unsigned int new_index;
4677
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004678 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004679 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004680 new_index = key - KEY_F1;
4681 if (new_index >= shell->workspaces.num)
4682 new_index = shell->workspaces.num - 1;
4683
4684 change_workspace(shell, new_index);
4685}
4686
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004687static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004688workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004689 const struct timespec *time, uint32_t key,
4690 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004691{
4692 struct desktop_shell *shell = data;
4693 unsigned int new_index = shell->workspaces.current;
4694
4695 if (shell->locked)
4696 return;
4697
4698 if (new_index != 0)
4699 new_index--;
4700
Derek Foreman8ae2db52015-07-15 13:00:36 -05004701 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004702}
4703
4704static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004705workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004706 const struct timespec *time, uint32_t key,
4707 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004708{
4709 struct desktop_shell *shell = data;
4710 unsigned int new_index = shell->workspaces.current;
4711
4712 if (shell->locked)
4713 return;
4714
4715 if (new_index < shell->workspaces.num - 1)
4716 new_index++;
4717
Derek Foreman8ae2db52015-07-15 13:00:36 -05004718 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004719}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004720
4721static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004722shell_reposition_view_on_output_destroy(struct weston_view *view)
4723{
4724 struct weston_output *output, *first_output;
4725 struct weston_compositor *ec = view->surface->compositor;
4726 struct shell_surface *shsurf;
4727 float x, y;
4728 int visible;
4729
4730 x = view->geometry.x;
4731 y = view->geometry.y;
4732
4733 /* At this point the destroyed output is not in the list anymore.
4734 * If the view is still visible somewhere, we leave where it is,
4735 * otherwise, move it to the first output. */
4736 visible = 0;
4737 wl_list_for_each(output, &ec->output_list, link) {
4738 if (pixman_region32_contains_point(&output->region,
4739 x, y, NULL)) {
4740 visible = 1;
4741 break;
4742 }
4743 }
4744
4745 if (!visible) {
4746 first_output = container_of(ec->output_list.next,
4747 struct weston_output, link);
4748
4749 x = first_output->x + first_output->width / 4;
4750 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004751
4752 weston_view_set_position(view, x, y);
4753 } else {
4754 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004755 }
4756
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004757
4758 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004759 if (!shsurf)
4760 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004761
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004762 shsurf->saved_position_valid = false;
4763 set_maximized(shsurf, false);
4764 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004765}
4766
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004767void
4768shell_for_each_layer(struct desktop_shell *shell,
4769 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004770{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004771 struct workspace **ws;
4772
4773 func(shell, &shell->fullscreen_layer, data);
4774 func(shell, &shell->panel_layer, data);
4775 func(shell, &shell->background_layer, data);
4776 func(shell, &shell->lock_layer, data);
4777 func(shell, &shell->input_panel_layer, data);
4778
4779 wl_array_for_each(ws, &shell->workspaces.array)
4780 func(shell, &(*ws)->layer, data);
4781}
4782
4783static void
4784shell_output_destroy_move_layer(struct desktop_shell *shell,
4785 struct weston_layer *layer,
4786 void *data)
4787{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004788 struct weston_view *view;
4789
Marius Vladea40d6d2018-02-08 18:46:42 +02004790 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004791 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004792}
4793
4794static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004795handle_output_destroy(struct wl_listener *listener, void *data)
4796{
4797 struct shell_output *output_listener =
4798 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004799 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004800
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004801 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004802
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004803 if (output_listener->panel_surface)
4804 wl_list_remove(&output_listener->panel_surface_listener.link);
4805 if (output_listener->background_surface)
4806 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004807 wl_list_remove(&output_listener->destroy_listener.link);
4808 wl_list_remove(&output_listener->link);
4809 free(output_listener);
4810}
4811
4812static void
David Fort50d962f2016-06-09 17:55:52 +02004813shell_resize_surface_to_output(struct desktop_shell *shell,
4814 struct weston_surface *surface,
4815 const struct weston_output *output)
4816{
4817 if (!surface)
4818 return;
4819
4820 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4821 surface->resource,
4822 output->width,
4823 output->height);
4824}
4825
4826
4827static void
4828handle_output_resized(struct wl_listener *listener, void *data)
4829{
4830 struct desktop_shell *shell =
4831 container_of(listener, struct desktop_shell, resized_listener);
4832 struct weston_output *output = (struct weston_output *)data;
4833 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4834
4835 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4836 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4837}
4838
4839static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004840create_shell_output(struct desktop_shell *shell,
4841 struct weston_output *output)
4842{
4843 struct shell_output *shell_output;
4844
4845 shell_output = zalloc(sizeof *shell_output);
4846 if (shell_output == NULL)
4847 return;
4848
4849 shell_output->output = output;
4850 shell_output->shell = shell;
4851 shell_output->destroy_listener.notify = handle_output_destroy;
4852 wl_signal_add(&output->destroy_signal,
4853 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004854 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004855}
4856
4857static void
4858handle_output_create(struct wl_listener *listener, void *data)
4859{
4860 struct desktop_shell *shell =
4861 container_of(listener, struct desktop_shell, output_create_listener);
4862 struct weston_output *output = (struct weston_output *)data;
4863
4864 create_shell_output(shell, output);
4865}
4866
4867static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004868handle_output_move_layer(struct desktop_shell *shell,
4869 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004870{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004871 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004872 struct weston_view *view;
4873 float x, y;
4874
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004875 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004876 if (view->output != output)
4877 continue;
4878
4879 x = view->geometry.x + output->move_x;
4880 y = view->geometry.y + output->move_y;
4881 weston_view_set_position(view, x, y);
4882 }
4883}
4884
4885static void
4886handle_output_move(struct wl_listener *listener, void *data)
4887{
4888 struct desktop_shell *shell;
4889
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004890 shell = container_of(listener, struct desktop_shell,
4891 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004892
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004893 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004894}
4895
4896static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004897setup_output_destroy_handler(struct weston_compositor *ec,
4898 struct desktop_shell *shell)
4899{
4900 struct weston_output *output;
4901
4902 wl_list_init(&shell->output_list);
4903 wl_list_for_each(output, &ec->output_list, link)
4904 create_shell_output(shell, output);
4905
4906 shell->output_create_listener.notify = handle_output_create;
4907 wl_signal_add(&ec->output_created_signal,
4908 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004909
4910 shell->output_move_listener.notify = handle_output_move;
4911 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004912}
4913
4914static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004915shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004916{
Tiago Vignattibe143262012-04-16 17:31:41 +03004917 struct desktop_shell *shell =
4918 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004919 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004920 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004921
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004922 /* Force state to unlocked so we don't try to fade */
4923 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004924
4925 if (shell->child.client) {
4926 /* disable respawn */
4927 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004928 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004929 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004930
Harsha M Mb8b2c722018-08-07 19:05:02 +05304931 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004932 wl_list_remove(&shell->idle_listener.link);
4933 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004934 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004935
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004936 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004937 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004938
Xiong Zhang6b481422013-10-23 13:58:32 +08004939 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4940 wl_list_remove(&shell_output->destroy_listener.link);
4941 wl_list_remove(&shell_output->link);
4942 free(shell_output);
4943 }
4944
4945 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004946 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004947 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004948
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004949 wl_array_for_each(ws, &shell->workspaces.array)
4950 workspace_destroy(*ws);
4951 wl_array_release(&shell->workspaces.array);
4952
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004953 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004954 free(shell);
4955}
4956
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004957static void
4958shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4959{
4960 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004961 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004962
Bob Ham744e6532016-01-12 10:21:48 +00004963 if (shell->allow_zap)
4964 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4965 MODIFIER_CTRL | MODIFIER_ALT,
4966 terminate_binding, ec);
4967
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004968 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004969 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4970 click_to_activate_binding,
4971 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004972 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4973 click_to_activate_binding,
4974 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004975 weston_compositor_add_touch_binding(ec, 0,
4976 touch_to_activate_binding,
4977 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004978 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4979 backlight_binding, ec);
4980 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4981 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004982
4983 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004984 if (shell->exposay_modifier)
4985 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4986 exposay_binding, shell);
4987
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004988 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004989 if (!mod)
4990 return;
4991
Ian Ray13404c42017-09-18 15:22:01 +03004992 /* This binding is not configurable, but is only enabled if there is a
4993 * valid binding modifier. */
4994 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4995 MODIFIER_SUPER | MODIFIER_ALT,
4996 surface_opacity_binding, NULL);
4997
Ian Rayab7c0b62017-09-18 15:22:00 +03004998 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4999 mod, zoom_axis_binding,
5000 NULL);
5001
Daniel Stone325fc2d2012-05-30 16:31:58 +01005002 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5003 zoom_key_binding, NULL);
5004 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5005 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005006 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5007 maximize_binding, NULL);
5008 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5009 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005010 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5011 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005012 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005013 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005014 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005015 weston_compositor_add_button_binding(ec, BTN_LEFT,
5016 mod | MODIFIER_SHIFT,
5017 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005018
5019 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005020 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005021 rotate_binding, NULL);
5022
Daniel Stone325fc2d2012-05-30 16:31:58 +01005023 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5024 shell);
5025 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5026 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005027 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5028 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005029 weston_compositor_add_key_binding(ec, KEY_K, mod,
5030 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005031 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5032 workspace_up_binding, shell);
5033 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5034 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005035 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5036 workspace_move_surface_up_binding,
5037 shell);
5038 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5039 workspace_move_surface_down_binding,
5040 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005041
5042 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5043 if (shell->workspaces.num > 1) {
5044 num_workspace_bindings = shell->workspaces.num;
5045 if (num_workspace_bindings > 6)
5046 num_workspace_bindings = 6;
5047 for (i = 0; i < num_workspace_bindings; i++)
5048 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5049 workspace_f_binding,
5050 shell);
5051 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005052
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005053 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005054}
5055
Jason Ekstrand024177c2014-04-21 19:42:58 -05005056static void
5057handle_seat_created(struct wl_listener *listener, void *data)
5058{
5059 struct weston_seat *seat = data;
5060
5061 create_shell_seat(seat);
5062}
5063
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005064WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005065wet_shell_init(struct weston_compositor *ec,
5066 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005067{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005068 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005069 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005070 struct workspace **pws;
5071 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005072 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005073
Peter Huttererf3d62272013-08-08 11:57:05 +10005074 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005075 if (shell == NULL)
5076 return -1;
5077
Kristian Høgsberg75840622011-09-06 13:48:16 -04005078 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005079
5080 shell->destroy_listener.notify = shell_destroy;
5081 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005082 shell->idle_listener.notify = idle_handler;
5083 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5084 shell->wake_listener.notify = wake_handler;
5085 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005086 shell->transform_listener.notify = transform_handler;
5087 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005088
Quentin Glidic82681572016-12-17 13:40:51 +01005089 weston_layer_init(&shell->fullscreen_layer, ec);
5090 weston_layer_init(&shell->panel_layer, ec);
5091 weston_layer_init(&shell->background_layer, ec);
5092 weston_layer_init(&shell->lock_layer, ec);
5093 weston_layer_init(&shell->input_panel_layer, ec);
5094
5095 weston_layer_set_position(&shell->fullscreen_layer,
5096 WESTON_LAYER_POSITION_FULLSCREEN);
5097 weston_layer_set_position(&shell->panel_layer,
5098 WESTON_LAYER_POSITION_UI);
5099 weston_layer_set_position(&shell->background_layer,
5100 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005101
5102 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005103 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005104
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005105 if (input_panel_setup(shell) < 0)
5106 return -1;
5107
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005108 shell->text_backend = text_backend_init(ec);
5109 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005110 return -1;
5111
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005112 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005113
Daniel Stonedf8133b2013-11-19 11:37:14 +01005114 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5115 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5116
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005117 for (i = 0; i < shell->workspaces.num; i++) {
5118 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5119 if (pws == NULL)
5120 return -1;
5121
Quentin Glidic82681572016-12-17 13:40:51 +01005122 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005123 if (*pws == NULL)
5124 return -1;
5125 }
5126 activate_workspace(shell, 0);
5127
Quentin Glidic82681572016-12-17 13:40:51 +01005128 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005129
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005130 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005131 wl_list_init(&shell->workspaces.animation.link);
5132 shell->workspaces.animation.frame = animate_workspace_change_frame;
5133
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005134 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5135 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005136 return -1;
5137
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005138 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005139 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005140 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005141 return -1;
5142
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005143 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005144
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005145 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005146
Xiong Zhang6b481422013-10-23 13:58:32 +08005147 setup_output_destroy_handler(ec, shell);
5148
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005149 loop = wl_display_get_event_loop(ec->wl_display);
5150 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005151
Jason Ekstrand024177c2014-04-21 19:42:58 -05005152 wl_list_for_each(seat, &ec->seat_list, link)
5153 handle_seat_created(NULL, seat);
5154 shell->seat_create_listener.notify = handle_seat_created;
5155 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005156
David Fort50d962f2016-06-09 17:55:52 +02005157 shell->resized_listener.notify = handle_output_resized;
5158 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5159
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005160 screenshooter_create(ec);
5161
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005162 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005163
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005164 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005165
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005166 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5167
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005168 return 0;
5169}