blob: 0e50c97b357639e682cfa39830e8a94a3ed99e1c [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
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010050#ifndef static_assert
51#define static_assert(cond, msg)
52#endif
53
Jonas Ådahl04769742012-06-13 00:01:24 +020054struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020055 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020056 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040057 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020058 struct weston_surface *keyboard_focus;
59 struct wl_list link;
60 struct wl_listener seat_destroy_listener;
61 struct wl_listener surface_destroy_listener;
62};
63
Philip Withnall648a4dd2013-11-25 18:01:44 +000064/*
65 * Surface stacking and ordering.
66 *
67 * This is handled using several linked lists of surfaces, organised into
68 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
69 * above all of the surfaces in the layer below. The set of layers is static and
70 * in the following order (top-most first):
71 * • Lock layer (only ever displayed on its own)
72 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010073 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000074 * • Fullscreen layer
75 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000076 * • Workspace layers
77 * • Background layer
78 *
79 * The list of layers may be manipulated to remove whole layers of surfaces from
80 * display. For example, when locking the screen, all layers except the lock
81 * layer are removed.
82 *
83 * A surface’s layer is modified on configuring the surface, in
84 * set_surface_type() (which is only called when the surface’s type change is
85 * _committed_). If a surface’s type changes (e.g. when making a window
86 * fullscreen) its layer changes too.
87 *
88 * In order to allow popup and transient surfaces to be correctly stacked above
89 * their parent surfaces, each surface tracks both its parent surface, and a
90 * linked list of its children. When a surface’s layer is updated, so are the
91 * layers of its children. Note that child surfaces are *not* the same as
92 * subsurfaces — child/parent surfaces are purely for maintaining stacking
93 * order.
94 *
95 * The children_link list of siblings of a surface (i.e. those surfaces which
96 * have the same parent) only contains weston_surfaces which have a
97 * shell_surface. Stacking is not implemented for non-shell_surface
98 * weston_surfaces. This means that the following implication does *not* hold:
99 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
100 */
101
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200102struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500103 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200104
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200105 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500106 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600107 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700108
Tiago Vignattibe143262012-04-16 17:31:41 +0300109 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200110
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500111 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800112 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800113 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700114 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800115 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800123 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800125 } fullscreen;
126
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200127 struct weston_transform workspace_transform;
128
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500129 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200131 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200132
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700133 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200134 bool fullscreen;
135 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100136 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200137 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700138
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200139 struct {
140 bool is_set;
141 int32_t x;
142 int32_t y;
143 } xwayland;
144
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500145 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500146
147 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200148};
149
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400151 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300152 struct shell_surface *shsurf;
153 struct wl_listener shsurf_destroy_listener;
154};
155
Rusty Lynch1084da52013-08-15 09:10:08 -0700156struct shell_touch_grab {
157 struct weston_touch_grab grab;
158 struct shell_surface *shsurf;
159 struct wl_listener shsurf_destroy_listener;
160 struct weston_touch *touch;
161};
162
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300163struct weston_move_grab {
164 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100165 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500166 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167};
168
Rusty Lynch1084da52013-08-15 09:10:08 -0700169struct weston_touch_move_grab {
170 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800171 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700172 wl_fixed_t dx, dy;
173};
174
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500177 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200178 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200179 float x;
180 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200181 } center;
182};
183
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184struct shell_seat {
185 struct weston_seat *seat;
186 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500187 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100188
Jason Ekstrand024177c2014-04-21 19:42:58 -0500189 struct wl_listener caps_changed_listener;
190 struct wl_listener pointer_focus_listener;
191 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100192};
193
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800194
Alex Wubd3354b2012-04-17 17:20:49 +0800195static struct desktop_shell *
196shell_surface_get_shell(struct shell_surface *shsurf);
197
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500198static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200199set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
200
201static void
Derek Foreman74de4692015-07-15 13:00:39 -0500202surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500203
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300204static void
205shell_fade_startup(struct desktop_shell *shell);
206
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700207static void
208shell_fade(struct desktop_shell *shell, enum fade_type type);
209
Philip Withnallbecb77e2013-11-25 18:01:30 +0000210static struct shell_seat *
211get_shell_seat(struct weston_seat *seat);
212
Jonny Lambd73c6942014-08-20 15:53:20 +0200213static void
214get_output_panel_size(struct desktop_shell *shell,
215 struct weston_output *output,
216 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700217
Philip Withnall648a4dd2013-11-25 18:01:44 +0000218static void
219shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
220
Pekka Paalanen8274d902014-08-06 19:36:51 +0300221static int
222shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
223{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300224 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200225 struct weston_desktop_surface *desktop_surface =
226 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300227
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200228 t = weston_desktop_surface_get_title(desktop_surface);
229 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300230
231 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200232 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300233 t ? " '" : "", t ?: "", t ? "'" : "",
234 c ? " of " : "", c ?: "");
235}
236
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500237static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400238destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300239{
240 struct shell_grab *grab;
241
242 grab = container_of(listener, struct shell_grab,
243 shsurf_destroy_listener);
244
245 grab->shsurf = NULL;
246}
247
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800248struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500249get_default_view(struct weston_surface *surface)
250{
251 struct shell_surface *shsurf;
252 struct weston_view *view;
253
254 if (!surface || wl_list_empty(&surface->views))
255 return NULL;
256
257 shsurf = get_shell_surface(surface);
258 if (shsurf)
259 return shsurf->view;
260
261 wl_list_for_each(view, &surface->views, surface_link)
262 if (weston_view_is_mapped(view))
263 return view;
264
265 return container_of(surface->views.next, struct weston_view, surface_link);
266}
267
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300269shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400270 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300271 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400272 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800273 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300275 struct desktop_shell *shell = shsurf->shell;
276
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200277 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400278
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279 grab->grab.interface = interface;
280 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400281 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500282 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400283 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700285 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400286 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400287 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800288 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400289 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500290 weston_pointer_set_focus(pointer,
291 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400292 wl_fixed_from_int(0),
293 wl_fixed_from_int(0));
294 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300295}
296
Jonny Lambd73c6942014-08-20 15:53:20 +0200297static void
Quentin Glidic581df062016-06-23 18:55:19 +0200298get_panel_size(struct desktop_shell *shell,
299 struct weston_view *view,
300 int *width,
301 int *height)
302{
303 float x1, y1;
304 float x2, y2;
305 weston_view_to_global_float(view, 0, 0, &x1, &y1);
306 weston_view_to_global_float(view,
307 view->surface->width,
308 view->surface->height,
309 &x2, &y2);
310 *width = (int)(x2 - x1);
311 *height = (int)(y2 - y1);
312}
313
314static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200315get_output_panel_size(struct desktop_shell *shell,
316 struct weston_output *output,
317 int *width,
318 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700319{
320 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200321
322 *width = 0;
323 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700324
325 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200326 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700327
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300328 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200329 if (view->surface->output == output) {
330 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200331 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700332 }
333 }
334
Jonny Lambd73c6942014-08-20 15:53:20 +0200335 /* the correct view wasn't found */
336}
337
338static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200339get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200340 struct weston_output *output,
341 pixman_rectangle32_t *area)
342{
343 int32_t panel_width = 0, panel_height = 0;
344
Derek Foremanf814c5d2015-04-15 12:23:54 -0500345 area->x = output->x;
346 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200347
348 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800350 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200351 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500352 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000353 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800354 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200355 area->width = output->width;
356 area->height = output->height - panel_height;
357 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800358 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500359 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000360 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800361 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200362 area->width = output->width - panel_width;
363 area->height = output->height;
364 break;
365 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700366}
367
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400368static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300369shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300370{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400372 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700373 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400374
375 if (grab->shsurf->resize_edges) {
376 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400377 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300379
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700380 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300381}
382
383static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700384shell_touch_grab_start(struct shell_touch_grab *grab,
385 const struct weston_touch_grab_interface *interface,
386 struct shell_surface *shsurf,
387 struct weston_touch *touch)
388{
389 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800390
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200391 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700392
Rusty Lynch1084da52013-08-15 09:10:08 -0700393 grab->grab.interface = interface;
394 grab->shsurf = shsurf;
395 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
396 wl_signal_add(&shsurf->destroy_signal,
397 &grab->shsurf_destroy_listener);
398
399 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700400 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700401
402 weston_touch_start_grab(touch, &grab->grab);
403 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500404 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700406}
407
408static void
409shell_touch_grab_end(struct shell_touch_grab *grab)
410{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700412 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700413 grab->shsurf->grabbed = 0;
414 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700415
416 weston_touch_end_grab(grab->touch);
417}
418
419static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800421 struct weston_output *output);
422
Daniel Stone496ca172012-05-30 16:31:42 +0100423static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300424get_modifier(char *modifier)
425{
426 if (!modifier)
427 return MODIFIER_SUPER;
428
429 if (!strcmp("ctrl", modifier))
430 return MODIFIER_CTRL;
431 else if (!strcmp("alt", modifier))
432 return MODIFIER_ALT;
433 else if (!strcmp("super", modifier))
434 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000435 else if (!strcmp("none", modifier))
436 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300437 else
438 return MODIFIER_SUPER;
439}
440
Juan Zhaoe10d2792012-04-25 19:09:52 +0800441static enum animation_type
442get_animation_type(char *animation)
443{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800444 if (!animation)
445 return ANIMATION_NONE;
446
Juan Zhaoe10d2792012-04-25 19:09:52 +0800447 if (!strcmp("zoom", animation))
448 return ANIMATION_ZOOM;
449 else if (!strcmp("fade", animation))
450 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100451 else if (!strcmp("dim-layer", animation))
452 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800453 else
454 return ANIMATION_NONE;
455}
456
Alex Wu4539b082012-03-01 12:57:46 +0800457static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400458shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200459{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400460 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500461 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300462 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000463 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200464
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300465 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300467 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
468 WESTON_SHELL_CLIENT);
469 if (ret < 0)
470 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400471 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500472 "client", &s, client);
473 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100474 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000475
476 weston_config_section_get_bool(section,
477 "allow-zap", &allow_zap, true);
478 shell->allow_zap = allow_zap;
479
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100480 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400481 "binding-modifier", &s, "super");
482 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200483 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800484
485 weston_config_section_get_string(section,
486 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000487 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800488 free(s);
489
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400490 weston_config_section_get_string(section, "animation", &s, "none");
491 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200492 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200493 weston_config_section_get_string(section, "close-animation", &s, "fade");
494 shell->win_close_animation_type = get_animation_type(s);
495 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700496 weston_config_section_get_string(section,
497 "startup-animation", &s, "fade");
498 shell->startup_animation_type = get_animation_type(s);
499 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700500 if (shell->startup_animation_type == ANIMATION_ZOOM)
501 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100502 weston_config_section_get_string(section, "focus-animation", &s, "none");
503 shell->focus_animation_type = get_animation_type(s);
504 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400505 weston_config_section_get_uint(section, "num-workspaces",
506 &shell->workspaces.num,
507 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200508}
509
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800510struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511get_default_output(struct weston_compositor *compositor)
512{
Armin Krezović10b06182016-06-23 11:59:30 +0200513 if (wl_list_empty(&compositor->output_list))
514 return NULL;
515
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516 return container_of(compositor->output_list.next,
517 struct weston_output, link);
518}
519
Pekka Paalanen8274d902014-08-06 19:36:51 +0300520static int
521focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
522{
523 return snprintf(buf, len, "focus highlight effect for output %s",
524 surface->output->name);
525}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100526
527/* no-op func for checking focus surface */
528static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200529focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530{
531}
532
533static struct focus_surface *
534get_focus_surface(struct weston_surface *surface)
535{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200536 if (surface->committed == focus_surface_committed)
537 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538 else
539 return NULL;
540}
541
542static bool
543is_focus_surface (struct weston_surface *es)
544{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200545 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100546}
547
548static bool
549is_focus_view (struct weston_view *view)
550{
551 return is_focus_surface (view->surface);
552}
553
554static struct focus_surface *
555create_focus_surface(struct weston_compositor *ec,
556 struct weston_output *output)
557{
558 struct focus_surface *fsurf = NULL;
559 struct weston_surface *surface = NULL;
560
561 fsurf = malloc(sizeof *fsurf);
562 if (!fsurf)
563 return NULL;
564
565 fsurf->surface = weston_surface_create(ec);
566 surface = fsurf->surface;
567 if (surface == NULL) {
568 free(fsurf);
569 return NULL;
570 }
571
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200572 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100573 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200574 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200575 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300576 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100577
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800578 fsurf->view = weston_view_create(surface);
579 if (fsurf->view == NULL) {
580 weston_surface_destroy(surface);
581 free(fsurf);
582 return NULL;
583 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200584 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200585 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600587 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600588 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
590 pixman_region32_fini(&surface->opaque);
591 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
592 output->width, output->height);
593 pixman_region32_fini(&surface->input);
594 pixman_region32_init(&surface->input);
595
596 wl_list_init(&fsurf->workspace_transform.link);
597
598 return fsurf;
599}
600
601static void
602focus_surface_destroy(struct focus_surface *fsurf)
603{
604 weston_surface_destroy(fsurf->surface);
605 free(fsurf);
606}
607
608static void
609focus_animation_done(struct weston_view_animation *animation, void *data)
610{
611 struct workspace *ws = data;
612
613 ws->focus_animation = NULL;
614}
615
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200616static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200617focus_state_destroy(struct focus_state *state)
618{
619 wl_list_remove(&state->seat_destroy_listener.link);
620 wl_list_remove(&state->surface_destroy_listener.link);
621 free(state);
622}
623
624static void
625focus_state_seat_destroy(struct wl_listener *listener, void *data)
626{
627 struct focus_state *state = container_of(listener,
628 struct focus_state,
629 seat_destroy_listener);
630
631 wl_list_remove(&state->link);
632 focus_state_destroy(state);
633}
634
635static void
636focus_state_surface_destroy(struct wl_listener *listener, void *data)
637{
638 struct focus_state *state = container_of(listener,
639 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400640 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200641 struct weston_surface *main_surface;
642 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200644
Pekka Paalanen01388e22013-04-25 13:57:44 +0300645 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
646
Kristian Høgsberge3778222012-07-31 17:29:30 -0400647 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300648 wl_list_for_each(view,
649 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500650 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400651 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100652 if (is_focus_view(view))
653 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200654 if (!get_shell_surface(view->surface))
655 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200657 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400658 break;
659 }
660
Pekka Paalanen01388e22013-04-25 13:57:44 +0300661 /* if the focus was a sub-surface, activate its main surface */
662 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200663 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300664
Kristian Høgsberge3778222012-07-31 17:29:30 -0400665 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100666 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200667 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200668 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400669 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200670 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100671 if (state->ws->focus_animation)
672 weston_view_animation_destroy(state->ws->focus_animation);
673
674 state->ws->focus_animation = weston_fade_run(
675 state->ws->fsurf_front->view,
676 state->ws->fsurf_front->view->alpha, 0.0, 300,
677 focus_animation_done, state->ws);
678 }
679
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 wl_list_remove(&state->link);
681 focus_state_destroy(state);
682 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200683}
684
685static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200686focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
687 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200688{
Jonas Ådahl04769742012-06-13 00:01:24 +0200689 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200690
691 state = malloc(sizeof *state);
692 if (state == NULL)
693 return NULL;
694
Quentin Glidicfff39812016-08-15 10:56:52 +0200695 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200700
701 state->seat_destroy_listener.notify = focus_state_seat_destroy;
702 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400703 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200704 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400705 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200706
707 return state;
708}
709
Jonas Ådahl8538b222012-08-29 22:13:03 +0200710static struct focus_state *
711ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
712{
713 struct workspace *ws = get_current_workspace(shell);
714 struct focus_state *state;
715
716 wl_list_for_each(state, &ws->focus_list, link)
717 if (state->seat == seat)
718 break;
719
720 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200721 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200722
723 return state;
724}
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800727focus_state_set_focus(struct focus_state *state,
728 struct weston_surface *surface)
729{
730 if (state->keyboard_focus) {
731 wl_list_remove(&state->surface_destroy_listener.link);
732 wl_list_init(&state->surface_destroy_listener.link);
733 }
734
735 state->keyboard_focus = surface;
736 if (surface)
737 wl_signal_add(&surface->destroy_signal,
738 &state->surface_destroy_listener);
739}
740
741static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400742restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200743{
744 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400745 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100746 struct wl_list pending_seat_list;
747 struct weston_seat *seat, *next_seat;
748
749 /* Temporarily steal the list of seats so that we can keep
750 * track of the seats we've already processed */
751 wl_list_init(&pending_seat_list);
752 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
753 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200754
755 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500756 struct weston_keyboard *keyboard =
757 weston_seat_get_keyboard(state->seat);
758
Neil Roberts4237f502014-04-09 16:33:31 +0100759 wl_list_remove(&state->seat->link);
760 wl_list_insert(&shell->compositor->seat_list,
761 &state->seat->link);
762
Derek Foreman1281a362015-07-31 16:55:32 -0500763 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800764 continue;
765
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200767
Derek Foreman1281a362015-07-31 16:55:32 -0500768 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200769 }
Neil Roberts4237f502014-04-09 16:33:31 +0100770
771 /* For any remaining seats that we don't have a focus state
772 * for we'll reset the keyboard focus to NULL */
773 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500774 struct weston_keyboard *keyboard =
775 weston_seat_get_keyboard(seat);
776
Neil Roberts4237f502014-04-09 16:33:31 +0100777 wl_list_insert(&shell->compositor->seat_list, &seat->link);
778
Derek Foreman1281a362015-07-31 16:55:32 -0500779 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100780 continue;
781
Derek Foreman1281a362015-07-31 16:55:32 -0500782 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100783 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200784}
785
786static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
788 struct weston_seat *seat)
789{
Derek Foreman1281a362015-07-31 16:55:32 -0500790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200792
793 wl_list_for_each(state, &ws->focus_list, link) {
794 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500795 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796 return;
797 }
798 }
799}
800
801static void
802drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
803 struct weston_surface *surface)
804{
805 struct focus_state *state;
806
807 wl_list_for_each(state, &ws->focus_list, link)
808 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800809 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200810}
811
812static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100813animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
814 struct weston_view *from, struct weston_view *to)
815{
816 struct weston_output *output;
817 bool focus_surface_created = false;
818
819 /* FIXME: Only support dim animation using two layers */
820 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
821 return;
822
823 output = get_default_output(shell->compositor);
824 if (ws->fsurf_front == NULL && (from || to)) {
825 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800826 if (ws->fsurf_front == NULL)
827 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800829
830 ws->fsurf_back = create_focus_surface(shell->compositor, output);
831 if (ws->fsurf_back == NULL) {
832 focus_surface_destroy(ws->fsurf_front);
833 return;
834 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800836
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 focus_surface_created = true;
838 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300839 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
840 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 }
842
843 if (ws->focus_animation) {
844 weston_view_animation_destroy(ws->focus_animation);
845 ws->focus_animation = NULL;
846 }
847
848 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300849 weston_layer_entry_insert(&to->layer_link,
850 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300852 weston_layer_entry_insert(&ws->layer.view_list,
853 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854
855 if (focus_surface_created) {
856 ws->focus_animation = weston_fade_run(
857 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200858 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 focus_animation_done, ws);
860 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&from->layer_link,
862 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->focus_animation = weston_stable_fade_run(
864 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200865 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 focus_animation_done, ws);
867 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300868 weston_layer_entry_insert(&ws->layer.view_list,
869 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 ws->focus_animation = weston_stable_fade_run(
871 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200872 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 focus_animation_done, ws);
874 }
875}
876
877static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200878workspace_destroy(struct workspace *ws)
879{
Jonas Ådahl04769742012-06-13 00:01:24 +0200880 struct focus_state *state, *next;
881
882 wl_list_for_each_safe(state, next, &ws->focus_list, link)
883 focus_state_destroy(state);
884
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100885 if (ws->fsurf_front)
886 focus_surface_destroy(ws->fsurf_front);
887 if (ws->fsurf_back)
888 focus_surface_destroy(ws->fsurf_back);
889
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200890 free(ws);
891}
892
Jonas Ådahl04769742012-06-13 00:01:24 +0200893static void
894seat_destroyed(struct wl_listener *listener, void *data)
895{
896 struct weston_seat *seat = data;
897 struct focus_state *state, *next;
898 struct workspace *ws = container_of(listener,
899 struct workspace,
900 seat_destroyed_listener);
901
902 wl_list_for_each_safe(state, next, &ws->focus_list, link)
903 if (state->seat == seat)
904 wl_list_remove(&state->link);
905}
906
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100908workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909{
910 struct workspace *ws = malloc(sizeof *ws);
911 if (ws == NULL)
912 return NULL;
913
Quentin Glidic82681572016-12-17 13:40:51 +0100914 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915
Jonas Ådahl04769742012-06-13 00:01:24 +0200916 wl_list_init(&ws->focus_list);
917 wl_list_init(&ws->seat_destroyed_listener.link);
918 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 ws->fsurf_front = NULL;
920 ws->fsurf_back = NULL;
921 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200922
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200923 return ws;
924}
925
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200926static int
927workspace_is_empty(struct workspace *ws)
928{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300929 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930}
931
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932static struct workspace *
933get_workspace(struct desktop_shell *shell, unsigned int index)
934{
935 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200936 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937 pws += index;
938 return *pws;
939}
940
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800941struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200942get_current_workspace(struct desktop_shell *shell)
943{
944 return get_workspace(shell, shell->workspaces.current);
945}
946
947static void
948activate_workspace(struct desktop_shell *shell, unsigned int index)
949{
950 struct workspace *ws;
951
952 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100953 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954
955 shell->workspaces.current = index;
956}
957
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958static unsigned int
959get_output_height(struct weston_output *output)
960{
961 return abs(output->region.extents.y1 - output->region.extents.y2);
962}
963
964static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100965view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200966{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967 struct weston_transform *transform;
968
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 if (is_focus_view(view)) {
970 struct focus_surface *fsurf = get_focus_surface(view->surface);
971 transform = &fsurf->workspace_transform;
972 } else {
973 struct shell_surface *shsurf = get_shell_surface(view->surface);
974 transform = &shsurf->workspace_transform;
975 }
976
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981 weston_matrix_init(&transform->matrix);
982 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985}
986
987static void
988workspace_translate_out(struct workspace *ws, double fraction)
989{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 unsigned int height;
992 double d;
993
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300994 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 d = height * fraction;
997
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999 }
1000}
1001
1002static void
1003workspace_translate_in(struct workspace *ws, double fraction)
1004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006 unsigned int height;
1007 double d;
1008
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001009 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001011
1012 if (fraction > 0)
1013 d = -(height - height * fraction);
1014 else
1015 d = height + height * fraction;
1016
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001017 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 }
1019}
1020
1021static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022reverse_workspace_change_animation(struct desktop_shell *shell,
1023 unsigned int index,
1024 struct workspace *from,
1025 struct workspace *to)
1026{
1027 shell->workspaces.current = index;
1028
1029 shell->workspaces.anim_to = to;
1030 shell->workspaces.anim_from = from;
1031 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001032 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033
Quentin Glidic82681572016-12-17 13:40:51 +01001034 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1035 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1036
Scott Moreau4272e632012-08-13 09:58:41 -06001037 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038}
1039
1040static void
1041workspace_deactivate_transforms(struct workspace *ws)
1042{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001044 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001045
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001046 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001047 if (is_focus_view(view)) {
1048 struct focus_surface *fsurf = get_focus_surface(view->surface);
1049 transform = &fsurf->workspace_transform;
1050 } else {
1051 struct shell_surface *shsurf = get_shell_surface(view->surface);
1052 transform = &shsurf->workspace_transform;
1053 }
1054
1055 if (!wl_list_empty(&transform->link)) {
1056 wl_list_remove(&transform->link);
1057 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001058 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060 }
1061}
1062
1063static void
1064finish_workspace_change_animation(struct desktop_shell *shell,
1065 struct workspace *from,
1066 struct workspace *to)
1067{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001068 struct weston_view *view;
1069
Scott Moreau4272e632012-08-13 09:58:41 -06001070 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001072 /* Views that extend past the bottom of the output are still
1073 * visible after the workspace animation ends but before its layer
1074 * is hidden. In that case, we need to damage below those views so
1075 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001076 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001077 weston_view_damage_below(view);
1078
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079 wl_list_remove(&shell->workspaces.animation.link);
1080 workspace_deactivate_transforms(from);
1081 workspace_deactivate_transforms(to);
1082 shell->workspaces.anim_to = NULL;
1083
Quentin Glidic82681572016-12-17 13:40:51 +01001084 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001085}
1086
1087static void
1088animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001089 struct weston_output *output,
1090 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091{
1092 struct desktop_shell *shell =
1093 container_of(animation, struct desktop_shell,
1094 workspaces.animation);
1095 struct workspace *from = shell->workspaces.anim_from;
1096 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001097 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098 double x, y;
1099
1100 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1101 finish_workspace_change_animation(shell, from, to);
1102 return;
1103 }
1104
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001105 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001107 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001109 timespec_add_msec(&shell->workspaces.anim_timestamp,
1110 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001112 -(asin(1.0 - shell->workspaces.anim_current) *
1113 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1114 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 }
1116
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001117 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001118
1119 /*
1120 * x = [0, π/2]
1121 * y(x) = sin(x)
1122 */
1123 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1124 y = sin(x);
1125
1126 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001127 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128
1129 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1130 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1131 shell->workspaces.anim_current = y;
1132
Scott Moreau4272e632012-08-13 09:58:41 -06001133 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001135 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137}
1138
1139static void
1140animate_workspace_change(struct desktop_shell *shell,
1141 unsigned int index,
1142 struct workspace *from,
1143 struct workspace *to)
1144{
1145 struct weston_output *output;
1146
1147 int dir;
1148
1149 if (index > shell->workspaces.current)
1150 dir = -1;
1151 else
1152 dir = 1;
1153
1154 shell->workspaces.current = index;
1155
1156 shell->workspaces.anim_dir = dir;
1157 shell->workspaces.anim_from = from;
1158 shell->workspaces.anim_to = to;
1159 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001160 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161
1162 output = container_of(shell->compositor->output_list.next,
1163 struct weston_output, link);
1164 wl_list_insert(&output->animation_list,
1165 &shell->workspaces.animation.link);
1166
Quentin Glidic82681572016-12-17 13:40:51 +01001167 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1168 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001169
1170 workspace_translate_in(to, 0);
1171
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001172 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001173
Scott Moreau4272e632012-08-13 09:58:41 -06001174 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175}
1176
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001177static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001178update_workspace(struct desktop_shell *shell, unsigned int index,
1179 struct workspace *from, struct workspace *to)
1180{
1181 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001182 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1183 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001184}
1185
1186static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001187change_workspace(struct desktop_shell *shell, unsigned int index)
1188{
1189 struct workspace *from;
1190 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001191 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192
1193 if (index == shell->workspaces.current)
1194 return;
1195
1196 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001197 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001198 return;
1199
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001200 from = get_current_workspace(shell);
1201 to = get_workspace(shell, index);
1202
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203 if (shell->workspaces.anim_from == to &&
1204 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001205 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001206 reverse_workspace_change_animation(shell, index, from, to);
1207 return;
1208 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001209
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210 if (shell->workspaces.anim_to != NULL)
1211 finish_workspace_change_animation(shell,
1212 shell->workspaces.anim_from,
1213 shell->workspaces.anim_to);
1214
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001215 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001216
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001217 if (shell->focus_animation_type != ANIMATION_NONE) {
1218 wl_list_for_each(state, &from->focus_list, link)
1219 if (state->keyboard_focus)
1220 animate_focus_change(shell, from,
1221 get_default_view(state->keyboard_focus), NULL);
1222
1223 wl_list_for_each(state, &to->focus_list, link)
1224 if (state->keyboard_focus)
1225 animate_focus_change(shell, to,
1226 NULL, get_default_view(state->keyboard_focus));
1227 }
1228
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229 if (workspace_is_empty(to) && workspace_is_empty(from))
1230 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 else
1232 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001233}
1234
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001235static bool
1236workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1237{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001238 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001239 struct wl_list *e;
1240
1241 if (wl_list_empty(list))
1242 return false;
1243
1244 e = list->next;
1245
1246 if (e->next != list)
1247 return false;
1248
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001249 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250}
1251
1252static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001253surface_keyboard_focus_lost(struct weston_surface *surface)
1254{
1255 struct weston_compositor *compositor = surface->compositor;
1256 struct weston_seat *seat;
1257 struct weston_surface *focus;
1258
1259 wl_list_for_each(seat, &compositor->seat_list, link) {
1260 struct weston_keyboard *keyboard =
1261 weston_seat_get_keyboard(seat);
1262
1263 if (!keyboard)
1264 continue;
1265
1266 focus = weston_surface_get_main_surface(keyboard->focus);
1267 if (focus == surface)
1268 weston_keyboard_set_focus(keyboard, NULL);
1269 }
1270}
1271
1272static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001273take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001274 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001275 unsigned int index)
1276{
Derek Foreman1281a362015-07-31 16:55:32 -05001277 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001278 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 struct shell_surface *shsurf;
1281 struct workspace *from;
1282 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001283 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284
Derek Foreman1281a362015-07-31 16:55:32 -05001285 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view = get_default_view(surface);
1287 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001288 index == shell->workspaces.current ||
1289 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290 return;
1291
1292 from = get_current_workspace(shell);
1293 to = get_workspace(shell, index);
1294
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001295 weston_layer_entry_remove(&view->layer_link);
1296 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297
Philip Withnall659163d2013-11-25 18:01:36 +00001298 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001299 if (shsurf != NULL)
1300 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001301
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 drop_focus_state(shell, from, surface);
1304
1305 if (shell->workspaces.anim_from == to &&
1306 shell->workspaces.anim_to == from) {
1307 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001309 return;
1310 }
1311
1312 if (shell->workspaces.anim_to != NULL)
1313 finish_workspace_change_animation(shell,
1314 shell->workspaces.anim_from,
1315 shell->workspaces.anim_to);
1316
1317 if (workspace_is_empty(from) &&
1318 workspace_has_only(to, surface))
1319 update_workspace(shell, index, from, to);
1320 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001321 if (shsurf != NULL &&
1322 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001323 wl_list_insert(&shell->workspaces.anim_sticky_list,
1324 &shsurf->workspace_transform.link);
1325
1326 animate_workspace_change(shell, index, from, to);
1327 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328
Jonas Ådahl8538b222012-08-29 22:13:03 +02001329 state = ensure_focus_state(shell, seat);
1330 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001331 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332}
1333
1334static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001335touch_move_grab_down(struct weston_touch_grab *grab,
1336 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001337 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001338{
1339}
1340
1341static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001342touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1343 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001344{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001345 struct weston_touch_move_grab *move =
1346 (struct weston_touch_move_grab *) container_of(
1347 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001348
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001349 if (touch_id == 0)
1350 move->active = 0;
1351
Jonas Ådahl9484b692013-12-02 22:05:03 +01001352 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001353 shell_touch_grab_end(&move->base);
1354 free(move);
1355 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001356}
1357
1358static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001359touch_move_grab_motion(struct weston_touch_grab *grab,
1360 const struct timespec *time, int touch_id,
1361 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001362{
1363 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1364 struct shell_surface *shsurf = move->base.shsurf;
1365 struct weston_surface *es;
1366 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1367 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1368
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001369 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001370 return;
1371
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001372 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001373
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001374 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001375
1376 weston_compositor_schedule_repaint(es->compositor);
1377}
1378
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001379static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001380touch_move_grab_frame(struct weston_touch_grab *grab)
1381{
1382}
1383
1384static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001385touch_move_grab_cancel(struct weston_touch_grab *grab)
1386{
1387 struct weston_touch_move_grab *move =
1388 (struct weston_touch_move_grab *) container_of(
1389 grab, struct shell_touch_grab, grab);
1390
1391 shell_touch_grab_end(&move->base);
1392 free(move);
1393}
1394
Rusty Lynch1084da52013-08-15 09:10:08 -07001395static const struct weston_touch_grab_interface touch_move_grab_interface = {
1396 touch_move_grab_down,
1397 touch_move_grab_up,
1398 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001399 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001400 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001401};
1402
1403static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001404surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001405{
1406 struct weston_touch_move_grab *move;
1407
1408 if (!shsurf)
1409 return -1;
1410
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001411 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1412 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001413 return 0;
1414
1415 move = malloc(sizeof *move);
1416 if (!move)
1417 return -1;
1418
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001419 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001421 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001423 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001424
1425 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001426 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001427
1428 return 0;
1429}
1430
1431static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001432noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001433{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001434}
1435
1436static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001437noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001438 const struct timespec *time,
1439 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001440{
1441}
1442
1443static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001444noop_grab_axis_source(struct weston_pointer_grab *grab,
1445 uint32_t source)
1446{
1447}
1448
1449static void
1450noop_grab_frame(struct weston_pointer_grab *grab)
1451{
1452}
1453
1454static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1456{
1457 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001458 struct weston_surface *surface =
1459 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001460 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001461 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001462 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001463 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001464 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
1466 x = wl_fixed_to_int(pointer->x + move->dx);
1467 y = wl_fixed_to_int(pointer->y + move->dy);
1468
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001469 if (shsurf->shell->panel_position ==
1470 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001471 get_output_work_area(shsurf->shell, surface->output, &area);
1472 geometry =
1473 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001474
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001475 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001476 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001477 y = area.y + safety - geometry.height
1478 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001479
1480 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001481 y + geometry.y < area.y)
1482 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001483 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001484
1485 *cx = x;
1486 *cy = y;
1487}
1488
1489static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001490move_grab_motion(struct weston_pointer_grab *grab,
1491 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001492 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001493{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001494 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001495 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001496 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001497 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001498 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001499
Jonas Ådahld2510102014-10-05 21:39:14 +02001500 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001501 if (!shsurf)
1502 return;
1503
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001504 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1505
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001506 constrain_position(move, &cx, &cy);
1507
1508 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001510 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511}
1512
1513static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001514move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001515 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001517 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1518 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001520 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001521
Daniel Stone4dbadb12012-05-30 16:31:51 +01001522 if (pointer->button_count == 0 &&
1523 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001524 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001525 free(grab);
1526 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527}
1528
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001529static void
1530move_grab_cancel(struct weston_pointer_grab *grab)
1531{
1532 struct shell_grab *shell_grab =
1533 container_of(grab, struct shell_grab, grab);
1534
1535 shell_grab_end(shell_grab);
1536 free(grab);
1537}
1538
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001539static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001540 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001541 move_grab_motion,
1542 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001543 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001544 noop_grab_axis_source,
1545 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001546 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001547};
1548
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001550surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001551 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552{
1553 struct weston_move_grab *move;
1554
1555 if (!shsurf)
1556 return -1;
1557
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001558 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001559 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1560 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 return 0;
1562
1563 move = malloc(sizeof *move);
1564 if (!move)
1565 return -1;
1566
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001568 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001570 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001571 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572
1573 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001574 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575
1576 return 0;
1577}
1578
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579struct weston_resize_grab {
1580 struct shell_grab base;
1581 uint32_t edges;
1582 int32_t width, height;
1583};
1584
1585static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001586resize_grab_motion(struct weston_pointer_grab *grab,
1587 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001588 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589{
1590 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001591 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 struct shell_surface *shsurf = resize->base.shsurf;
1593 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001594 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595 wl_fixed_t from_x, from_y;
1596 wl_fixed_t to_x, to_y;
1597
Jonas Ådahld2510102014-10-05 21:39:14 +02001598 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001599
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001600 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 return;
1602
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 weston_view_from_global_fixed(shsurf->view,
1604 pointer->grab_x, pointer->grab_y,
1605 &from_x, &from_y);
1606 weston_view_from_global_fixed(shsurf->view,
1607 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608
1609 width = resize->width;
1610 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1611 width += wl_fixed_to_int(from_x - to_x);
1612 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1613 width += wl_fixed_to_int(to_x - from_x);
1614 }
1615
1616 height = resize->height;
1617 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1618 height += wl_fixed_to_int(from_y - to_y);
1619 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1620 height += wl_fixed_to_int(to_y - from_y);
1621 }
1622
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001623 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1624 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1625
1626 min_size.width = MAX(1, min_size.width);
1627 min_size.height = MAX(1, min_size.height);
1628
1629 if (width < min_size.width)
1630 width = min_size.width;
1631 else if (max_size.width > 0 && width > max_size.width)
1632 width = max_size.width;
1633 if (height < min_size.height)
1634 height = min_size.height;
1635 else if (max_size.width > 0 && width > max_size.width)
1636 width = max_size.width;
1637 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001638}
1639
1640static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001641resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001642 const struct timespec *time,
1643 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644{
1645 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001646 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001648 struct weston_desktop_surface *desktop_surface =
1649 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001650
1651 if (pointer->button_count == 0 &&
1652 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001653 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654 shell_grab_end(&resize->base);
1655 free(grab);
1656 }
1657}
1658
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001659static void
1660resize_grab_cancel(struct weston_pointer_grab *grab)
1661{
1662 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001663 struct weston_desktop_surface *desktop_surface =
1664 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001665
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001666 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001667 shell_grab_end(&resize->base);
1668 free(grab);
1669}
1670
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001671static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001672 noop_grab_focus,
1673 resize_grab_motion,
1674 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001675 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001676 noop_grab_axis_source,
1677 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001678 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001679};
1680
Giulio Camuffob8366642013-04-25 13:57:46 +03001681/*
1682 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001683 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001684static void
1685surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1686 int32_t *y, int32_t *w, int32_t *h) {
1687 pixman_region32_t region;
1688 pixman_box32_t *box;
1689 struct weston_subsurface *subsurface;
1690
1691 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 surface->width,
1693 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001694
1695 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1696 pixman_region32_union_rect(&region, &region,
1697 subsurface->position.x,
1698 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 subsurface->surface->width,
1700 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001701 }
1702
1703 box = pixman_region32_extents(&region);
1704 if (x)
1705 *x = box->x1;
1706 if (y)
1707 *y = box->y1;
1708 if (w)
1709 *w = box->x2 - box->x1;
1710 if (h)
1711 *h = box->y2 - box->y1;
1712
1713 pixman_region32_fini(&region);
1714}
1715
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716static int
1717surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001718 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719{
1720 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001721 const unsigned resize_topbottom =
1722 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1723 const unsigned resize_leftright =
1724 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1725 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001726 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001728 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001729 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1730 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 return 0;
1732
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001733 /* Check for invalid edge combinations. */
1734 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1735 (edges & resize_topbottom) == resize_topbottom ||
1736 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737 return 0;
1738
1739 resize = malloc(sizeof *resize);
1740 if (!resize)
1741 return -1;
1742
1743 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001745 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1746 resize->width = geometry.width;
1747 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001748
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001749 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001750 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001752 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753
1754 return 0;
1755}
1756
1757static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001758busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001760 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001761 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001762 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001764 wl_fixed_t sx, sy;
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 view = weston_compositor_pick_view(pointer->seat->compositor,
1767 pointer->x, pointer->y,
1768 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001769 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001771 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001772 shell_grab_end(grab);
1773 free(grab);
1774 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001775}
1776
1777static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001778busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1779 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001780 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001781{
Jonas Ådahld2510102014-10-05 21:39:14 +02001782 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001786busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001787 const struct timespec *time,
1788 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001789{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001790 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001791 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001792 struct weston_pointer *pointer = grab->grab.pointer;
1793 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001794
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001795 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001796 activate(shsurf->shell, shsurf->view, seat,
1797 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001798 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001799 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001800 activate(shsurf->shell, shsurf->view, seat,
1801 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001802 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001803 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804}
1805
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001806static void
1807busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1808{
1809 struct shell_grab *grab = (struct shell_grab *) base;
1810
1811 shell_grab_end(grab);
1812 free(grab);
1813}
1814
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001815static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816 busy_cursor_grab_focus,
1817 busy_cursor_grab_motion,
1818 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001819 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001820 noop_grab_axis_source,
1821 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001822 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001823};
1824
1825static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826handle_pointer_focus(struct wl_listener *listener, void *data)
1827{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001828 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001830 struct shell_surface *shsurf;
1831 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834 return;
1835
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001836 shsurf = get_shell_surface(view->surface);
1837 if (!shsurf)
1838 return;
1839
1840 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1841
1842 if (shsurf->unresponsive)
1843 set_busy_cursor(shsurf, pointer);
1844 else
1845 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846}
1847
1848static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001849shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1850{
1851 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001852 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001853}
1854
1855static void
1856shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1857{
1858 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001859 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001860}
1861
1862static void
1863handle_keyboard_focus(struct wl_listener *listener, void *data)
1864{
1865 struct weston_keyboard *keyboard = data;
1866 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1867
1868 if (seat->focused_surface) {
1869 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1870 if (shsurf)
1871 shell_surface_lose_keyboard_focus(shsurf);
1872 }
1873
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001874 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001875
1876 if (seat->focused_surface) {
1877 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1878 if (shsurf)
1879 shell_surface_gain_keyboard_focus(shsurf);
1880 }
1881}
1882
Philip Withnall07926d92013-11-25 18:01:40 +00001883/* The surface will be inserted into the list immediately after the link
1884 * returned by this function (i.e. will be stacked immediately above the
1885 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001886static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001887shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1888{
1889 struct workspace *ws;
1890
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001891 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1892 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001893 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001894 }
1895
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001896 /* Move the surface to a normal workspace layer so that surfaces
1897 * which were previously fullscreen or transient are no longer
1898 * rendered on top. */
1899 ws = get_current_workspace(shsurf->shell);
1900 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001901}
1902
Philip Withnall648a4dd2013-11-25 18:01:44 +00001903static void
1904shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1905{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001906 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001907}
1908
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001909/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001910 * geometry to ensure the changes are redrawn.
1911 *
1912 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1913 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001914static void
1915shell_surface_update_layer(struct shell_surface *shsurf)
1916{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001917 struct weston_surface *surface =
1918 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001919 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001920
1921 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1922
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001923 if (new_layer_link == NULL)
1924 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001925 if (new_layer_link == &shsurf->view->layer_link)
1926 return;
1927
1928 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001929 weston_layer_entry_remove(&shsurf->view->layer_link);
1930 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001931 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001932 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001933
1934 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001935}
1936
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001937static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001938notify_output_destroy(struct wl_listener *listener, void *data)
1939{
1940 struct shell_surface *shsurf =
1941 container_of(listener,
1942 struct shell_surface, output_destroy_listener);
1943
1944 shsurf->output = NULL;
1945 shsurf->output_destroy_listener.notify = NULL;
1946}
1947
1948static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001949shell_surface_set_output(struct shell_surface *shsurf,
1950 struct weston_output *output)
1951{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001952 struct weston_surface *es =
1953 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001954
1955 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001956 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001957 if (output)
1958 shsurf->output = output;
1959 else if (es->output)
1960 shsurf->output = es->output;
1961 else
1962 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001963
1964 if (shsurf->output_destroy_listener.notify) {
1965 wl_list_remove(&shsurf->output_destroy_listener.link);
1966 shsurf->output_destroy_listener.notify = NULL;
1967 }
1968
1969 if (!shsurf->output)
1970 return;
1971
1972 shsurf->output_destroy_listener.notify = notify_output_destroy;
1973 wl_signal_add(&shsurf->output->destroy_signal,
1974 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001975}
1976
1977static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001978weston_view_set_initial_position(struct weston_view *view,
1979 struct desktop_shell *shell);
1980
1981static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001982unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001983{
Philip Withnallf85fe842013-11-25 18:01:37 +00001984 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001985 wl_list_remove(&shsurf->fullscreen.transform.link);
1986 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001987
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 if (shsurf->fullscreen.black_view)
1989 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1990 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001991
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001992 if (shsurf->saved_position_valid)
1993 weston_view_set_position(shsurf->view,
1994 shsurf->saved_x, shsurf->saved_y);
1995 else
1996 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001997 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001998
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001999 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002001 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002002 shsurf->saved_rotation_valid = false;
2003 }
Alex Wu4539b082012-03-01 12:57:46 +08002004}
2005
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002006static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002007unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002008{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002009 struct weston_surface *surface =
2010 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2011
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002012 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002013 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002014
2015 if (shsurf->saved_position_valid)
2016 weston_view_set_position(shsurf->view,
2017 shsurf->saved_x, shsurf->saved_y);
2018 else
2019 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002020 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002021
2022 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 wl_list_insert(&shsurf->view->geometry.transformation_list,
2024 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002025 shsurf->saved_rotation_valid = false;
2026 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002027}
2028
Philip Withnallbecb77e2013-11-25 18:01:30 +00002029static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002030set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002031{
2032 struct shell_surface *shsurf;
2033 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002034 struct weston_view *view;
2035
2036 view = get_default_view(surface);
2037 if (!view)
2038 return;
2039
2040 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2041
2042 shsurf = get_shell_surface(surface);
2043 current_ws = get_current_workspace(shsurf->shell);
2044
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002045 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002046 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002047
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002048 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002049 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002050
2051 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002052 weston_view_damage_below(view);
2053}
2054
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002055
Tiago Vignattibe143262012-04-16 17:31:41 +03002056static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002057shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002058{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002059 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002060}
2061
Pekka Paalanen8274d902014-08-06 19:36:51 +03002062static int
2063black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2064{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002065 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002066 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002067 int n;
2068 int rem;
2069 int ret;
2070
2071 n = snprintf(buf, len, "black background surface for ");
2072 if (n < 0)
2073 return n;
2074
2075 rem = (int)len - n;
2076 if (rem < 0)
2077 rem = 0;
2078
2079 if (fs_surface->get_label)
2080 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2081 else
2082 ret = snprintf(buf + n, rem, "<unknown>");
2083
2084 if (ret < 0)
2085 return n;
2086
2087 return n + ret;
2088}
2089
Alex Wu21858432012-04-01 20:13:08 +08002090static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002091black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002094create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002095 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002096 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002097{
2098 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002100
2101 surface = weston_surface_create(ec);
2102 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002103 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002104 return NULL;
2105 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 view = weston_view_create(surface);
2107 if (surface == NULL) {
2108 weston_log("no memory\n");
2109 weston_surface_destroy(surface);
2110 return NULL;
2111 }
Alex Wu4539b082012-03-01 12:57:46 +08002112
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002113 surface->committed = black_surface_committed;
2114 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002115 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002116 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002117 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002118 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002119 pixman_region32_fini(&surface->input);
2120 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002121
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002122 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002123 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124
2125 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002126}
2127
Philip Withnalled8f1a92013-11-25 18:01:43 +00002128static void
2129shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2130{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002131 struct weston_surface *surface =
2132 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002133 struct weston_output *output = shsurf->fullscreen_output;
2134
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002135 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002136
2137 if (!shsurf->fullscreen.black_view)
2138 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002139 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002140 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002141 output->x, output->y,
2142 output->width,
2143 output->height);
2144
2145 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002146 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2147 weston_layer_entry_insert(&shsurf->view->layer_link,
2148 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002149 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002150 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002151
Armin Krezović4663aca2016-06-30 06:04:29 +02002152 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002153 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002154}
2155
Alex Wu4539b082012-03-01 12:57:46 +08002156/* Create black surface and append it to the associated fullscreen surface.
2157 * Handle size dismatch and positioning according to the method. */
2158static void
2159shell_configure_fullscreen(struct shell_surface *shsurf)
2160{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002161 struct weston_surface *surface =
2162 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002163 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002164
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002165 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002166 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002167 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2168 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002169
Philip Withnalled8f1a92013-11-25 18:01:43 +00002170 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002171
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002172 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002173 &surf_width, &surf_height);
2174
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002175 if (surface->buffer_ref.buffer)
2176 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002177}
2178
Alex Wu4539b082012-03-01 12:57:46 +08002179static void
2180shell_map_fullscreen(struct shell_surface *shsurf)
2181{
Alex Wubd3354b2012-04-17 17:20:49 +08002182 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002183}
2184
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002185static struct weston_output *
2186get_focused_output(struct weston_compositor *compositor)
2187{
2188 struct weston_seat *seat;
2189 struct weston_output *output = NULL;
2190
2191 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002192 struct weston_touch *touch = weston_seat_get_touch(seat);
2193 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2194 struct weston_keyboard *keyboard =
2195 weston_seat_get_keyboard(seat);
2196
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002197 /* Priority has touch focus, then pointer and
2198 * then keyboard focus. We should probably have
2199 * three for loops and check frist for touch,
2200 * then for pointer, etc. but unless somebody has some
2201 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002202 if (touch && touch->focus)
2203 output = touch->focus->output;
2204 else if (pointer && pointer->focus)
2205 output = pointer->focus->output;
2206 else if (keyboard && keyboard->focus)
2207 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002208
2209 if (output)
2210 break;
2211 }
2212
2213 return output;
2214}
2215
2216static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002217destroy_shell_seat(struct wl_listener *listener, void *data)
2218{
2219 struct shell_seat *shseat =
2220 container_of(listener,
2221 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002222
2223 wl_list_remove(&shseat->seat_destroy_listener.link);
2224 free(shseat);
2225}
2226
Jason Ekstrand024177c2014-04-21 19:42:58 -05002227static void
2228shell_seat_caps_changed(struct wl_listener *listener, void *data)
2229{
Derek Foreman1281a362015-07-31 16:55:32 -05002230 struct weston_keyboard *keyboard;
2231 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002232 struct shell_seat *seat;
2233
2234 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002235 keyboard = weston_seat_get_keyboard(seat->seat);
2236 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002237
Derek Foreman1281a362015-07-31 16:55:32 -05002238 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002239 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002240 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002241 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002242 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002243 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002244 wl_list_init(&seat->keyboard_focus_listener.link);
2245 }
2246
Derek Foreman1281a362015-07-31 16:55:32 -05002247 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002248 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002249 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002251 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002252 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002253 wl_list_init(&seat->pointer_focus_listener.link);
2254 }
2255}
2256
Giulio Camuffo5085a752013-03-25 21:42:45 +01002257static struct shell_seat *
2258create_shell_seat(struct weston_seat *seat)
2259{
2260 struct shell_seat *shseat;
2261
2262 shseat = calloc(1, sizeof *shseat);
2263 if (!shseat) {
2264 weston_log("no memory to allocate shell seat\n");
2265 return NULL;
2266 }
2267
2268 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002269
2270 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2271 wl_signal_add(&seat->destroy_signal,
2272 &shseat->seat_destroy_listener);
2273
Jason Ekstrand024177c2014-04-21 19:42:58 -05002274 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2275 wl_list_init(&shseat->keyboard_focus_listener.link);
2276
2277 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2278 wl_list_init(&shseat->pointer_focus_listener.link);
2279
2280 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2281 wl_signal_add(&seat->updated_caps_signal,
2282 &shseat->caps_changed_listener);
2283 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2284
Giulio Camuffo5085a752013-03-25 21:42:45 +01002285 return shseat;
2286}
2287
2288static struct shell_seat *
2289get_shell_seat(struct weston_seat *seat)
2290{
2291 struct wl_listener *listener;
2292
2293 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002294 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002295
2296 return container_of(listener,
2297 struct shell_seat, seat_destroy_listener);
2298}
2299
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002300static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002301fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002302{
2303 struct shell_surface *shsurf = data;
2304
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002305 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002306
2307 if (shsurf->output_destroy_listener.notify) {
2308 wl_list_remove(&shsurf->output_destroy_listener.link);
2309 shsurf->output_destroy_listener.notify = NULL;
2310 }
2311
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002312 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002313}
2314
2315static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002316fade_out_done(struct weston_view_animation *animation, void *data)
2317{
2318 struct shell_surface *shsurf = data;
2319 struct wl_event_loop *loop;
2320
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002321 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002322
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002323 if (weston_view_is_mapped(shsurf->view)) {
2324 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002325 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002326 }
2327}
2328
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002329struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002330get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002331{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002332 if (weston_surface_is_desktop_surface(surface)) {
2333 struct weston_desktop_surface *desktop_surface =
2334 weston_surface_get_desktop_surface(surface);
2335 return weston_desktop_surface_get_user_data(desktop_surface);
2336 }
2337 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002338}
2339
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002340/*
2341 * libweston-desktop
2342 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002343
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344static void
2345desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2346 void *shell)
2347{
2348 struct weston_desktop_client *client =
2349 weston_desktop_surface_get_client(desktop_surface);
2350 struct wl_client *wl_client =
2351 weston_desktop_client_get_client(client);
2352 struct weston_view *view;
2353 struct shell_surface *shsurf;
2354 struct weston_surface *surface =
2355 weston_desktop_surface_get_surface(desktop_surface);
2356
2357 view = weston_desktop_surface_create_view(desktop_surface);
2358 if (!view)
2359 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002360
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002361 shsurf = calloc(1, sizeof *shsurf);
2362 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002363 if (wl_client)
2364 wl_client_post_no_memory(wl_client);
2365 else
2366 weston_log("no memory to allocate shell surface\n");
2367 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002368 }
2369
Pekka Paalanen8274d902014-08-06 19:36:51 +03002370 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002371
Tiago Vignattibc052c92012-04-19 16:18:18 +03002372 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002373 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002374 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002375 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002376 shsurf->desktop_surface = desktop_surface;
2377 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002379 wl_list_init(&shsurf->fullscreen.transform.link);
2380
Semi Malinen99f8c082018-05-02 11:10:32 +02002381 shell_surface_set_output(
2382 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002383
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002384 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002385
Pekka Paalanen460099f2012-01-20 16:48:25 +02002386 /* empty when not in use */
2387 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002388 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002389
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002390 wl_list_init(&shsurf->workspace_transform.link);
2391
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002392 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2393 weston_desktop_surface_set_activated(desktop_surface,
2394 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002395}
2396
Tiago Vignattibc052c92012-04-19 16:18:18 +03002397static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002398desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2399 void *shell)
2400{
2401 struct shell_surface *shsurf =
2402 weston_desktop_surface_get_user_data(desktop_surface);
2403 struct weston_surface *surface =
2404 weston_desktop_surface_get_surface(desktop_surface);
2405
2406 if (!shsurf)
2407 return;
2408
2409 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2410
2411 if (shsurf->fullscreen.black_view)
2412 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2413
2414 weston_surface_set_label_func(surface, NULL);
2415 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2416 shsurf->desktop_surface = NULL;
2417
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002418 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419 if (weston_surface_is_mapped(surface) &&
2420 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2421 pixman_region32_fini(&surface->pending.input);
2422 pixman_region32_init(&surface->pending.input);
2423 pixman_region32_fini(&surface->input);
2424 pixman_region32_init(&surface->input);
2425 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2426 fade_out_done, shsurf);
2427 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002428 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002429
2430 if (shsurf->output_destroy_listener.notify) {
2431 wl_list_remove(&shsurf->output_destroy_listener.link);
2432 shsurf->output_destroy_listener.notify = NULL;
2433 }
2434
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 free(shsurf);
2436 }
2437}
2438
2439static void
2440set_maximized_position(struct desktop_shell *shell,
2441 struct shell_surface *shsurf)
2442{
2443 pixman_rectangle32_t area;
2444 struct weston_geometry geometry;
2445
2446 get_output_work_area(shell, shsurf->output, &area);
2447 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2448
2449 weston_view_set_position(shsurf->view,
2450 area.x - geometry.x,
2451 area.y - geometry.y);
2452}
2453
2454static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002455set_position_from_xwayland(struct shell_surface *shsurf)
2456{
2457 struct weston_geometry geometry;
2458 float x;
2459 float y;
2460
2461 assert(shsurf->xwayland.is_set);
2462
2463 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2464 x = shsurf->xwayland.x - geometry.x;
2465 y = shsurf->xwayland.y - geometry.y;
2466
2467 weston_view_set_position(shsurf->view, x, y);
2468
2469#ifdef WM_DEBUG
2470 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2471 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2472 geometry.x, geometry.y, x, y);
2473#endif
2474}
2475
2476static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002477map(struct desktop_shell *shell, struct shell_surface *shsurf,
2478 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002479{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002480 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002481 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2482 struct weston_compositor *compositor = shell->compositor;
2483 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002484
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002485 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002486 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002487 center_on_output(shsurf->view, shsurf->fullscreen_output);
2488 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002489 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002490 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002491 } else if (shsurf->xwayland.is_set) {
2492 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002493 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002494 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002495 }
2496
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002497 /* Surface stacking order, see also activate(). */
2498 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002499
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500 weston_view_update_transform(shsurf->view);
2501 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002502 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002504 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002505 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002506
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002507 if (!shell->locked) {
2508 wl_list_for_each(seat, &compositor->seat_list, link)
2509 activate(shell, shsurf->view, seat,
2510 WESTON_ACTIVATE_FLAG_CONFIGURE);
2511 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002512
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002513 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002514 switch (shell->win_animation_type) {
2515 case ANIMATION_FADE:
2516 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2517 break;
2518 case ANIMATION_ZOOM:
2519 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2520 break;
2521 case ANIMATION_NONE:
2522 default:
2523 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002524 }
2525 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002526}
2527
2528static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2530 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002531{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002532 struct shell_surface *shsurf =
2533 weston_desktop_surface_get_user_data(desktop_surface);
2534 struct weston_surface *surface =
2535 weston_desktop_surface_get_surface(desktop_surface);
2536 struct weston_view *view = shsurf->view;
2537 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002538 bool was_fullscreen;
2539 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002540
2541 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002542 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002543
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002544 was_fullscreen = shsurf->state.fullscreen;
2545 was_maximized = shsurf->state.maximized;
2546
2547 shsurf->state.fullscreen =
2548 weston_desktop_surface_get_fullscreen(desktop_surface);
2549 shsurf->state.maximized =
2550 weston_desktop_surface_get_maximized(desktop_surface);
2551
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 if (!weston_surface_is_mapped(surface)) {
2553 map(shell, shsurf, sx, sy);
2554 surface->is_mapped = true;
2555 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2556 ++surface->ref_count;
2557 return;
2558 }
2559
2560 if (sx == 0 && sy == 0 &&
2561 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002562 shsurf->last_height == surface->height &&
2563 was_fullscreen == shsurf->state.fullscreen &&
2564 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002565 return;
2566
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002567 if (was_fullscreen)
2568 unset_fullscreen(shsurf);
2569 if (was_maximized)
2570 unset_maximized(shsurf);
2571
Quentin Glidic920cf042016-08-16 14:26:20 +02002572 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2573 !shsurf->saved_position_valid) {
2574 shsurf->saved_x = shsurf->view->geometry.x;
2575 shsurf->saved_y = shsurf->view->geometry.y;
2576 shsurf->saved_position_valid = true;
2577
2578 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2579 wl_list_remove(&shsurf->rotation.transform.link);
2580 wl_list_init(&shsurf->rotation.transform.link);
2581 weston_view_geometry_dirty(shsurf->view);
2582 shsurf->saved_rotation_valid = true;
2583 }
2584 }
2585
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002586 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002588 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002589 set_maximized_position(shell, shsurf);
2590 surface->output = shsurf->output;
2591 } else {
2592 float from_x, from_y;
2593 float to_x, to_y;
2594 float x, y;
2595
2596 if (shsurf->resize_edges) {
2597 sx = 0;
2598 sy = 0;
2599 }
2600
2601 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2602 sx = shsurf->last_width - surface->width;
2603 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2604 sy = shsurf->last_height - surface->height;
2605
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002606 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2607 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2608 x = shsurf->view->geometry.x + to_x - from_x;
2609 y = shsurf->view->geometry.y + to_y - from_y;
2610
2611 weston_view_set_position(shsurf->view, x, y);
2612 }
2613
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002614 shsurf->last_width = surface->width;
2615 shsurf->last_height = surface->height;
2616
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002617 /* XXX: would a fullscreen surface need the same handling? */
2618 if (surface->output) {
2619 wl_list_for_each(view, &surface->views, surface_link)
2620 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002621 }
2622}
2623
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002624static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002625get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2626{
2627 struct desktop_shell *shell;
2628 pixman_rectangle32_t area;
2629
2630 shell = shell_surface_get_shell(shsurf);
2631 get_output_work_area(shell, shsurf->output, &area);
2632
2633 *width = area.width;
2634 *height = area.height;
2635}
2636
2637static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002638set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2639 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002640{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002641 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2642 struct weston_surface *surface =
2643 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2644 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002645
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002646 if (fullscreen) {
2647 /* handle clients launching in fullscreen */
2648 if (output == NULL && !weston_surface_is_mapped(surface)) {
2649 /* Set the output to the one that has focus currently. */
2650 output = get_focused_output(surface->compositor);
2651 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002652
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002653 shell_surface_set_output(shsurf, output);
2654 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002655
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002656 width = shsurf->output->width;
2657 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002658 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2659 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002660 }
2661 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2662 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002663}
2664
2665static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002666desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2667 struct weston_seat *seat, uint32_t serial, void *shell)
2668{
2669 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2670 struct weston_touch *touch = weston_seat_get_touch(seat);
2671 struct shell_surface *shsurf =
2672 weston_desktop_surface_get_user_data(desktop_surface);
2673 struct weston_surface *surface =
2674 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2675 struct wl_resource *resource = surface->resource;
2676 struct weston_surface *focus;
2677
2678 if (pointer &&
2679 pointer->focus &&
2680 pointer->button_count > 0 &&
2681 pointer->grab_serial == serial) {
2682 focus = weston_surface_get_main_surface(pointer->focus->surface);
2683 if ((focus == surface) &&
2684 (surface_move(shsurf, pointer, true) < 0))
2685 wl_resource_post_no_memory(resource);
2686 } else if (touch &&
2687 touch->focus &&
2688 touch->grab_serial == serial) {
2689 focus = weston_surface_get_main_surface(touch->focus->surface);
2690 if ((focus == surface) &&
2691 (surface_touch_move(shsurf, touch) < 0))
2692 wl_resource_post_no_memory(resource);
2693 }
2694}
2695
2696static void
2697desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2698 struct weston_seat *seat, uint32_t serial,
2699 enum weston_desktop_surface_edge edges, void *shell)
2700{
2701 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2702 struct shell_surface *shsurf =
2703 weston_desktop_surface_get_user_data(desktop_surface);
2704 struct weston_surface *surface =
2705 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2706 struct wl_resource *resource = surface->resource;
2707 struct weston_surface *focus;
2708
2709 if (!pointer ||
2710 pointer->button_count == 0 ||
2711 pointer->grab_serial != serial ||
2712 pointer->focus == NULL)
2713 return;
2714
2715 focus = weston_surface_get_main_surface(pointer->focus->surface);
2716 if (focus != surface)
2717 return;
2718
2719 if (surface_resize(shsurf, pointer, edges) < 0)
2720 wl_resource_post_no_memory(resource);
2721}
2722
2723static void
2724desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2725 bool fullscreen,
2726 struct weston_output *output, void *shell)
2727{
2728 struct shell_surface *shsurf =
2729 weston_desktop_surface_get_user_data(desktop_surface);
2730
2731 set_fullscreen(shsurf, fullscreen, output);
2732}
2733
2734static void
2735set_maximized(struct shell_surface *shsurf, bool maximized)
2736{
2737 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2738 struct weston_surface *surface =
2739 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2740 int32_t width = 0, height = 0;
2741
2742 if (maximized) {
2743 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002744
2745 if (!weston_surface_is_mapped(surface))
2746 output = get_focused_output(surface->compositor);
2747 else
2748 output = surface->output;
2749
2750 shell_surface_set_output(shsurf, output);
2751
Derek Foreman927d9e22017-10-06 14:37:44 -05002752 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002753 }
2754 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2755 weston_desktop_surface_set_size(desktop_surface, width, height);
2756}
2757
2758static void
2759desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2760 bool maximized, void *shell)
2761{
2762 struct shell_surface *shsurf =
2763 weston_desktop_surface_get_user_data(desktop_surface);
2764
2765 set_maximized(shsurf, maximized);
2766}
2767
2768static void
2769desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2770 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771{
2772 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002773 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002774
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002775 /* apply compositor's own minimization logic (hide) */
2776 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002777}
2778
Rafael Antognollie2a34552013-12-03 15:35:45 -02002779static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002781{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002782 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002783
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784 if (pointer->grab->interface == &busy_cursor_grab_interface)
2785 return;
2786
2787 grab = malloc(sizeof *grab);
2788 if (!grab)
2789 return;
2790
2791 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2792 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2793 /* Mark the shsurf as ungrabbed so that button binding is able
2794 * to move it. */
2795 shsurf->grabbed = 0;
2796}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002797
2798static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002799end_busy_cursor(struct weston_compositor *compositor,
2800 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002801{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002802 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002803 struct shell_grab *grab;
2804 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002805
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002806 wl_list_for_each(seat, &compositor->seat_list, link) {
2807 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2808 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002809
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810 if (!pointer)
2811 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002812
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002813 if (pointer->grab->interface != &busy_cursor_grab_interface)
2814 continue;
2815
2816 grab = (struct shell_grab *) pointer->grab;
2817 shsurf = grab->shsurf;
2818 if (!shsurf)
2819 continue;
2820
2821 grab_client =
2822 weston_desktop_surface_get_client(shsurf->desktop_surface);
2823 if (grab_client == desktop_client) {
2824 shell_grab_end(grab);
2825 free(grab);
2826 }
2827 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002828}
2829
2830static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2832 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002833{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002834 struct shell_surface *shsurf =
2835 weston_desktop_surface_get_user_data(desktop_surface);
2836 bool *unresponsive = user_data;
2837
2838 shsurf->unresponsive = *unresponsive;
2839}
2840
2841static void
2842desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2843 void *shell_)
2844{
2845 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002846 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847 struct weston_seat *seat;
2848 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002849
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002850 weston_desktop_client_for_each_surface(desktop_client,
2851 desktop_surface_set_unresponsive,
2852 &unresponsive);
2853
2854
2855 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2856 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2857 struct weston_desktop_client *grab_client;
2858
2859 if (!pointer || !pointer->focus)
2860 continue;
2861
2862 shsurf = get_shell_surface(pointer->focus->surface);
2863 if (!shsurf)
2864 continue;
2865
2866 grab_client =
2867 weston_desktop_surface_get_client(shsurf->desktop_surface);
2868 if (grab_client == desktop_client)
2869 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002870 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002871}
2872
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002873static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002874desktop_surface_pong(struct weston_desktop_client *desktop_client,
2875 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002876{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877 struct desktop_shell *shell = shell_;
2878 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002879
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002880 weston_desktop_client_for_each_surface(desktop_client,
2881 desktop_surface_set_unresponsive,
2882 &unresponsive);
2883 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002884}
2885
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002886static void
2887desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2888 int32_t x, int32_t y, void *shell_)
2889{
2890 struct shell_surface *shsurf =
2891 weston_desktop_surface_get_user_data(surface);
2892
2893 shsurf->xwayland.x = x;
2894 shsurf->xwayland.y = y;
2895 shsurf->xwayland.is_set = true;
2896}
2897
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002898static const struct weston_desktop_api shell_desktop_api = {
2899 .struct_size = sizeof(struct weston_desktop_api),
2900 .surface_added = desktop_surface_added,
2901 .surface_removed = desktop_surface_removed,
2902 .committed = desktop_surface_committed,
2903 .move = desktop_surface_move,
2904 .resize = desktop_surface_resize,
2905 .fullscreen_requested = desktop_surface_fullscreen_requested,
2906 .maximized_requested = desktop_surface_maximized_requested,
2907 .minimized_requested = desktop_surface_minimized_requested,
2908 .ping_timeout = desktop_surface_ping_timeout,
2909 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002910 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002911};
2912
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002913/* ************************ *
2914 * end of libweston-desktop *
2915 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002916static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002917configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002918{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002920
Semi Malinene7a52fb2018-04-26 11:08:10 +02002921 if (!ev->output)
2922 return;
2923
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002924 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002925 if (v->output == ev->output && v != ev) {
2926 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002927 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002928 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002929 }
2930 }
2931
Quentin Glidice8bf9592016-06-23 18:55:20 +02002932 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002933 ev->surface->is_mapped = true;
2934 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002935
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002936 if (wl_list_empty(&ev->layer_link.link)) {
2937 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002939 }
2940}
2941
David Fort50d962f2016-06-09 17:55:52 +02002942
2943static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002944find_shell_output_from_weston_output(struct desktop_shell *shell,
2945 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002946{
2947 struct shell_output *shell_output;
2948
2949 wl_list_for_each(shell_output, &shell->output_list, link) {
2950 if (shell_output->output == output)
2951 return shell_output;
2952 }
2953
2954 return NULL;
2955}
2956
Pekka Paalanen8274d902014-08-06 19:36:51 +03002957static int
2958background_get_label(struct weston_surface *surface, char *buf, size_t len)
2959{
2960 return snprintf(buf, len, "background for output %s",
2961 surface->output->name);
2962}
2963
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002964static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002965background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002966{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002967 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002969
Jason Ekstranda7af7042013-10-12 22:38:11 -05002970 view = container_of(es->views.next, struct weston_view, surface_link);
2971
Quentin Glidice8bf9592016-06-23 18:55:20 +02002972 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002973}
2974
2975static void
David Fort50d962f2016-06-09 17:55:52 +02002976handle_background_surface_destroy(struct wl_listener *listener, void *data)
2977{
2978 struct shell_output *output =
2979 container_of(listener, struct shell_output, background_surface_listener);
2980
2981 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002982 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002983 output->background_surface = NULL;
2984}
2985
2986static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002987desktop_shell_set_background(struct wl_client *client,
2988 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002989 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002990 struct wl_resource *surface_resource)
2991{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002992 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002993 struct weston_surface *surface =
2994 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002995 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002997
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002998 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002999 wl_resource_post_error(surface_resource,
3000 WL_DISPLAY_ERROR_INVALID_OBJECT,
3001 "surface role already assigned");
3002 return;
3003 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003004
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3006 weston_view_destroy(view);
3007 view = weston_view_create(surface);
3008
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003009 surface->committed = background_committed;
3010 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003011 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003012 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003013 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003014
3015 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003016 if (sh_output->background_surface) {
3017 /* The output already has a background, tell our helper
3018 * there is no need for another one. */
3019 weston_desktop_shell_send_configure(resource, 0,
3020 surface_resource,
3021 0, 0);
3022 } else {
3023 weston_desktop_shell_send_configure(resource, 0,
3024 surface_resource,
3025 surface->output->width,
3026 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003027
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003028 sh_output->background_surface = surface;
3029
3030 sh_output->background_surface_listener.notify =
3031 handle_background_surface_destroy;
3032 wl_signal_add(&surface->destroy_signal,
3033 &sh_output->background_surface_listener);
3034 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003035}
3036
Pekka Paalanen8274d902014-08-06 19:36:51 +03003037static int
3038panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3039{
3040 return snprintf(buf, len, "panel for output %s",
3041 surface->output->name);
3042}
3043
Kristian Høgsberg75840622011-09-06 13:48:16 -04003044static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003045panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003046{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003047 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003048 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003049 int width, height;
3050 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003051
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 view = container_of(es->views.next, struct weston_view, surface_link);
3053
Quentin Glidice8bf9592016-06-23 18:55:20 +02003054 get_panel_size(shell, view, &width, &height);
3055 switch (shell->panel_position) {
3056 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3057 break;
3058 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3059 y = view->output->height - height;
3060 break;
3061 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3062 break;
3063 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3064 x = view->output->width - width;
3065 break;
3066 }
3067
3068 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003069}
3070
3071static void
David Fort50d962f2016-06-09 17:55:52 +02003072handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3073{
3074 struct shell_output *output =
3075 container_of(listener, struct shell_output, panel_surface_listener);
3076
3077 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003078 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003079 output->panel_surface = NULL;
3080}
3081
3082
3083static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003084desktop_shell_set_panel(struct wl_client *client,
3085 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003086 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003087 struct wl_resource *surface_resource)
3088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003089 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003090 struct weston_surface *surface =
3091 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003092 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003093 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003094
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003095 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003096 wl_resource_post_error(surface_resource,
3097 WL_DISPLAY_ERROR_INVALID_OBJECT,
3098 "surface role already assigned");
3099 return;
3100 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003101
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3103 weston_view_destroy(view);
3104 view = weston_view_create(surface);
3105
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003106 surface->committed = panel_committed;
3107 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003108 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003109 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003110 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003111
3112 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003113 if (sh_output->panel_surface) {
3114 /* The output already has a panel, tell our helper
3115 * there is no need for another one. */
3116 weston_desktop_shell_send_configure(resource, 0,
3117 surface_resource,
3118 0, 0);
3119 } else {
3120 weston_desktop_shell_send_configure(resource, 0,
3121 surface_resource,
3122 surface->output->width,
3123 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003124
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003125 sh_output->panel_surface = surface;
3126
3127 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3128 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3129 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003130}
3131
Pekka Paalanen8274d902014-08-06 19:36:51 +03003132static int
3133lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3134{
3135 return snprintf(buf, len, "lock window");
3136}
3137
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003138static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003139lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003140{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003141 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003142 struct weston_view *view;
3143
3144 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003145
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003146 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003147 return;
3148
Jason Ekstranda7af7042013-10-12 22:38:11 -05003149 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003150
3151 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003152 weston_layer_entry_insert(&shell->lock_layer.view_list,
3153 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003155 surface->is_mapped = true;
3156 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003157 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003158 }
3159}
3160
3161static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003162handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003163{
Tiago Vignattibe143262012-04-16 17:31:41 +03003164 struct desktop_shell *shell =
3165 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003166
Martin Minarik6d118362012-06-07 18:01:59 +02003167 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003168 shell->lock_surface = NULL;
3169}
3170
3171static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003172desktop_shell_set_lock_surface(struct wl_client *client,
3173 struct wl_resource *resource,
3174 struct wl_resource *surface_resource)
3175{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003176 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003177 struct weston_surface *surface =
3178 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003179
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003180 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003181
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003182 if (!shell->locked)
3183 return;
3184
Pekka Paalanen98262232011-12-01 10:42:22 +02003185 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003186
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003187 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003188 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003189 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003190
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003191 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003192 surface->committed = lock_surface_committed;
3193 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003194 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003195}
3196
3197static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003198resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003199{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003200 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003201
Quentin Glidic82681572016-12-17 13:40:51 +01003202 weston_layer_unset_position(&shell->lock_layer);
3203
3204 if (shell->showing_input_panels)
3205 weston_layer_set_position(&shell->input_panel_layer,
3206 WESTON_LAYER_POSITION_TOP_UI);
3207 weston_layer_set_position(&shell->fullscreen_layer,
3208 WESTON_LAYER_POSITION_FULLSCREEN);
3209 weston_layer_set_position(&shell->panel_layer,
3210 WESTON_LAYER_POSITION_UI);
3211 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003212
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003213 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003214
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003215 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003216 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003217 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003218}
3219
3220static void
3221desktop_shell_unlock(struct wl_client *client,
3222 struct wl_resource *resource)
3223{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003224 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003225
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003226 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003227
3228 if (shell->locked)
3229 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003230}
3231
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003232static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003233desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003234 struct wl_resource *resource,
3235 struct wl_resource *surface_resource)
3236{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003237 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003238
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003239 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003240 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003241}
3242
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003243static void
3244desktop_shell_desktop_ready(struct wl_client *client,
3245 struct wl_resource *resource)
3246{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003247 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003248
3249 shell_fade_startup(shell);
3250}
3251
Jonny Lamb765760d2014-08-20 15:53:19 +02003252static void
3253desktop_shell_set_panel_position(struct wl_client *client,
3254 struct wl_resource *resource,
3255 uint32_t position)
3256{
3257 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3258
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003259 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3260 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3261 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3262 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003263 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003264 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003265 "bad position argument");
3266 return;
3267 }
3268
3269 shell->panel_position = position;
3270}
3271
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003272static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003273 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003274 desktop_shell_set_panel,
3275 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003276 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003277 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003278 desktop_shell_desktop_ready,
3279 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003280};
3281
3282static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003283move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003284 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003285{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003286 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003287 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003288 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003289
Derek Foreman8ae2db52015-07-15 13:00:36 -05003290 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003291 return;
3292
Derek Foreman8ae2db52015-07-15 13:00:36 -05003293 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003294
Pekka Paalanen01388e22013-04-25 13:57:44 +03003295 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003296 if (surface == NULL)
3297 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003298
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003299 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003300 if (shsurf == NULL ||
3301 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3302 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003303 return;
3304
Derek Foreman794fa0e2015-07-15 13:00:38 -05003305 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003306}
3307
3308static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003309maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003310 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003311{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003312 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003313 struct weston_surface *surface;
3314 struct shell_surface *shsurf;
3315
3316 surface = weston_surface_get_main_surface(focus);
3317 if (surface == NULL)
3318 return;
3319
3320 shsurf = get_shell_surface(surface);
3321 if (shsurf == NULL)
3322 return;
3323
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003324 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003325}
3326
3327static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003328fullscreen_binding(struct weston_keyboard *keyboard,
3329 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003330{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003331 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003332 struct weston_surface *surface;
3333 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003334 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003335
3336 surface = weston_surface_get_main_surface(focus);
3337 if (surface == NULL)
3338 return;
3339
3340 shsurf = get_shell_surface(surface);
3341 if (shsurf == NULL)
3342 return;
3343
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003344 fullscreen =
3345 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003346
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003347 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003348}
3349
3350static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003351touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003352{
Derek Foreman362656b2014-09-04 10:23:05 -05003353 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003354 struct weston_surface *surface;
3355 struct shell_surface *shsurf;
3356
Derek Foreman8ae2db52015-07-15 13:00:36 -05003357 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003358 return;
3359
Derek Foreman8ae2db52015-07-15 13:00:36 -05003360 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003361 surface = weston_surface_get_main_surface(focus);
3362 if (surface == NULL)
3363 return;
3364
3365 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003366 if (shsurf == NULL ||
3367 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3368 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003369 return;
3370
Derek Foremanb7674ae2015-07-15 13:00:37 -05003371 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003372}
3373
3374static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003375resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003376 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003377{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003378 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003379 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003380 uint32_t edges = 0;
3381 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003382 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003383
Derek Foreman8ae2db52015-07-15 13:00:36 -05003384 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003385 return;
3386
Derek Foreman8ae2db52015-07-15 13:00:36 -05003387 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003388
Pekka Paalanen01388e22013-04-25 13:57:44 +03003389 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003390 if (surface == NULL)
3391 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003392
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003393 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003394 if (shsurf == NULL ||
3395 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3396 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003397 return;
3398
Jason Ekstranda7af7042013-10-12 22:38:11 -05003399 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003400 wl_fixed_to_int(pointer->grab_x),
3401 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003402 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003403
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003404 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003405 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003406 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003407 edges |= 0;
3408 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003409 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003410
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003411 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003412 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003413 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003414 edges |= 0;
3415 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003416 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003417
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003418 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003419}
3420
3421static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003422surface_opacity_binding(struct weston_pointer *pointer,
3423 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003424 struct weston_pointer_axis_event *event,
3425 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003426{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003427 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003428 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003429 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003430 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003431
Pekka Paalanen01388e22013-04-25 13:57:44 +03003432 /* XXX: broken for windows containing sub-surfaces */
3433 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003434 if (surface == NULL)
3435 return;
3436
3437 shsurf = get_shell_surface(surface);
3438 if (!shsurf)
3439 return;
3440
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003441 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003442
Jason Ekstranda7af7042013-10-12 22:38:11 -05003443 if (shsurf->view->alpha > 1.0)
3444 shsurf->view->alpha = 1.0;
3445 if (shsurf->view->alpha < step)
3446 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003447
Jason Ekstranda7af7042013-10-12 22:38:11 -05003448 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003449 weston_surface_damage(surface);
3450}
3451
3452static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003453do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3454 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003455{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003456 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003457 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003458 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003459 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003460
Derek Foreman1281a362015-07-31 16:55:32 -05003461 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003462 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3463 return;
3464 }
3465
Scott Moreauccbf29d2012-02-22 14:21:41 -07003466 wl_list_for_each(output, &compositor->output_list, link) {
3467 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003468 wl_fixed_to_double(pointer->x),
3469 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003470 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003471 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003472 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003473 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003474 increment = -output->zoom.increment;
3475 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003476 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003477 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003478 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003479 else
3480 increment = 0;
3481
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003482 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003483
Scott Moreaue6603982012-06-11 13:07:51 -06003484 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003485 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003486 else if (output->zoom.level > output->zoom.max_level)
3487 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003488
3489 if (!output->zoom.active) {
3490 if (output->zoom.level <= 0.0)
3491 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003492 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003493 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003494
Scott Moreaue6603982012-06-11 13:07:51 -06003495 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496
Jason Ekstranda7af7042013-10-12 22:38:11 -05003497 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003498 }
3499 }
3500}
3501
Scott Moreauccbf29d2012-02-22 14:21:41 -07003502static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003503zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003504 struct weston_pointer_axis_event *event,
3505 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003506{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003507 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003508}
3509
3510static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003511zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003512 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003513{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003514 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003515}
3516
3517static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003518terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003519 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003521 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003522
Daniel Stone325fc2d2012-05-30 16:31:58 +01003523 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003524}
3525
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003526static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003527rotate_grab_motion(struct weston_pointer_grab *grab,
3528 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003529 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003530{
3531 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003532 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003533 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003534 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003535 struct weston_surface *surface =
3536 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003537 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003538
Jonas Ådahld2510102014-10-05 21:39:14 +02003539 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003540
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003541 if (!shsurf)
3542 return;
3543
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003544 cx = 0.5f * surface->width;
3545 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003546
Daniel Stone37816df2012-05-16 18:45:18 +01003547 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3548 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003549 r = sqrtf(dx * dx + dy * dy);
3550
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003551 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003553
3554 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003555 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003556 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003557
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003558 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003559 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003560
3561 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003562 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003563 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003564 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003565 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003566
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003567 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003569 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003570 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003571 wl_list_init(&shsurf->rotation.transform.link);
3572 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003573 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003574 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003575
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003576 /* We need to adjust the position of the surface
3577 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 cposx = shsurf->view->geometry.x + cx;
3579 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003580 dposx = rotate->center.x - cposx;
3581 dposy = rotate->center.y - cposy;
3582 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003583 weston_view_set_position(shsurf->view,
3584 shsurf->view->geometry.x + dposx,
3585 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003586 }
3587
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003588 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003589 * lazily applies the damage due to rotation update.
3590 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003591 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003592}
3593
3594static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003595rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003596 const struct timespec *time,
3597 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003598{
3599 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003600 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003601 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003602 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003603 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003604
Daniel Stone4dbadb12012-05-30 16:31:51 +01003605 if (pointer->button_count == 0 &&
3606 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003607 if (shsurf)
3608 weston_matrix_multiply(&shsurf->rotation.rotation,
3609 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003610 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003611 free(rotate);
3612 }
3613}
3614
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003615static void
3616rotate_grab_cancel(struct weston_pointer_grab *grab)
3617{
3618 struct rotate_grab *rotate =
3619 container_of(grab, struct rotate_grab, base.grab);
3620
3621 shell_grab_end(&rotate->base);
3622 free(rotate);
3623}
3624
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003625static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626 noop_grab_focus,
3627 rotate_grab_motion,
3628 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003629 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003630 noop_grab_axis_source,
3631 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003632 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003633};
3634
3635static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003636surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003637{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003638 struct weston_surface *surface =
3639 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003640 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003641 float dx, dy;
3642 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643
Pekka Paalanen460099f2012-01-20 16:48:25 +02003644 rotate = malloc(sizeof *rotate);
3645 if (!rotate)
3646 return;
3647
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003648 weston_view_to_global_float(shsurf->view,
3649 surface->width * 0.5f,
3650 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003651 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003652
Derek Foreman74de4692015-07-15 13:00:39 -05003653 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3654 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003655 r = sqrtf(dx * dx + dy * dy);
3656 if (r > 20.0f) {
3657 struct weston_matrix inverse;
3658
3659 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003660 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003661 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003662
3663 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003664 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003665 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003666 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003667 weston_matrix_init(&rotate->rotation);
3668 }
3669
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003670 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003671 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003672}
3673
3674static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003675rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3676 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003677{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003678 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003679 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003680 struct shell_surface *surface;
3681
Derek Foreman8ae2db52015-07-15 13:00:36 -05003682 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003683 return;
3684
Derek Foreman8ae2db52015-07-15 13:00:36 -05003685 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003686
Pekka Paalanen01388e22013-04-25 13:57:44 +03003687 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003688 if (base_surface == NULL)
3689 return;
3690
3691 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003692 if (surface == NULL ||
3693 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3694 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003695 return;
3696
Derek Foreman74de4692015-07-15 13:00:39 -05003697 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003698}
3699
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003700/* Move all fullscreen layers down to the current workspace and hide their
3701 * black views. The surfaces' state is set to both fullscreen and lowered,
3702 * and this is reversed when such a surface is re-configured, see
3703 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3704 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003705 * lowering_output = NULL - Lower on all outputs, else only lower on the
3706 * specified output.
3707 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003708 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003709 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003710void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003711lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003712 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003713{
3714 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003716
3717 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003718 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003719 &shell->fullscreen_layer.view_list.link,
3720 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003721 struct shell_surface *shsurf = get_shell_surface(view->surface);
3722
3723 if (!shsurf)
3724 continue;
3725
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003726 /* Only lower surfaces which have lowering_output as their fullscreen
3727 * output, unless a NULL output asks for lowering on all outputs.
3728 */
3729 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3730 continue;
3731
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003732 /* We can have a non-fullscreen popup for a fullscreen surface
3733 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003734 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003735 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003736 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3737 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003738 weston_view_damage_below(shsurf->fullscreen.black_view);
3739
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003740 }
3741
3742 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003743 weston_layer_entry_remove(&view->layer_link);
3744 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003745 weston_view_damage_below(view);
3746 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003747
3748 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003749 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003750}
3751
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003752void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003753activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003754 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003755{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003756 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003757 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003758 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003759 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003760 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003761 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003762
Pekka Paalanen01388e22013-04-25 13:57:44 +03003763 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003764 shsurf = get_shell_surface(main_surface);
3765 assert(shsurf);
3766
3767 /* Only demote fullscreen surfaces on the output of activated shsurf.
3768 * Leave fullscreen surfaces on unrelated outputs alone. */
3769 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003770
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003771 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003772
Jonas Ådahl8538b222012-08-29 22:13:03 +02003773 state = ensure_focus_state(shell, seat);
3774 if (state == NULL)
3775 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003776
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003777 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003778 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003780 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3781 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003782 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003783
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003784 /* Update the surface’s layer. This brings it to the top of the stacking
3785 * order as appropriate. */
3786 shell_surface_update_layer(shsurf);
3787
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003788 if (shell->focus_animation_type != ANIMATION_NONE) {
3789 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003790 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003791 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003792}
3793
Alex Wu21858432012-04-01 20:13:08 +08003794/* no-op func for checking black surface */
3795static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003796black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003797{
3798}
3799
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003800static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003801is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003802{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003803 struct weston_surface *surface = view->surface;
3804
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003805 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003806 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003807 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003808 return true;
3809 }
3810 return false;
3811}
3812
Kristian Høgsberg75840622011-09-06 13:48:16 -04003813static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003814activate_binding(struct weston_seat *seat,
3815 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003816 struct weston_view *focus_view,
3817 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003818{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003819 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003820 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003821
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003822 if (!focus_view)
3823 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003824
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003825 if (is_black_surface_view(focus_view, &main_view))
3826 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003827
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003828 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003829 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003830 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003831
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003832 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003833}
3834
3835static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003836click_to_activate_binding(struct weston_pointer *pointer,
3837 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003838 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003839{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003840 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003841 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003842 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003843 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003844
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003845 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003846 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003847 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003848}
3849
3850static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003851touch_to_activate_binding(struct weston_touch *touch,
3852 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003853 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003854{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003855 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003856 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003857 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003858 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003859
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003860 activate_binding(touch->seat, data, touch->focus,
3861 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003862}
3863
3864static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003865unfocus_all_seats(struct desktop_shell *shell)
3866{
3867 struct weston_seat *seat, *next;
3868
3869 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003870 struct weston_keyboard *keyboard =
3871 weston_seat_get_keyboard(seat);
3872
3873 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003874 continue;
3875
Derek Foreman1281a362015-07-31 16:55:32 -05003876 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003877 }
3878}
3879
3880static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003881lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003882{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003883 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003884
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003885 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003886 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003887 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003888 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003889
3890 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003891
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003892 /* Hide all surfaces by removing the fullscreen, panel and
3893 * toplevel layers. This way nothing else can show or receive
3894 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003895
Quentin Glidic82681572016-12-17 13:40:51 +01003896 weston_layer_unset_position(&shell->panel_layer);
3897 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003898 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003899 weston_layer_unset_position(&shell->input_panel_layer);
3900 weston_layer_unset_position(&ws->layer);
3901
3902 weston_layer_set_position(&shell->lock_layer,
3903 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904
Derek Foreman004b4a12015-07-20 16:28:13 -05003905 weston_compositor_sleep(shell->compositor);
3906
Neil Robertsb4a91702014-04-09 16:33:32 +01003907 /* Remove the keyboard focus on all seats. This will be
3908 * restored to the workspace's saved state via
3909 * restore_focus_state when the compositor is unlocked */
3910 unfocus_all_seats(shell);
3911
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912 /* TODO: disable bindings that should not work while locked. */
3913
3914 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003915}
3916
3917static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003918unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003919{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003920 struct wl_resource *shell_resource;
3921
Pekka Paalanend81c2162011-11-16 13:47:34 +02003922 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003923 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003924 return;
3925 }
3926
3927 /* If desktop-shell client has gone away, unlock immediately. */
3928 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003929 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003930 return;
3931 }
3932
3933 if (shell->prepare_event_sent)
3934 return;
3935
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003936 shell_resource = shell->child.desktop_shell;
3937 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003938 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003939}
3940
3941static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003942shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003943{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003944 struct shell_output *shell_output = data;
3945 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003946
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003947 shell_output->fade.animation = NULL;
3948 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003949 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003950 weston_surface_destroy(shell_output->fade.view->surface);
3951 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003952 break;
3953 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003954 lock(shell);
3955 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003956 default:
3957 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003958 }
3959}
3960
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003962shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963{
3964 struct weston_compositor *compositor = shell->compositor;
3965 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003967
3968 surface = weston_surface_create(compositor);
3969 if (!surface)
3970 return NULL;
3971
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 view = weston_view_create(surface);
3973 if (!view) {
3974 weston_surface_destroy(surface);
3975 return NULL;
3976 }
3977
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003978 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3979 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003980 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003981 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3982 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003983 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003984 surface->is_mapped = true;
3985 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003986
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003988}
3989
3990static void
3991shell_fade(struct desktop_shell *shell, enum fade_type type)
3992{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003994 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003995
3996 switch (type) {
3997 case FADE_IN:
3998 tint = 0.0;
3999 break;
4000 case FADE_OUT:
4001 tint = 1.0;
4002 break;
4003 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004004 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004005 return;
4006 }
4007
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004008 /* Create a separate fade surface for each output */
4009 wl_list_for_each(shell_output, &shell->output_list, link) {
4010 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004011
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004012 if (shell_output->fade.view == NULL) {
4013 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4014 if (!shell_output->fade.view)
4015 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004016
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004017 shell_output->fade.view->alpha = 1.0 - tint;
4018 weston_view_update_transform(shell_output->fade.view);
4019 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004020
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004021 if (shell_output->fade.view->output == NULL) {
4022 /* If the black view gets a NULL output, we lost the
4023 * last output and we'll just cancel the fade. This
4024 * happens when you close the last window under the
4025 * X11 or Wayland backends. */
4026 shell->locked = false;
4027 weston_surface_destroy(shell_output->fade.view->surface);
4028 shell_output->fade.view = NULL;
4029 } else if (shell_output->fade.animation) {
4030 weston_fade_update(shell_output->fade.animation, tint);
4031 } else {
4032 shell_output->fade.animation =
4033 weston_fade_run(shell_output->fade.view,
4034 1.0 - tint, tint, 300.0,
4035 shell_fade_done_for_output, shell_output);
4036 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004037 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004038}
4039
4040static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004041do_shell_fade_startup(void *data)
4042{
4043 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004044 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004045
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004046 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004047 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004048 } else {
4049 weston_log("desktop shell: "
4050 "unexpected fade-in animation type %d\n",
4051 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004052 wl_list_for_each(shell_output, &shell->output_list, link) {
4053 weston_surface_destroy(shell_output->fade.view->surface);
4054 shell_output->fade.view = NULL;
4055 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004056 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004057}
4058
4059static void
4060shell_fade_startup(struct desktop_shell *shell)
4061{
4062 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004063 struct shell_output *shell_output;
4064 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004065
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004066 wl_list_for_each(shell_output, &shell->output_list, link) {
4067 if (!shell_output->fade.startup_timer)
4068 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004069
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004070 wl_event_source_remove(shell_output->fade.startup_timer);
4071 shell_output->fade.startup_timer = NULL;
4072 has_fade = true;
4073 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004074
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004075 if (has_fade) {
4076 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4077 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4078 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004079}
4080
4081static int
4082fade_startup_timeout(void *data)
4083{
4084 struct desktop_shell *shell = data;
4085
4086 shell_fade_startup(shell);
4087 return 0;
4088}
4089
4090static void
4091shell_fade_init(struct desktop_shell *shell)
4092{
4093 /* Make compositor output all black, and wait for the desktop-shell
4094 * client to signal it is ready, then fade in. The timer triggers a
4095 * fade-in, in case the desktop-shell client takes too long.
4096 */
4097
4098 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004099 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004100
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004101 if (shell->startup_animation_type == ANIMATION_NONE)
4102 return;
4103
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004104 wl_list_for_each(shell_output, &shell->output_list, link) {
4105 if (shell_output->fade.view != NULL) {
4106 weston_log("%s: warning: fade surface already exists\n",
4107 __func__);
4108 continue;
4109 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004110
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004111 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4112 if (!shell_output->fade.view)
4113 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004114
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004115 weston_view_update_transform(shell_output->fade.view);
4116 weston_surface_damage(shell_output->fade.view->surface);
4117
4118 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4119 shell_output->fade.startup_timer =
4120 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4121 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4122 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004123}
4124
4125static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004126idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004127{
4128 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004129 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004130
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004131 struct weston_seat *seat;
4132
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004133 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4134 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004135
4136 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004137 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004138}
4139
4140static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004141wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004142{
4143 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004144 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004145
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004146 unlock(shell);
4147}
4148
4149static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004150transform_handler(struct wl_listener *listener, void *data)
4151{
4152 struct weston_surface *surface = data;
4153 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004154 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004155 int x, y;
4156
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004157 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004158 return;
4159
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004160 api = shsurf->shell->xwayland_surface_api;
4161 if (!api) {
4162 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4163 shsurf->shell->xwayland_surface_api = api;
4164 }
4165
4166 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004167 return;
4168
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004169 if (!weston_view_is_mapped(shsurf->view))
4170 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004171
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004172 x = shsurf->view->geometry.x;
4173 y = shsurf->view->geometry.y;
4174
4175 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004176}
4177
4178static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004179center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004180{
Giulio Camuffob8366642013-04-25 13:57:46 +03004181 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004182 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004183
Jason Ekstranda7af7042013-10-12 22:38:11 -05004184 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004185
4186 x = output->x + (output->width - width) / 2 - surf_x / 2;
4187 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004188
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004189 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004190}
4191
4192static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004193weston_view_set_initial_position(struct weston_view *view,
4194 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004195{
4196 struct weston_compositor *compositor = shell->compositor;
4197 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004198 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004199 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004200 struct weston_output *output, *target_output = NULL;
4201 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004202 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004203
4204 /* As a heuristic place the new window on the same output as the
4205 * pointer. Falling back to the output containing 0, 0.
4206 *
4207 * TODO: Do something clever for touch too?
4208 */
4209 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004210 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4211
4212 if (pointer) {
4213 ix = wl_fixed_to_int(pointer->x);
4214 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004215 break;
4216 }
4217 }
4218
4219 wl_list_for_each(output, &compositor->output_list, link) {
4220 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4221 target_output = output;
4222 break;
4223 }
4224 }
4225
4226 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227 weston_view_set_position(view, 10 + random() % 400,
4228 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004229 return;
4230 }
4231
4232 /* Valid range within output where the surface will still be onscreen.
4233 * If this is negative it means that the surface is bigger than
4234 * output.
4235 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004236 get_output_work_area(shell, target_output, &area);
4237
Derek Foremanf814c5d2015-04-15 12:23:54 -05004238 x = area.x;
4239 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004240 range_x = area.width - view->surface->width;
4241 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004242
Rob Bradford4cb88c72012-08-13 15:18:44 +01004243 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004244 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004245
4246 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004247 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004248
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004250}
4251
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004252static bool
4253check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4254{
4255 struct timespec now;
4256
4257 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4258 return false;
4259
4260 /*
4261 * If the shell helper client dies before the session has been
4262 * up for roughly 30 seconds, better just make Weston shut down,
4263 * because the user likely has no way to interact with the desktop
4264 * anyway.
4265 */
4266 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4267 weston_log("Error: %s apparently cannot run at all.\n",
4268 shell->client);
4269 weston_log_continue(STAMP_SPACE "Quitting...");
4270 wl_display_terminate(shell->compositor->wl_display);
4271
4272 return true;
4273 }
4274
4275 return false;
4276}
4277
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004278static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004279
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004280static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004281respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004282{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004283 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004284
4285 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004286 weston_compositor_get_time(&time);
4287 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004288 shell->child.deathstamp = time;
4289 shell->child.deathcount = 0;
4290 }
4291
4292 shell->child.deathcount++;
4293 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004294 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004295 return;
4296 }
4297
Pekka Paalanen826dc142014-08-27 12:11:53 +03004298 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004299 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004300}
4301
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004302static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004303desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4304{
4305 struct desktop_shell *shell;
4306
4307 shell = container_of(listener, struct desktop_shell,
4308 child.client_destroy_listener);
4309
Pekka Paalanen826dc142014-08-27 12:11:53 +03004310 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004311 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004312 /*
4313 * unbind_desktop_shell() will reset shell->child.desktop_shell
4314 * before the respawned process has a chance to create a new
4315 * desktop_shell object, because we are being called from the
4316 * wl_client destructor which destroys all wl_resources before
4317 * returning.
4318 */
4319
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004320 if (!check_desktop_shell_crash_too_early(shell))
4321 respawn_desktop_shell_process(shell);
4322
Pekka Paalanen826dc142014-08-27 12:11:53 +03004323 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004324}
4325
4326static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004327launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004328{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004329 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004330
Pekka Paalanen826dc142014-08-27 12:11:53 +03004331 shell->child.client = weston_client_start(shell->compositor,
4332 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004333
Arnaud Vrac42631192014-08-25 20:56:46 +02004334 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004335 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004336 return;
4337 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004338
4339 shell->child.client_destroy_listener.notify =
4340 desktop_shell_client_destroy;
4341 wl_client_add_destroy_listener(shell->child.client,
4342 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004343}
4344
4345static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004346unbind_desktop_shell(struct wl_resource *resource)
4347{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004348 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004349
4350 if (shell->locked)
4351 resume_desktop(shell);
4352
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004353 shell->child.desktop_shell = NULL;
4354 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004355}
4356
4357static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004358bind_desktop_shell(struct wl_client *client,
4359 void *data, uint32_t version, uint32_t id)
4360{
Tiago Vignattibe143262012-04-16 17:31:41 +03004361 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004362 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004363
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004364 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4365 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004366
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004367 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004368 wl_resource_set_implementation(resource,
4369 &desktop_shell_implementation,
4370 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004371 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004372 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004373 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004374
4375 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4376 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004377}
4378
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004380 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004381 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004382 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004383 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004384 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004385};
4386
4387static void
4388switcher_next(struct switcher *switcher)
4389{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004391 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004392 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004393 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004394
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004395 /* temporary re-display minimized surfaces */
4396 struct weston_view *tmp;
4397 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004398 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4399 weston_layer_entry_remove(&view->layer_link);
4400 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004401 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4402 *minimized = view;
4403 }
4404
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004405 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004406 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004407 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004408 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004409 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004410 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004411 next = view;
4412 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 view->alpha = 0.25;
4414 weston_view_geometry_dirty(view);
4415 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004416 }
Alex Wu1659daa2012-04-01 20:13:09 +08004417
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004418 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004419 view->alpha = 0.25;
4420 weston_view_geometry_dirty(view);
4421 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004422 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004423 }
4424
4425 if (next == NULL)
4426 next = first;
4427
Alex Wu07b26062012-03-12 16:06:01 +08004428 if (next == NULL)
4429 return;
4430
Kristian Høgsberg07045392012-02-19 18:52:44 -05004431 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004432 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004433
4434 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004435 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004437
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004438 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004439 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004441}
4442
4443static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004444switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004445{
4446 struct switcher *switcher =
4447 container_of(listener, struct switcher, listener);
4448
4449 switcher_next(switcher);
4450}
4451
4452static void
Daniel Stone351eb612012-05-31 15:27:47 -04004453switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004454{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004456 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004457 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004458
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004459 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004460 if (is_focus_view(view))
4461 continue;
4462
Jason Ekstranda7af7042013-10-12 22:38:11 -05004463 view->alpha = 1.0;
4464 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004465 }
4466
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004467 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004468 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004469 keyboard->seat,
4470 WESTON_ACTIVATE_FLAG_CONFIGURE);
4471 }
4472
Kristian Høgsberg07045392012-02-19 18:52:44 -05004473 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004474 weston_keyboard_end_grab(keyboard);
4475 if (keyboard->input_method_resource)
4476 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004477
4478 /* re-hide surfaces that were temporary shown during the switch */
4479 struct weston_view **minimized;
4480 wl_array_for_each(minimized, &switcher->minimized_array) {
4481 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004482 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004483 weston_layer_entry_remove(&(*minimized)->layer_link);
4484 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004485 weston_view_damage_below(*minimized);
4486 }
4487 }
4488 wl_array_release(&switcher->minimized_array);
4489
Kristian Høgsberg07045392012-02-19 18:52:44 -05004490 free(switcher);
4491}
4492
4493static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004494switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004495 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004496{
4497 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004498 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004499
Daniel Stonec9785ea2012-05-30 16:31:52 +01004500 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004501 switcher_next(switcher);
4502}
4503
4504static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004505switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004506 uint32_t mods_depressed, uint32_t mods_latched,
4507 uint32_t mods_locked, uint32_t group)
4508{
4509 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004510 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004511
Daniel Stone351eb612012-05-31 15:27:47 -04004512 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4513 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004514}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004515
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004516static void
4517switcher_cancel(struct weston_keyboard_grab *grab)
4518{
4519 struct switcher *switcher = container_of(grab, struct switcher, grab);
4520
4521 switcher_destroy(switcher);
4522}
4523
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004524static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004525 switcher_key,
4526 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004527 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004528};
4529
4530static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004531switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004532 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533{
Tiago Vignattibe143262012-04-16 17:31:41 +03004534 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004535 struct switcher *switcher;
4536
4537 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004538 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004539 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004540 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004541 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004542 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004543
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004544 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004546 weston_keyboard_start_grab(keyboard, &switcher->grab);
4547 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004548 switcher_next(switcher);
4549}
4550
Pekka Paalanen3c647232011-12-22 13:43:43 +02004551static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004552backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004553 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004554{
4555 struct weston_compositor *compositor = data;
4556 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004557 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004558
4559 /* TODO: we're limiting to simple use cases, where we assume just
4560 * control on the primary display. We'd have to extend later if we
4561 * ever get support for setting backlights on random desktop LCD
4562 * panels though */
4563 output = get_default_output(compositor);
4564 if (!output)
4565 return;
4566
4567 if (!output->set_backlight)
4568 return;
4569
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004570 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4571 backlight_new = output->backlight_current - 25;
4572 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4573 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004574
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004575 if (backlight_new < 5)
4576 backlight_new = 5;
4577 if (backlight_new > 255)
4578 backlight_new = 255;
4579
4580 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004581 output->set_backlight(output, output->backlight_current);
4582}
4583
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004584static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004585force_kill_binding(struct weston_keyboard *keyboard,
4586 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004587{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004588 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004589 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004590 struct desktop_shell *shell = data;
4591 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004592 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004593
Derek Foreman8ae2db52015-07-15 13:00:36 -05004594 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004595 if (!focus_surface)
4596 return;
4597
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004598 wl_signal_emit(&compositor->kill_signal, focus_surface);
4599
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004600 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004601 wl_client_get_credentials(client, &pid, NULL, NULL);
4602
4603 /* Skip clients that we launched ourselves (the credentials of
4604 * the socketpair is ours) */
4605 if (pid == getpid())
4606 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004607
Daniel Stone325fc2d2012-05-30 16:31:58 +01004608 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004609}
4610
4611static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004612workspace_up_binding(struct weston_keyboard *keyboard,
4613 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004614{
4615 struct desktop_shell *shell = data;
4616 unsigned int new_index = shell->workspaces.current;
4617
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004618 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004619 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004620 if (new_index != 0)
4621 new_index--;
4622
4623 change_workspace(shell, new_index);
4624}
4625
4626static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004627workspace_down_binding(struct weston_keyboard *keyboard,
4628 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004629{
4630 struct desktop_shell *shell = data;
4631 unsigned int new_index = shell->workspaces.current;
4632
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004633 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004634 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004635 if (new_index < shell->workspaces.num - 1)
4636 new_index++;
4637
4638 change_workspace(shell, new_index);
4639}
4640
4641static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004642workspace_f_binding(struct weston_keyboard *keyboard,
4643 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004644{
4645 struct desktop_shell *shell = data;
4646 unsigned int new_index;
4647
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004648 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004649 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004650 new_index = key - KEY_F1;
4651 if (new_index >= shell->workspaces.num)
4652 new_index = shell->workspaces.num - 1;
4653
4654 change_workspace(shell, new_index);
4655}
4656
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004657static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004658workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004659 const struct timespec *time, uint32_t key,
4660 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004661{
4662 struct desktop_shell *shell = data;
4663 unsigned int new_index = shell->workspaces.current;
4664
4665 if (shell->locked)
4666 return;
4667
4668 if (new_index != 0)
4669 new_index--;
4670
Derek Foreman8ae2db52015-07-15 13:00:36 -05004671 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004672}
4673
4674static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004675workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004676 const struct timespec *time, uint32_t key,
4677 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004678{
4679 struct desktop_shell *shell = data;
4680 unsigned int new_index = shell->workspaces.current;
4681
4682 if (shell->locked)
4683 return;
4684
4685 if (new_index < shell->workspaces.num - 1)
4686 new_index++;
4687
Derek Foreman8ae2db52015-07-15 13:00:36 -05004688 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004689}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004690
4691static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004692shell_reposition_view_on_output_destroy(struct weston_view *view)
4693{
4694 struct weston_output *output, *first_output;
4695 struct weston_compositor *ec = view->surface->compositor;
4696 struct shell_surface *shsurf;
4697 float x, y;
4698 int visible;
4699
4700 x = view->geometry.x;
4701 y = view->geometry.y;
4702
4703 /* At this point the destroyed output is not in the list anymore.
4704 * If the view is still visible somewhere, we leave where it is,
4705 * otherwise, move it to the first output. */
4706 visible = 0;
4707 wl_list_for_each(output, &ec->output_list, link) {
4708 if (pixman_region32_contains_point(&output->region,
4709 x, y, NULL)) {
4710 visible = 1;
4711 break;
4712 }
4713 }
4714
4715 if (!visible) {
4716 first_output = container_of(ec->output_list.next,
4717 struct weston_output, link);
4718
4719 x = first_output->x + first_output->width / 4;
4720 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004721
4722 weston_view_set_position(view, x, y);
4723 } else {
4724 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004725 }
4726
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004727
4728 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004729 if (!shsurf)
4730 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004731
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004732 shsurf->saved_position_valid = false;
4733 set_maximized(shsurf, false);
4734 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004735}
4736
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004737void
4738shell_for_each_layer(struct desktop_shell *shell,
4739 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004740{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004741 struct workspace **ws;
4742
4743 func(shell, &shell->fullscreen_layer, data);
4744 func(shell, &shell->panel_layer, data);
4745 func(shell, &shell->background_layer, data);
4746 func(shell, &shell->lock_layer, data);
4747 func(shell, &shell->input_panel_layer, data);
4748
4749 wl_array_for_each(ws, &shell->workspaces.array)
4750 func(shell, &(*ws)->layer, data);
4751}
4752
4753static void
4754shell_output_destroy_move_layer(struct desktop_shell *shell,
4755 struct weston_layer *layer,
4756 void *data)
4757{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004758 struct weston_view *view;
4759
Marius Vladea40d6d2018-02-08 18:46:42 +02004760 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004761 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004762}
4763
4764static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004765handle_output_destroy(struct wl_listener *listener, void *data)
4766{
4767 struct shell_output *output_listener =
4768 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004769 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004770
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004771 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004772
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004773 if (output_listener->panel_surface)
4774 wl_list_remove(&output_listener->panel_surface_listener.link);
4775 if (output_listener->background_surface)
4776 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004777 wl_list_remove(&output_listener->destroy_listener.link);
4778 wl_list_remove(&output_listener->link);
4779 free(output_listener);
4780}
4781
4782static void
David Fort50d962f2016-06-09 17:55:52 +02004783shell_resize_surface_to_output(struct desktop_shell *shell,
4784 struct weston_surface *surface,
4785 const struct weston_output *output)
4786{
4787 if (!surface)
4788 return;
4789
4790 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4791 surface->resource,
4792 output->width,
4793 output->height);
4794}
4795
4796
4797static void
4798handle_output_resized(struct wl_listener *listener, void *data)
4799{
4800 struct desktop_shell *shell =
4801 container_of(listener, struct desktop_shell, resized_listener);
4802 struct weston_output *output = (struct weston_output *)data;
4803 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4804
4805 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4806 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4807}
4808
4809static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004810create_shell_output(struct desktop_shell *shell,
4811 struct weston_output *output)
4812{
4813 struct shell_output *shell_output;
4814
4815 shell_output = zalloc(sizeof *shell_output);
4816 if (shell_output == NULL)
4817 return;
4818
4819 shell_output->output = output;
4820 shell_output->shell = shell;
4821 shell_output->destroy_listener.notify = handle_output_destroy;
4822 wl_signal_add(&output->destroy_signal,
4823 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004824 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004825}
4826
4827static void
4828handle_output_create(struct wl_listener *listener, void *data)
4829{
4830 struct desktop_shell *shell =
4831 container_of(listener, struct desktop_shell, output_create_listener);
4832 struct weston_output *output = (struct weston_output *)data;
4833
4834 create_shell_output(shell, output);
4835}
4836
4837static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004838handle_output_move_layer(struct desktop_shell *shell,
4839 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004840{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004841 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004842 struct weston_view *view;
4843 float x, y;
4844
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004845 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004846 if (view->output != output)
4847 continue;
4848
4849 x = view->geometry.x + output->move_x;
4850 y = view->geometry.y + output->move_y;
4851 weston_view_set_position(view, x, y);
4852 }
4853}
4854
4855static void
4856handle_output_move(struct wl_listener *listener, void *data)
4857{
4858 struct desktop_shell *shell;
4859
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004860 shell = container_of(listener, struct desktop_shell,
4861 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004862
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004863 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004864}
4865
4866static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004867setup_output_destroy_handler(struct weston_compositor *ec,
4868 struct desktop_shell *shell)
4869{
4870 struct weston_output *output;
4871
4872 wl_list_init(&shell->output_list);
4873 wl_list_for_each(output, &ec->output_list, link)
4874 create_shell_output(shell, output);
4875
4876 shell->output_create_listener.notify = handle_output_create;
4877 wl_signal_add(&ec->output_created_signal,
4878 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004879
4880 shell->output_move_listener.notify = handle_output_move;
4881 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004882}
4883
4884static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004885shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004886{
Tiago Vignattibe143262012-04-16 17:31:41 +03004887 struct desktop_shell *shell =
4888 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004889 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004890 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004891
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004892 /* Force state to unlocked so we don't try to fade */
4893 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004894
4895 if (shell->child.client) {
4896 /* disable respawn */
4897 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004898 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004899 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004900
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004901 wl_list_remove(&shell->idle_listener.link);
4902 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004903 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004904
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004905 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004906 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004907
Xiong Zhang6b481422013-10-23 13:58:32 +08004908 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4909 wl_list_remove(&shell_output->destroy_listener.link);
4910 wl_list_remove(&shell_output->link);
4911 free(shell_output);
4912 }
4913
4914 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004915 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004916 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004917
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004918 wl_array_for_each(ws, &shell->workspaces.array)
4919 workspace_destroy(*ws);
4920 wl_array_release(&shell->workspaces.array);
4921
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004922 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004923 free(shell);
4924}
4925
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004926static void
4927shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4928{
4929 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004930 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004931
Bob Ham744e6532016-01-12 10:21:48 +00004932 if (shell->allow_zap)
4933 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4934 MODIFIER_CTRL | MODIFIER_ALT,
4935 terminate_binding, ec);
4936
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004937 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004938 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4939 click_to_activate_binding,
4940 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004941 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4942 click_to_activate_binding,
4943 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004944 weston_compositor_add_touch_binding(ec, 0,
4945 touch_to_activate_binding,
4946 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004947 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4948 backlight_binding, ec);
4949 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4950 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004951
4952 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004953 if (shell->exposay_modifier)
4954 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4955 exposay_binding, shell);
4956
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004957 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004958 if (!mod)
4959 return;
4960
Ian Ray13404c42017-09-18 15:22:01 +03004961 /* This binding is not configurable, but is only enabled if there is a
4962 * valid binding modifier. */
4963 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4964 MODIFIER_SUPER | MODIFIER_ALT,
4965 surface_opacity_binding, NULL);
4966
Ian Rayab7c0b62017-09-18 15:22:00 +03004967 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4968 mod, zoom_axis_binding,
4969 NULL);
4970
Daniel Stone325fc2d2012-05-30 16:31:58 +01004971 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4972 zoom_key_binding, NULL);
4973 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4974 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004975 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4976 maximize_binding, NULL);
4977 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4978 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004979 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4980 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004981 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004982 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004983 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004984 weston_compositor_add_button_binding(ec, BTN_LEFT,
4985 mod | MODIFIER_SHIFT,
4986 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004987
4988 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004989 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004990 rotate_binding, NULL);
4991
Daniel Stone325fc2d2012-05-30 16:31:58 +01004992 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4993 shell);
4994 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4995 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004996 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4997 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004998 weston_compositor_add_key_binding(ec, KEY_K, mod,
4999 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005000 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5001 workspace_up_binding, shell);
5002 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5003 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005004 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5005 workspace_move_surface_up_binding,
5006 shell);
5007 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5008 workspace_move_surface_down_binding,
5009 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005010
5011 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5012 if (shell->workspaces.num > 1) {
5013 num_workspace_bindings = shell->workspaces.num;
5014 if (num_workspace_bindings > 6)
5015 num_workspace_bindings = 6;
5016 for (i = 0; i < num_workspace_bindings; i++)
5017 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5018 workspace_f_binding,
5019 shell);
5020 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005021
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005022 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005023}
5024
Jason Ekstrand024177c2014-04-21 19:42:58 -05005025static void
5026handle_seat_created(struct wl_listener *listener, void *data)
5027{
5028 struct weston_seat *seat = data;
5029
5030 create_shell_seat(seat);
5031}
5032
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005033WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005034wet_shell_init(struct weston_compositor *ec,
5035 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005036{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005037 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005038 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005039 struct workspace **pws;
5040 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005041 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005042
Peter Huttererf3d62272013-08-08 11:57:05 +10005043 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005044 if (shell == NULL)
5045 return -1;
5046
Kristian Høgsberg75840622011-09-06 13:48:16 -04005047 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005048
5049 shell->destroy_listener.notify = shell_destroy;
5050 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005051 shell->idle_listener.notify = idle_handler;
5052 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5053 shell->wake_listener.notify = wake_handler;
5054 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005055 shell->transform_listener.notify = transform_handler;
5056 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005057
Quentin Glidic82681572016-12-17 13:40:51 +01005058 weston_layer_init(&shell->fullscreen_layer, ec);
5059 weston_layer_init(&shell->panel_layer, ec);
5060 weston_layer_init(&shell->background_layer, ec);
5061 weston_layer_init(&shell->lock_layer, ec);
5062 weston_layer_init(&shell->input_panel_layer, ec);
5063
5064 weston_layer_set_position(&shell->fullscreen_layer,
5065 WESTON_LAYER_POSITION_FULLSCREEN);
5066 weston_layer_set_position(&shell->panel_layer,
5067 WESTON_LAYER_POSITION_UI);
5068 weston_layer_set_position(&shell->background_layer,
5069 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005070
5071 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005072 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005073
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005074 if (input_panel_setup(shell) < 0)
5075 return -1;
5076
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005077 shell->text_backend = text_backend_init(ec);
5078 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005079 return -1;
5080
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005081 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005082
Daniel Stonedf8133b2013-11-19 11:37:14 +01005083 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5084 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5085
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005086 for (i = 0; i < shell->workspaces.num; i++) {
5087 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5088 if (pws == NULL)
5089 return -1;
5090
Quentin Glidic82681572016-12-17 13:40:51 +01005091 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005092 if (*pws == NULL)
5093 return -1;
5094 }
5095 activate_workspace(shell, 0);
5096
Quentin Glidic82681572016-12-17 13:40:51 +01005097 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005098
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005099 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005100 wl_list_init(&shell->workspaces.animation.link);
5101 shell->workspaces.animation.frame = animate_workspace_change_frame;
5102
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005103 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5104 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005105 return -1;
5106
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005107 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005108 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005109 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005110 return -1;
5111
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005112 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005113
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005114 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005115
Xiong Zhang6b481422013-10-23 13:58:32 +08005116 setup_output_destroy_handler(ec, shell);
5117
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005118 loop = wl_display_get_event_loop(ec->wl_display);
5119 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005120
Jason Ekstrand024177c2014-04-21 19:42:58 -05005121 wl_list_for_each(seat, &ec->seat_list, link)
5122 handle_seat_created(NULL, seat);
5123 shell->seat_create_listener.notify = handle_seat_created;
5124 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005125
David Fort50d962f2016-06-09 17:55:52 +02005126 shell->resized_listener.notify = handle_output_resized;
5127 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5128
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005129 screenshooter_create(ec);
5130
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005131 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005132
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005133 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005134
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005135 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5136
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005137 return 0;
5138}