blob: aac23ac7c4e837a52fdcb7debde9a200e8b768fd [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) {
Greg V15d3d302018-12-07 01:33:34 +0300666 if (state->keyboard_focus) {
667 wl_list_remove(&state->surface_destroy_listener.link);
668 wl_list_init(&state->surface_destroy_listener.link);
669 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100670 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200671 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200672 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400673 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200674 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100675 if (state->ws->focus_animation)
676 weston_view_animation_destroy(state->ws->focus_animation);
677
678 state->ws->focus_animation = weston_fade_run(
679 state->ws->fsurf_front->view,
680 state->ws->fsurf_front->view->alpha, 0.0, 300,
681 focus_animation_done, state->ws);
682 }
683
Kristian Høgsberge3778222012-07-31 17:29:30 -0400684 wl_list_remove(&state->link);
685 focus_state_destroy(state);
686 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200687}
688
689static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200690focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
691 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200692{
Jonas Ådahl04769742012-06-13 00:01:24 +0200693 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200694
695 state = malloc(sizeof *state);
696 if (state == NULL)
697 return NULL;
698
Quentin Glidicfff39812016-08-15 10:56:52 +0200699 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100700 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400701 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200702 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200704
705 state->seat_destroy_listener.notify = focus_state_seat_destroy;
706 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400707 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200708 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400709 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200710
711 return state;
712}
713
Jonas Ådahl8538b222012-08-29 22:13:03 +0200714static struct focus_state *
715ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
716{
717 struct workspace *ws = get_current_workspace(shell);
718 struct focus_state *state;
719
720 wl_list_for_each(state, &ws->focus_list, link)
721 if (state->seat == seat)
722 break;
723
724 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200725 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200726
727 return state;
728}
729
Jonas Ådahl04769742012-06-13 00:01:24 +0200730static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800731focus_state_set_focus(struct focus_state *state,
732 struct weston_surface *surface)
733{
734 if (state->keyboard_focus) {
735 wl_list_remove(&state->surface_destroy_listener.link);
736 wl_list_init(&state->surface_destroy_listener.link);
737 }
738
739 state->keyboard_focus = surface;
740 if (surface)
741 wl_signal_add(&surface->destroy_signal,
742 &state->surface_destroy_listener);
743}
744
745static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400746restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200747{
748 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400749 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100750 struct wl_list pending_seat_list;
751 struct weston_seat *seat, *next_seat;
752
753 /* Temporarily steal the list of seats so that we can keep
754 * track of the seats we've already processed */
755 wl_list_init(&pending_seat_list);
756 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
757 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200758
759 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500760 struct weston_keyboard *keyboard =
761 weston_seat_get_keyboard(state->seat);
762
Neil Roberts4237f502014-04-09 16:33:31 +0100763 wl_list_remove(&state->seat->link);
764 wl_list_insert(&shell->compositor->seat_list,
765 &state->seat->link);
766
Derek Foreman1281a362015-07-31 16:55:32 -0500767 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800768 continue;
769
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400770 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200771
Derek Foreman1281a362015-07-31 16:55:32 -0500772 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200773 }
Neil Roberts4237f502014-04-09 16:33:31 +0100774
775 /* For any remaining seats that we don't have a focus state
776 * for we'll reset the keyboard focus to NULL */
777 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500778 struct weston_keyboard *keyboard =
779 weston_seat_get_keyboard(seat);
780
Neil Roberts4237f502014-04-09 16:33:31 +0100781 wl_list_insert(&shell->compositor->seat_list, &seat->link);
782
Derek Foreman1281a362015-07-31 16:55:32 -0500783 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100784 continue;
785
Derek Foreman1281a362015-07-31 16:55:32 -0500786 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100787 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200788}
789
790static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
792 struct weston_seat *seat)
793{
Derek Foreman1281a362015-07-31 16:55:32 -0500794 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200795 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796
797 wl_list_for_each(state, &ws->focus_list, link) {
798 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500799 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200800 return;
801 }
802 }
803}
804
805static void
806drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
807 struct weston_surface *surface)
808{
809 struct focus_state *state;
810
811 wl_list_for_each(state, &ws->focus_list, link)
812 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800813 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814}
815
816static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100817animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
818 struct weston_view *from, struct weston_view *to)
819{
820 struct weston_output *output;
821 bool focus_surface_created = false;
822
823 /* FIXME: Only support dim animation using two layers */
824 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
825 return;
826
827 output = get_default_output(shell->compositor);
828 if (ws->fsurf_front == NULL && (from || to)) {
829 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800830 if (ws->fsurf_front == NULL)
831 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800833
834 ws->fsurf_back = create_focus_surface(shell->compositor, output);
835 if (ws->fsurf_back == NULL) {
836 focus_surface_destroy(ws->fsurf_front);
837 return;
838 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800840
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 focus_surface_created = true;
842 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300843 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
844 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 }
846
847 if (ws->focus_animation) {
848 weston_view_animation_destroy(ws->focus_animation);
849 ws->focus_animation = NULL;
850 }
851
852 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300853 weston_layer_entry_insert(&to->layer_link,
854 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100855 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300856 weston_layer_entry_insert(&ws->layer.view_list,
857 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858
859 if (focus_surface_created) {
860 ws->focus_animation = weston_fade_run(
861 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200862 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 focus_animation_done, ws);
864 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300865 weston_layer_entry_insert(&from->layer_link,
866 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 ws->focus_animation = weston_stable_fade_run(
868 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200869 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 focus_animation_done, ws);
871 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300872 weston_layer_entry_insert(&ws->layer.view_list,
873 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100874 ws->focus_animation = weston_stable_fade_run(
875 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200876 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100877 focus_animation_done, ws);
878 }
879}
880
881static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200882workspace_destroy(struct workspace *ws)
883{
Jonas Ådahl04769742012-06-13 00:01:24 +0200884 struct focus_state *state, *next;
885
886 wl_list_for_each_safe(state, next, &ws->focus_list, link)
887 focus_state_destroy(state);
888
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100889 if (ws->fsurf_front)
890 focus_surface_destroy(ws->fsurf_front);
891 if (ws->fsurf_back)
892 focus_surface_destroy(ws->fsurf_back);
893
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200894 free(ws);
895}
896
Jonas Ådahl04769742012-06-13 00:01:24 +0200897static void
898seat_destroyed(struct wl_listener *listener, void *data)
899{
900 struct weston_seat *seat = data;
901 struct focus_state *state, *next;
902 struct workspace *ws = container_of(listener,
903 struct workspace,
904 seat_destroyed_listener);
905
906 wl_list_for_each_safe(state, next, &ws->focus_list, link)
907 if (state->seat == seat)
908 wl_list_remove(&state->link);
909}
910
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200911static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100912workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913{
914 struct workspace *ws = malloc(sizeof *ws);
915 if (ws == NULL)
916 return NULL;
917
Quentin Glidic82681572016-12-17 13:40:51 +0100918 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919
Jonas Ådahl04769742012-06-13 00:01:24 +0200920 wl_list_init(&ws->focus_list);
921 wl_list_init(&ws->seat_destroyed_listener.link);
922 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 ws->fsurf_front = NULL;
924 ws->fsurf_back = NULL;
925 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200926
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200927 return ws;
928}
929
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930static int
931workspace_is_empty(struct workspace *ws)
932{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300933 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934}
935
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200936static struct workspace *
937get_workspace(struct desktop_shell *shell, unsigned int index)
938{
939 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200940 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941 pws += index;
942 return *pws;
943}
944
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800945struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200946get_current_workspace(struct desktop_shell *shell)
947{
948 return get_workspace(shell, shell->workspaces.current);
949}
950
951static void
952activate_workspace(struct desktop_shell *shell, unsigned int index)
953{
954 struct workspace *ws;
955
956 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100957 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200958
959 shell->workspaces.current = index;
960}
961
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962static unsigned int
963get_output_height(struct weston_output *output)
964{
965 return abs(output->region.extents.y1 - output->region.extents.y2);
966}
967
Greg Vf57774e2018-07-24 23:21:55 +0300968static struct weston_transform *
969view_get_transform(struct weston_view *view)
970{
971 struct focus_surface *fsurf = NULL;
972 struct shell_surface *shsurf = NULL;
973
974 if (is_focus_view(view)) {
975 fsurf = get_focus_surface(view->surface);
976 return &fsurf->workspace_transform;
977 }
978
979 shsurf = get_shell_surface(view->surface);
980 if (shsurf)
981 return &shsurf->workspace_transform;
982
983 return NULL;
984}
985
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100987view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988{
Greg Vf57774e2018-07-24 23:21:55 +0300989 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990
Greg Vf57774e2018-07-24 23:21:55 +0300991 if (!transform)
992 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 weston_matrix_init(&transform->matrix);
999 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002}
1003
1004static void
1005workspace_translate_out(struct workspace *ws, double fraction)
1006{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008 unsigned int height;
1009 double d;
1010
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001011 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 d = height * fraction;
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
1017}
1018
1019static void
1020workspace_translate_in(struct workspace *ws, double fraction)
1021{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001023 unsigned int height;
1024 double d;
1025
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001026 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028
1029 if (fraction > 0)
1030 d = -(height - height * fraction);
1031 else
1032 d = height + height * fraction;
1033
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035 }
1036}
1037
1038static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001039reverse_workspace_change_animation(struct desktop_shell *shell,
1040 unsigned int index,
1041 struct workspace *from,
1042 struct workspace *to)
1043{
1044 shell->workspaces.current = index;
1045
1046 shell->workspaces.anim_to = to;
1047 shell->workspaces.anim_from = from;
1048 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001049 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050
Quentin Glidic82681572016-12-17 13:40:51 +01001051 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1052 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1053
Scott Moreau4272e632012-08-13 09:58:41 -06001054 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055}
1056
1057static void
1058workspace_deactivate_transforms(struct workspace *ws)
1059{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001061 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001063 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001064 transform = view_get_transform(view);
1065 if (!transform)
1066 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001067
1068 if (!wl_list_empty(&transform->link)) {
1069 wl_list_remove(&transform->link);
1070 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001071 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073 }
1074}
1075
1076static void
1077finish_workspace_change_animation(struct desktop_shell *shell,
1078 struct workspace *from,
1079 struct workspace *to)
1080{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001081 struct weston_view *view;
1082
Scott Moreau4272e632012-08-13 09:58:41 -06001083 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001084
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001085 /* Views that extend past the bottom of the output are still
1086 * visible after the workspace animation ends but before its layer
1087 * is hidden. In that case, we need to damage below those views so
1088 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001089 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001090 weston_view_damage_below(view);
1091
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001092 wl_list_remove(&shell->workspaces.animation.link);
1093 workspace_deactivate_transforms(from);
1094 workspace_deactivate_transforms(to);
1095 shell->workspaces.anim_to = NULL;
1096
Quentin Glidic82681572016-12-17 13:40:51 +01001097 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098}
1099
1100static void
1101animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001102 struct weston_output *output,
1103 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104{
1105 struct desktop_shell *shell =
1106 container_of(animation, struct desktop_shell,
1107 workspaces.animation);
1108 struct workspace *from = shell->workspaces.anim_from;
1109 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001110 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111 double x, y;
1112
1113 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1114 finish_workspace_change_animation(shell, from, to);
1115 return;
1116 }
1117
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001118 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001120 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001122 timespec_add_msec(&shell->workspaces.anim_timestamp,
1123 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001125 -(asin(1.0 - shell->workspaces.anim_current) *
1126 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1127 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 }
1129
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001130 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131
1132 /*
1133 * x = [0, π/2]
1134 * y(x) = sin(x)
1135 */
1136 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1137 y = sin(x);
1138
1139 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001140 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141
1142 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1143 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1144 shell->workspaces.anim_current = y;
1145
Scott Moreau4272e632012-08-13 09:58:41 -06001146 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001148 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150}
1151
1152static void
1153animate_workspace_change(struct desktop_shell *shell,
1154 unsigned int index,
1155 struct workspace *from,
1156 struct workspace *to)
1157{
1158 struct weston_output *output;
1159
1160 int dir;
1161
1162 if (index > shell->workspaces.current)
1163 dir = -1;
1164 else
1165 dir = 1;
1166
1167 shell->workspaces.current = index;
1168
1169 shell->workspaces.anim_dir = dir;
1170 shell->workspaces.anim_from = from;
1171 shell->workspaces.anim_to = to;
1172 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001173 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174
1175 output = container_of(shell->compositor->output_list.next,
1176 struct weston_output, link);
1177 wl_list_insert(&output->animation_list,
1178 &shell->workspaces.animation.link);
1179
Quentin Glidic82681572016-12-17 13:40:51 +01001180 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1181 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182
1183 workspace_translate_in(to, 0);
1184
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001185 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001186
Scott Moreau4272e632012-08-13 09:58:41 -06001187 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188}
1189
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001190static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191update_workspace(struct desktop_shell *shell, unsigned int index,
1192 struct workspace *from, struct workspace *to)
1193{
1194 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001195 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1196 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197}
1198
1199static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001200change_workspace(struct desktop_shell *shell, unsigned int index)
1201{
1202 struct workspace *from;
1203 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001204 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001205
1206 if (index == shell->workspaces.current)
1207 return;
1208
1209 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001210 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001211 return;
1212
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001213 from = get_current_workspace(shell);
1214 to = get_workspace(shell, index);
1215
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001216 if (shell->workspaces.anim_from == to &&
1217 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219 reverse_workspace_change_animation(shell, index, from, to);
1220 return;
1221 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001222
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001223 if (shell->workspaces.anim_to != NULL)
1224 finish_workspace_change_animation(shell,
1225 shell->workspaces.anim_from,
1226 shell->workspaces.anim_to);
1227
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001229
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001230 if (shell->focus_animation_type != ANIMATION_NONE) {
1231 wl_list_for_each(state, &from->focus_list, link)
1232 if (state->keyboard_focus)
1233 animate_focus_change(shell, from,
1234 get_default_view(state->keyboard_focus), NULL);
1235
1236 wl_list_for_each(state, &to->focus_list, link)
1237 if (state->keyboard_focus)
1238 animate_focus_change(shell, to,
1239 NULL, get_default_view(state->keyboard_focus));
1240 }
1241
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242 if (workspace_is_empty(to) && workspace_is_empty(from))
1243 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001244 else
1245 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001246}
1247
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001248static bool
1249workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1250{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001251 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001252 struct wl_list *e;
1253
1254 if (wl_list_empty(list))
1255 return false;
1256
1257 e = list->next;
1258
1259 if (e->next != list)
1260 return false;
1261
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001262 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263}
1264
1265static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001266surface_keyboard_focus_lost(struct weston_surface *surface)
1267{
1268 struct weston_compositor *compositor = surface->compositor;
1269 struct weston_seat *seat;
1270 struct weston_surface *focus;
1271
1272 wl_list_for_each(seat, &compositor->seat_list, link) {
1273 struct weston_keyboard *keyboard =
1274 weston_seat_get_keyboard(seat);
1275
1276 if (!keyboard)
1277 continue;
1278
1279 focus = weston_surface_get_main_surface(keyboard->focus);
1280 if (focus == surface)
1281 weston_keyboard_set_focus(keyboard, NULL);
1282 }
1283}
1284
1285static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001287 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288 unsigned int index)
1289{
Derek Foreman1281a362015-07-31 16:55:32 -05001290 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001291 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293 struct shell_surface *shsurf;
1294 struct workspace *from;
1295 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001296 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297
Derek Foreman1281a362015-07-31 16:55:32 -05001298 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 view = get_default_view(surface);
1300 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001301 index == shell->workspaces.current ||
1302 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 return;
1304
1305 from = get_current_workspace(shell);
1306 to = get_workspace(shell, index);
1307
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001308 weston_layer_entry_remove(&view->layer_link);
1309 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001310
Philip Withnall659163d2013-11-25 18:01:36 +00001311 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001312 if (shsurf != NULL)
1313 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001314
Jonas Ådahle9d22502012-08-29 22:13:01 +02001315 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001316 drop_focus_state(shell, from, surface);
1317
1318 if (shell->workspaces.anim_from == to &&
1319 shell->workspaces.anim_to == from) {
1320 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001322 return;
1323 }
1324
1325 if (shell->workspaces.anim_to != NULL)
1326 finish_workspace_change_animation(shell,
1327 shell->workspaces.anim_from,
1328 shell->workspaces.anim_to);
1329
1330 if (workspace_is_empty(from) &&
1331 workspace_has_only(to, surface))
1332 update_workspace(shell, index, from, to);
1333 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001334 if (shsurf != NULL &&
1335 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001336 wl_list_insert(&shell->workspaces.anim_sticky_list,
1337 &shsurf->workspace_transform.link);
1338
1339 animate_workspace_change(shell, index, from, to);
1340 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341
Jonas Ådahl8538b222012-08-29 22:13:03 +02001342 state = ensure_focus_state(shell, seat);
1343 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001344 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001345}
1346
1347static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001348touch_move_grab_down(struct weston_touch_grab *grab,
1349 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001350 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001351{
1352}
1353
1354static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001355touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1356 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001357{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001358 struct weston_touch_move_grab *move =
1359 (struct weston_touch_move_grab *) container_of(
1360 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001361
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001362 if (touch_id == 0)
1363 move->active = 0;
1364
Jonas Ådahl9484b692013-12-02 22:05:03 +01001365 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001366 shell_touch_grab_end(&move->base);
1367 free(move);
1368 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001369}
1370
1371static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001372touch_move_grab_motion(struct weston_touch_grab *grab,
1373 const struct timespec *time, int touch_id,
1374 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001375{
1376 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1377 struct shell_surface *shsurf = move->base.shsurf;
1378 struct weston_surface *es;
1379 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1380 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1381
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001382 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001383 return;
1384
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001385 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001386
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001387 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001388
1389 weston_compositor_schedule_repaint(es->compositor);
1390}
1391
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001392static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001393touch_move_grab_frame(struct weston_touch_grab *grab)
1394{
1395}
1396
1397static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001398touch_move_grab_cancel(struct weston_touch_grab *grab)
1399{
1400 struct weston_touch_move_grab *move =
1401 (struct weston_touch_move_grab *) container_of(
1402 grab, struct shell_touch_grab, grab);
1403
1404 shell_touch_grab_end(&move->base);
1405 free(move);
1406}
1407
Rusty Lynch1084da52013-08-15 09:10:08 -07001408static const struct weston_touch_grab_interface touch_move_grab_interface = {
1409 touch_move_grab_down,
1410 touch_move_grab_up,
1411 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001412 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001413 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001414};
1415
1416static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001417surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001418{
1419 struct weston_touch_move_grab *move;
1420
1421 if (!shsurf)
1422 return -1;
1423
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001424 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1425 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001426 return 0;
1427
1428 move = malloc(sizeof *move);
1429 if (!move)
1430 return -1;
1431
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001432 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001434 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001436 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001437
1438 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001439 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001440
1441 return 0;
1442}
1443
1444static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001445noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001446{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001447}
1448
1449static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001450noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001451 const struct timespec *time,
1452 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001453{
1454}
1455
1456static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001457noop_grab_axis_source(struct weston_pointer_grab *grab,
1458 uint32_t source)
1459{
1460}
1461
1462static void
1463noop_grab_frame(struct weston_pointer_grab *grab)
1464{
1465}
1466
1467static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001468constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1469{
1470 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001471 struct weston_surface *surface =
1472 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001473 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001474 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001476 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001477 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001478
1479 x = wl_fixed_to_int(pointer->x + move->dx);
1480 y = wl_fixed_to_int(pointer->y + move->dy);
1481
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001482 if (shsurf->shell->panel_position ==
1483 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 get_output_work_area(shsurf->shell, surface->output, &area);
1485 geometry =
1486 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001487
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001488 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001489 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 y = area.y + safety - geometry.height
1491 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001492
1493 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001494 y + geometry.y < area.y)
1495 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001496 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001497
1498 *cx = x;
1499 *cy = y;
1500}
1501
1502static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001503move_grab_motion(struct weston_pointer_grab *grab,
1504 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001505 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001508 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001509 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001510 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001511 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001512
Jonas Ådahld2510102014-10-05 21:39:14 +02001513 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001514 if (!shsurf)
1515 return;
1516
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001517 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1518
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001519 constrain_position(move, &cx, &cy);
1520
1521 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001523 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001524}
1525
1526static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001528 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001530 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1531 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001532 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001533 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534
Daniel Stone4dbadb12012-05-30 16:31:51 +01001535 if (pointer->button_count == 0 &&
1536 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001537 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001538 free(grab);
1539 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001540}
1541
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001542static void
1543move_grab_cancel(struct weston_pointer_grab *grab)
1544{
1545 struct shell_grab *shell_grab =
1546 container_of(grab, struct shell_grab, grab);
1547
1548 shell_grab_end(shell_grab);
1549 free(grab);
1550}
1551
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001552static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001553 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001554 move_grab_motion,
1555 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001556 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001557 noop_grab_axis_source,
1558 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001559 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001560};
1561
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001563surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001564 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565{
1566 struct weston_move_grab *move;
1567
1568 if (!shsurf)
1569 return -1;
1570
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001571 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001572 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1573 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574 return 0;
1575
1576 move = malloc(sizeof *move);
1577 if (!move)
1578 return -1;
1579
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001581 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001583 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001584 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585
1586 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001587 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588
1589 return 0;
1590}
1591
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592struct weston_resize_grab {
1593 struct shell_grab base;
1594 uint32_t edges;
1595 int32_t width, height;
1596};
1597
1598static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001599resize_grab_motion(struct weston_pointer_grab *grab,
1600 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001601 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001602{
1603 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001604 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605 struct shell_surface *shsurf = resize->base.shsurf;
1606 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001607 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 wl_fixed_t from_x, from_y;
1609 wl_fixed_t to_x, to_y;
1610
Jonas Ådahld2510102014-10-05 21:39:14 +02001611 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001612
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001613 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001614 return;
1615
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616 weston_view_from_global_fixed(shsurf->view,
1617 pointer->grab_x, pointer->grab_y,
1618 &from_x, &from_y);
1619 weston_view_from_global_fixed(shsurf->view,
1620 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621
1622 width = resize->width;
1623 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1624 width += wl_fixed_to_int(from_x - to_x);
1625 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1626 width += wl_fixed_to_int(to_x - from_x);
1627 }
1628
1629 height = resize->height;
1630 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1631 height += wl_fixed_to_int(from_y - to_y);
1632 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1633 height += wl_fixed_to_int(to_y - from_y);
1634 }
1635
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001636 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1637 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1638
1639 min_size.width = MAX(1, min_size.width);
1640 min_size.height = MAX(1, min_size.height);
1641
1642 if (width < min_size.width)
1643 width = min_size.width;
1644 else if (max_size.width > 0 && width > max_size.width)
1645 width = max_size.width;
1646 if (height < min_size.height)
1647 height = min_size.height;
1648 else if (max_size.width > 0 && width > max_size.width)
1649 width = max_size.width;
1650 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651}
1652
1653static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001655 const struct timespec *time,
1656 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660 enum wl_pointer_button_state state = state_w;
1661
1662 if (pointer->button_count == 0 &&
1663 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001664 if (resize->base.shsurf != NULL) {
1665 struct weston_desktop_surface *desktop_surface =
1666 resize->base.shsurf->desktop_surface;
1667 weston_desktop_surface_set_resizing(desktop_surface,
1668 false);
1669 }
1670
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 shell_grab_end(&resize->base);
1672 free(grab);
1673 }
1674}
1675
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001676static void
1677resize_grab_cancel(struct weston_pointer_grab *grab)
1678{
1679 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1680
Greg Vec3f7792018-11-08 00:24:56 +03001681 if (resize->base.shsurf != NULL) {
1682 struct weston_desktop_surface *desktop_surface =
1683 resize->base.shsurf->desktop_surface;
1684 weston_desktop_surface_set_resizing(desktop_surface, false);
1685 }
1686
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001687 shell_grab_end(&resize->base);
1688 free(grab);
1689}
1690
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001691static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692 noop_grab_focus,
1693 resize_grab_motion,
1694 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001695 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001696 noop_grab_axis_source,
1697 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001698 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699};
1700
Giulio Camuffob8366642013-04-25 13:57:46 +03001701/*
1702 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001703 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001704static void
1705surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1706 int32_t *y, int32_t *w, int32_t *h) {
1707 pixman_region32_t region;
1708 pixman_box32_t *box;
1709 struct weston_subsurface *subsurface;
1710
1711 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 surface->width,
1713 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001714
1715 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1716 pixman_region32_union_rect(&region, &region,
1717 subsurface->position.x,
1718 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 subsurface->surface->width,
1720 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001721 }
1722
1723 box = pixman_region32_extents(&region);
1724 if (x)
1725 *x = box->x1;
1726 if (y)
1727 *y = box->y1;
1728 if (w)
1729 *w = box->x2 - box->x1;
1730 if (h)
1731 *h = box->y2 - box->y1;
1732
1733 pixman_region32_fini(&region);
1734}
1735
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736static int
1737surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001738 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739{
1740 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001741 const unsigned resize_topbottom =
1742 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1743 const unsigned resize_leftright =
1744 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1745 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001746 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001748 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001749 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1750 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751 return 0;
1752
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001753 /* Check for invalid edge combinations. */
1754 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1755 (edges & resize_topbottom) == resize_topbottom ||
1756 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757 return 0;
1758
1759 resize = malloc(sizeof *resize);
1760 if (!resize)
1761 return -1;
1762
1763 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001765 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1766 resize->width = geometry.width;
1767 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001768
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001769 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001770 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001772 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773
1774 return 0;
1775}
1776
1777static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001778busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001781 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001782 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001784 wl_fixed_t sx, sy;
1785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 view = weston_compositor_pick_view(pointer->seat->compositor,
1787 pointer->x, pointer->y,
1788 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001789 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001791 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001792 shell_grab_end(grab);
1793 free(grab);
1794 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001795}
1796
1797static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001798busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1799 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001800 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001801{
Jonas Ådahld2510102014-10-05 21:39:14 +02001802 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001804
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001806busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001807 const struct timespec *time,
1808 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001810 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001811 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001812 struct weston_pointer *pointer = grab->grab.pointer;
1813 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001814
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001815 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001816 activate(shsurf->shell, shsurf->view, seat,
1817 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001818 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001819 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001820 activate(shsurf->shell, shsurf->view, seat,
1821 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001822 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001823 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001824}
1825
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001826static void
1827busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1828{
1829 struct shell_grab *grab = (struct shell_grab *) base;
1830
1831 shell_grab_end(grab);
1832 free(grab);
1833}
1834
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001835static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 busy_cursor_grab_focus,
1837 busy_cursor_grab_motion,
1838 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001839 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001840 noop_grab_axis_source,
1841 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001842 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001843};
1844
1845static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846handle_pointer_focus(struct wl_listener *listener, void *data)
1847{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001848 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001850 struct shell_surface *shsurf;
1851 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001852
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001854 return;
1855
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001856 shsurf = get_shell_surface(view->surface);
1857 if (!shsurf)
1858 return;
1859
1860 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1861
1862 if (shsurf->unresponsive)
1863 set_busy_cursor(shsurf, pointer);
1864 else
1865 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001866}
1867
1868static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001869shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1870{
1871 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001872 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001873}
1874
1875static void
1876shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1877{
1878 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001879 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001880}
1881
1882static void
1883handle_keyboard_focus(struct wl_listener *listener, void *data)
1884{
1885 struct weston_keyboard *keyboard = data;
1886 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1887
1888 if (seat->focused_surface) {
1889 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1890 if (shsurf)
1891 shell_surface_lose_keyboard_focus(shsurf);
1892 }
1893
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001894 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001895
1896 if (seat->focused_surface) {
1897 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1898 if (shsurf)
1899 shell_surface_gain_keyboard_focus(shsurf);
1900 }
1901}
1902
Philip Withnall07926d92013-11-25 18:01:40 +00001903/* The surface will be inserted into the list immediately after the link
1904 * returned by this function (i.e. will be stacked immediately above the
1905 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001906static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001907shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1908{
1909 struct workspace *ws;
1910
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001911 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1912 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001913 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001914 }
1915
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001916 /* Move the surface to a normal workspace layer so that surfaces
1917 * which were previously fullscreen or transient are no longer
1918 * rendered on top. */
1919 ws = get_current_workspace(shsurf->shell);
1920 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001921}
1922
Philip Withnall648a4dd2013-11-25 18:01:44 +00001923static void
1924shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1925{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001926 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001927}
1928
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001929/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001930 * geometry to ensure the changes are redrawn.
1931 *
1932 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1933 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001934static void
1935shell_surface_update_layer(struct shell_surface *shsurf)
1936{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001937 struct weston_surface *surface =
1938 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001939 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001940
1941 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1942
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001943 if (new_layer_link == NULL)
1944 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001945 if (new_layer_link == &shsurf->view->layer_link)
1946 return;
1947
1948 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001949 weston_layer_entry_remove(&shsurf->view->layer_link);
1950 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001951 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001952 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001953
1954 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001955}
1956
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001957static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001958notify_output_destroy(struct wl_listener *listener, void *data)
1959{
1960 struct shell_surface *shsurf =
1961 container_of(listener,
1962 struct shell_surface, output_destroy_listener);
1963
1964 shsurf->output = NULL;
1965 shsurf->output_destroy_listener.notify = NULL;
1966}
1967
1968static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001969shell_surface_set_output(struct shell_surface *shsurf,
1970 struct weston_output *output)
1971{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001972 struct weston_surface *es =
1973 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001974
1975 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001976 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001977 if (output)
1978 shsurf->output = output;
1979 else if (es->output)
1980 shsurf->output = es->output;
1981 else
1982 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001983
1984 if (shsurf->output_destroy_listener.notify) {
1985 wl_list_remove(&shsurf->output_destroy_listener.link);
1986 shsurf->output_destroy_listener.notify = NULL;
1987 }
1988
1989 if (!shsurf->output)
1990 return;
1991
1992 shsurf->output_destroy_listener.notify = notify_output_destroy;
1993 wl_signal_add(&shsurf->output->destroy_signal,
1994 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001995}
1996
1997static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001998weston_view_set_initial_position(struct weston_view *view,
1999 struct desktop_shell *shell);
2000
2001static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002002unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002003{
Philip Withnallf85fe842013-11-25 18:01:37 +00002004 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08002005 wl_list_remove(&shsurf->fullscreen.transform.link);
2006 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002007
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 if (shsurf->fullscreen.black_view)
2009 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2010 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002011
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002012 if (shsurf->saved_position_valid)
2013 weston_view_set_position(shsurf->view,
2014 shsurf->saved_x, shsurf->saved_y);
2015 else
2016 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002017 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002018
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002019 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002021 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002022 shsurf->saved_rotation_valid = false;
2023 }
Alex Wu4539b082012-03-01 12:57:46 +08002024}
2025
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002026static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002027unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002028{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002029 struct weston_surface *surface =
2030 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2031
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002032 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002033 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002034
2035 if (shsurf->saved_position_valid)
2036 weston_view_set_position(shsurf->view,
2037 shsurf->saved_x, shsurf->saved_y);
2038 else
2039 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002040 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002041
2042 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043 wl_list_insert(&shsurf->view->geometry.transformation_list,
2044 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002045 shsurf->saved_rotation_valid = false;
2046 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002047}
2048
Philip Withnallbecb77e2013-11-25 18:01:30 +00002049static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002050set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002051{
2052 struct shell_surface *shsurf;
2053 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002054 struct weston_view *view;
2055
2056 view = get_default_view(surface);
2057 if (!view)
2058 return;
2059
2060 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2061
2062 shsurf = get_shell_surface(surface);
2063 current_ws = get_current_workspace(shsurf->shell);
2064
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002065 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002066 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002067
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002068 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002069 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002070
2071 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002072 weston_view_damage_below(view);
2073}
2074
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002075
Tiago Vignattibe143262012-04-16 17:31:41 +03002076static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002077shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002078{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002079 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002080}
2081
Pekka Paalanen8274d902014-08-06 19:36:51 +03002082static int
2083black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2084{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002085 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002086 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002087 int n;
2088 int rem;
2089 int ret;
2090
2091 n = snprintf(buf, len, "black background surface for ");
2092 if (n < 0)
2093 return n;
2094
2095 rem = (int)len - n;
2096 if (rem < 0)
2097 rem = 0;
2098
2099 if (fs_surface->get_label)
2100 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2101 else
2102 ret = snprintf(buf + n, rem, "<unknown>");
2103
2104 if (ret < 0)
2105 return n;
2106
2107 return n + ret;
2108}
2109
Alex Wu21858432012-04-01 20:13:08 +08002110static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002111black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002112
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002114create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002115 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002116 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002117{
2118 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002120
2121 surface = weston_surface_create(ec);
2122 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002123 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002124 return NULL;
2125 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 view = weston_view_create(surface);
2127 if (surface == NULL) {
2128 weston_log("no memory\n");
2129 weston_surface_destroy(surface);
2130 return NULL;
2131 }
Alex Wu4539b082012-03-01 12:57:46 +08002132
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002133 surface->committed = black_surface_committed;
2134 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002135 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002136 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002137 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002138 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002139 pixman_region32_fini(&surface->input);
2140 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002141
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002142 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002143 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144
2145 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002146}
2147
Philip Withnalled8f1a92013-11-25 18:01:43 +00002148static void
2149shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2150{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002151 struct weston_surface *surface =
2152 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002153 struct weston_output *output = shsurf->fullscreen_output;
2154
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002155 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002156
2157 if (!shsurf->fullscreen.black_view)
2158 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002159 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002160 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002161 output->x, output->y,
2162 output->width,
2163 output->height);
2164
2165 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002166 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2167 weston_layer_entry_insert(&shsurf->view->layer_link,
2168 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002169 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002170 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002171
Armin Krezović4663aca2016-06-30 06:04:29 +02002172 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002173 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002174}
2175
Alex Wu4539b082012-03-01 12:57:46 +08002176/* Create black surface and append it to the associated fullscreen surface.
2177 * Handle size dismatch and positioning according to the method. */
2178static void
2179shell_configure_fullscreen(struct shell_surface *shsurf)
2180{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002181 struct weston_surface *surface =
2182 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002183 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002184
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002185 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002186 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002187 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2188 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002189
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002190 if (!shsurf->fullscreen_output) {
2191 /* If there is no output, there's not much we can do.
2192 * Position the window somewhere, whatever. */
2193 weston_view_set_position(shsurf->view, 0, 0);
2194 return;
2195 }
2196
Philip Withnalled8f1a92013-11-25 18:01:43 +00002197 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002198
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002199 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002200 &surf_width, &surf_height);
2201
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002202 if (surface->buffer_ref.buffer)
2203 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002204}
2205
Alex Wu4539b082012-03-01 12:57:46 +08002206static void
2207shell_map_fullscreen(struct shell_surface *shsurf)
2208{
Alex Wubd3354b2012-04-17 17:20:49 +08002209 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002210}
2211
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002212static struct weston_output *
2213get_focused_output(struct weston_compositor *compositor)
2214{
2215 struct weston_seat *seat;
2216 struct weston_output *output = NULL;
2217
2218 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002219 struct weston_touch *touch = weston_seat_get_touch(seat);
2220 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2221 struct weston_keyboard *keyboard =
2222 weston_seat_get_keyboard(seat);
2223
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002224 /* Priority has touch focus, then pointer and
2225 * then keyboard focus. We should probably have
2226 * three for loops and check frist for touch,
2227 * then for pointer, etc. but unless somebody has some
2228 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002229 if (touch && touch->focus)
2230 output = touch->focus->output;
2231 else if (pointer && pointer->focus)
2232 output = pointer->focus->output;
2233 else if (keyboard && keyboard->focus)
2234 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002235
2236 if (output)
2237 break;
2238 }
2239
2240 return output;
2241}
2242
2243static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002244destroy_shell_seat(struct wl_listener *listener, void *data)
2245{
2246 struct shell_seat *shseat =
2247 container_of(listener,
2248 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002249
2250 wl_list_remove(&shseat->seat_destroy_listener.link);
2251 free(shseat);
2252}
2253
Jason Ekstrand024177c2014-04-21 19:42:58 -05002254static void
2255shell_seat_caps_changed(struct wl_listener *listener, void *data)
2256{
Derek Foreman1281a362015-07-31 16:55:32 -05002257 struct weston_keyboard *keyboard;
2258 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002259 struct shell_seat *seat;
2260
2261 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002262 keyboard = weston_seat_get_keyboard(seat->seat);
2263 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002264
Derek Foreman1281a362015-07-31 16:55:32 -05002265 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002266 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002267 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002268 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002269 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002270 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002271 wl_list_init(&seat->keyboard_focus_listener.link);
2272 }
2273
Derek Foreman1281a362015-07-31 16:55:32 -05002274 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002275 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002276 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002277 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002278 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002279 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002280 wl_list_init(&seat->pointer_focus_listener.link);
2281 }
2282}
2283
Giulio Camuffo5085a752013-03-25 21:42:45 +01002284static struct shell_seat *
2285create_shell_seat(struct weston_seat *seat)
2286{
2287 struct shell_seat *shseat;
2288
2289 shseat = calloc(1, sizeof *shseat);
2290 if (!shseat) {
2291 weston_log("no memory to allocate shell seat\n");
2292 return NULL;
2293 }
2294
2295 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002296
2297 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2298 wl_signal_add(&seat->destroy_signal,
2299 &shseat->seat_destroy_listener);
2300
Jason Ekstrand024177c2014-04-21 19:42:58 -05002301 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2302 wl_list_init(&shseat->keyboard_focus_listener.link);
2303
2304 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2305 wl_list_init(&shseat->pointer_focus_listener.link);
2306
2307 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2308 wl_signal_add(&seat->updated_caps_signal,
2309 &shseat->caps_changed_listener);
2310 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2311
Giulio Camuffo5085a752013-03-25 21:42:45 +01002312 return shseat;
2313}
2314
2315static struct shell_seat *
2316get_shell_seat(struct weston_seat *seat)
2317{
2318 struct wl_listener *listener;
2319
2320 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002321 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002322
2323 return container_of(listener,
2324 struct shell_seat, seat_destroy_listener);
2325}
2326
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002327static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002328fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002329{
2330 struct shell_surface *shsurf = data;
2331
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002332 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002333
2334 if (shsurf->output_destroy_listener.notify) {
2335 wl_list_remove(&shsurf->output_destroy_listener.link);
2336 shsurf->output_destroy_listener.notify = NULL;
2337 }
2338
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002339 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002340}
2341
2342static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002343fade_out_done(struct weston_view_animation *animation, void *data)
2344{
2345 struct shell_surface *shsurf = data;
2346 struct wl_event_loop *loop;
2347
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002348 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002349
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002350 if (weston_view_is_mapped(shsurf->view)) {
2351 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002352 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002353 }
2354}
2355
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002356struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002357get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002358{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002359 if (weston_surface_is_desktop_surface(surface)) {
2360 struct weston_desktop_surface *desktop_surface =
2361 weston_surface_get_desktop_surface(surface);
2362 return weston_desktop_surface_get_user_data(desktop_surface);
2363 }
2364 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002365}
2366
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367/*
2368 * libweston-desktop
2369 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002370
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002371static void
2372desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2373 void *shell)
2374{
2375 struct weston_desktop_client *client =
2376 weston_desktop_surface_get_client(desktop_surface);
2377 struct wl_client *wl_client =
2378 weston_desktop_client_get_client(client);
2379 struct weston_view *view;
2380 struct shell_surface *shsurf;
2381 struct weston_surface *surface =
2382 weston_desktop_surface_get_surface(desktop_surface);
2383
2384 view = weston_desktop_surface_create_view(desktop_surface);
2385 if (!view)
2386 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002387
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002388 shsurf = calloc(1, sizeof *shsurf);
2389 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002390 if (wl_client)
2391 wl_client_post_no_memory(wl_client);
2392 else
2393 weston_log("no memory to allocate shell surface\n");
2394 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002395 }
2396
Pekka Paalanen8274d902014-08-06 19:36:51 +03002397 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002398
Tiago Vignattibc052c92012-04-19 16:18:18 +03002399 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002400 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002401 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002402 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002403 shsurf->desktop_surface = desktop_surface;
2404 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002406 wl_list_init(&shsurf->fullscreen.transform.link);
2407
Semi Malinen99f8c082018-05-02 11:10:32 +02002408 shell_surface_set_output(
2409 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002410
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002411 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002412
Pekka Paalanen460099f2012-01-20 16:48:25 +02002413 /* empty when not in use */
2414 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002415 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002416
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002417 wl_list_init(&shsurf->workspace_transform.link);
2418
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2420 weston_desktop_surface_set_activated(desktop_surface,
2421 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002422}
2423
Tiago Vignattibc052c92012-04-19 16:18:18 +03002424static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002425desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2426 void *shell)
2427{
2428 struct shell_surface *shsurf =
2429 weston_desktop_surface_get_user_data(desktop_surface);
2430 struct weston_surface *surface =
2431 weston_desktop_surface_get_surface(desktop_surface);
2432
2433 if (!shsurf)
2434 return;
2435
2436 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2437
2438 if (shsurf->fullscreen.black_view)
2439 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2440
2441 weston_surface_set_label_func(surface, NULL);
2442 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2443 shsurf->desktop_surface = NULL;
2444
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002445 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002446 if (weston_surface_is_mapped(surface) &&
2447 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2448 pixman_region32_fini(&surface->pending.input);
2449 pixman_region32_init(&surface->pending.input);
2450 pixman_region32_fini(&surface->input);
2451 pixman_region32_init(&surface->input);
2452 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2453 fade_out_done, shsurf);
2454 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002456
2457 if (shsurf->output_destroy_listener.notify) {
2458 wl_list_remove(&shsurf->output_destroy_listener.link);
2459 shsurf->output_destroy_listener.notify = NULL;
2460 }
2461
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002462 free(shsurf);
2463 }
2464}
2465
2466static void
2467set_maximized_position(struct desktop_shell *shell,
2468 struct shell_surface *shsurf)
2469{
2470 pixman_rectangle32_t area;
2471 struct weston_geometry geometry;
2472
2473 get_output_work_area(shell, shsurf->output, &area);
2474 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2475
2476 weston_view_set_position(shsurf->view,
2477 area.x - geometry.x,
2478 area.y - geometry.y);
2479}
2480
2481static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002482set_position_from_xwayland(struct shell_surface *shsurf)
2483{
2484 struct weston_geometry geometry;
2485 float x;
2486 float y;
2487
2488 assert(shsurf->xwayland.is_set);
2489
2490 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2491 x = shsurf->xwayland.x - geometry.x;
2492 y = shsurf->xwayland.y - geometry.y;
2493
2494 weston_view_set_position(shsurf->view, x, y);
2495
2496#ifdef WM_DEBUG
2497 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2498 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2499 geometry.x, geometry.y, x, y);
2500#endif
2501}
2502
2503static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002504map(struct desktop_shell *shell, struct shell_surface *shsurf,
2505 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002506{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002507 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002508 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2509 struct weston_compositor *compositor = shell->compositor;
2510 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002511
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002512 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002513 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002514 center_on_output(shsurf->view, shsurf->fullscreen_output);
2515 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002516 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002518 } else if (shsurf->xwayland.is_set) {
2519 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002520 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002521 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002522 }
2523
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002524 /* Surface stacking order, see also activate(). */
2525 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002526
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002527 weston_view_update_transform(shsurf->view);
2528 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002529 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002530 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002531 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002532 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002533
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002534 if (!shell->locked) {
2535 wl_list_for_each(seat, &compositor->seat_list, link)
2536 activate(shell, shsurf->view, seat,
2537 WESTON_ACTIVATE_FLAG_CONFIGURE);
2538 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002539
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002540 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002541 switch (shell->win_animation_type) {
2542 case ANIMATION_FADE:
2543 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2544 break;
2545 case ANIMATION_ZOOM:
2546 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2547 break;
2548 case ANIMATION_NONE:
2549 default:
2550 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002551 }
2552 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002553}
2554
2555static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002556desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2557 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002558{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002559 struct shell_surface *shsurf =
2560 weston_desktop_surface_get_user_data(desktop_surface);
2561 struct weston_surface *surface =
2562 weston_desktop_surface_get_surface(desktop_surface);
2563 struct weston_view *view = shsurf->view;
2564 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002565 bool was_fullscreen;
2566 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002567
2568 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002569 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002570
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002571 was_fullscreen = shsurf->state.fullscreen;
2572 was_maximized = shsurf->state.maximized;
2573
2574 shsurf->state.fullscreen =
2575 weston_desktop_surface_get_fullscreen(desktop_surface);
2576 shsurf->state.maximized =
2577 weston_desktop_surface_get_maximized(desktop_surface);
2578
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002579 if (!weston_surface_is_mapped(surface)) {
2580 map(shell, shsurf, sx, sy);
2581 surface->is_mapped = true;
2582 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2583 ++surface->ref_count;
2584 return;
2585 }
2586
2587 if (sx == 0 && sy == 0 &&
2588 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002589 shsurf->last_height == surface->height &&
2590 was_fullscreen == shsurf->state.fullscreen &&
2591 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002592 return;
2593
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002594 if (was_fullscreen)
2595 unset_fullscreen(shsurf);
2596 if (was_maximized)
2597 unset_maximized(shsurf);
2598
Quentin Glidic920cf042016-08-16 14:26:20 +02002599 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2600 !shsurf->saved_position_valid) {
2601 shsurf->saved_x = shsurf->view->geometry.x;
2602 shsurf->saved_y = shsurf->view->geometry.y;
2603 shsurf->saved_position_valid = true;
2604
2605 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2606 wl_list_remove(&shsurf->rotation.transform.link);
2607 wl_list_init(&shsurf->rotation.transform.link);
2608 weston_view_geometry_dirty(shsurf->view);
2609 shsurf->saved_rotation_valid = true;
2610 }
2611 }
2612
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002613 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002614 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002615 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002616 set_maximized_position(shell, shsurf);
2617 surface->output = shsurf->output;
2618 } else {
2619 float from_x, from_y;
2620 float to_x, to_y;
2621 float x, y;
2622
2623 if (shsurf->resize_edges) {
2624 sx = 0;
2625 sy = 0;
2626 }
2627
2628 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2629 sx = shsurf->last_width - surface->width;
2630 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2631 sy = shsurf->last_height - surface->height;
2632
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002633 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2634 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2635 x = shsurf->view->geometry.x + to_x - from_x;
2636 y = shsurf->view->geometry.y + to_y - from_y;
2637
2638 weston_view_set_position(shsurf->view, x, y);
2639 }
2640
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002641 shsurf->last_width = surface->width;
2642 shsurf->last_height = surface->height;
2643
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002644 /* XXX: would a fullscreen surface need the same handling? */
2645 if (surface->output) {
2646 wl_list_for_each(view, &surface->views, surface_link)
2647 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002648 }
2649}
2650
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002651static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002652get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2653{
2654 struct desktop_shell *shell;
2655 pixman_rectangle32_t area;
2656
2657 shell = shell_surface_get_shell(shsurf);
2658 get_output_work_area(shell, shsurf->output, &area);
2659
2660 *width = area.width;
2661 *height = area.height;
2662}
2663
2664static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002665set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2666 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002667{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002668 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2669 struct weston_surface *surface =
2670 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2671 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002672
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002673 if (fullscreen) {
2674 /* handle clients launching in fullscreen */
2675 if (output == NULL && !weston_surface_is_mapped(surface)) {
2676 /* Set the output to the one that has focus currently. */
2677 output = get_focused_output(surface->compositor);
2678 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002679
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002680 shell_surface_set_output(shsurf, output);
2681 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002682
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002683 width = shsurf->output->width;
2684 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002685 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2686 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002687 }
2688 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2689 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002690}
2691
2692static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002693desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2694 struct weston_seat *seat, uint32_t serial, void *shell)
2695{
2696 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2697 struct weston_touch *touch = weston_seat_get_touch(seat);
2698 struct shell_surface *shsurf =
2699 weston_desktop_surface_get_user_data(desktop_surface);
2700 struct weston_surface *surface =
2701 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2702 struct wl_resource *resource = surface->resource;
2703 struct weston_surface *focus;
2704
2705 if (pointer &&
2706 pointer->focus &&
2707 pointer->button_count > 0 &&
2708 pointer->grab_serial == serial) {
2709 focus = weston_surface_get_main_surface(pointer->focus->surface);
2710 if ((focus == surface) &&
2711 (surface_move(shsurf, pointer, true) < 0))
2712 wl_resource_post_no_memory(resource);
2713 } else if (touch &&
2714 touch->focus &&
2715 touch->grab_serial == serial) {
2716 focus = weston_surface_get_main_surface(touch->focus->surface);
2717 if ((focus == surface) &&
2718 (surface_touch_move(shsurf, touch) < 0))
2719 wl_resource_post_no_memory(resource);
2720 }
2721}
2722
2723static void
2724desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2725 struct weston_seat *seat, uint32_t serial,
2726 enum weston_desktop_surface_edge edges, void *shell)
2727{
2728 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2729 struct shell_surface *shsurf =
2730 weston_desktop_surface_get_user_data(desktop_surface);
2731 struct weston_surface *surface =
2732 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2733 struct wl_resource *resource = surface->resource;
2734 struct weston_surface *focus;
2735
2736 if (!pointer ||
2737 pointer->button_count == 0 ||
2738 pointer->grab_serial != serial ||
2739 pointer->focus == NULL)
2740 return;
2741
2742 focus = weston_surface_get_main_surface(pointer->focus->surface);
2743 if (focus != surface)
2744 return;
2745
2746 if (surface_resize(shsurf, pointer, edges) < 0)
2747 wl_resource_post_no_memory(resource);
2748}
2749
2750static void
2751desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2752 bool fullscreen,
2753 struct weston_output *output, void *shell)
2754{
2755 struct shell_surface *shsurf =
2756 weston_desktop_surface_get_user_data(desktop_surface);
2757
2758 set_fullscreen(shsurf, fullscreen, output);
2759}
2760
2761static void
2762set_maximized(struct shell_surface *shsurf, bool maximized)
2763{
2764 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2765 struct weston_surface *surface =
2766 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2767 int32_t width = 0, height = 0;
2768
2769 if (maximized) {
2770 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002771
2772 if (!weston_surface_is_mapped(surface))
2773 output = get_focused_output(surface->compositor);
2774 else
2775 output = surface->output;
2776
2777 shell_surface_set_output(shsurf, output);
2778
Derek Foreman927d9e22017-10-06 14:37:44 -05002779 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780 }
2781 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2782 weston_desktop_surface_set_size(desktop_surface, width, height);
2783}
2784
2785static void
2786desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2787 bool maximized, void *shell)
2788{
2789 struct shell_surface *shsurf =
2790 weston_desktop_surface_get_user_data(desktop_surface);
2791
2792 set_maximized(shsurf, maximized);
2793}
2794
2795static void
2796desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2797 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002798{
2799 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002800 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002801
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002802 /* apply compositor's own minimization logic (hide) */
2803 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002804}
2805
Rafael Antognollie2a34552013-12-03 15:35:45 -02002806static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002807set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002808{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002809 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002810
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002811 if (pointer->grab->interface == &busy_cursor_grab_interface)
2812 return;
2813
2814 grab = malloc(sizeof *grab);
2815 if (!grab)
2816 return;
2817
2818 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2819 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2820 /* Mark the shsurf as ungrabbed so that button binding is able
2821 * to move it. */
2822 shsurf->grabbed = 0;
2823}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002824
2825static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002826end_busy_cursor(struct weston_compositor *compositor,
2827 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002828{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002829 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002830 struct shell_grab *grab;
2831 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002832
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002833 wl_list_for_each(seat, &compositor->seat_list, link) {
2834 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2835 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002836
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002837 if (!pointer)
2838 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002839
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002840 if (pointer->grab->interface != &busy_cursor_grab_interface)
2841 continue;
2842
2843 grab = (struct shell_grab *) pointer->grab;
2844 shsurf = grab->shsurf;
2845 if (!shsurf)
2846 continue;
2847
2848 grab_client =
2849 weston_desktop_surface_get_client(shsurf->desktop_surface);
2850 if (grab_client == desktop_client) {
2851 shell_grab_end(grab);
2852 free(grab);
2853 }
2854 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002855}
2856
2857static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002858desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2859 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002860{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002861 struct shell_surface *shsurf =
2862 weston_desktop_surface_get_user_data(desktop_surface);
2863 bool *unresponsive = user_data;
2864
2865 shsurf->unresponsive = *unresponsive;
2866}
2867
2868static void
2869desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2870 void *shell_)
2871{
2872 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002873 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002874 struct weston_seat *seat;
2875 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002876
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877 weston_desktop_client_for_each_surface(desktop_client,
2878 desktop_surface_set_unresponsive,
2879 &unresponsive);
2880
2881
2882 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2883 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2884 struct weston_desktop_client *grab_client;
2885
2886 if (!pointer || !pointer->focus)
2887 continue;
2888
2889 shsurf = get_shell_surface(pointer->focus->surface);
2890 if (!shsurf)
2891 continue;
2892
2893 grab_client =
2894 weston_desktop_surface_get_client(shsurf->desktop_surface);
2895 if (grab_client == desktop_client)
2896 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002897 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002898}
2899
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002900static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002901desktop_surface_pong(struct weston_desktop_client *desktop_client,
2902 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002903{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002904 struct desktop_shell *shell = shell_;
2905 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002906
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002907 weston_desktop_client_for_each_surface(desktop_client,
2908 desktop_surface_set_unresponsive,
2909 &unresponsive);
2910 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002911}
2912
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002913static void
2914desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2915 int32_t x, int32_t y, void *shell_)
2916{
2917 struct shell_surface *shsurf =
2918 weston_desktop_surface_get_user_data(surface);
2919
2920 shsurf->xwayland.x = x;
2921 shsurf->xwayland.y = y;
2922 shsurf->xwayland.is_set = true;
2923}
2924
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002925static const struct weston_desktop_api shell_desktop_api = {
2926 .struct_size = sizeof(struct weston_desktop_api),
2927 .surface_added = desktop_surface_added,
2928 .surface_removed = desktop_surface_removed,
2929 .committed = desktop_surface_committed,
2930 .move = desktop_surface_move,
2931 .resize = desktop_surface_resize,
2932 .fullscreen_requested = desktop_surface_fullscreen_requested,
2933 .maximized_requested = desktop_surface_maximized_requested,
2934 .minimized_requested = desktop_surface_minimized_requested,
2935 .ping_timeout = desktop_surface_ping_timeout,
2936 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002937 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002938};
2939
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002940/* ************************ *
2941 * end of libweston-desktop *
2942 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002943static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002944configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002945{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002947
Semi Malinene7a52fb2018-04-26 11:08:10 +02002948 if (!ev->output)
2949 return;
2950
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002951 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 if (v->output == ev->output && v != ev) {
2953 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002954 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002955 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002956 }
2957 }
2958
Quentin Glidice8bf9592016-06-23 18:55:20 +02002959 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002960 ev->surface->is_mapped = true;
2961 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002962
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002963 if (wl_list_empty(&ev->layer_link.link)) {
2964 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002965 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002966 }
2967}
2968
David Fort50d962f2016-06-09 17:55:52 +02002969
2970static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002971find_shell_output_from_weston_output(struct desktop_shell *shell,
2972 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002973{
2974 struct shell_output *shell_output;
2975
2976 wl_list_for_each(shell_output, &shell->output_list, link) {
2977 if (shell_output->output == output)
2978 return shell_output;
2979 }
2980
2981 return NULL;
2982}
2983
Pekka Paalanen8274d902014-08-06 19:36:51 +03002984static int
2985background_get_label(struct weston_surface *surface, char *buf, size_t len)
2986{
2987 return snprintf(buf, len, "background for output %s",
2988 surface->output->name);
2989}
2990
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002991static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002992background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002993{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002994 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002996
Jason Ekstranda7af7042013-10-12 22:38:11 -05002997 view = container_of(es->views.next, struct weston_view, surface_link);
2998
Quentin Glidice8bf9592016-06-23 18:55:20 +02002999 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003000}
3001
3002static void
David Fort50d962f2016-06-09 17:55:52 +02003003handle_background_surface_destroy(struct wl_listener *listener, void *data)
3004{
3005 struct shell_output *output =
3006 container_of(listener, struct shell_output, background_surface_listener);
3007
3008 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003009 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003010 output->background_surface = NULL;
3011}
3012
3013static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003014desktop_shell_set_background(struct wl_client *client,
3015 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003016 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003017 struct wl_resource *surface_resource)
3018{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003019 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003020 struct weston_surface *surface =
3021 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003022 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003023 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003024
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003025 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003026 wl_resource_post_error(surface_resource,
3027 WL_DISPLAY_ERROR_INVALID_OBJECT,
3028 "surface role already assigned");
3029 return;
3030 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003031
Jason Ekstranda7af7042013-10-12 22:38:11 -05003032 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3033 weston_view_destroy(view);
3034 view = weston_view_create(surface);
3035
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003036 surface->committed = background_committed;
3037 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003038 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003039 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003040 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003041
3042 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003043 if (sh_output->background_surface) {
3044 /* The output already has a background, tell our helper
3045 * there is no need for another one. */
3046 weston_desktop_shell_send_configure(resource, 0,
3047 surface_resource,
3048 0, 0);
3049 } else {
3050 weston_desktop_shell_send_configure(resource, 0,
3051 surface_resource,
3052 surface->output->width,
3053 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003054
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003055 sh_output->background_surface = surface;
3056
3057 sh_output->background_surface_listener.notify =
3058 handle_background_surface_destroy;
3059 wl_signal_add(&surface->destroy_signal,
3060 &sh_output->background_surface_listener);
3061 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003062}
3063
Pekka Paalanen8274d902014-08-06 19:36:51 +03003064static int
3065panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3066{
3067 return snprintf(buf, len, "panel for output %s",
3068 surface->output->name);
3069}
3070
Kristian Høgsberg75840622011-09-06 13:48:16 -04003071static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003072panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003073{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003074 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003076 int width, height;
3077 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003078
Jason Ekstranda7af7042013-10-12 22:38:11 -05003079 view = container_of(es->views.next, struct weston_view, surface_link);
3080
Quentin Glidice8bf9592016-06-23 18:55:20 +02003081 get_panel_size(shell, view, &width, &height);
3082 switch (shell->panel_position) {
3083 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3084 break;
3085 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3086 y = view->output->height - height;
3087 break;
3088 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3089 break;
3090 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3091 x = view->output->width - width;
3092 break;
3093 }
3094
3095 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003096}
3097
3098static void
David Fort50d962f2016-06-09 17:55:52 +02003099handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3100{
3101 struct shell_output *output =
3102 container_of(listener, struct shell_output, panel_surface_listener);
3103
3104 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003105 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003106 output->panel_surface = NULL;
3107}
3108
3109
3110static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003111desktop_shell_set_panel(struct wl_client *client,
3112 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003113 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003114 struct wl_resource *surface_resource)
3115{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003116 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003117 struct weston_surface *surface =
3118 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003119 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003120 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003121
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003122 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003123 wl_resource_post_error(surface_resource,
3124 WL_DISPLAY_ERROR_INVALID_OBJECT,
3125 "surface role already assigned");
3126 return;
3127 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003128
Jason Ekstranda7af7042013-10-12 22:38:11 -05003129 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3130 weston_view_destroy(view);
3131 view = weston_view_create(surface);
3132
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003133 surface->committed = panel_committed;
3134 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003135 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003136 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003137 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003138
3139 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003140 if (sh_output->panel_surface) {
3141 /* The output already has a panel, tell our helper
3142 * there is no need for another one. */
3143 weston_desktop_shell_send_configure(resource, 0,
3144 surface_resource,
3145 0, 0);
3146 } else {
3147 weston_desktop_shell_send_configure(resource, 0,
3148 surface_resource,
3149 surface->output->width,
3150 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003151
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003152 sh_output->panel_surface = surface;
3153
3154 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3155 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3156 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003157}
3158
Pekka Paalanen8274d902014-08-06 19:36:51 +03003159static int
3160lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3161{
3162 return snprintf(buf, len, "lock window");
3163}
3164
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003165static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003166lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003167{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003168 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003169 struct weston_view *view;
3170
3171 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003172
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003173 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003174 return;
3175
Jason Ekstranda7af7042013-10-12 22:38:11 -05003176 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003177
3178 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003179 weston_layer_entry_insert(&shell->lock_layer.view_list,
3180 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003181 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003182 surface->is_mapped = true;
3183 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003184 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003185 }
3186}
3187
3188static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003189handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003190{
Tiago Vignattibe143262012-04-16 17:31:41 +03003191 struct desktop_shell *shell =
3192 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003193
Martin Minarik6d118362012-06-07 18:01:59 +02003194 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003195 shell->lock_surface = NULL;
3196}
3197
3198static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003199desktop_shell_set_lock_surface(struct wl_client *client,
3200 struct wl_resource *resource,
3201 struct wl_resource *surface_resource)
3202{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003203 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003204 struct weston_surface *surface =
3205 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003206
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003207 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003208
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003209 if (!shell->locked)
3210 return;
3211
Pekka Paalanen98262232011-12-01 10:42:22 +02003212 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003213
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003214 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003215 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003216 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003217
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003218 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003219 surface->committed = lock_surface_committed;
3220 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003221 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003222}
3223
3224static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003225resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003226{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003227 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003228
Quentin Glidic82681572016-12-17 13:40:51 +01003229 weston_layer_unset_position(&shell->lock_layer);
3230
3231 if (shell->showing_input_panels)
3232 weston_layer_set_position(&shell->input_panel_layer,
3233 WESTON_LAYER_POSITION_TOP_UI);
3234 weston_layer_set_position(&shell->fullscreen_layer,
3235 WESTON_LAYER_POSITION_FULLSCREEN);
3236 weston_layer_set_position(&shell->panel_layer,
3237 WESTON_LAYER_POSITION_UI);
3238 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003239
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003240 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003241
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003242 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003243 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003244 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003245}
3246
3247static void
3248desktop_shell_unlock(struct wl_client *client,
3249 struct wl_resource *resource)
3250{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003251 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003252
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003253 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003254
3255 if (shell->locked)
3256 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003257}
3258
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003259static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003260desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003261 struct wl_resource *resource,
3262 struct wl_resource *surface_resource)
3263{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003264 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003265
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003266 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003267 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003268}
3269
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003270static void
3271desktop_shell_desktop_ready(struct wl_client *client,
3272 struct wl_resource *resource)
3273{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003274 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003275
3276 shell_fade_startup(shell);
3277}
3278
Jonny Lamb765760d2014-08-20 15:53:19 +02003279static void
3280desktop_shell_set_panel_position(struct wl_client *client,
3281 struct wl_resource *resource,
3282 uint32_t position)
3283{
3284 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3285
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003286 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3287 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3288 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3289 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003290 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003291 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003292 "bad position argument");
3293 return;
3294 }
3295
3296 shell->panel_position = position;
3297}
3298
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003299static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003300 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003301 desktop_shell_set_panel,
3302 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003303 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003304 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003305 desktop_shell_desktop_ready,
3306 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003307};
3308
3309static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003310move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003311 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003312{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003313 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003314 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003315 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003316
Derek Foreman8ae2db52015-07-15 13:00:36 -05003317 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003318 return;
3319
Derek Foreman8ae2db52015-07-15 13:00:36 -05003320 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003321
Pekka Paalanen01388e22013-04-25 13:57:44 +03003322 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003323 if (surface == NULL)
3324 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003325
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003326 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003327 if (shsurf == NULL ||
3328 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3329 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003330 return;
3331
Derek Foreman794fa0e2015-07-15 13:00:38 -05003332 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003333}
3334
3335static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003336maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003337 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003338{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003339 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003340 struct weston_surface *surface;
3341 struct shell_surface *shsurf;
3342
3343 surface = weston_surface_get_main_surface(focus);
3344 if (surface == NULL)
3345 return;
3346
3347 shsurf = get_shell_surface(surface);
3348 if (shsurf == NULL)
3349 return;
3350
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003351 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003352}
3353
3354static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003355fullscreen_binding(struct weston_keyboard *keyboard,
3356 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003357{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003358 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003359 struct weston_surface *surface;
3360 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003361 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003362
3363 surface = weston_surface_get_main_surface(focus);
3364 if (surface == NULL)
3365 return;
3366
3367 shsurf = get_shell_surface(surface);
3368 if (shsurf == NULL)
3369 return;
3370
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003371 fullscreen =
3372 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003373
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003374 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003375}
3376
3377static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003378touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003379{
Derek Foreman362656b2014-09-04 10:23:05 -05003380 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003381 struct weston_surface *surface;
3382 struct shell_surface *shsurf;
3383
Derek Foreman8ae2db52015-07-15 13:00:36 -05003384 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003385 return;
3386
Derek Foreman8ae2db52015-07-15 13:00:36 -05003387 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003388 surface = weston_surface_get_main_surface(focus);
3389 if (surface == NULL)
3390 return;
3391
3392 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003393 if (shsurf == NULL ||
3394 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3395 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003396 return;
3397
Derek Foremanb7674ae2015-07-15 13:00:37 -05003398 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003399}
3400
3401static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003402resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003403 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003404{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003405 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003406 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003407 uint32_t edges = 0;
3408 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003409 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003410
Derek Foreman8ae2db52015-07-15 13:00:36 -05003411 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003412 return;
3413
Derek Foreman8ae2db52015-07-15 13:00:36 -05003414 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003415
Pekka Paalanen01388e22013-04-25 13:57:44 +03003416 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003417 if (surface == NULL)
3418 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003419
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003420 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003421 if (shsurf == NULL ||
3422 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3423 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003424 return;
3425
Jason Ekstranda7af7042013-10-12 22:38:11 -05003426 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003427 wl_fixed_to_int(pointer->grab_x),
3428 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003429 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003430
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003431 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003432 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003433 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003434 edges |= 0;
3435 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003436 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003437
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003438 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003439 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003440 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003441 edges |= 0;
3442 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003443 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003444
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003445 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003446}
3447
3448static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003449surface_opacity_binding(struct weston_pointer *pointer,
3450 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003451 struct weston_pointer_axis_event *event,
3452 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003453{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003454 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003455 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003456 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003457 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003458
Pekka Paalanen01388e22013-04-25 13:57:44 +03003459 /* XXX: broken for windows containing sub-surfaces */
3460 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003461 if (surface == NULL)
3462 return;
3463
3464 shsurf = get_shell_surface(surface);
3465 if (!shsurf)
3466 return;
3467
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003468 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003469
Jason Ekstranda7af7042013-10-12 22:38:11 -05003470 if (shsurf->view->alpha > 1.0)
3471 shsurf->view->alpha = 1.0;
3472 if (shsurf->view->alpha < step)
3473 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003474
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003476 weston_surface_damage(surface);
3477}
3478
3479static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003480do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3481 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003482{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003483 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003484 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003485 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003486 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003487
Derek Foreman1281a362015-07-31 16:55:32 -05003488 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003489 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3490 return;
3491 }
3492
Scott Moreauccbf29d2012-02-22 14:21:41 -07003493 wl_list_for_each(output, &compositor->output_list, link) {
3494 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003495 wl_fixed_to_double(pointer->x),
3496 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003497 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003498 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003499 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003500 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003501 increment = -output->zoom.increment;
3502 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003503 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003504 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003505 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003506 else
3507 increment = 0;
3508
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003509 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003510
Scott Moreaue6603982012-06-11 13:07:51 -06003511 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003512 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003513 else if (output->zoom.level > output->zoom.max_level)
3514 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003515
3516 if (!output->zoom.active) {
3517 if (output->zoom.level <= 0.0)
3518 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003519 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003520 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003521
Scott Moreaue6603982012-06-11 13:07:51 -06003522 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003523
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003525 }
3526 }
3527}
3528
Scott Moreauccbf29d2012-02-22 14:21:41 -07003529static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003530zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003531 struct weston_pointer_axis_event *event,
3532 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003533{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003534 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003535}
3536
3537static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003538zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003539 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003540{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003541 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003542}
3543
3544static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003545terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003546 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003547{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003548 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003549
Pekka Paalanen052032d2019-02-06 10:44:37 +02003550 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003551}
3552
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003553static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003554rotate_grab_motion(struct weston_pointer_grab *grab,
3555 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003556 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003557{
3558 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003559 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003560 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003561 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003562 struct weston_surface *surface =
3563 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003564 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003565
Jonas Ådahld2510102014-10-05 21:39:14 +02003566 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003567
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003568 if (!shsurf)
3569 return;
3570
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003571 cx = 0.5f * surface->width;
3572 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003573
Daniel Stone37816df2012-05-16 18:45:18 +01003574 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3575 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003576 r = sqrtf(dx * dx + dy * dy);
3577
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003578 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580
3581 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003582 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003583 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003584
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003585 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003586 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003587
3588 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003589 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003590 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003591 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003592 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003593
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003594 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003595 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003596 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003597 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003598 wl_list_init(&shsurf->rotation.transform.link);
3599 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003600 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003601 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003602
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003603 /* We need to adjust the position of the surface
3604 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003605 cposx = shsurf->view->geometry.x + cx;
3606 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003607 dposx = rotate->center.x - cposx;
3608 dposy = rotate->center.y - cposy;
3609 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003610 weston_view_set_position(shsurf->view,
3611 shsurf->view->geometry.x + dposx,
3612 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003613 }
3614
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003615 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003616 * lazily applies the damage due to rotation update.
3617 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003618 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003619}
3620
3621static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003622rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003623 const struct timespec *time,
3624 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625{
3626 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003627 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003628 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003629 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003630 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003631
Daniel Stone4dbadb12012-05-30 16:31:51 +01003632 if (pointer->button_count == 0 &&
3633 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003634 if (shsurf)
3635 weston_matrix_multiply(&shsurf->rotation.rotation,
3636 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003637 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003638 free(rotate);
3639 }
3640}
3641
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003642static void
3643rotate_grab_cancel(struct weston_pointer_grab *grab)
3644{
3645 struct rotate_grab *rotate =
3646 container_of(grab, struct rotate_grab, base.grab);
3647
3648 shell_grab_end(&rotate->base);
3649 free(rotate);
3650}
3651
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003652static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653 noop_grab_focus,
3654 rotate_grab_motion,
3655 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003656 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003657 noop_grab_axis_source,
3658 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003659 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660};
3661
3662static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003663surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003664{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003665 struct weston_surface *surface =
3666 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003667 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003668 float dx, dy;
3669 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003670
Pekka Paalanen460099f2012-01-20 16:48:25 +02003671 rotate = malloc(sizeof *rotate);
3672 if (!rotate)
3673 return;
3674
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003675 weston_view_to_global_float(shsurf->view,
3676 surface->width * 0.5f,
3677 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003678 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003679
Derek Foreman74de4692015-07-15 13:00:39 -05003680 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3681 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003682 r = sqrtf(dx * dx + dy * dy);
3683 if (r > 20.0f) {
3684 struct weston_matrix inverse;
3685
3686 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003687 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003688 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003689
3690 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003691 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003692 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003693 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003694 weston_matrix_init(&rotate->rotation);
3695 }
3696
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003697 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003698 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003699}
3700
3701static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003702rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3703 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003704{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003705 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003706 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003707 struct shell_surface *surface;
3708
Derek Foreman8ae2db52015-07-15 13:00:36 -05003709 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003710 return;
3711
Derek Foreman8ae2db52015-07-15 13:00:36 -05003712 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003713
Pekka Paalanen01388e22013-04-25 13:57:44 +03003714 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003715 if (base_surface == NULL)
3716 return;
3717
3718 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003719 if (surface == NULL ||
3720 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3721 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003722 return;
3723
Derek Foreman74de4692015-07-15 13:00:39 -05003724 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003725}
3726
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003727/* Move all fullscreen layers down to the current workspace and hide their
3728 * black views. The surfaces' state is set to both fullscreen and lowered,
3729 * and this is reversed when such a surface is re-configured, see
3730 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3731 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003732 * lowering_output = NULL - Lower on all outputs, else only lower on the
3733 * specified output.
3734 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003735 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003736 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003737void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003738lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003739 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003740{
3741 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003742 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003743
3744 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003745 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003746 &shell->fullscreen_layer.view_list.link,
3747 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003748 struct shell_surface *shsurf = get_shell_surface(view->surface);
3749
3750 if (!shsurf)
3751 continue;
3752
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003753 /* Only lower surfaces which have lowering_output as their fullscreen
3754 * output, unless a NULL output asks for lowering on all outputs.
3755 */
3756 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3757 continue;
3758
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003759 /* We can have a non-fullscreen popup for a fullscreen surface
3760 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003761 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003762 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003763 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3764 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003765 weston_view_damage_below(shsurf->fullscreen.black_view);
3766
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003767 }
3768
3769 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003770 weston_layer_entry_remove(&view->layer_link);
3771 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003772 weston_view_damage_below(view);
3773 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003774
3775 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003776 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003777}
3778
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003779void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003780activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003781 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003783 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003784 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003785 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003786 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003787 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003788 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003789
Pekka Paalanen01388e22013-04-25 13:57:44 +03003790 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003791 shsurf = get_shell_surface(main_surface);
3792 assert(shsurf);
3793
3794 /* Only demote fullscreen surfaces on the output of activated shsurf.
3795 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003796 if (shsurf->output)
3797 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003798
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003799 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003800
Jonas Ådahl8538b222012-08-29 22:13:03 +02003801 state = ensure_focus_state(shell, seat);
3802 if (state == NULL)
3803 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003804
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003805 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003806 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003807
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003808 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3809 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003810 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003811
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003812 /* Update the surface’s layer. This brings it to the top of the stacking
3813 * order as appropriate. */
3814 shell_surface_update_layer(shsurf);
3815
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003816 if (shell->focus_animation_type != ANIMATION_NONE) {
3817 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003818 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003819 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003820}
3821
Alex Wu21858432012-04-01 20:13:08 +08003822/* no-op func for checking black surface */
3823static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003824black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003825{
3826}
3827
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003828static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003829is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003830{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003831 struct weston_surface *surface = view->surface;
3832
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003833 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003834 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003835 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003836 return true;
3837 }
3838 return false;
3839}
3840
Kristian Høgsberg75840622011-09-06 13:48:16 -04003841static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003842activate_binding(struct weston_seat *seat,
3843 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003844 struct weston_view *focus_view,
3845 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003846{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003847 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003848 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003849
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003850 if (!focus_view)
3851 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003852
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003853 if (is_black_surface_view(focus_view, &main_view))
3854 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003855
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003856 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003857 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003858 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003859
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003860 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003861}
3862
3863static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003864click_to_activate_binding(struct weston_pointer *pointer,
3865 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003866 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003867{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003868 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003869 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003870 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003871 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003872
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003873 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003874 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003875 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003876}
3877
3878static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003879touch_to_activate_binding(struct weston_touch *touch,
3880 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003881 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003882{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003883 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003884 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003885 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003886 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003887
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003888 activate_binding(touch->seat, data, touch->focus,
3889 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003890}
3891
3892static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003893unfocus_all_seats(struct desktop_shell *shell)
3894{
3895 struct weston_seat *seat, *next;
3896
3897 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003898 struct weston_keyboard *keyboard =
3899 weston_seat_get_keyboard(seat);
3900
3901 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003902 continue;
3903
Derek Foreman1281a362015-07-31 16:55:32 -05003904 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003905 }
3906}
3907
3908static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003909lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003910{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003911 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003913 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003914 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003915 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003916 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003917
3918 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003919
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003920 /* Hide all surfaces by removing the fullscreen, panel and
3921 * toplevel layers. This way nothing else can show or receive
3922 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003923
Quentin Glidic82681572016-12-17 13:40:51 +01003924 weston_layer_unset_position(&shell->panel_layer);
3925 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003926 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003927 weston_layer_unset_position(&shell->input_panel_layer);
3928 weston_layer_unset_position(&ws->layer);
3929
3930 weston_layer_set_position(&shell->lock_layer,
3931 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003932
Derek Foreman004b4a12015-07-20 16:28:13 -05003933 weston_compositor_sleep(shell->compositor);
3934
Neil Robertsb4a91702014-04-09 16:33:32 +01003935 /* Remove the keyboard focus on all seats. This will be
3936 * restored to the workspace's saved state via
3937 * restore_focus_state when the compositor is unlocked */
3938 unfocus_all_seats(shell);
3939
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003940 /* TODO: disable bindings that should not work while locked. */
3941
3942 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003943}
3944
3945static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003946unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003947{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003948 struct wl_resource *shell_resource;
3949
Pekka Paalanend81c2162011-11-16 13:47:34 +02003950 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003951 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003952 return;
3953 }
3954
3955 /* If desktop-shell client has gone away, unlock immediately. */
3956 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003957 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003958 return;
3959 }
3960
3961 if (shell->prepare_event_sent)
3962 return;
3963
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003964 shell_resource = shell->child.desktop_shell;
3965 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003966 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003967}
3968
3969static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003970shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003972 struct shell_output *shell_output = data;
3973 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003974
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003975 shell_output->fade.animation = NULL;
3976 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003977 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003978 weston_surface_destroy(shell_output->fade.view->surface);
3979 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003980 break;
3981 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982 lock(shell);
3983 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003984 default:
3985 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003986 }
3987}
3988
Jason Ekstranda7af7042013-10-12 22:38:11 -05003989static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003990shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003991{
3992 struct weston_compositor *compositor = shell->compositor;
3993 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003994 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003995
3996 surface = weston_surface_create(compositor);
3997 if (!surface)
3998 return NULL;
3999
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 view = weston_view_create(surface);
4001 if (!view) {
4002 weston_surface_destroy(surface);
4003 return NULL;
4004 }
4005
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004006 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4007 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004008 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004009 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4010 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004011 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004012 surface->is_mapped = true;
4013 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004014
Jason Ekstranda7af7042013-10-12 22:38:11 -05004015 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004016}
4017
4018static void
4019shell_fade(struct desktop_shell *shell, enum fade_type type)
4020{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004021 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004022 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004023
4024 switch (type) {
4025 case FADE_IN:
4026 tint = 0.0;
4027 break;
4028 case FADE_OUT:
4029 tint = 1.0;
4030 break;
4031 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004032 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004033 return;
4034 }
4035
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004036 /* Create a separate fade surface for each output */
4037 wl_list_for_each(shell_output, &shell->output_list, link) {
4038 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004039
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004040 if (shell_output->fade.view == NULL) {
4041 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4042 if (!shell_output->fade.view)
4043 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004044
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004045 shell_output->fade.view->alpha = 1.0 - tint;
4046 weston_view_update_transform(shell_output->fade.view);
4047 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004048
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004049 if (shell_output->fade.view->output == NULL) {
4050 /* If the black view gets a NULL output, we lost the
4051 * last output and we'll just cancel the fade. This
4052 * happens when you close the last window under the
4053 * X11 or Wayland backends. */
4054 shell->locked = false;
4055 weston_surface_destroy(shell_output->fade.view->surface);
4056 shell_output->fade.view = NULL;
4057 } else if (shell_output->fade.animation) {
4058 weston_fade_update(shell_output->fade.animation, tint);
4059 } else {
4060 shell_output->fade.animation =
4061 weston_fade_run(shell_output->fade.view,
4062 1.0 - tint, tint, 300.0,
4063 shell_fade_done_for_output, shell_output);
4064 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004065 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004066}
4067
4068static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004069do_shell_fade_startup(void *data)
4070{
4071 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004072 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004073
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004074 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004075 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004076 } else {
4077 weston_log("desktop shell: "
4078 "unexpected fade-in animation type %d\n",
4079 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004080 wl_list_for_each(shell_output, &shell->output_list, link) {
4081 weston_surface_destroy(shell_output->fade.view->surface);
4082 shell_output->fade.view = NULL;
4083 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004084 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004085}
4086
4087static void
4088shell_fade_startup(struct desktop_shell *shell)
4089{
4090 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004091 struct shell_output *shell_output;
4092 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004093
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004094 wl_list_for_each(shell_output, &shell->output_list, link) {
4095 if (!shell_output->fade.startup_timer)
4096 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004097
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004098 wl_event_source_remove(shell_output->fade.startup_timer);
4099 shell_output->fade.startup_timer = NULL;
4100 has_fade = true;
4101 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004102
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004103 if (has_fade) {
4104 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4105 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4106 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004107}
4108
4109static int
4110fade_startup_timeout(void *data)
4111{
4112 struct desktop_shell *shell = data;
4113
4114 shell_fade_startup(shell);
4115 return 0;
4116}
4117
4118static void
4119shell_fade_init(struct desktop_shell *shell)
4120{
4121 /* Make compositor output all black, and wait for the desktop-shell
4122 * client to signal it is ready, then fade in. The timer triggers a
4123 * fade-in, in case the desktop-shell client takes too long.
4124 */
4125
4126 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004127 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004128
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004129 if (shell->startup_animation_type == ANIMATION_NONE)
4130 return;
4131
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004132 wl_list_for_each(shell_output, &shell->output_list, link) {
4133 if (shell_output->fade.view != NULL) {
4134 weston_log("%s: warning: fade surface already exists\n",
4135 __func__);
4136 continue;
4137 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004138
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004139 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4140 if (!shell_output->fade.view)
4141 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004142
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004143 weston_view_update_transform(shell_output->fade.view);
4144 weston_surface_damage(shell_output->fade.view->surface);
4145
4146 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4147 shell_output->fade.startup_timer =
4148 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4149 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4150 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004151}
4152
4153static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004154idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004155{
4156 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004157 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004158
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004159 struct weston_seat *seat;
4160
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004161 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4162 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004163
4164 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004165 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004166}
4167
4168static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004169wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004170{
4171 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004172 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004173
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004174 unlock(shell);
4175}
4176
4177static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004178transform_handler(struct wl_listener *listener, void *data)
4179{
4180 struct weston_surface *surface = data;
4181 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004182 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004183 int x, y;
4184
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004185 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004186 return;
4187
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004188 api = shsurf->shell->xwayland_surface_api;
4189 if (!api) {
4190 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4191 shsurf->shell->xwayland_surface_api = api;
4192 }
4193
4194 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004195 return;
4196
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004197 if (!weston_view_is_mapped(shsurf->view))
4198 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004199
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004200 x = shsurf->view->geometry.x;
4201 y = shsurf->view->geometry.y;
4202
4203 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004204}
4205
4206static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004208{
Giulio Camuffob8366642013-04-25 13:57:46 +03004209 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004210 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004211
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004212 if (!output) {
4213 weston_view_set_position(view, 0, 0);
4214 return;
4215 }
4216
Jason Ekstranda7af7042013-10-12 22:38:11 -05004217 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004218
4219 x = output->x + (output->width - width) / 2 - surf_x / 2;
4220 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004221
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004222 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004223}
4224
4225static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226weston_view_set_initial_position(struct weston_view *view,
4227 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004228{
4229 struct weston_compositor *compositor = shell->compositor;
4230 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004231 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004232 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004233 struct weston_output *output, *target_output = NULL;
4234 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004235 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004236
4237 /* As a heuristic place the new window on the same output as the
4238 * pointer. Falling back to the output containing 0, 0.
4239 *
4240 * TODO: Do something clever for touch too?
4241 */
4242 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004243 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4244
4245 if (pointer) {
4246 ix = wl_fixed_to_int(pointer->x);
4247 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004248 break;
4249 }
4250 }
4251
4252 wl_list_for_each(output, &compositor->output_list, link) {
4253 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4254 target_output = output;
4255 break;
4256 }
4257 }
4258
4259 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004260 weston_view_set_position(view, 10 + random() % 400,
4261 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004262 return;
4263 }
4264
4265 /* Valid range within output where the surface will still be onscreen.
4266 * If this is negative it means that the surface is bigger than
4267 * output.
4268 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004269 get_output_work_area(shell, target_output, &area);
4270
Derek Foremanf814c5d2015-04-15 12:23:54 -05004271 x = area.x;
4272 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004273 range_x = area.width - view->surface->width;
4274 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004275
Rob Bradford4cb88c72012-08-13 15:18:44 +01004276 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004277 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004278
4279 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004280 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004281
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004283}
4284
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004285static bool
4286check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4287{
4288 struct timespec now;
4289
4290 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4291 return false;
4292
4293 /*
4294 * If the shell helper client dies before the session has been
4295 * up for roughly 30 seconds, better just make Weston shut down,
4296 * because the user likely has no way to interact with the desktop
4297 * anyway.
4298 */
4299 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4300 weston_log("Error: %s apparently cannot run at all.\n",
4301 shell->client);
4302 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004303 weston_compositor_exit_with_code(shell->compositor,
4304 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004305
4306 return true;
4307 }
4308
4309 return false;
4310}
4311
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004312static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004313
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004314static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004315respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004316{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004317 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004318
4319 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004320 weston_compositor_get_time(&time);
4321 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004322 shell->child.deathstamp = time;
4323 shell->child.deathcount = 0;
4324 }
4325
4326 shell->child.deathcount++;
4327 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004328 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004329 return;
4330 }
4331
Pekka Paalanen826dc142014-08-27 12:11:53 +03004332 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004333 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004334}
4335
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004336static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004337desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4338{
4339 struct desktop_shell *shell;
4340
4341 shell = container_of(listener, struct desktop_shell,
4342 child.client_destroy_listener);
4343
Pekka Paalanen826dc142014-08-27 12:11:53 +03004344 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004345 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004346 /*
4347 * unbind_desktop_shell() will reset shell->child.desktop_shell
4348 * before the respawned process has a chance to create a new
4349 * desktop_shell object, because we are being called from the
4350 * wl_client destructor which destroys all wl_resources before
4351 * returning.
4352 */
4353
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004354 if (!check_desktop_shell_crash_too_early(shell))
4355 respawn_desktop_shell_process(shell);
4356
Pekka Paalanen826dc142014-08-27 12:11:53 +03004357 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004358}
4359
4360static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004361launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004362{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004363 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004364
Pekka Paalanen826dc142014-08-27 12:11:53 +03004365 shell->child.client = weston_client_start(shell->compositor,
4366 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004367
Arnaud Vrac42631192014-08-25 20:56:46 +02004368 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004369 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004370 return;
4371 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004372
4373 shell->child.client_destroy_listener.notify =
4374 desktop_shell_client_destroy;
4375 wl_client_add_destroy_listener(shell->child.client,
4376 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004377}
4378
4379static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004380unbind_desktop_shell(struct wl_resource *resource)
4381{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004382 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004383
4384 if (shell->locked)
4385 resume_desktop(shell);
4386
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004387 shell->child.desktop_shell = NULL;
4388 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004389}
4390
4391static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004392bind_desktop_shell(struct wl_client *client,
4393 void *data, uint32_t version, uint32_t id)
4394{
Tiago Vignattibe143262012-04-16 17:31:41 +03004395 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004396 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004397
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004398 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4399 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004400
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004401 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004402 wl_resource_set_implementation(resource,
4403 &desktop_shell_implementation,
4404 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004405 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004406 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004407 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004408
4409 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4410 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004411}
4412
Kristian Høgsberg07045392012-02-19 18:52:44 -05004413struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004414 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004415 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004416 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004417 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004418 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004419};
4420
4421static void
4422switcher_next(struct switcher *switcher)
4423{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004425 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004426 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004427 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004428
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004429 /* temporary re-display minimized surfaces */
4430 struct weston_view *tmp;
4431 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004432 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4433 weston_layer_entry_remove(&view->layer_link);
4434 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004435 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4436 *minimized = view;
4437 }
4438
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004439 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004440 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004441 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004442 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004443 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004444 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004445 next = view;
4446 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 view->alpha = 0.25;
4448 weston_view_geometry_dirty(view);
4449 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004450 }
Alex Wu1659daa2012-04-01 20:13:09 +08004451
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004452 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 view->alpha = 0.25;
4454 weston_view_geometry_dirty(view);
4455 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004456 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004457 }
4458
4459 if (next == NULL)
4460 next = first;
4461
Alex Wu07b26062012-03-12 16:06:01 +08004462 if (next == NULL)
4463 return;
4464
Kristian Høgsberg07045392012-02-19 18:52:44 -05004465 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004466 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004467
4468 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004469 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004471
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004472 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004473 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004475}
4476
4477static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004478switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004479{
4480 struct switcher *switcher =
4481 container_of(listener, struct switcher, listener);
4482
4483 switcher_next(switcher);
4484}
4485
4486static void
Daniel Stone351eb612012-05-31 15:27:47 -04004487switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004489 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004490 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004491 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004492
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004493 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004494 if (is_focus_view(view))
4495 continue;
4496
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 view->alpha = 1.0;
4498 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004499 }
4500
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004501 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004502 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004503 keyboard->seat,
4504 WESTON_ACTIVATE_FLAG_CONFIGURE);
4505 }
4506
Kristian Høgsberg07045392012-02-19 18:52:44 -05004507 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004508 weston_keyboard_end_grab(keyboard);
4509 if (keyboard->input_method_resource)
4510 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004511
4512 /* re-hide surfaces that were temporary shown during the switch */
4513 struct weston_view **minimized;
4514 wl_array_for_each(minimized, &switcher->minimized_array) {
4515 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004516 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004517 weston_layer_entry_remove(&(*minimized)->layer_link);
4518 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004519 weston_view_damage_below(*minimized);
4520 }
4521 }
4522 wl_array_release(&switcher->minimized_array);
4523
Kristian Høgsberg07045392012-02-19 18:52:44 -05004524 free(switcher);
4525}
4526
4527static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004528switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004529 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004530{
4531 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004532 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004533
Daniel Stonec9785ea2012-05-30 16:31:52 +01004534 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004535 switcher_next(switcher);
4536}
4537
4538static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004539switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004540 uint32_t mods_depressed, uint32_t mods_latched,
4541 uint32_t mods_locked, uint32_t group)
4542{
4543 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004544 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545
Daniel Stone351eb612012-05-31 15:27:47 -04004546 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4547 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004548}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004549
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004550static void
4551switcher_cancel(struct weston_keyboard_grab *grab)
4552{
4553 struct switcher *switcher = container_of(grab, struct switcher, grab);
4554
4555 switcher_destroy(switcher);
4556}
4557
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004558static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004559 switcher_key,
4560 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004561 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004562};
4563
4564static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004565switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004566 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004567{
Tiago Vignattibe143262012-04-16 17:31:41 +03004568 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004569 struct switcher *switcher;
4570
4571 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004572 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004573 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004574 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004575 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004576 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004577
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004578 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004579 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004580 weston_keyboard_start_grab(keyboard, &switcher->grab);
4581 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004582 switcher_next(switcher);
4583}
4584
Pekka Paalanen3c647232011-12-22 13:43:43 +02004585static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004586backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004587 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004588{
4589 struct weston_compositor *compositor = data;
4590 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004591 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004592
4593 /* TODO: we're limiting to simple use cases, where we assume just
4594 * control on the primary display. We'd have to extend later if we
4595 * ever get support for setting backlights on random desktop LCD
4596 * panels though */
4597 output = get_default_output(compositor);
4598 if (!output)
4599 return;
4600
4601 if (!output->set_backlight)
4602 return;
4603
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004604 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4605 backlight_new = output->backlight_current - 25;
4606 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4607 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004608
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004609 if (backlight_new < 5)
4610 backlight_new = 5;
4611 if (backlight_new > 255)
4612 backlight_new = 255;
4613
4614 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004615 output->set_backlight(output, output->backlight_current);
4616}
4617
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004618static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004619force_kill_binding(struct weston_keyboard *keyboard,
4620 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004621{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004622 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004623 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004624 struct desktop_shell *shell = data;
4625 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004626 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004627
Derek Foreman8ae2db52015-07-15 13:00:36 -05004628 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004629 if (!focus_surface)
4630 return;
4631
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004632 wl_signal_emit(&compositor->kill_signal, focus_surface);
4633
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004634 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004635 wl_client_get_credentials(client, &pid, NULL, NULL);
4636
4637 /* Skip clients that we launched ourselves (the credentials of
4638 * the socketpair is ours) */
4639 if (pid == getpid())
4640 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004641
Daniel Stone325fc2d2012-05-30 16:31:58 +01004642 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004643}
4644
4645static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004646workspace_up_binding(struct weston_keyboard *keyboard,
4647 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004648{
4649 struct desktop_shell *shell = data;
4650 unsigned int new_index = shell->workspaces.current;
4651
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004652 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004653 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004654 if (new_index != 0)
4655 new_index--;
4656
4657 change_workspace(shell, new_index);
4658}
4659
4660static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004661workspace_down_binding(struct weston_keyboard *keyboard,
4662 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004663{
4664 struct desktop_shell *shell = data;
4665 unsigned int new_index = shell->workspaces.current;
4666
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004667 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004668 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004669 if (new_index < shell->workspaces.num - 1)
4670 new_index++;
4671
4672 change_workspace(shell, new_index);
4673}
4674
4675static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004676workspace_f_binding(struct weston_keyboard *keyboard,
4677 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004678{
4679 struct desktop_shell *shell = data;
4680 unsigned int new_index;
4681
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004682 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004683 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004684 new_index = key - KEY_F1;
4685 if (new_index >= shell->workspaces.num)
4686 new_index = shell->workspaces.num - 1;
4687
4688 change_workspace(shell, new_index);
4689}
4690
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004691static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004692workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004693 const struct timespec *time, uint32_t key,
4694 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004695{
4696 struct desktop_shell *shell = data;
4697 unsigned int new_index = shell->workspaces.current;
4698
4699 if (shell->locked)
4700 return;
4701
4702 if (new_index != 0)
4703 new_index--;
4704
Derek Foreman8ae2db52015-07-15 13:00:36 -05004705 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004706}
4707
4708static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004709workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004710 const struct timespec *time, uint32_t key,
4711 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004712{
4713 struct desktop_shell *shell = data;
4714 unsigned int new_index = shell->workspaces.current;
4715
4716 if (shell->locked)
4717 return;
4718
4719 if (new_index < shell->workspaces.num - 1)
4720 new_index++;
4721
Derek Foreman8ae2db52015-07-15 13:00:36 -05004722 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004723}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004724
4725static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004726shell_reposition_view_on_output_destroy(struct weston_view *view)
4727{
4728 struct weston_output *output, *first_output;
4729 struct weston_compositor *ec = view->surface->compositor;
4730 struct shell_surface *shsurf;
4731 float x, y;
4732 int visible;
4733
4734 x = view->geometry.x;
4735 y = view->geometry.y;
4736
4737 /* At this point the destroyed output is not in the list anymore.
4738 * If the view is still visible somewhere, we leave where it is,
4739 * otherwise, move it to the first output. */
4740 visible = 0;
4741 wl_list_for_each(output, &ec->output_list, link) {
4742 if (pixman_region32_contains_point(&output->region,
4743 x, y, NULL)) {
4744 visible = 1;
4745 break;
4746 }
4747 }
4748
4749 if (!visible) {
4750 first_output = container_of(ec->output_list.next,
4751 struct weston_output, link);
4752
4753 x = first_output->x + first_output->width / 4;
4754 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004755
4756 weston_view_set_position(view, x, y);
4757 } else {
4758 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004759 }
4760
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004761
4762 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004763 if (!shsurf)
4764 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004765
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004766 shsurf->saved_position_valid = false;
4767 set_maximized(shsurf, false);
4768 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004769}
4770
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004771void
4772shell_for_each_layer(struct desktop_shell *shell,
4773 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004774{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004775 struct workspace **ws;
4776
4777 func(shell, &shell->fullscreen_layer, data);
4778 func(shell, &shell->panel_layer, data);
4779 func(shell, &shell->background_layer, data);
4780 func(shell, &shell->lock_layer, data);
4781 func(shell, &shell->input_panel_layer, data);
4782
4783 wl_array_for_each(ws, &shell->workspaces.array)
4784 func(shell, &(*ws)->layer, data);
4785}
4786
4787static void
4788shell_output_destroy_move_layer(struct desktop_shell *shell,
4789 struct weston_layer *layer,
4790 void *data)
4791{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004792 struct weston_view *view;
4793
Marius Vladea40d6d2018-02-08 18:46:42 +02004794 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004795 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004796}
4797
4798static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004799handle_output_destroy(struct wl_listener *listener, void *data)
4800{
4801 struct shell_output *output_listener =
4802 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004803 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004804
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004805 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004806
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004807 if (output_listener->panel_surface)
4808 wl_list_remove(&output_listener->panel_surface_listener.link);
4809 if (output_listener->background_surface)
4810 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004811 wl_list_remove(&output_listener->destroy_listener.link);
4812 wl_list_remove(&output_listener->link);
4813 free(output_listener);
4814}
4815
4816static void
David Fort50d962f2016-06-09 17:55:52 +02004817shell_resize_surface_to_output(struct desktop_shell *shell,
4818 struct weston_surface *surface,
4819 const struct weston_output *output)
4820{
4821 if (!surface)
4822 return;
4823
4824 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4825 surface->resource,
4826 output->width,
4827 output->height);
4828}
4829
4830
4831static void
4832handle_output_resized(struct wl_listener *listener, void *data)
4833{
4834 struct desktop_shell *shell =
4835 container_of(listener, struct desktop_shell, resized_listener);
4836 struct weston_output *output = (struct weston_output *)data;
4837 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4838
4839 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4840 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4841}
4842
4843static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004844create_shell_output(struct desktop_shell *shell,
4845 struct weston_output *output)
4846{
4847 struct shell_output *shell_output;
4848
4849 shell_output = zalloc(sizeof *shell_output);
4850 if (shell_output == NULL)
4851 return;
4852
4853 shell_output->output = output;
4854 shell_output->shell = shell;
4855 shell_output->destroy_listener.notify = handle_output_destroy;
4856 wl_signal_add(&output->destroy_signal,
4857 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004858 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004859}
4860
4861static void
4862handle_output_create(struct wl_listener *listener, void *data)
4863{
4864 struct desktop_shell *shell =
4865 container_of(listener, struct desktop_shell, output_create_listener);
4866 struct weston_output *output = (struct weston_output *)data;
4867
4868 create_shell_output(shell, output);
4869}
4870
4871static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004872handle_output_move_layer(struct desktop_shell *shell,
4873 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004874{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004875 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004876 struct weston_view *view;
4877 float x, y;
4878
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004879 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004880 if (view->output != output)
4881 continue;
4882
4883 x = view->geometry.x + output->move_x;
4884 y = view->geometry.y + output->move_y;
4885 weston_view_set_position(view, x, y);
4886 }
4887}
4888
4889static void
4890handle_output_move(struct wl_listener *listener, void *data)
4891{
4892 struct desktop_shell *shell;
4893
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004894 shell = container_of(listener, struct desktop_shell,
4895 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004896
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004897 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004898}
4899
4900static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004901setup_output_destroy_handler(struct weston_compositor *ec,
4902 struct desktop_shell *shell)
4903{
4904 struct weston_output *output;
4905
4906 wl_list_init(&shell->output_list);
4907 wl_list_for_each(output, &ec->output_list, link)
4908 create_shell_output(shell, output);
4909
4910 shell->output_create_listener.notify = handle_output_create;
4911 wl_signal_add(&ec->output_created_signal,
4912 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004913
4914 shell->output_move_listener.notify = handle_output_move;
4915 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004916}
4917
4918static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004919shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004920{
Tiago Vignattibe143262012-04-16 17:31:41 +03004921 struct desktop_shell *shell =
4922 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004923 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004924 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004925
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004926 /* Force state to unlocked so we don't try to fade */
4927 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004928
4929 if (shell->child.client) {
4930 /* disable respawn */
4931 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004932 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004933 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004934
Harsha M Mb8b2c722018-08-07 19:05:02 +05304935 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004936 wl_list_remove(&shell->idle_listener.link);
4937 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004938 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004939
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004940 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004941 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004942
Xiong Zhang6b481422013-10-23 13:58:32 +08004943 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4944 wl_list_remove(&shell_output->destroy_listener.link);
4945 wl_list_remove(&shell_output->link);
4946 free(shell_output);
4947 }
4948
4949 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004950 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004951 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004952
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004953 wl_array_for_each(ws, &shell->workspaces.array)
4954 workspace_destroy(*ws);
4955 wl_array_release(&shell->workspaces.array);
4956
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004957 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004958 free(shell);
4959}
4960
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004961static void
4962shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4963{
4964 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004965 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004966
Bob Ham744e6532016-01-12 10:21:48 +00004967 if (shell->allow_zap)
4968 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4969 MODIFIER_CTRL | MODIFIER_ALT,
4970 terminate_binding, ec);
4971
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004972 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004973 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4974 click_to_activate_binding,
4975 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004976 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4977 click_to_activate_binding,
4978 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004979 weston_compositor_add_touch_binding(ec, 0,
4980 touch_to_activate_binding,
4981 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004982 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4983 backlight_binding, ec);
4984 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4985 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004986
4987 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004988 if (shell->exposay_modifier)
4989 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4990 exposay_binding, shell);
4991
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004992 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004993 if (!mod)
4994 return;
4995
Ian Ray13404c42017-09-18 15:22:01 +03004996 /* This binding is not configurable, but is only enabled if there is a
4997 * valid binding modifier. */
4998 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4999 MODIFIER_SUPER | MODIFIER_ALT,
5000 surface_opacity_binding, NULL);
5001
Ian Rayab7c0b62017-09-18 15:22:00 +03005002 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5003 mod, zoom_axis_binding,
5004 NULL);
5005
Daniel Stone325fc2d2012-05-30 16:31:58 +01005006 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5007 zoom_key_binding, NULL);
5008 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5009 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005010 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5011 maximize_binding, NULL);
5012 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5013 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005014 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5015 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005016 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005017 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005018 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005019 weston_compositor_add_button_binding(ec, BTN_LEFT,
5020 mod | MODIFIER_SHIFT,
5021 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005022
5023 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005024 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005025 rotate_binding, NULL);
5026
Daniel Stone325fc2d2012-05-30 16:31:58 +01005027 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5028 shell);
5029 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5030 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005031 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5032 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005033 weston_compositor_add_key_binding(ec, KEY_K, mod,
5034 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005035 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5036 workspace_up_binding, shell);
5037 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5038 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005039 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5040 workspace_move_surface_up_binding,
5041 shell);
5042 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5043 workspace_move_surface_down_binding,
5044 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005045
5046 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5047 if (shell->workspaces.num > 1) {
5048 num_workspace_bindings = shell->workspaces.num;
5049 if (num_workspace_bindings > 6)
5050 num_workspace_bindings = 6;
5051 for (i = 0; i < num_workspace_bindings; i++)
5052 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5053 workspace_f_binding,
5054 shell);
5055 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005056
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005057 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005058}
5059
Jason Ekstrand024177c2014-04-21 19:42:58 -05005060static void
5061handle_seat_created(struct wl_listener *listener, void *data)
5062{
5063 struct weston_seat *seat = data;
5064
5065 create_shell_seat(seat);
5066}
5067
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005068WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005069wet_shell_init(struct weston_compositor *ec,
5070 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005071{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005072 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005073 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005074 struct workspace **pws;
5075 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005076 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005077
Peter Huttererf3d62272013-08-08 11:57:05 +10005078 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005079 if (shell == NULL)
5080 return -1;
5081
Kristian Høgsberg75840622011-09-06 13:48:16 -04005082 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005083
5084 shell->destroy_listener.notify = shell_destroy;
5085 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005086 shell->idle_listener.notify = idle_handler;
5087 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5088 shell->wake_listener.notify = wake_handler;
5089 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005090 shell->transform_listener.notify = transform_handler;
5091 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005092
Quentin Glidic82681572016-12-17 13:40:51 +01005093 weston_layer_init(&shell->fullscreen_layer, ec);
5094 weston_layer_init(&shell->panel_layer, ec);
5095 weston_layer_init(&shell->background_layer, ec);
5096 weston_layer_init(&shell->lock_layer, ec);
5097 weston_layer_init(&shell->input_panel_layer, ec);
5098
5099 weston_layer_set_position(&shell->fullscreen_layer,
5100 WESTON_LAYER_POSITION_FULLSCREEN);
5101 weston_layer_set_position(&shell->panel_layer,
5102 WESTON_LAYER_POSITION_UI);
5103 weston_layer_set_position(&shell->background_layer,
5104 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005105
5106 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005107 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005108
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005109 if (input_panel_setup(shell) < 0)
5110 return -1;
5111
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005112 shell->text_backend = text_backend_init(ec);
5113 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005114 return -1;
5115
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005116 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005117
Daniel Stonedf8133b2013-11-19 11:37:14 +01005118 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5119 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5120
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005121 for (i = 0; i < shell->workspaces.num; i++) {
5122 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5123 if (pws == NULL)
5124 return -1;
5125
Quentin Glidic82681572016-12-17 13:40:51 +01005126 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005127 if (*pws == NULL)
5128 return -1;
5129 }
5130 activate_workspace(shell, 0);
5131
Quentin Glidic82681572016-12-17 13:40:51 +01005132 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005133
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005134 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005135 wl_list_init(&shell->workspaces.animation.link);
5136 shell->workspaces.animation.frame = animate_workspace_change_frame;
5137
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005138 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5139 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005140 return -1;
5141
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005142 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005143 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005144 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005145 return -1;
5146
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005147 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005148
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005149 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005150
Xiong Zhang6b481422013-10-23 13:58:32 +08005151 setup_output_destroy_handler(ec, shell);
5152
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005153 loop = wl_display_get_event_loop(ec->wl_display);
5154 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005155
Jason Ekstrand024177c2014-04-21 19:42:58 -05005156 wl_list_for_each(seat, &ec->seat_list, link)
5157 handle_seat_created(NULL, seat);
5158 shell->seat_create_listener.notify = handle_seat_created;
5159 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005160
David Fort50d962f2016-06-09 17:55:52 +02005161 shell->resized_listener.notify = handle_output_resized;
5162 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5163
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005164 screenshooter_create(ec);
5165
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005166 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005167
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005168 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005169
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005170 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5171
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005172 return 0;
5173}