blob: 64db89fc288f401e18adf64ac4444489f825b9d5 [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);
2306 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002307}
2308
2309static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002310fade_out_done(struct weston_view_animation *animation, void *data)
2311{
2312 struct shell_surface *shsurf = data;
2313 struct wl_event_loop *loop;
2314
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002315 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002316
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002317 if (weston_view_is_mapped(shsurf->view)) {
2318 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002319 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002320 }
2321}
2322
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002323struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002324get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002325{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002326 if (weston_surface_is_desktop_surface(surface)) {
2327 struct weston_desktop_surface *desktop_surface =
2328 weston_surface_get_desktop_surface(surface);
2329 return weston_desktop_surface_get_user_data(desktop_surface);
2330 }
2331 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002332}
2333
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002334/*
2335 * libweston-desktop
2336 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002337
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002338static void
2339desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2340 void *shell)
2341{
2342 struct weston_desktop_client *client =
2343 weston_desktop_surface_get_client(desktop_surface);
2344 struct wl_client *wl_client =
2345 weston_desktop_client_get_client(client);
2346 struct weston_view *view;
2347 struct shell_surface *shsurf;
2348 struct weston_surface *surface =
2349 weston_desktop_surface_get_surface(desktop_surface);
2350
2351 view = weston_desktop_surface_create_view(desktop_surface);
2352 if (!view)
2353 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002354
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002355 shsurf = calloc(1, sizeof *shsurf);
2356 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002357 if (wl_client)
2358 wl_client_post_no_memory(wl_client);
2359 else
2360 weston_log("no memory to allocate shell surface\n");
2361 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002362 }
2363
Pekka Paalanen8274d902014-08-06 19:36:51 +03002364 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002365
Tiago Vignattibc052c92012-04-19 16:18:18 +03002366 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002367 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002368 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002369 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002370 shsurf->desktop_surface = desktop_surface;
2371 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002373 wl_list_init(&shsurf->fullscreen.transform.link);
2374
Semi Malinen99f8c082018-05-02 11:10:32 +02002375 shell_surface_set_output(
2376 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002377
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002378 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002379
Pekka Paalanen460099f2012-01-20 16:48:25 +02002380 /* empty when not in use */
2381 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002382 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002383
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002384 wl_list_init(&shsurf->workspace_transform.link);
2385
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002386 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2387 weston_desktop_surface_set_activated(desktop_surface,
2388 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002389}
2390
Tiago Vignattibc052c92012-04-19 16:18:18 +03002391static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002392desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2393 void *shell)
2394{
2395 struct shell_surface *shsurf =
2396 weston_desktop_surface_get_user_data(desktop_surface);
2397 struct weston_surface *surface =
2398 weston_desktop_surface_get_surface(desktop_surface);
2399
2400 if (!shsurf)
2401 return;
2402
2403 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2404
2405 if (shsurf->fullscreen.black_view)
2406 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2407
2408 weston_surface_set_label_func(surface, NULL);
2409 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2410 shsurf->desktop_surface = NULL;
2411
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002412 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002413 if (weston_surface_is_mapped(surface) &&
2414 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2415 pixman_region32_fini(&surface->pending.input);
2416 pixman_region32_init(&surface->pending.input);
2417 pixman_region32_fini(&surface->input);
2418 pixman_region32_init(&surface->input);
2419 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2420 fade_out_done, shsurf);
2421 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002422 weston_view_destroy(shsurf->view);
2423 free(shsurf);
2424 }
2425}
2426
2427static void
2428set_maximized_position(struct desktop_shell *shell,
2429 struct shell_surface *shsurf)
2430{
2431 pixman_rectangle32_t area;
2432 struct weston_geometry geometry;
2433
2434 get_output_work_area(shell, shsurf->output, &area);
2435 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2436
2437 weston_view_set_position(shsurf->view,
2438 area.x - geometry.x,
2439 area.y - geometry.y);
2440}
2441
2442static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002443set_position_from_xwayland(struct shell_surface *shsurf)
2444{
2445 struct weston_geometry geometry;
2446 float x;
2447 float y;
2448
2449 assert(shsurf->xwayland.is_set);
2450
2451 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2452 x = shsurf->xwayland.x - geometry.x;
2453 y = shsurf->xwayland.y - geometry.y;
2454
2455 weston_view_set_position(shsurf->view, x, y);
2456
2457#ifdef WM_DEBUG
2458 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2459 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2460 geometry.x, geometry.y, x, y);
2461#endif
2462}
2463
2464static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002465map(struct desktop_shell *shell, struct shell_surface *shsurf,
2466 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002467{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002468 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002469 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2470 struct weston_compositor *compositor = shell->compositor;
2471 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002472
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002473 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002474 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002475 center_on_output(shsurf->view, shsurf->fullscreen_output);
2476 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002477 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002478 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002479 } else if (shsurf->xwayland.is_set) {
2480 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002481 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002482 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002483 }
2484
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002485 /* Surface stacking order, see also activate(). */
2486 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002487
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002488 weston_view_update_transform(shsurf->view);
2489 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002490 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002491 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002492 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002493 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002494
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002495 if (!shell->locked) {
2496 wl_list_for_each(seat, &compositor->seat_list, link)
2497 activate(shell, shsurf->view, seat,
2498 WESTON_ACTIVATE_FLAG_CONFIGURE);
2499 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002500
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002501 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002502 switch (shell->win_animation_type) {
2503 case ANIMATION_FADE:
2504 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2505 break;
2506 case ANIMATION_ZOOM:
2507 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2508 break;
2509 case ANIMATION_NONE:
2510 default:
2511 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002512 }
2513 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002514}
2515
2516static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2518 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002519{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002520 struct shell_surface *shsurf =
2521 weston_desktop_surface_get_user_data(desktop_surface);
2522 struct weston_surface *surface =
2523 weston_desktop_surface_get_surface(desktop_surface);
2524 struct weston_view *view = shsurf->view;
2525 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002526 bool was_fullscreen;
2527 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002528
2529 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002530 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002532 was_fullscreen = shsurf->state.fullscreen;
2533 was_maximized = shsurf->state.maximized;
2534
2535 shsurf->state.fullscreen =
2536 weston_desktop_surface_get_fullscreen(desktop_surface);
2537 shsurf->state.maximized =
2538 weston_desktop_surface_get_maximized(desktop_surface);
2539
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002540 if (!weston_surface_is_mapped(surface)) {
2541 map(shell, shsurf, sx, sy);
2542 surface->is_mapped = true;
2543 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2544 ++surface->ref_count;
2545 return;
2546 }
2547
2548 if (sx == 0 && sy == 0 &&
2549 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002550 shsurf->last_height == surface->height &&
2551 was_fullscreen == shsurf->state.fullscreen &&
2552 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002553 return;
2554
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002555 if (was_fullscreen)
2556 unset_fullscreen(shsurf);
2557 if (was_maximized)
2558 unset_maximized(shsurf);
2559
Quentin Glidic920cf042016-08-16 14:26:20 +02002560 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2561 !shsurf->saved_position_valid) {
2562 shsurf->saved_x = shsurf->view->geometry.x;
2563 shsurf->saved_y = shsurf->view->geometry.y;
2564 shsurf->saved_position_valid = true;
2565
2566 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2567 wl_list_remove(&shsurf->rotation.transform.link);
2568 wl_list_init(&shsurf->rotation.transform.link);
2569 weston_view_geometry_dirty(shsurf->view);
2570 shsurf->saved_rotation_valid = true;
2571 }
2572 }
2573
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002574 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002575 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002576 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002577 set_maximized_position(shell, shsurf);
2578 surface->output = shsurf->output;
2579 } else {
2580 float from_x, from_y;
2581 float to_x, to_y;
2582 float x, y;
2583
2584 if (shsurf->resize_edges) {
2585 sx = 0;
2586 sy = 0;
2587 }
2588
2589 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2590 sx = shsurf->last_width - surface->width;
2591 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2592 sy = shsurf->last_height - surface->height;
2593
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002594 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2595 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2596 x = shsurf->view->geometry.x + to_x - from_x;
2597 y = shsurf->view->geometry.y + to_y - from_y;
2598
2599 weston_view_set_position(shsurf->view, x, y);
2600 }
2601
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002602 shsurf->last_width = surface->width;
2603 shsurf->last_height = surface->height;
2604
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002605 /* XXX: would a fullscreen surface need the same handling? */
2606 if (surface->output) {
2607 wl_list_for_each(view, &surface->views, surface_link)
2608 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002609 }
2610}
2611
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002612static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002613get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2614{
2615 struct desktop_shell *shell;
2616 pixman_rectangle32_t area;
2617
2618 shell = shell_surface_get_shell(shsurf);
2619 get_output_work_area(shell, shsurf->output, &area);
2620
2621 *width = area.width;
2622 *height = area.height;
2623}
2624
2625static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002626set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2627 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002628{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002629 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2630 struct weston_surface *surface =
2631 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2632 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002633
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002634 if (fullscreen) {
2635 /* handle clients launching in fullscreen */
2636 if (output == NULL && !weston_surface_is_mapped(surface)) {
2637 /* Set the output to the one that has focus currently. */
2638 output = get_focused_output(surface->compositor);
2639 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002640
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002641 shell_surface_set_output(shsurf, output);
2642 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002643
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002644 width = shsurf->output->width;
2645 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002646 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2647 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002648 }
2649 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2650 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002651}
2652
2653static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002654desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2655 struct weston_seat *seat, uint32_t serial, void *shell)
2656{
2657 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2658 struct weston_touch *touch = weston_seat_get_touch(seat);
2659 struct shell_surface *shsurf =
2660 weston_desktop_surface_get_user_data(desktop_surface);
2661 struct weston_surface *surface =
2662 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2663 struct wl_resource *resource = surface->resource;
2664 struct weston_surface *focus;
2665
2666 if (pointer &&
2667 pointer->focus &&
2668 pointer->button_count > 0 &&
2669 pointer->grab_serial == serial) {
2670 focus = weston_surface_get_main_surface(pointer->focus->surface);
2671 if ((focus == surface) &&
2672 (surface_move(shsurf, pointer, true) < 0))
2673 wl_resource_post_no_memory(resource);
2674 } else if (touch &&
2675 touch->focus &&
2676 touch->grab_serial == serial) {
2677 focus = weston_surface_get_main_surface(touch->focus->surface);
2678 if ((focus == surface) &&
2679 (surface_touch_move(shsurf, touch) < 0))
2680 wl_resource_post_no_memory(resource);
2681 }
2682}
2683
2684static void
2685desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2686 struct weston_seat *seat, uint32_t serial,
2687 enum weston_desktop_surface_edge edges, void *shell)
2688{
2689 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2690 struct shell_surface *shsurf =
2691 weston_desktop_surface_get_user_data(desktop_surface);
2692 struct weston_surface *surface =
2693 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2694 struct wl_resource *resource = surface->resource;
2695 struct weston_surface *focus;
2696
2697 if (!pointer ||
2698 pointer->button_count == 0 ||
2699 pointer->grab_serial != serial ||
2700 pointer->focus == NULL)
2701 return;
2702
2703 focus = weston_surface_get_main_surface(pointer->focus->surface);
2704 if (focus != surface)
2705 return;
2706
2707 if (surface_resize(shsurf, pointer, edges) < 0)
2708 wl_resource_post_no_memory(resource);
2709}
2710
2711static void
2712desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2713 bool fullscreen,
2714 struct weston_output *output, void *shell)
2715{
2716 struct shell_surface *shsurf =
2717 weston_desktop_surface_get_user_data(desktop_surface);
2718
2719 set_fullscreen(shsurf, fullscreen, output);
2720}
2721
2722static void
2723set_maximized(struct shell_surface *shsurf, bool maximized)
2724{
2725 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2726 struct weston_surface *surface =
2727 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2728 int32_t width = 0, height = 0;
2729
2730 if (maximized) {
2731 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002732
2733 if (!weston_surface_is_mapped(surface))
2734 output = get_focused_output(surface->compositor);
2735 else
2736 output = surface->output;
2737
2738 shell_surface_set_output(shsurf, output);
2739
Derek Foreman927d9e22017-10-06 14:37:44 -05002740 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002741 }
2742 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2743 weston_desktop_surface_set_size(desktop_surface, width, height);
2744}
2745
2746static void
2747desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2748 bool maximized, void *shell)
2749{
2750 struct shell_surface *shsurf =
2751 weston_desktop_surface_get_user_data(desktop_surface);
2752
2753 set_maximized(shsurf, maximized);
2754}
2755
2756static void
2757desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2758 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002759{
2760 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002761 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002762
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002763 /* apply compositor's own minimization logic (hide) */
2764 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002765}
2766
Rafael Antognollie2a34552013-12-03 15:35:45 -02002767static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002768set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002769{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002772 if (pointer->grab->interface == &busy_cursor_grab_interface)
2773 return;
2774
2775 grab = malloc(sizeof *grab);
2776 if (!grab)
2777 return;
2778
2779 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2780 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2781 /* Mark the shsurf as ungrabbed so that button binding is able
2782 * to move it. */
2783 shsurf->grabbed = 0;
2784}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002785
2786static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002787end_busy_cursor(struct weston_compositor *compositor,
2788 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002789{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002790 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002791 struct shell_grab *grab;
2792 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002793
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002794 wl_list_for_each(seat, &compositor->seat_list, link) {
2795 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2796 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002797
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798 if (!pointer)
2799 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002800
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002801 if (pointer->grab->interface != &busy_cursor_grab_interface)
2802 continue;
2803
2804 grab = (struct shell_grab *) pointer->grab;
2805 shsurf = grab->shsurf;
2806 if (!shsurf)
2807 continue;
2808
2809 grab_client =
2810 weston_desktop_surface_get_client(shsurf->desktop_surface);
2811 if (grab_client == desktop_client) {
2812 shell_grab_end(grab);
2813 free(grab);
2814 }
2815 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002816}
2817
2818static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002819desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2820 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002821{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822 struct shell_surface *shsurf =
2823 weston_desktop_surface_get_user_data(desktop_surface);
2824 bool *unresponsive = user_data;
2825
2826 shsurf->unresponsive = *unresponsive;
2827}
2828
2829static void
2830desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2831 void *shell_)
2832{
2833 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002834 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002835 struct weston_seat *seat;
2836 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002837
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002838 weston_desktop_client_for_each_surface(desktop_client,
2839 desktop_surface_set_unresponsive,
2840 &unresponsive);
2841
2842
2843 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2844 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2845 struct weston_desktop_client *grab_client;
2846
2847 if (!pointer || !pointer->focus)
2848 continue;
2849
2850 shsurf = get_shell_surface(pointer->focus->surface);
2851 if (!shsurf)
2852 continue;
2853
2854 grab_client =
2855 weston_desktop_surface_get_client(shsurf->desktop_surface);
2856 if (grab_client == desktop_client)
2857 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002858 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002859}
2860
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002861static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002862desktop_surface_pong(struct weston_desktop_client *desktop_client,
2863 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002864{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002865 struct desktop_shell *shell = shell_;
2866 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002867
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002868 weston_desktop_client_for_each_surface(desktop_client,
2869 desktop_surface_set_unresponsive,
2870 &unresponsive);
2871 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002872}
2873
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002874static void
2875desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2876 int32_t x, int32_t y, void *shell_)
2877{
2878 struct shell_surface *shsurf =
2879 weston_desktop_surface_get_user_data(surface);
2880
2881 shsurf->xwayland.x = x;
2882 shsurf->xwayland.y = y;
2883 shsurf->xwayland.is_set = true;
2884}
2885
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002886static const struct weston_desktop_api shell_desktop_api = {
2887 .struct_size = sizeof(struct weston_desktop_api),
2888 .surface_added = desktop_surface_added,
2889 .surface_removed = desktop_surface_removed,
2890 .committed = desktop_surface_committed,
2891 .move = desktop_surface_move,
2892 .resize = desktop_surface_resize,
2893 .fullscreen_requested = desktop_surface_fullscreen_requested,
2894 .maximized_requested = desktop_surface_maximized_requested,
2895 .minimized_requested = desktop_surface_minimized_requested,
2896 .ping_timeout = desktop_surface_ping_timeout,
2897 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002898 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002899};
2900
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002901/* ************************ *
2902 * end of libweston-desktop *
2903 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002904static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002905configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002906{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002908
Semi Malinene7a52fb2018-04-26 11:08:10 +02002909 if (!ev->output)
2910 return;
2911
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002912 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 if (v->output == ev->output && v != ev) {
2914 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002915 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002916 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002917 }
2918 }
2919
Quentin Glidice8bf9592016-06-23 18:55:20 +02002920 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002921 ev->surface->is_mapped = true;
2922 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002923
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002924 if (wl_list_empty(&ev->layer_link.link)) {
2925 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002927 }
2928}
2929
David Fort50d962f2016-06-09 17:55:52 +02002930
2931static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002932find_shell_output_from_weston_output(struct desktop_shell *shell,
2933 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002934{
2935 struct shell_output *shell_output;
2936
2937 wl_list_for_each(shell_output, &shell->output_list, link) {
2938 if (shell_output->output == output)
2939 return shell_output;
2940 }
2941
2942 return NULL;
2943}
2944
Pekka Paalanen8274d902014-08-06 19:36:51 +03002945static int
2946background_get_label(struct weston_surface *surface, char *buf, size_t len)
2947{
2948 return snprintf(buf, len, "background for output %s",
2949 surface->output->name);
2950}
2951
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002952static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002953background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002954{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002955 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002957
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 view = container_of(es->views.next, struct weston_view, surface_link);
2959
Quentin Glidice8bf9592016-06-23 18:55:20 +02002960 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002961}
2962
2963static void
David Fort50d962f2016-06-09 17:55:52 +02002964handle_background_surface_destroy(struct wl_listener *listener, void *data)
2965{
2966 struct shell_output *output =
2967 container_of(listener, struct shell_output, background_surface_listener);
2968
2969 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002970 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002971 output->background_surface = NULL;
2972}
2973
2974static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002975desktop_shell_set_background(struct wl_client *client,
2976 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002977 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002978 struct wl_resource *surface_resource)
2979{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002980 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002981 struct weston_surface *surface =
2982 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002983 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002985
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002986 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002987 wl_resource_post_error(surface_resource,
2988 WL_DISPLAY_ERROR_INVALID_OBJECT,
2989 "surface role already assigned");
2990 return;
2991 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002992
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2994 weston_view_destroy(view);
2995 view = weston_view_create(surface);
2996
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002997 surface->committed = background_committed;
2998 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002999 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003000 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003001 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003002
3003 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003004 if (sh_output->background_surface) {
3005 /* The output already has a background, tell our helper
3006 * there is no need for another one. */
3007 weston_desktop_shell_send_configure(resource, 0,
3008 surface_resource,
3009 0, 0);
3010 } else {
3011 weston_desktop_shell_send_configure(resource, 0,
3012 surface_resource,
3013 surface->output->width,
3014 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003015
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003016 sh_output->background_surface = surface;
3017
3018 sh_output->background_surface_listener.notify =
3019 handle_background_surface_destroy;
3020 wl_signal_add(&surface->destroy_signal,
3021 &sh_output->background_surface_listener);
3022 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003023}
3024
Pekka Paalanen8274d902014-08-06 19:36:51 +03003025static int
3026panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3027{
3028 return snprintf(buf, len, "panel for output %s",
3029 surface->output->name);
3030}
3031
Kristian Høgsberg75840622011-09-06 13:48:16 -04003032static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003033panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003034{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003035 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003036 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003037 int width, height;
3038 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003039
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 view = container_of(es->views.next, struct weston_view, surface_link);
3041
Quentin Glidice8bf9592016-06-23 18:55:20 +02003042 get_panel_size(shell, view, &width, &height);
3043 switch (shell->panel_position) {
3044 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3045 break;
3046 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3047 y = view->output->height - height;
3048 break;
3049 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3050 break;
3051 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3052 x = view->output->width - width;
3053 break;
3054 }
3055
3056 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003057}
3058
3059static void
David Fort50d962f2016-06-09 17:55:52 +02003060handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3061{
3062 struct shell_output *output =
3063 container_of(listener, struct shell_output, panel_surface_listener);
3064
3065 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003066 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003067 output->panel_surface = NULL;
3068}
3069
3070
3071static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003072desktop_shell_set_panel(struct wl_client *client,
3073 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003074 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003075 struct wl_resource *surface_resource)
3076{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003077 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003078 struct weston_surface *surface =
3079 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003081 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003082
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003083 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003084 wl_resource_post_error(surface_resource,
3085 WL_DISPLAY_ERROR_INVALID_OBJECT,
3086 "surface role already assigned");
3087 return;
3088 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003089
Jason Ekstranda7af7042013-10-12 22:38:11 -05003090 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3091 weston_view_destroy(view);
3092 view = weston_view_create(surface);
3093
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003094 surface->committed = panel_committed;
3095 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003096 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003097 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003098 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003099
3100 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003101 if (sh_output->panel_surface) {
3102 /* The output already has a panel, tell our helper
3103 * there is no need for another one. */
3104 weston_desktop_shell_send_configure(resource, 0,
3105 surface_resource,
3106 0, 0);
3107 } else {
3108 weston_desktop_shell_send_configure(resource, 0,
3109 surface_resource,
3110 surface->output->width,
3111 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003112
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003113 sh_output->panel_surface = surface;
3114
3115 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3116 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3117 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003118}
3119
Pekka Paalanen8274d902014-08-06 19:36:51 +03003120static int
3121lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3122{
3123 return snprintf(buf, len, "lock window");
3124}
3125
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003126static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003127lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003128{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003129 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 struct weston_view *view;
3131
3132 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003133
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003134 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003135 return;
3136
Jason Ekstranda7af7042013-10-12 22:38:11 -05003137 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003138
3139 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003140 weston_layer_entry_insert(&shell->lock_layer.view_list,
3141 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003142 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003143 surface->is_mapped = true;
3144 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003145 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003146 }
3147}
3148
3149static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003150handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003151{
Tiago Vignattibe143262012-04-16 17:31:41 +03003152 struct desktop_shell *shell =
3153 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003154
Martin Minarik6d118362012-06-07 18:01:59 +02003155 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003156 shell->lock_surface = NULL;
3157}
3158
3159static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003160desktop_shell_set_lock_surface(struct wl_client *client,
3161 struct wl_resource *resource,
3162 struct wl_resource *surface_resource)
3163{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003164 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003165 struct weston_surface *surface =
3166 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003167
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003168 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003169
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003170 if (!shell->locked)
3171 return;
3172
Pekka Paalanen98262232011-12-01 10:42:22 +02003173 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003174
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003175 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003176 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003177 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003178
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003179 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003180 surface->committed = lock_surface_committed;
3181 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003182 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003183}
3184
3185static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003186resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003187{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003188 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003189
Quentin Glidic82681572016-12-17 13:40:51 +01003190 weston_layer_unset_position(&shell->lock_layer);
3191
3192 if (shell->showing_input_panels)
3193 weston_layer_set_position(&shell->input_panel_layer,
3194 WESTON_LAYER_POSITION_TOP_UI);
3195 weston_layer_set_position(&shell->fullscreen_layer,
3196 WESTON_LAYER_POSITION_FULLSCREEN);
3197 weston_layer_set_position(&shell->panel_layer,
3198 WESTON_LAYER_POSITION_UI);
3199 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003200
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003201 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003202
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003203 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003204 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003205 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003206}
3207
3208static void
3209desktop_shell_unlock(struct wl_client *client,
3210 struct wl_resource *resource)
3211{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003212 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003213
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003214 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003215
3216 if (shell->locked)
3217 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003218}
3219
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003220static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003221desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003222 struct wl_resource *resource,
3223 struct wl_resource *surface_resource)
3224{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003225 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003226
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003227 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003228 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003229}
3230
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003231static void
3232desktop_shell_desktop_ready(struct wl_client *client,
3233 struct wl_resource *resource)
3234{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003235 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003236
3237 shell_fade_startup(shell);
3238}
3239
Jonny Lamb765760d2014-08-20 15:53:19 +02003240static void
3241desktop_shell_set_panel_position(struct wl_client *client,
3242 struct wl_resource *resource,
3243 uint32_t position)
3244{
3245 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3246
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003247 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3248 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3249 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3250 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003251 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003252 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003253 "bad position argument");
3254 return;
3255 }
3256
3257 shell->panel_position = position;
3258}
3259
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003260static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003261 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003262 desktop_shell_set_panel,
3263 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003264 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003265 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003266 desktop_shell_desktop_ready,
3267 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003268};
3269
3270static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003271move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003272 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003273{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003274 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003275 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003276 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003277
Derek Foreman8ae2db52015-07-15 13:00:36 -05003278 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003279 return;
3280
Derek Foreman8ae2db52015-07-15 13:00:36 -05003281 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003282
Pekka Paalanen01388e22013-04-25 13:57:44 +03003283 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003284 if (surface == NULL)
3285 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003286
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003287 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003288 if (shsurf == NULL ||
3289 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3290 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003291 return;
3292
Derek Foreman794fa0e2015-07-15 13:00:38 -05003293 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003294}
3295
3296static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003297maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003298 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003299{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003300 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003301 struct weston_surface *surface;
3302 struct shell_surface *shsurf;
3303
3304 surface = weston_surface_get_main_surface(focus);
3305 if (surface == NULL)
3306 return;
3307
3308 shsurf = get_shell_surface(surface);
3309 if (shsurf == NULL)
3310 return;
3311
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003312 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003313}
3314
3315static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003316fullscreen_binding(struct weston_keyboard *keyboard,
3317 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003318{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003319 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003320 struct weston_surface *surface;
3321 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003323
3324 surface = weston_surface_get_main_surface(focus);
3325 if (surface == NULL)
3326 return;
3327
3328 shsurf = get_shell_surface(surface);
3329 if (shsurf == NULL)
3330 return;
3331
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003332 fullscreen =
3333 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003334
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003335 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003336}
3337
3338static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003339touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003340{
Derek Foreman362656b2014-09-04 10:23:05 -05003341 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003342 struct weston_surface *surface;
3343 struct shell_surface *shsurf;
3344
Derek Foreman8ae2db52015-07-15 13:00:36 -05003345 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003346 return;
3347
Derek Foreman8ae2db52015-07-15 13:00:36 -05003348 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003349 surface = weston_surface_get_main_surface(focus);
3350 if (surface == NULL)
3351 return;
3352
3353 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003354 if (shsurf == NULL ||
3355 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3356 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003357 return;
3358
Derek Foremanb7674ae2015-07-15 13:00:37 -05003359 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003360}
3361
3362static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003363resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003364 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003365{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003366 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003367 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003368 uint32_t edges = 0;
3369 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003370 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003371
Derek Foreman8ae2db52015-07-15 13:00:36 -05003372 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003373 return;
3374
Derek Foreman8ae2db52015-07-15 13:00:36 -05003375 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003376
Pekka Paalanen01388e22013-04-25 13:57:44 +03003377 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003378 if (surface == NULL)
3379 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003380
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003381 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003382 if (shsurf == NULL ||
3383 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3384 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003385 return;
3386
Jason Ekstranda7af7042013-10-12 22:38:11 -05003387 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003388 wl_fixed_to_int(pointer->grab_x),
3389 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003390 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003391
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003392 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003393 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003394 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003395 edges |= 0;
3396 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003397 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003398
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003399 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003400 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003401 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003402 edges |= 0;
3403 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003404 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003405
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003406 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003407}
3408
3409static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003410surface_opacity_binding(struct weston_pointer *pointer,
3411 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003412 struct weston_pointer_axis_event *event,
3413 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003414{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003415 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003416 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003417 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003418 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003419
Pekka Paalanen01388e22013-04-25 13:57:44 +03003420 /* XXX: broken for windows containing sub-surfaces */
3421 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003422 if (surface == NULL)
3423 return;
3424
3425 shsurf = get_shell_surface(surface);
3426 if (!shsurf)
3427 return;
3428
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003429 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003430
Jason Ekstranda7af7042013-10-12 22:38:11 -05003431 if (shsurf->view->alpha > 1.0)
3432 shsurf->view->alpha = 1.0;
3433 if (shsurf->view->alpha < step)
3434 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003435
Jason Ekstranda7af7042013-10-12 22:38:11 -05003436 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003437 weston_surface_damage(surface);
3438}
3439
3440static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003441do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3442 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003443{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003444 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003445 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003446 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003447 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003448
Derek Foreman1281a362015-07-31 16:55:32 -05003449 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003450 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3451 return;
3452 }
3453
Scott Moreauccbf29d2012-02-22 14:21:41 -07003454 wl_list_for_each(output, &compositor->output_list, link) {
3455 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003456 wl_fixed_to_double(pointer->x),
3457 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003458 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003459 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003460 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003461 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003462 increment = -output->zoom.increment;
3463 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003464 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003465 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003466 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003467 else
3468 increment = 0;
3469
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003470 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003471
Scott Moreaue6603982012-06-11 13:07:51 -06003472 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003473 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003474 else if (output->zoom.level > output->zoom.max_level)
3475 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003476
3477 if (!output->zoom.active) {
3478 if (output->zoom.level <= 0.0)
3479 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003480 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003481 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003482
Scott Moreaue6603982012-06-11 13:07:51 -06003483 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003484
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003486 }
3487 }
3488}
3489
Scott Moreauccbf29d2012-02-22 14:21:41 -07003490static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003491zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003492 struct weston_pointer_axis_event *event,
3493 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003494{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003495 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003496}
3497
3498static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003499zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003500 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003501{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003502 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003503}
3504
3505static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003506terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003507 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003509 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003510
Daniel Stone325fc2d2012-05-30 16:31:58 +01003511 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003512}
3513
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003514static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003515rotate_grab_motion(struct weston_pointer_grab *grab,
3516 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003517 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003518{
3519 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003520 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003521 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003522 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003523 struct weston_surface *surface =
3524 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003525 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003526
Jonas Ådahld2510102014-10-05 21:39:14 +02003527 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003528
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003529 if (!shsurf)
3530 return;
3531
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003532 cx = 0.5f * surface->width;
3533 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003534
Daniel Stone37816df2012-05-16 18:45:18 +01003535 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3536 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003537 r = sqrtf(dx * dx + dy * dy);
3538
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003539 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003541
3542 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003543 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003544 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003545
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003546 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003547 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548
3549 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003550 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003551 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003552 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003553 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003554
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003555 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003556 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003557 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003559 wl_list_init(&shsurf->rotation.transform.link);
3560 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003561 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003562 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003563
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003564 /* We need to adjust the position of the surface
3565 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003566 cposx = shsurf->view->geometry.x + cx;
3567 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003568 dposx = rotate->center.x - cposx;
3569 dposy = rotate->center.y - cposy;
3570 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003571 weston_view_set_position(shsurf->view,
3572 shsurf->view->geometry.x + dposx,
3573 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003574 }
3575
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003576 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003577 * lazily applies the damage due to rotation update.
3578 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003579 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580}
3581
3582static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003583rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003584 const struct timespec *time,
3585 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003586{
3587 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003588 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003589 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003590 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003591 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003592
Daniel Stone4dbadb12012-05-30 16:31:51 +01003593 if (pointer->button_count == 0 &&
3594 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003595 if (shsurf)
3596 weston_matrix_multiply(&shsurf->rotation.rotation,
3597 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003598 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003599 free(rotate);
3600 }
3601}
3602
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003603static void
3604rotate_grab_cancel(struct weston_pointer_grab *grab)
3605{
3606 struct rotate_grab *rotate =
3607 container_of(grab, struct rotate_grab, base.grab);
3608
3609 shell_grab_end(&rotate->base);
3610 free(rotate);
3611}
3612
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003613static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003614 noop_grab_focus,
3615 rotate_grab_motion,
3616 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003617 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003618 noop_grab_axis_source,
3619 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003620 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003621};
3622
3623static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003624surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003626 struct weston_surface *surface =
3627 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003628 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003629 float dx, dy;
3630 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003631
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632 rotate = malloc(sizeof *rotate);
3633 if (!rotate)
3634 return;
3635
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003636 weston_view_to_global_float(shsurf->view,
3637 surface->width * 0.5f,
3638 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003640
Derek Foreman74de4692015-07-15 13:00:39 -05003641 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3642 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003643 r = sqrtf(dx * dx + dy * dy);
3644 if (r > 20.0f) {
3645 struct weston_matrix inverse;
3646
3647 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003648 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003649 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003650
3651 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003652 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003653 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003654 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003655 weston_matrix_init(&rotate->rotation);
3656 }
3657
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003658 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003659 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660}
3661
3662static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003663rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3664 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003665{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003666 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003667 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003668 struct shell_surface *surface;
3669
Derek Foreman8ae2db52015-07-15 13:00:36 -05003670 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003671 return;
3672
Derek Foreman8ae2db52015-07-15 13:00:36 -05003673 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003674
Pekka Paalanen01388e22013-04-25 13:57:44 +03003675 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003676 if (base_surface == NULL)
3677 return;
3678
3679 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003680 if (surface == NULL ||
3681 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3682 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003683 return;
3684
Derek Foreman74de4692015-07-15 13:00:39 -05003685 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003686}
3687
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003688/* Move all fullscreen layers down to the current workspace and hide their
3689 * black views. The surfaces' state is set to both fullscreen and lowered,
3690 * and this is reversed when such a surface is re-configured, see
3691 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3692 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003693 * lowering_output = NULL - Lower on all outputs, else only lower on the
3694 * specified output.
3695 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003696 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003697 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003698void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003699lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003700 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003701{
3702 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003703 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003704
3705 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003707 &shell->fullscreen_layer.view_list.link,
3708 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003709 struct shell_surface *shsurf = get_shell_surface(view->surface);
3710
3711 if (!shsurf)
3712 continue;
3713
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003714 /* Only lower surfaces which have lowering_output as their fullscreen
3715 * output, unless a NULL output asks for lowering on all outputs.
3716 */
3717 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3718 continue;
3719
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003720 /* We can have a non-fullscreen popup for a fullscreen surface
3721 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003722 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003723 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003724 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3725 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003726 weston_view_damage_below(shsurf->fullscreen.black_view);
3727
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003728 }
3729
3730 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003731 weston_layer_entry_remove(&view->layer_link);
3732 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003733 weston_view_damage_below(view);
3734 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003735
3736 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003737 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003738}
3739
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003740void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003741activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003742 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003743{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003744 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003745 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003746 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003747 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003748 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003749 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003750
Pekka Paalanen01388e22013-04-25 13:57:44 +03003751 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003752 shsurf = get_shell_surface(main_surface);
3753 assert(shsurf);
3754
3755 /* Only demote fullscreen surfaces on the output of activated shsurf.
3756 * Leave fullscreen surfaces on unrelated outputs alone. */
3757 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003758
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003759 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003760
Jonas Ådahl8538b222012-08-29 22:13:03 +02003761 state = ensure_focus_state(shell, seat);
3762 if (state == NULL)
3763 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003764
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003765 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003766 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003767
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003768 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3769 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003770 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003771
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003772 /* Update the surface’s layer. This brings it to the top of the stacking
3773 * order as appropriate. */
3774 shell_surface_update_layer(shsurf);
3775
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003776 if (shell->focus_animation_type != ANIMATION_NONE) {
3777 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003778 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003779 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003780}
3781
Alex Wu21858432012-04-01 20:13:08 +08003782/* no-op func for checking black surface */
3783static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003784black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003785{
3786}
3787
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003788static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003789is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003790{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003791 struct weston_surface *surface = view->surface;
3792
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003793 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003794 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003795 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003796 return true;
3797 }
3798 return false;
3799}
3800
Kristian Høgsberg75840622011-09-06 13:48:16 -04003801static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003802activate_binding(struct weston_seat *seat,
3803 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003804 struct weston_view *focus_view,
3805 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003806{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003807 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003808 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003809
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003810 if (!focus_view)
3811 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003812
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003813 if (is_black_surface_view(focus_view, &main_view))
3814 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003815
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003816 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003817 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003818 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003819
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003820 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003821}
3822
3823static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003824click_to_activate_binding(struct weston_pointer *pointer,
3825 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003826 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003827{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003828 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003829 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003830 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003831 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003832
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003833 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003834 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003835 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003836}
3837
3838static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003839touch_to_activate_binding(struct weston_touch *touch,
3840 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003841 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003842{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003843 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003844 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003845 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003846 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003847
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003848 activate_binding(touch->seat, data, touch->focus,
3849 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003850}
3851
3852static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003853unfocus_all_seats(struct desktop_shell *shell)
3854{
3855 struct weston_seat *seat, *next;
3856
3857 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003858 struct weston_keyboard *keyboard =
3859 weston_seat_get_keyboard(seat);
3860
3861 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003862 continue;
3863
Derek Foreman1281a362015-07-31 16:55:32 -05003864 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003865 }
3866}
3867
3868static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003869lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003870{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003871 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003872
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003873 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003874 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003875 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003876 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003877
3878 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003879
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003880 /* Hide all surfaces by removing the fullscreen, panel and
3881 * toplevel layers. This way nothing else can show or receive
3882 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003883
Quentin Glidic82681572016-12-17 13:40:51 +01003884 weston_layer_unset_position(&shell->panel_layer);
3885 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003886 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003887 weston_layer_unset_position(&shell->input_panel_layer);
3888 weston_layer_unset_position(&ws->layer);
3889
3890 weston_layer_set_position(&shell->lock_layer,
3891 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003892
Derek Foreman004b4a12015-07-20 16:28:13 -05003893 weston_compositor_sleep(shell->compositor);
3894
Neil Robertsb4a91702014-04-09 16:33:32 +01003895 /* Remove the keyboard focus on all seats. This will be
3896 * restored to the workspace's saved state via
3897 * restore_focus_state when the compositor is unlocked */
3898 unfocus_all_seats(shell);
3899
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003900 /* TODO: disable bindings that should not work while locked. */
3901
3902 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003903}
3904
3905static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003906unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003907{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003908 struct wl_resource *shell_resource;
3909
Pekka Paalanend81c2162011-11-16 13:47:34 +02003910 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003911 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003912 return;
3913 }
3914
3915 /* If desktop-shell client has gone away, unlock immediately. */
3916 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003917 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003918 return;
3919 }
3920
3921 if (shell->prepare_event_sent)
3922 return;
3923
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003924 shell_resource = shell->child.desktop_shell;
3925 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003926 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003927}
3928
3929static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003930shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003931{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003932 struct shell_output *shell_output = data;
3933 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003934
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003935 shell_output->fade.animation = NULL;
3936 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003937 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003938 weston_surface_destroy(shell_output->fade.view->surface);
3939 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003940 break;
3941 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003942 lock(shell);
3943 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003944 default:
3945 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003946 }
3947}
3948
Jason Ekstranda7af7042013-10-12 22:38:11 -05003949static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003950shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003951{
3952 struct weston_compositor *compositor = shell->compositor;
3953 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003955
3956 surface = weston_surface_create(compositor);
3957 if (!surface)
3958 return NULL;
3959
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 view = weston_view_create(surface);
3961 if (!view) {
3962 weston_surface_destroy(surface);
3963 return NULL;
3964 }
3965
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003966 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3967 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003968 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003969 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3970 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003971 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003972 surface->is_mapped = true;
3973 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003974
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003976}
3977
3978static void
3979shell_fade(struct desktop_shell *shell, enum fade_type type)
3980{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003981 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003982 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003983
3984 switch (type) {
3985 case FADE_IN:
3986 tint = 0.0;
3987 break;
3988 case FADE_OUT:
3989 tint = 1.0;
3990 break;
3991 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003992 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993 return;
3994 }
3995
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003996 /* Create a separate fade surface for each output */
3997 wl_list_for_each(shell_output, &shell->output_list, link) {
3998 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003999
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004000 if (shell_output->fade.view == NULL) {
4001 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4002 if (!shell_output->fade.view)
4003 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004004
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004005 shell_output->fade.view->alpha = 1.0 - tint;
4006 weston_view_update_transform(shell_output->fade.view);
4007 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004008
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004009 if (shell_output->fade.view->output == NULL) {
4010 /* If the black view gets a NULL output, we lost the
4011 * last output and we'll just cancel the fade. This
4012 * happens when you close the last window under the
4013 * X11 or Wayland backends. */
4014 shell->locked = false;
4015 weston_surface_destroy(shell_output->fade.view->surface);
4016 shell_output->fade.view = NULL;
4017 } else if (shell_output->fade.animation) {
4018 weston_fade_update(shell_output->fade.animation, tint);
4019 } else {
4020 shell_output->fade.animation =
4021 weston_fade_run(shell_output->fade.view,
4022 1.0 - tint, tint, 300.0,
4023 shell_fade_done_for_output, shell_output);
4024 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004025 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004026}
4027
4028static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004029do_shell_fade_startup(void *data)
4030{
4031 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004032 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004033
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004034 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004035 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004036 } else {
4037 weston_log("desktop shell: "
4038 "unexpected fade-in animation type %d\n",
4039 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004040 wl_list_for_each(shell_output, &shell->output_list, link) {
4041 weston_surface_destroy(shell_output->fade.view->surface);
4042 shell_output->fade.view = NULL;
4043 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004044 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004045}
4046
4047static void
4048shell_fade_startup(struct desktop_shell *shell)
4049{
4050 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004051 struct shell_output *shell_output;
4052 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004053
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004054 wl_list_for_each(shell_output, &shell->output_list, link) {
4055 if (!shell_output->fade.startup_timer)
4056 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004057
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004058 wl_event_source_remove(shell_output->fade.startup_timer);
4059 shell_output->fade.startup_timer = NULL;
4060 has_fade = true;
4061 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004062
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004063 if (has_fade) {
4064 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4065 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4066 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004067}
4068
4069static int
4070fade_startup_timeout(void *data)
4071{
4072 struct desktop_shell *shell = data;
4073
4074 shell_fade_startup(shell);
4075 return 0;
4076}
4077
4078static void
4079shell_fade_init(struct desktop_shell *shell)
4080{
4081 /* Make compositor output all black, and wait for the desktop-shell
4082 * client to signal it is ready, then fade in. The timer triggers a
4083 * fade-in, in case the desktop-shell client takes too long.
4084 */
4085
4086 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004087 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004088
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004089 if (shell->startup_animation_type == ANIMATION_NONE)
4090 return;
4091
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004092 wl_list_for_each(shell_output, &shell->output_list, link) {
4093 if (shell_output->fade.view != NULL) {
4094 weston_log("%s: warning: fade surface already exists\n",
4095 __func__);
4096 continue;
4097 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004098
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004099 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4100 if (!shell_output->fade.view)
4101 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004102
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004103 weston_view_update_transform(shell_output->fade.view);
4104 weston_surface_damage(shell_output->fade.view->surface);
4105
4106 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4107 shell_output->fade.startup_timer =
4108 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4109 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4110 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004111}
4112
4113static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004114idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004115{
4116 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004117 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004118
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004119 struct weston_seat *seat;
4120
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004121 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4122 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004123
4124 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004125 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004126}
4127
4128static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004129wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004130{
4131 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004132 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004133
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004134 unlock(shell);
4135}
4136
4137static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004138transform_handler(struct wl_listener *listener, void *data)
4139{
4140 struct weston_surface *surface = data;
4141 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004142 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004143 int x, y;
4144
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004145 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004146 return;
4147
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004148 api = shsurf->shell->xwayland_surface_api;
4149 if (!api) {
4150 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4151 shsurf->shell->xwayland_surface_api = api;
4152 }
4153
4154 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004155 return;
4156
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004157 if (!weston_view_is_mapped(shsurf->view))
4158 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004159
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004160 x = shsurf->view->geometry.x;
4161 y = shsurf->view->geometry.y;
4162
4163 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004164}
4165
4166static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004167center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004168{
Giulio Camuffob8366642013-04-25 13:57:46 +03004169 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004170 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004171
Jason Ekstranda7af7042013-10-12 22:38:11 -05004172 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004173
4174 x = output->x + (output->width - width) / 2 - surf_x / 2;
4175 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004176
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004177 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004178}
4179
4180static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004181weston_view_set_initial_position(struct weston_view *view,
4182 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004183{
4184 struct weston_compositor *compositor = shell->compositor;
4185 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004186 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004187 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004188 struct weston_output *output, *target_output = NULL;
4189 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004190 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004191
4192 /* As a heuristic place the new window on the same output as the
4193 * pointer. Falling back to the output containing 0, 0.
4194 *
4195 * TODO: Do something clever for touch too?
4196 */
4197 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004198 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4199
4200 if (pointer) {
4201 ix = wl_fixed_to_int(pointer->x);
4202 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004203 break;
4204 }
4205 }
4206
4207 wl_list_for_each(output, &compositor->output_list, link) {
4208 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4209 target_output = output;
4210 break;
4211 }
4212 }
4213
4214 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004215 weston_view_set_position(view, 10 + random() % 400,
4216 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004217 return;
4218 }
4219
4220 /* Valid range within output where the surface will still be onscreen.
4221 * If this is negative it means that the surface is bigger than
4222 * output.
4223 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004224 get_output_work_area(shell, target_output, &area);
4225
Derek Foremanf814c5d2015-04-15 12:23:54 -05004226 x = area.x;
4227 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004228 range_x = area.width - view->surface->width;
4229 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004230
Rob Bradford4cb88c72012-08-13 15:18:44 +01004231 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004232 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004233
4234 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004235 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004236
Jason Ekstranda7af7042013-10-12 22:38:11 -05004237 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004238}
4239
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004240static bool
4241check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4242{
4243 struct timespec now;
4244
4245 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4246 return false;
4247
4248 /*
4249 * If the shell helper client dies before the session has been
4250 * up for roughly 30 seconds, better just make Weston shut down,
4251 * because the user likely has no way to interact with the desktop
4252 * anyway.
4253 */
4254 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4255 weston_log("Error: %s apparently cannot run at all.\n",
4256 shell->client);
4257 weston_log_continue(STAMP_SPACE "Quitting...");
4258 wl_display_terminate(shell->compositor->wl_display);
4259
4260 return true;
4261 }
4262
4263 return false;
4264}
4265
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004266static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004267
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004268static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004269respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004270{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004271 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004272
4273 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004274 weston_compositor_get_time(&time);
4275 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004276 shell->child.deathstamp = time;
4277 shell->child.deathcount = 0;
4278 }
4279
4280 shell->child.deathcount++;
4281 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004282 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004283 return;
4284 }
4285
Pekka Paalanen826dc142014-08-27 12:11:53 +03004286 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004287 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004288}
4289
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004290static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004291desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4292{
4293 struct desktop_shell *shell;
4294
4295 shell = container_of(listener, struct desktop_shell,
4296 child.client_destroy_listener);
4297
Pekka Paalanen826dc142014-08-27 12:11:53 +03004298 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004299 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004300 /*
4301 * unbind_desktop_shell() will reset shell->child.desktop_shell
4302 * before the respawned process has a chance to create a new
4303 * desktop_shell object, because we are being called from the
4304 * wl_client destructor which destroys all wl_resources before
4305 * returning.
4306 */
4307
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004308 if (!check_desktop_shell_crash_too_early(shell))
4309 respawn_desktop_shell_process(shell);
4310
Pekka Paalanen826dc142014-08-27 12:11:53 +03004311 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004312}
4313
4314static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004315launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004316{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004317 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004318
Pekka Paalanen826dc142014-08-27 12:11:53 +03004319 shell->child.client = weston_client_start(shell->compositor,
4320 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004321
Arnaud Vrac42631192014-08-25 20:56:46 +02004322 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004323 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004324 return;
4325 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004326
4327 shell->child.client_destroy_listener.notify =
4328 desktop_shell_client_destroy;
4329 wl_client_add_destroy_listener(shell->child.client,
4330 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004331}
4332
4333static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004334unbind_desktop_shell(struct wl_resource *resource)
4335{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004336 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004337
4338 if (shell->locked)
4339 resume_desktop(shell);
4340
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004341 shell->child.desktop_shell = NULL;
4342 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004343}
4344
4345static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004346bind_desktop_shell(struct wl_client *client,
4347 void *data, uint32_t version, uint32_t id)
4348{
Tiago Vignattibe143262012-04-16 17:31:41 +03004349 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004350 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004351
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004352 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4353 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004354
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004355 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004356 wl_resource_set_implementation(resource,
4357 &desktop_shell_implementation,
4358 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004359 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004360 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004361 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004362
4363 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4364 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004365}
4366
Kristian Høgsberg07045392012-02-19 18:52:44 -05004367struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004368 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004369 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004371 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004372 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373};
4374
4375static void
4376switcher_next(struct switcher *switcher)
4377{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004379 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004380 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004381 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004382
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004383 /* temporary re-display minimized surfaces */
4384 struct weston_view *tmp;
4385 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004386 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4387 weston_layer_entry_remove(&view->layer_link);
4388 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004389 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4390 *minimized = view;
4391 }
4392
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004393 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004394 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004395 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004396 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004397 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004398 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004399 next = view;
4400 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 view->alpha = 0.25;
4402 weston_view_geometry_dirty(view);
4403 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004404 }
Alex Wu1659daa2012-04-01 20:13:09 +08004405
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004406 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 view->alpha = 0.25;
4408 weston_view_geometry_dirty(view);
4409 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004410 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004411 }
4412
4413 if (next == NULL)
4414 next = first;
4415
Alex Wu07b26062012-03-12 16:06:01 +08004416 if (next == NULL)
4417 return;
4418
Kristian Høgsberg07045392012-02-19 18:52:44 -05004419 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004420 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004421
4422 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004423 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004425
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004426 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004427 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004428 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004429}
4430
4431static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004432switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004433{
4434 struct switcher *switcher =
4435 container_of(listener, struct switcher, listener);
4436
4437 switcher_next(switcher);
4438}
4439
4440static void
Daniel Stone351eb612012-05-31 15:27:47 -04004441switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004442{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004444 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004445 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004446
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004447 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004448 if (is_focus_view(view))
4449 continue;
4450
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 view->alpha = 1.0;
4452 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004453 }
4454
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004455 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004456 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004457 keyboard->seat,
4458 WESTON_ACTIVATE_FLAG_CONFIGURE);
4459 }
4460
Kristian Høgsberg07045392012-02-19 18:52:44 -05004461 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004462 weston_keyboard_end_grab(keyboard);
4463 if (keyboard->input_method_resource)
4464 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004465
4466 /* re-hide surfaces that were temporary shown during the switch */
4467 struct weston_view **minimized;
4468 wl_array_for_each(minimized, &switcher->minimized_array) {
4469 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004470 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004471 weston_layer_entry_remove(&(*minimized)->layer_link);
4472 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004473 weston_view_damage_below(*minimized);
4474 }
4475 }
4476 wl_array_release(&switcher->minimized_array);
4477
Kristian Høgsberg07045392012-02-19 18:52:44 -05004478 free(switcher);
4479}
4480
4481static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004482switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004483 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004484{
4485 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004486 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004487
Daniel Stonec9785ea2012-05-30 16:31:52 +01004488 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004489 switcher_next(switcher);
4490}
4491
4492static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004493switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004494 uint32_t mods_depressed, uint32_t mods_latched,
4495 uint32_t mods_locked, uint32_t group)
4496{
4497 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004498 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004499
Daniel Stone351eb612012-05-31 15:27:47 -04004500 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4501 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004502}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004504static void
4505switcher_cancel(struct weston_keyboard_grab *grab)
4506{
4507 struct switcher *switcher = container_of(grab, struct switcher, grab);
4508
4509 switcher_destroy(switcher);
4510}
4511
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004512static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004513 switcher_key,
4514 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004515 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004516};
4517
4518static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004519switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004520 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004521{
Tiago Vignattibe143262012-04-16 17:31:41 +03004522 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004523 struct switcher *switcher;
4524
4525 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004526 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004527 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004528 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004529 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004530 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004531
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004532 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004534 weston_keyboard_start_grab(keyboard, &switcher->grab);
4535 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004536 switcher_next(switcher);
4537}
4538
Pekka Paalanen3c647232011-12-22 13:43:43 +02004539static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004540backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004541 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004542{
4543 struct weston_compositor *compositor = data;
4544 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004545 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004546
4547 /* TODO: we're limiting to simple use cases, where we assume just
4548 * control on the primary display. We'd have to extend later if we
4549 * ever get support for setting backlights on random desktop LCD
4550 * panels though */
4551 output = get_default_output(compositor);
4552 if (!output)
4553 return;
4554
4555 if (!output->set_backlight)
4556 return;
4557
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004558 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4559 backlight_new = output->backlight_current - 25;
4560 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4561 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004562
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004563 if (backlight_new < 5)
4564 backlight_new = 5;
4565 if (backlight_new > 255)
4566 backlight_new = 255;
4567
4568 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004569 output->set_backlight(output, output->backlight_current);
4570}
4571
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004572static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004573force_kill_binding(struct weston_keyboard *keyboard,
4574 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004575{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004576 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004577 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004578 struct desktop_shell *shell = data;
4579 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004580 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004581
Derek Foreman8ae2db52015-07-15 13:00:36 -05004582 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004583 if (!focus_surface)
4584 return;
4585
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004586 wl_signal_emit(&compositor->kill_signal, focus_surface);
4587
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004588 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004589 wl_client_get_credentials(client, &pid, NULL, NULL);
4590
4591 /* Skip clients that we launched ourselves (the credentials of
4592 * the socketpair is ours) */
4593 if (pid == getpid())
4594 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004595
Daniel Stone325fc2d2012-05-30 16:31:58 +01004596 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004597}
4598
4599static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004600workspace_up_binding(struct weston_keyboard *keyboard,
4601 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004602{
4603 struct desktop_shell *shell = data;
4604 unsigned int new_index = shell->workspaces.current;
4605
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004606 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004607 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004608 if (new_index != 0)
4609 new_index--;
4610
4611 change_workspace(shell, new_index);
4612}
4613
4614static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004615workspace_down_binding(struct weston_keyboard *keyboard,
4616 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004617{
4618 struct desktop_shell *shell = data;
4619 unsigned int new_index = shell->workspaces.current;
4620
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004621 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004622 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004623 if (new_index < shell->workspaces.num - 1)
4624 new_index++;
4625
4626 change_workspace(shell, new_index);
4627}
4628
4629static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004630workspace_f_binding(struct weston_keyboard *keyboard,
4631 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004632{
4633 struct desktop_shell *shell = data;
4634 unsigned int new_index;
4635
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004636 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004637 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004638 new_index = key - KEY_F1;
4639 if (new_index >= shell->workspaces.num)
4640 new_index = shell->workspaces.num - 1;
4641
4642 change_workspace(shell, new_index);
4643}
4644
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004645static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004646workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004647 const struct timespec *time, uint32_t key,
4648 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004649{
4650 struct desktop_shell *shell = data;
4651 unsigned int new_index = shell->workspaces.current;
4652
4653 if (shell->locked)
4654 return;
4655
4656 if (new_index != 0)
4657 new_index--;
4658
Derek Foreman8ae2db52015-07-15 13:00:36 -05004659 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004660}
4661
4662static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004663workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004664 const struct timespec *time, uint32_t key,
4665 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004666{
4667 struct desktop_shell *shell = data;
4668 unsigned int new_index = shell->workspaces.current;
4669
4670 if (shell->locked)
4671 return;
4672
4673 if (new_index < shell->workspaces.num - 1)
4674 new_index++;
4675
Derek Foreman8ae2db52015-07-15 13:00:36 -05004676 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004677}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004678
4679static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004680shell_reposition_view_on_output_destroy(struct weston_view *view)
4681{
4682 struct weston_output *output, *first_output;
4683 struct weston_compositor *ec = view->surface->compositor;
4684 struct shell_surface *shsurf;
4685 float x, y;
4686 int visible;
4687
4688 x = view->geometry.x;
4689 y = view->geometry.y;
4690
4691 /* At this point the destroyed output is not in the list anymore.
4692 * If the view is still visible somewhere, we leave where it is,
4693 * otherwise, move it to the first output. */
4694 visible = 0;
4695 wl_list_for_each(output, &ec->output_list, link) {
4696 if (pixman_region32_contains_point(&output->region,
4697 x, y, NULL)) {
4698 visible = 1;
4699 break;
4700 }
4701 }
4702
4703 if (!visible) {
4704 first_output = container_of(ec->output_list.next,
4705 struct weston_output, link);
4706
4707 x = first_output->x + first_output->width / 4;
4708 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004709
4710 weston_view_set_position(view, x, y);
4711 } else {
4712 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004713 }
4714
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004715
4716 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004717 if (!shsurf)
4718 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004719
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004720 shsurf->saved_position_valid = false;
4721 set_maximized(shsurf, false);
4722 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004723}
4724
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004725void
4726shell_for_each_layer(struct desktop_shell *shell,
4727 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004728{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004729 struct workspace **ws;
4730
4731 func(shell, &shell->fullscreen_layer, data);
4732 func(shell, &shell->panel_layer, data);
4733 func(shell, &shell->background_layer, data);
4734 func(shell, &shell->lock_layer, data);
4735 func(shell, &shell->input_panel_layer, data);
4736
4737 wl_array_for_each(ws, &shell->workspaces.array)
4738 func(shell, &(*ws)->layer, data);
4739}
4740
4741static void
4742shell_output_destroy_move_layer(struct desktop_shell *shell,
4743 struct weston_layer *layer,
4744 void *data)
4745{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004746 struct weston_view *view;
4747
Marius Vladea40d6d2018-02-08 18:46:42 +02004748 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004749 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004750}
4751
4752static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004753handle_output_destroy(struct wl_listener *listener, void *data)
4754{
4755 struct shell_output *output_listener =
4756 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004757 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004758
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004759 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004760
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004761 if (output_listener->panel_surface)
4762 wl_list_remove(&output_listener->panel_surface_listener.link);
4763 if (output_listener->background_surface)
4764 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004765 wl_list_remove(&output_listener->destroy_listener.link);
4766 wl_list_remove(&output_listener->link);
4767 free(output_listener);
4768}
4769
4770static void
David Fort50d962f2016-06-09 17:55:52 +02004771shell_resize_surface_to_output(struct desktop_shell *shell,
4772 struct weston_surface *surface,
4773 const struct weston_output *output)
4774{
4775 if (!surface)
4776 return;
4777
4778 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4779 surface->resource,
4780 output->width,
4781 output->height);
4782}
4783
4784
4785static void
4786handle_output_resized(struct wl_listener *listener, void *data)
4787{
4788 struct desktop_shell *shell =
4789 container_of(listener, struct desktop_shell, resized_listener);
4790 struct weston_output *output = (struct weston_output *)data;
4791 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4792
4793 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4794 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4795}
4796
4797static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004798create_shell_output(struct desktop_shell *shell,
4799 struct weston_output *output)
4800{
4801 struct shell_output *shell_output;
4802
4803 shell_output = zalloc(sizeof *shell_output);
4804 if (shell_output == NULL)
4805 return;
4806
4807 shell_output->output = output;
4808 shell_output->shell = shell;
4809 shell_output->destroy_listener.notify = handle_output_destroy;
4810 wl_signal_add(&output->destroy_signal,
4811 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004812 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004813}
4814
4815static void
4816handle_output_create(struct wl_listener *listener, void *data)
4817{
4818 struct desktop_shell *shell =
4819 container_of(listener, struct desktop_shell, output_create_listener);
4820 struct weston_output *output = (struct weston_output *)data;
4821
4822 create_shell_output(shell, output);
4823}
4824
4825static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004826handle_output_move_layer(struct desktop_shell *shell,
4827 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004828{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004829 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004830 struct weston_view *view;
4831 float x, y;
4832
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004833 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004834 if (view->output != output)
4835 continue;
4836
4837 x = view->geometry.x + output->move_x;
4838 y = view->geometry.y + output->move_y;
4839 weston_view_set_position(view, x, y);
4840 }
4841}
4842
4843static void
4844handle_output_move(struct wl_listener *listener, void *data)
4845{
4846 struct desktop_shell *shell;
4847
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004848 shell = container_of(listener, struct desktop_shell,
4849 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004850
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004851 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004852}
4853
4854static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004855setup_output_destroy_handler(struct weston_compositor *ec,
4856 struct desktop_shell *shell)
4857{
4858 struct weston_output *output;
4859
4860 wl_list_init(&shell->output_list);
4861 wl_list_for_each(output, &ec->output_list, link)
4862 create_shell_output(shell, output);
4863
4864 shell->output_create_listener.notify = handle_output_create;
4865 wl_signal_add(&ec->output_created_signal,
4866 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004867
4868 shell->output_move_listener.notify = handle_output_move;
4869 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004870}
4871
4872static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004873shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004874{
Tiago Vignattibe143262012-04-16 17:31:41 +03004875 struct desktop_shell *shell =
4876 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004877 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004878 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004879
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004880 /* Force state to unlocked so we don't try to fade */
4881 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004882
4883 if (shell->child.client) {
4884 /* disable respawn */
4885 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004886 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004887 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004888
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004889 wl_list_remove(&shell->idle_listener.link);
4890 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004891 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004892
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004893 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004894 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004895
Xiong Zhang6b481422013-10-23 13:58:32 +08004896 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4897 wl_list_remove(&shell_output->destroy_listener.link);
4898 wl_list_remove(&shell_output->link);
4899 free(shell_output);
4900 }
4901
4902 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004903 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004904 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004905
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004906 wl_array_for_each(ws, &shell->workspaces.array)
4907 workspace_destroy(*ws);
4908 wl_array_release(&shell->workspaces.array);
4909
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004910 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004911 free(shell);
4912}
4913
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004914static void
4915shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4916{
4917 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004918 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004919
Bob Ham744e6532016-01-12 10:21:48 +00004920 if (shell->allow_zap)
4921 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4922 MODIFIER_CTRL | MODIFIER_ALT,
4923 terminate_binding, ec);
4924
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004925 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004926 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4927 click_to_activate_binding,
4928 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004929 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4930 click_to_activate_binding,
4931 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004932 weston_compositor_add_touch_binding(ec, 0,
4933 touch_to_activate_binding,
4934 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004935 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4936 backlight_binding, ec);
4937 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4938 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004939
4940 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004941 if (shell->exposay_modifier)
4942 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4943 exposay_binding, shell);
4944
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004945 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004946 if (!mod)
4947 return;
4948
Ian Ray13404c42017-09-18 15:22:01 +03004949 /* This binding is not configurable, but is only enabled if there is a
4950 * valid binding modifier. */
4951 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4952 MODIFIER_SUPER | MODIFIER_ALT,
4953 surface_opacity_binding, NULL);
4954
Ian Rayab7c0b62017-09-18 15:22:00 +03004955 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4956 mod, zoom_axis_binding,
4957 NULL);
4958
Daniel Stone325fc2d2012-05-30 16:31:58 +01004959 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4960 zoom_key_binding, NULL);
4961 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4962 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004963 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4964 maximize_binding, NULL);
4965 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4966 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004967 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4968 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004969 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004970 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004971 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004972 weston_compositor_add_button_binding(ec, BTN_LEFT,
4973 mod | MODIFIER_SHIFT,
4974 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004975
4976 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004977 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004978 rotate_binding, NULL);
4979
Daniel Stone325fc2d2012-05-30 16:31:58 +01004980 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4981 shell);
4982 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4983 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004984 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4985 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004986 weston_compositor_add_key_binding(ec, KEY_K, mod,
4987 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004988 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4989 workspace_up_binding, shell);
4990 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4991 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004992 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4993 workspace_move_surface_up_binding,
4994 shell);
4995 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4996 workspace_move_surface_down_binding,
4997 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004998
4999 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5000 if (shell->workspaces.num > 1) {
5001 num_workspace_bindings = shell->workspaces.num;
5002 if (num_workspace_bindings > 6)
5003 num_workspace_bindings = 6;
5004 for (i = 0; i < num_workspace_bindings; i++)
5005 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5006 workspace_f_binding,
5007 shell);
5008 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005009
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005010 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005011}
5012
Jason Ekstrand024177c2014-04-21 19:42:58 -05005013static void
5014handle_seat_created(struct wl_listener *listener, void *data)
5015{
5016 struct weston_seat *seat = data;
5017
5018 create_shell_seat(seat);
5019}
5020
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005021WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005022wet_shell_init(struct weston_compositor *ec,
5023 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005024{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005025 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005026 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005027 struct workspace **pws;
5028 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005029 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005030
Peter Huttererf3d62272013-08-08 11:57:05 +10005031 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005032 if (shell == NULL)
5033 return -1;
5034
Kristian Høgsberg75840622011-09-06 13:48:16 -04005035 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005036
5037 shell->destroy_listener.notify = shell_destroy;
5038 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005039 shell->idle_listener.notify = idle_handler;
5040 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5041 shell->wake_listener.notify = wake_handler;
5042 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005043 shell->transform_listener.notify = transform_handler;
5044 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005045
Quentin Glidic82681572016-12-17 13:40:51 +01005046 weston_layer_init(&shell->fullscreen_layer, ec);
5047 weston_layer_init(&shell->panel_layer, ec);
5048 weston_layer_init(&shell->background_layer, ec);
5049 weston_layer_init(&shell->lock_layer, ec);
5050 weston_layer_init(&shell->input_panel_layer, ec);
5051
5052 weston_layer_set_position(&shell->fullscreen_layer,
5053 WESTON_LAYER_POSITION_FULLSCREEN);
5054 weston_layer_set_position(&shell->panel_layer,
5055 WESTON_LAYER_POSITION_UI);
5056 weston_layer_set_position(&shell->background_layer,
5057 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005058
5059 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005060 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005061
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005062 if (input_panel_setup(shell) < 0)
5063 return -1;
5064
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005065 shell->text_backend = text_backend_init(ec);
5066 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005067 return -1;
5068
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005069 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005070
Daniel Stonedf8133b2013-11-19 11:37:14 +01005071 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5072 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5073
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005074 for (i = 0; i < shell->workspaces.num; i++) {
5075 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5076 if (pws == NULL)
5077 return -1;
5078
Quentin Glidic82681572016-12-17 13:40:51 +01005079 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005080 if (*pws == NULL)
5081 return -1;
5082 }
5083 activate_workspace(shell, 0);
5084
Quentin Glidic82681572016-12-17 13:40:51 +01005085 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005086
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005087 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005088 wl_list_init(&shell->workspaces.animation.link);
5089 shell->workspaces.animation.frame = animate_workspace_change_frame;
5090
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005091 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5092 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005093 return -1;
5094
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005095 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005096 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005097 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005098 return -1;
5099
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005100 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005101
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005102 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005103
Xiong Zhang6b481422013-10-23 13:58:32 +08005104 setup_output_destroy_handler(ec, shell);
5105
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005106 loop = wl_display_get_event_loop(ec->wl_display);
5107 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005108
Jason Ekstrand024177c2014-04-21 19:42:58 -05005109 wl_list_for_each(seat, &ec->seat_list, link)
5110 handle_seat_created(NULL, seat);
5111 shell->seat_create_listener.notify = handle_seat_created;
5112 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005113
David Fort50d962f2016-06-09 17:55:52 +02005114 shell->resized_listener.notify = handle_output_resized;
5115 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5116
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005117 screenshooter_create(ec);
5118
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005119 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005120
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005121 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005122
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005123 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5124
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005125 return 0;
5126}