blob: fb2d5e8532b6f6a5adcb8fd56ca1d9b5a41675a8 [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
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200345 if (!output) {
346 area->x = 0;
347 area->y = 0;
348 area->width = 0;
349 area->height = 0;
350
351 return;
352 }
353
Derek Foremanf814c5d2015-04-15 12:23:54 -0500354 area->x = output->x;
355 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200356
357 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200358 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500361 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000362 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800363 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200364 area->width = output->width;
365 area->height = output->height - panel_height;
366 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800367 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500368 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000369 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800370 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200371 area->width = output->width - panel_width;
372 area->height = output->height;
373 break;
374 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700375}
376
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400377static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700380 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400381 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700382 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400383
384 if (grab->shsurf->resize_edges) {
385 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400386 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300388
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700389 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300390}
391
392static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700393shell_touch_grab_start(struct shell_touch_grab *grab,
394 const struct weston_touch_grab_interface *interface,
395 struct shell_surface *shsurf,
396 struct weston_touch *touch)
397{
398 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800399
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200400 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700401
Rusty Lynch1084da52013-08-15 09:10:08 -0700402 grab->grab.interface = interface;
403 grab->shsurf = shsurf;
404 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
405 wl_signal_add(&shsurf->destroy_signal,
406 &grab->shsurf_destroy_listener);
407
408 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700409 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700410
411 weston_touch_start_grab(touch, &grab->grab);
412 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500413 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700415}
416
417static void
418shell_touch_grab_end(struct shell_touch_grab *grab)
419{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700420 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700421 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700422 grab->shsurf->grabbed = 0;
423 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700424
425 weston_touch_end_grab(grab->touch);
426}
427
428static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800430 struct weston_output *output);
431
Daniel Stone496ca172012-05-30 16:31:42 +0100432static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300433get_modifier(char *modifier)
434{
435 if (!modifier)
436 return MODIFIER_SUPER;
437
438 if (!strcmp("ctrl", modifier))
439 return MODIFIER_CTRL;
440 else if (!strcmp("alt", modifier))
441 return MODIFIER_ALT;
442 else if (!strcmp("super", modifier))
443 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000444 else if (!strcmp("none", modifier))
445 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300446 else
447 return MODIFIER_SUPER;
448}
449
Juan Zhaoe10d2792012-04-25 19:09:52 +0800450static enum animation_type
451get_animation_type(char *animation)
452{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800453 if (!animation)
454 return ANIMATION_NONE;
455
Juan Zhaoe10d2792012-04-25 19:09:52 +0800456 if (!strcmp("zoom", animation))
457 return ANIMATION_ZOOM;
458 else if (!strcmp("fade", animation))
459 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100460 else if (!strcmp("dim-layer", animation))
461 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800462 else
463 return ANIMATION_NONE;
464}
465
Alex Wu4539b082012-03-01 12:57:46 +0800466static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400467shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200468{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500470 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300471 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000472 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200473
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300474 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400475 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300476 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
477 WESTON_SHELL_CLIENT);
478 if (ret < 0)
479 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400480 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500481 "client", &s, client);
482 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100483 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000484
485 weston_config_section_get_bool(section,
486 "allow-zap", &allow_zap, true);
487 shell->allow_zap = allow_zap;
488
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100489 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400490 "binding-modifier", &s, "super");
491 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200492 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800493
494 weston_config_section_get_string(section,
495 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000496 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800497 free(s);
498
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400499 weston_config_section_get_string(section, "animation", &s, "none");
500 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200501 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200502 weston_config_section_get_string(section, "close-animation", &s, "fade");
503 shell->win_close_animation_type = get_animation_type(s);
504 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700505 weston_config_section_get_string(section,
506 "startup-animation", &s, "fade");
507 shell->startup_animation_type = get_animation_type(s);
508 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700509 if (shell->startup_animation_type == ANIMATION_ZOOM)
510 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511 weston_config_section_get_string(section, "focus-animation", &s, "none");
512 shell->focus_animation_type = get_animation_type(s);
513 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400514 weston_config_section_get_uint(section, "num-workspaces",
515 &shell->workspaces.num,
516 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200517}
518
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800519struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100520get_default_output(struct weston_compositor *compositor)
521{
Armin Krezović10b06182016-06-23 11:59:30 +0200522 if (wl_list_empty(&compositor->output_list))
523 return NULL;
524
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525 return container_of(compositor->output_list.next,
526 struct weston_output, link);
527}
528
Pekka Paalanen8274d902014-08-06 19:36:51 +0300529static int
530focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
531{
532 return snprintf(buf, len, "focus highlight effect for output %s",
533 surface->output->name);
534}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535
536/* no-op func for checking focus surface */
537static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200538focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100539{
540}
541
542static struct focus_surface *
543get_focus_surface(struct weston_surface *surface)
544{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200545 if (surface->committed == focus_surface_committed)
546 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100547 else
548 return NULL;
549}
550
551static bool
552is_focus_surface (struct weston_surface *es)
553{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200554 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100555}
556
557static bool
558is_focus_view (struct weston_view *view)
559{
560 return is_focus_surface (view->surface);
561}
562
563static struct focus_surface *
564create_focus_surface(struct weston_compositor *ec,
565 struct weston_output *output)
566{
567 struct focus_surface *fsurf = NULL;
568 struct weston_surface *surface = NULL;
569
570 fsurf = malloc(sizeof *fsurf);
571 if (!fsurf)
572 return NULL;
573
574 fsurf->surface = weston_surface_create(ec);
575 surface = fsurf->surface;
576 if (surface == NULL) {
577 free(fsurf);
578 return NULL;
579 }
580
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200581 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100582 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200583 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200584 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300585 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800587 fsurf->view = weston_view_create(surface);
588 if (fsurf->view == NULL) {
589 weston_surface_destroy(surface);
590 free(fsurf);
591 return NULL;
592 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200593 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200594 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100595
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600596 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600597 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100598 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
599 pixman_region32_fini(&surface->opaque);
600 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
601 output->width, output->height);
602 pixman_region32_fini(&surface->input);
603 pixman_region32_init(&surface->input);
604
605 wl_list_init(&fsurf->workspace_transform.link);
606
607 return fsurf;
608}
609
610static void
611focus_surface_destroy(struct focus_surface *fsurf)
612{
613 weston_surface_destroy(fsurf->surface);
614 free(fsurf);
615}
616
617static void
618focus_animation_done(struct weston_view_animation *animation, void *data)
619{
620 struct workspace *ws = data;
621
622 ws->focus_animation = NULL;
623}
624
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200625static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200626focus_state_destroy(struct focus_state *state)
627{
628 wl_list_remove(&state->seat_destroy_listener.link);
629 wl_list_remove(&state->surface_destroy_listener.link);
630 free(state);
631}
632
633static void
634focus_state_seat_destroy(struct wl_listener *listener, void *data)
635{
636 struct focus_state *state = container_of(listener,
637 struct focus_state,
638 seat_destroy_listener);
639
640 wl_list_remove(&state->link);
641 focus_state_destroy(state);
642}
643
644static void
645focus_state_surface_destroy(struct wl_listener *listener, void *data)
646{
647 struct focus_state *state = container_of(listener,
648 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400649 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200650 struct weston_surface *main_surface;
651 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500652 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200653
Pekka Paalanen01388e22013-04-25 13:57:44 +0300654 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
655
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300657 wl_list_for_each(view,
658 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400660 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100661 if (is_focus_view(view))
662 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200663 if (!get_shell_surface(view->surface))
664 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400665
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200666 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 break;
668 }
669
Pekka Paalanen01388e22013-04-25 13:57:44 +0300670 /* if the focus was a sub-surface, activate its main surface */
671 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200672 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300673
Kristian Høgsberge3778222012-07-31 17:29:30 -0400674 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100675 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200676 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200677 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400678 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200679 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680 if (state->ws->focus_animation)
681 weston_view_animation_destroy(state->ws->focus_animation);
682
683 state->ws->focus_animation = weston_fade_run(
684 state->ws->fsurf_front->view,
685 state->ws->fsurf_front->view->alpha, 0.0, 300,
686 focus_animation_done, state->ws);
687 }
688
Kristian Høgsberge3778222012-07-31 17:29:30 -0400689 wl_list_remove(&state->link);
690 focus_state_destroy(state);
691 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200692}
693
694static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200695focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
696 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200697{
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200699
700 state = malloc(sizeof *state);
701 if (state == NULL)
702 return NULL;
703
Quentin Glidicfff39812016-08-15 10:56:52 +0200704 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400706 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200707 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400708 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200709
710 state->seat_destroy_listener.notify = focus_state_seat_destroy;
711 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400712 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200713 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400714 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
716 return state;
717}
718
Jonas Ådahl8538b222012-08-29 22:13:03 +0200719static struct focus_state *
720ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
721{
722 struct workspace *ws = get_current_workspace(shell);
723 struct focus_state *state;
724
725 wl_list_for_each(state, &ws->focus_list, link)
726 if (state->seat == seat)
727 break;
728
729 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200730 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200731
732 return state;
733}
734
Jonas Ådahl04769742012-06-13 00:01:24 +0200735static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800736focus_state_set_focus(struct focus_state *state,
737 struct weston_surface *surface)
738{
739 if (state->keyboard_focus) {
740 wl_list_remove(&state->surface_destroy_listener.link);
741 wl_list_init(&state->surface_destroy_listener.link);
742 }
743
744 state->keyboard_focus = surface;
745 if (surface)
746 wl_signal_add(&surface->destroy_signal,
747 &state->surface_destroy_listener);
748}
749
750static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400751restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200752{
753 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400754 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100755 struct wl_list pending_seat_list;
756 struct weston_seat *seat, *next_seat;
757
758 /* Temporarily steal the list of seats so that we can keep
759 * track of the seats we've already processed */
760 wl_list_init(&pending_seat_list);
761 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
762 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200763
764 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500765 struct weston_keyboard *keyboard =
766 weston_seat_get_keyboard(state->seat);
767
Neil Roberts4237f502014-04-09 16:33:31 +0100768 wl_list_remove(&state->seat->link);
769 wl_list_insert(&shell->compositor->seat_list,
770 &state->seat->link);
771
Derek Foreman1281a362015-07-31 16:55:32 -0500772 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800773 continue;
774
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400775 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200778 }
Neil Roberts4237f502014-04-09 16:33:31 +0100779
780 /* For any remaining seats that we don't have a focus state
781 * for we'll reset the keyboard focus to NULL */
782 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500783 struct weston_keyboard *keyboard =
784 weston_seat_get_keyboard(seat);
785
Neil Roberts4237f502014-04-09 16:33:31 +0100786 wl_list_insert(&shell->compositor->seat_list, &seat->link);
787
Derek Foreman1281a362015-07-31 16:55:32 -0500788 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100789 continue;
790
Derek Foreman1281a362015-07-31 16:55:32 -0500791 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100792 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200793}
794
795static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
797 struct weston_seat *seat)
798{
Derek Foreman1281a362015-07-31 16:55:32 -0500799 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200800 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200801
802 wl_list_for_each(state, &ws->focus_list, link) {
803 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500804 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805 return;
806 }
807 }
808}
809
810static void
811drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
812 struct weston_surface *surface)
813{
814 struct focus_state *state;
815
816 wl_list_for_each(state, &ws->focus_list, link)
817 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800818 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819}
820
821static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100822animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
823 struct weston_view *from, struct weston_view *to)
824{
825 struct weston_output *output;
826 bool focus_surface_created = false;
827
828 /* FIXME: Only support dim animation using two layers */
829 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
830 return;
831
832 output = get_default_output(shell->compositor);
833 if (ws->fsurf_front == NULL && (from || to)) {
834 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800835 if (ws->fsurf_front == NULL)
836 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800838
839 ws->fsurf_back = create_focus_surface(shell->compositor, output);
840 if (ws->fsurf_back == NULL) {
841 focus_surface_destroy(ws->fsurf_front);
842 return;
843 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800845
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 focus_surface_created = true;
847 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
849 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 }
851
852 if (ws->focus_animation) {
853 weston_view_animation_destroy(ws->focus_animation);
854 ws->focus_animation = NULL;
855 }
856
857 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300858 weston_layer_entry_insert(&to->layer_link,
859 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&ws->layer.view_list,
862 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863
864 if (focus_surface_created) {
865 ws->focus_animation = weston_fade_run(
866 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200867 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 focus_animation_done, ws);
869 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300870 weston_layer_entry_insert(&from->layer_link,
871 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 ws->focus_animation = weston_stable_fade_run(
873 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200874 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100875 focus_animation_done, ws);
876 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300877 weston_layer_entry_insert(&ws->layer.view_list,
878 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100879 ws->focus_animation = weston_stable_fade_run(
880 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200881 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 focus_animation_done, ws);
883 }
884}
885
886static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887workspace_destroy(struct workspace *ws)
888{
Jonas Ådahl04769742012-06-13 00:01:24 +0200889 struct focus_state *state, *next;
890
891 wl_list_for_each_safe(state, next, &ws->focus_list, link)
892 focus_state_destroy(state);
893
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100894 if (ws->fsurf_front)
895 focus_surface_destroy(ws->fsurf_front);
896 if (ws->fsurf_back)
897 focus_surface_destroy(ws->fsurf_back);
898
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200899 free(ws);
900}
901
Jonas Ådahl04769742012-06-13 00:01:24 +0200902static void
903seat_destroyed(struct wl_listener *listener, void *data)
904{
905 struct weston_seat *seat = data;
906 struct focus_state *state, *next;
907 struct workspace *ws = container_of(listener,
908 struct workspace,
909 seat_destroyed_listener);
910
911 wl_list_for_each_safe(state, next, &ws->focus_list, link)
912 if (state->seat == seat)
913 wl_list_remove(&state->link);
914}
915
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200916static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100917workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918{
919 struct workspace *ws = malloc(sizeof *ws);
920 if (ws == NULL)
921 return NULL;
922
Quentin Glidic82681572016-12-17 13:40:51 +0100923 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924
Jonas Ådahl04769742012-06-13 00:01:24 +0200925 wl_list_init(&ws->focus_list);
926 wl_list_init(&ws->seat_destroyed_listener.link);
927 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100928 ws->fsurf_front = NULL;
929 ws->fsurf_back = NULL;
930 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200931
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932 return ws;
933}
934
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935static int
936workspace_is_empty(struct workspace *ws)
937{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300938 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939}
940
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941static struct workspace *
942get_workspace(struct desktop_shell *shell, unsigned int index)
943{
944 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200945 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200946 pws += index;
947 return *pws;
948}
949
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800950struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200951get_current_workspace(struct desktop_shell *shell)
952{
953 return get_workspace(shell, shell->workspaces.current);
954}
955
956static void
957activate_workspace(struct desktop_shell *shell, unsigned int index)
958{
959 struct workspace *ws;
960
961 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100962 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200963
964 shell->workspaces.current = index;
965}
966
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967static unsigned int
968get_output_height(struct weston_output *output)
969{
970 return abs(output->region.extents.y1 - output->region.extents.y2);
971}
972
973static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976 struct weston_transform *transform;
977
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 if (is_focus_view(view)) {
979 struct focus_surface *fsurf = get_focus_surface(view->surface);
980 transform = &fsurf->workspace_transform;
981 } else {
982 struct shell_surface *shsurf = get_shell_surface(view->surface);
983 transform = &shsurf->workspace_transform;
984 }
985
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 weston_matrix_init(&transform->matrix);
991 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200992 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994}
995
996static void
997workspace_translate_out(struct workspace *ws, double fraction)
998{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 unsigned int height;
1001 double d;
1002
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001003 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 d = height * fraction;
1006
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008 }
1009}
1010
1011static void
1012workspace_translate_in(struct workspace *ws, double fraction)
1013{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 unsigned int height;
1016 double d;
1017
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001018 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020
1021 if (fraction > 0)
1022 d = -(height - height * fraction);
1023 else
1024 d = height + height * fraction;
1025
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027 }
1028}
1029
1030static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031reverse_workspace_change_animation(struct desktop_shell *shell,
1032 unsigned int index,
1033 struct workspace *from,
1034 struct workspace *to)
1035{
1036 shell->workspaces.current = index;
1037
1038 shell->workspaces.anim_to = to;
1039 shell->workspaces.anim_from = from;
1040 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001041 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001042
Quentin Glidic82681572016-12-17 13:40:51 +01001043 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1044 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1045
Scott Moreau4272e632012-08-13 09:58:41 -06001046 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047}
1048
1049static void
1050workspace_deactivate_transforms(struct workspace *ws)
1051{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001053 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001054
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001055 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001056 if (is_focus_view(view)) {
1057 struct focus_surface *fsurf = get_focus_surface(view->surface);
1058 transform = &fsurf->workspace_transform;
1059 } else {
1060 struct shell_surface *shsurf = get_shell_surface(view->surface);
1061 transform = &shsurf->workspace_transform;
1062 }
1063
1064 if (!wl_list_empty(&transform->link)) {
1065 wl_list_remove(&transform->link);
1066 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001067 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069 }
1070}
1071
1072static void
1073finish_workspace_change_animation(struct desktop_shell *shell,
1074 struct workspace *from,
1075 struct workspace *to)
1076{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001077 struct weston_view *view;
1078
Scott Moreau4272e632012-08-13 09:58:41 -06001079 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001081 /* Views that extend past the bottom of the output are still
1082 * visible after the workspace animation ends but before its layer
1083 * is hidden. In that case, we need to damage below those views so
1084 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001085 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001086 weston_view_damage_below(view);
1087
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 wl_list_remove(&shell->workspaces.animation.link);
1089 workspace_deactivate_transforms(from);
1090 workspace_deactivate_transforms(to);
1091 shell->workspaces.anim_to = NULL;
1092
Quentin Glidic82681572016-12-17 13:40:51 +01001093 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094}
1095
1096static void
1097animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001098 struct weston_output *output,
1099 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100{
1101 struct desktop_shell *shell =
1102 container_of(animation, struct desktop_shell,
1103 workspaces.animation);
1104 struct workspace *from = shell->workspaces.anim_from;
1105 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001106 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 double x, y;
1108
1109 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1110 finish_workspace_change_animation(shell, from, to);
1111 return;
1112 }
1113
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001114 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001118 timespec_add_msec(&shell->workspaces.anim_timestamp,
1119 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001121 -(asin(1.0 - shell->workspaces.anim_current) *
1122 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1123 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 }
1125
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001126 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 /*
1129 * x = [0, π/2]
1130 * y(x) = sin(x)
1131 */
1132 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1133 y = sin(x);
1134
1135 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001136 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137
1138 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1139 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1140 shell->workspaces.anim_current = y;
1141
Scott Moreau4272e632012-08-13 09:58:41 -06001142 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001144 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146}
1147
1148static void
1149animate_workspace_change(struct desktop_shell *shell,
1150 unsigned int index,
1151 struct workspace *from,
1152 struct workspace *to)
1153{
1154 struct weston_output *output;
1155
1156 int dir;
1157
1158 if (index > shell->workspaces.current)
1159 dir = -1;
1160 else
1161 dir = 1;
1162
1163 shell->workspaces.current = index;
1164
1165 shell->workspaces.anim_dir = dir;
1166 shell->workspaces.anim_from = from;
1167 shell->workspaces.anim_to = to;
1168 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001169 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170
1171 output = container_of(shell->compositor->output_list.next,
1172 struct weston_output, link);
1173 wl_list_insert(&output->animation_list,
1174 &shell->workspaces.animation.link);
1175
Quentin Glidic82681572016-12-17 13:40:51 +01001176 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1177 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178
1179 workspace_translate_in(to, 0);
1180
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001181 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001182
Scott Moreau4272e632012-08-13 09:58:41 -06001183 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184}
1185
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187update_workspace(struct desktop_shell *shell, unsigned int index,
1188 struct workspace *from, struct workspace *to)
1189{
1190 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001191 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1192 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193}
1194
1195static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001196change_workspace(struct desktop_shell *shell, unsigned int index)
1197{
1198 struct workspace *from;
1199 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001200 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
1202 if (index == shell->workspaces.current)
1203 return;
1204
1205 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001206 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207 return;
1208
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001209 from = get_current_workspace(shell);
1210 to = get_workspace(shell, index);
1211
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001212 if (shell->workspaces.anim_from == to &&
1213 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001215 reverse_workspace_change_animation(shell, index, from, to);
1216 return;
1217 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219 if (shell->workspaces.anim_to != NULL)
1220 finish_workspace_change_animation(shell,
1221 shell->workspaces.anim_from,
1222 shell->workspaces.anim_to);
1223
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001225
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001226 if (shell->focus_animation_type != ANIMATION_NONE) {
1227 wl_list_for_each(state, &from->focus_list, link)
1228 if (state->keyboard_focus)
1229 animate_focus_change(shell, from,
1230 get_default_view(state->keyboard_focus), NULL);
1231
1232 wl_list_for_each(state, &to->focus_list, link)
1233 if (state->keyboard_focus)
1234 animate_focus_change(shell, to,
1235 NULL, get_default_view(state->keyboard_focus));
1236 }
1237
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 if (workspace_is_empty(to) && workspace_is_empty(from))
1239 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240 else
1241 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001242}
1243
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244static bool
1245workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1246{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001247 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001248 struct wl_list *e;
1249
1250 if (wl_list_empty(list))
1251 return false;
1252
1253 e = list->next;
1254
1255 if (e->next != list)
1256 return false;
1257
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001258 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001259}
1260
1261static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001262surface_keyboard_focus_lost(struct weston_surface *surface)
1263{
1264 struct weston_compositor *compositor = surface->compositor;
1265 struct weston_seat *seat;
1266 struct weston_surface *focus;
1267
1268 wl_list_for_each(seat, &compositor->seat_list, link) {
1269 struct weston_keyboard *keyboard =
1270 weston_seat_get_keyboard(seat);
1271
1272 if (!keyboard)
1273 continue;
1274
1275 focus = weston_surface_get_main_surface(keyboard->focus);
1276 if (focus == surface)
1277 weston_keyboard_set_focus(keyboard, NULL);
1278 }
1279}
1280
1281static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001283 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284 unsigned int index)
1285{
Derek Foreman1281a362015-07-31 16:55:32 -05001286 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001287 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 struct shell_surface *shsurf;
1290 struct workspace *from;
1291 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001292 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293
Derek Foreman1281a362015-07-31 16:55:32 -05001294 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 view = get_default_view(surface);
1296 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001297 index == shell->workspaces.current ||
1298 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300
1301 from = get_current_workspace(shell);
1302 to = get_workspace(shell, index);
1303
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001304 weston_layer_entry_remove(&view->layer_link);
1305 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306
Philip Withnall659163d2013-11-25 18:01:36 +00001307 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001308 if (shsurf != NULL)
1309 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001310
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001312 drop_focus_state(shell, from, surface);
1313
1314 if (shell->workspaces.anim_from == to &&
1315 shell->workspaces.anim_to == from) {
1316 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 return;
1319 }
1320
1321 if (shell->workspaces.anim_to != NULL)
1322 finish_workspace_change_animation(shell,
1323 shell->workspaces.anim_from,
1324 shell->workspaces.anim_to);
1325
1326 if (workspace_is_empty(from) &&
1327 workspace_has_only(to, surface))
1328 update_workspace(shell, index, from, to);
1329 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001330 if (shsurf != NULL &&
1331 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 wl_list_insert(&shell->workspaces.anim_sticky_list,
1333 &shsurf->workspace_transform.link);
1334
1335 animate_workspace_change(shell, index, from, to);
1336 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Jonas Ådahl8538b222012-08-29 22:13:03 +02001338 state = ensure_focus_state(shell, seat);
1339 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001340 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001344touch_move_grab_down(struct weston_touch_grab *grab,
1345 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001346 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001347{
1348}
1349
1350static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001351touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1352 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001354 struct weston_touch_move_grab *move =
1355 (struct weston_touch_move_grab *) container_of(
1356 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001357
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001358 if (touch_id == 0)
1359 move->active = 0;
1360
Jonas Ådahl9484b692013-12-02 22:05:03 +01001361 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001362 shell_touch_grab_end(&move->base);
1363 free(move);
1364 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001365}
1366
1367static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001368touch_move_grab_motion(struct weston_touch_grab *grab,
1369 const struct timespec *time, int touch_id,
1370 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001371{
1372 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1373 struct shell_surface *shsurf = move->base.shsurf;
1374 struct weston_surface *es;
1375 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1376 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1377
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001378 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001379 return;
1380
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001381 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001383 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001384
1385 weston_compositor_schedule_repaint(es->compositor);
1386}
1387
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001388static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001389touch_move_grab_frame(struct weston_touch_grab *grab)
1390{
1391}
1392
1393static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394touch_move_grab_cancel(struct weston_touch_grab *grab)
1395{
1396 struct weston_touch_move_grab *move =
1397 (struct weston_touch_move_grab *) container_of(
1398 grab, struct shell_touch_grab, grab);
1399
1400 shell_touch_grab_end(&move->base);
1401 free(move);
1402}
1403
Rusty Lynch1084da52013-08-15 09:10:08 -07001404static const struct weston_touch_grab_interface touch_move_grab_interface = {
1405 touch_move_grab_down,
1406 touch_move_grab_up,
1407 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001408 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001409 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001410};
1411
1412static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001413surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001414{
1415 struct weston_touch_move_grab *move;
1416
1417 if (!shsurf)
1418 return -1;
1419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001420 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1421 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001422 return 0;
1423
1424 move = malloc(sizeof *move);
1425 if (!move)
1426 return -1;
1427
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001428 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001430 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001432 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001433
1434 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001435 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001436
1437 return 0;
1438}
1439
1440static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001441noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001442{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001443}
1444
1445static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001446noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001447 const struct timespec *time,
1448 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001449{
1450}
1451
1452static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001453noop_grab_axis_source(struct weston_pointer_grab *grab,
1454 uint32_t source)
1455{
1456}
1457
1458static void
1459noop_grab_frame(struct weston_pointer_grab *grab)
1460{
1461}
1462
1463static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001464constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1465{
1466 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 struct weston_surface *surface =
1468 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001469 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001470 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001471 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001472 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001474
1475 x = wl_fixed_to_int(pointer->x + move->dx);
1476 y = wl_fixed_to_int(pointer->y + move->dy);
1477
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001478 if (shsurf->shell->panel_position ==
1479 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001480 get_output_work_area(shsurf->shell, surface->output, &area);
1481 geometry =
1482 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001483
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001485 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001486 y = area.y + safety - geometry.height
1487 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001488
1489 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 y + geometry.y < area.y)
1491 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001492 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001493
1494 *cx = x;
1495 *cy = y;
1496}
1497
1498static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001499move_grab_motion(struct weston_pointer_grab *grab,
1500 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001501 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001504 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001505 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001506 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001507 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001508
Jonas Ådahld2510102014-10-05 21:39:14 +02001509 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001510 if (!shsurf)
1511 return;
1512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001513 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1514
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001515 constrain_position(move, &cx, &cy);
1516
1517 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001519 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520}
1521
1522static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001523move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001524 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001526 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1527 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001528 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001529 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001530
Daniel Stone4dbadb12012-05-30 16:31:51 +01001531 if (pointer->button_count == 0 &&
1532 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001533 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001534 free(grab);
1535 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536}
1537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538static void
1539move_grab_cancel(struct weston_pointer_grab *grab)
1540{
1541 struct shell_grab *shell_grab =
1542 container_of(grab, struct shell_grab, grab);
1543
1544 shell_grab_end(shell_grab);
1545 free(grab);
1546}
1547
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001548static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001549 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001550 move_grab_motion,
1551 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001552 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001553 noop_grab_axis_source,
1554 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001555 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001556};
1557
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001559surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001560 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561{
1562 struct weston_move_grab *move;
1563
1564 if (!shsurf)
1565 return -1;
1566
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001567 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001568 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1569 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 return 0;
1571
1572 move = malloc(sizeof *move);
1573 if (!move)
1574 return -1;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001577 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001579 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001580 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
1582 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001583 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 return 0;
1586}
1587
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588struct weston_resize_grab {
1589 struct shell_grab base;
1590 uint32_t edges;
1591 int32_t width, height;
1592};
1593
1594static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001595resize_grab_motion(struct weston_pointer_grab *grab,
1596 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001597 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 struct shell_surface *shsurf = resize->base.shsurf;
1602 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001603 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 wl_fixed_t from_x, from_y;
1605 wl_fixed_t to_x, to_y;
1606
Jonas Ådahld2510102014-10-05 21:39:14 +02001607 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001608
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001609 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 return;
1611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_view_from_global_fixed(shsurf->view,
1613 pointer->grab_x, pointer->grab_y,
1614 &from_x, &from_y);
1615 weston_view_from_global_fixed(shsurf->view,
1616 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617
1618 width = resize->width;
1619 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1620 width += wl_fixed_to_int(from_x - to_x);
1621 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1622 width += wl_fixed_to_int(to_x - from_x);
1623 }
1624
1625 height = resize->height;
1626 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1627 height += wl_fixed_to_int(from_y - to_y);
1628 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1629 height += wl_fixed_to_int(to_y - from_y);
1630 }
1631
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001632 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1633 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1634
1635 min_size.width = MAX(1, min_size.width);
1636 min_size.height = MAX(1, min_size.height);
1637
1638 if (width < min_size.width)
1639 width = min_size.width;
1640 else if (max_size.width > 0 && width > max_size.width)
1641 width = max_size.width;
1642 if (height < min_size.height)
1643 height = min_size.height;
1644 else if (max_size.width > 0 && width > max_size.width)
1645 width = max_size.width;
1646 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647}
1648
1649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001651 const struct timespec *time,
1652 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
1654 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001655 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001657 struct weston_desktop_surface *desktop_surface =
1658 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659
1660 if (pointer->button_count == 0 &&
1661 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001662 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 shell_grab_end(&resize->base);
1664 free(grab);
1665 }
1666}
1667
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668static void
1669resize_grab_cancel(struct weston_pointer_grab *grab)
1670{
1671 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001672 struct weston_desktop_surface *desktop_surface =
1673 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001675 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001676 shell_grab_end(&resize->base);
1677 free(grab);
1678}
1679
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001680static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681 noop_grab_focus,
1682 resize_grab_motion,
1683 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001684 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001685 noop_grab_axis_source,
1686 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001687 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688};
1689
Giulio Camuffob8366642013-04-25 13:57:46 +03001690/*
1691 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001692 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001693static void
1694surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1695 int32_t *y, int32_t *w, int32_t *h) {
1696 pixman_region32_t region;
1697 pixman_box32_t *box;
1698 struct weston_subsurface *subsurface;
1699
1700 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 surface->width,
1702 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001703
1704 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1705 pixman_region32_union_rect(&region, &region,
1706 subsurface->position.x,
1707 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 subsurface->surface->width,
1709 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001710 }
1711
1712 box = pixman_region32_extents(&region);
1713 if (x)
1714 *x = box->x1;
1715 if (y)
1716 *y = box->y1;
1717 if (w)
1718 *w = box->x2 - box->x1;
1719 if (h)
1720 *h = box->y2 - box->y1;
1721
1722 pixman_region32_fini(&region);
1723}
1724
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725static int
1726surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001727 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728{
1729 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001730 const unsigned resize_topbottom =
1731 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1732 const unsigned resize_leftright =
1733 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1734 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001735 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001737 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001738 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1739 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740 return 0;
1741
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001742 /* Check for invalid edge combinations. */
1743 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1744 (edges & resize_topbottom) == resize_topbottom ||
1745 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746 return 0;
1747
1748 resize = malloc(sizeof *resize);
1749 if (!resize)
1750 return -1;
1751
1752 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001754 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1755 resize->width = geometry.width;
1756 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001757
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001758 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001759 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001761 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
1763 return 0;
1764}
1765
1766static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001767busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001768{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001770 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001771 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001773 wl_fixed_t sx, sy;
1774
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 view = weston_compositor_pick_view(pointer->seat->compositor,
1776 pointer->x, pointer->y,
1777 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001778 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001780 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001781 shell_grab_end(grab);
1782 free(grab);
1783 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784}
1785
1786static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001787busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1788 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001789 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790{
Jonas Ådahld2510102014-10-05 21:39:14 +02001791 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001796 const struct timespec *time,
1797 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001800 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001801 struct weston_pointer *pointer = grab->grab.pointer;
1802 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001803
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001804 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001805 activate(shsurf->shell, shsurf->view, seat,
1806 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001807 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001808 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001809 activate(shsurf->shell, shsurf->view, seat,
1810 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001811 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001812 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813}
1814
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001815static void
1816busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1817{
1818 struct shell_grab *grab = (struct shell_grab *) base;
1819
1820 shell_grab_end(grab);
1821 free(grab);
1822}
1823
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825 busy_cursor_grab_focus,
1826 busy_cursor_grab_motion,
1827 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001828 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001829 noop_grab_axis_source,
1830 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001831 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832};
1833
1834static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835handle_pointer_focus(struct wl_listener *listener, void *data)
1836{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001837 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001839 struct shell_surface *shsurf;
1840 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001843 return;
1844
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001845 shsurf = get_shell_surface(view->surface);
1846 if (!shsurf)
1847 return;
1848
1849 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1850
1851 if (shsurf->unresponsive)
1852 set_busy_cursor(shsurf, pointer);
1853 else
1854 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001855}
1856
1857static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001858shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1859{
1860 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001861 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001862}
1863
1864static void
1865shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1866{
1867 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001868 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001869}
1870
1871static void
1872handle_keyboard_focus(struct wl_listener *listener, void *data)
1873{
1874 struct weston_keyboard *keyboard = data;
1875 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1876
1877 if (seat->focused_surface) {
1878 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1879 if (shsurf)
1880 shell_surface_lose_keyboard_focus(shsurf);
1881 }
1882
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001883 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001884
1885 if (seat->focused_surface) {
1886 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1887 if (shsurf)
1888 shell_surface_gain_keyboard_focus(shsurf);
1889 }
1890}
1891
Philip Withnall07926d92013-11-25 18:01:40 +00001892/* The surface will be inserted into the list immediately after the link
1893 * returned by this function (i.e. will be stacked immediately above the
1894 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001895static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001896shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1897{
1898 struct workspace *ws;
1899
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001900 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1901 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001902 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001903 }
1904
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001905 /* Move the surface to a normal workspace layer so that surfaces
1906 * which were previously fullscreen or transient are no longer
1907 * rendered on top. */
1908 ws = get_current_workspace(shsurf->shell);
1909 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001910}
1911
Philip Withnall648a4dd2013-11-25 18:01:44 +00001912static void
1913shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1914{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001915 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001916}
1917
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001918/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001919 * geometry to ensure the changes are redrawn.
1920 *
1921 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1922 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001923static void
1924shell_surface_update_layer(struct shell_surface *shsurf)
1925{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001926 struct weston_surface *surface =
1927 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001928 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001929
1930 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1931
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001932 if (new_layer_link == NULL)
1933 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001934 if (new_layer_link == &shsurf->view->layer_link)
1935 return;
1936
1937 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001938 weston_layer_entry_remove(&shsurf->view->layer_link);
1939 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001940 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001941 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001942
1943 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001944}
1945
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001946static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001947notify_output_destroy(struct wl_listener *listener, void *data)
1948{
1949 struct shell_surface *shsurf =
1950 container_of(listener,
1951 struct shell_surface, output_destroy_listener);
1952
1953 shsurf->output = NULL;
1954 shsurf->output_destroy_listener.notify = NULL;
1955}
1956
1957static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001958shell_surface_set_output(struct shell_surface *shsurf,
1959 struct weston_output *output)
1960{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001961 struct weston_surface *es =
1962 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001963
1964 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001965 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001966 if (output)
1967 shsurf->output = output;
1968 else if (es->output)
1969 shsurf->output = es->output;
1970 else
1971 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001972
1973 if (shsurf->output_destroy_listener.notify) {
1974 wl_list_remove(&shsurf->output_destroy_listener.link);
1975 shsurf->output_destroy_listener.notify = NULL;
1976 }
1977
1978 if (!shsurf->output)
1979 return;
1980
1981 shsurf->output_destroy_listener.notify = notify_output_destroy;
1982 wl_signal_add(&shsurf->output->destroy_signal,
1983 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001984}
1985
1986static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001987weston_view_set_initial_position(struct weston_view *view,
1988 struct desktop_shell *shell);
1989
1990static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001991unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001992{
Philip Withnallf85fe842013-11-25 18:01:37 +00001993 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001994 wl_list_remove(&shsurf->fullscreen.transform.link);
1995 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001996
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 if (shsurf->fullscreen.black_view)
1998 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1999 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002000
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002001 if (shsurf->saved_position_valid)
2002 weston_view_set_position(shsurf->view,
2003 shsurf->saved_x, shsurf->saved_y);
2004 else
2005 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002006 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002007
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002008 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002010 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002011 shsurf->saved_rotation_valid = false;
2012 }
Alex Wu4539b082012-03-01 12:57:46 +08002013}
2014
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002015static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002016unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002017{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002018 struct weston_surface *surface =
2019 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2020
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002021 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002022 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002023
2024 if (shsurf->saved_position_valid)
2025 weston_view_set_position(shsurf->view,
2026 shsurf->saved_x, shsurf->saved_y);
2027 else
2028 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002029 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002030
2031 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 wl_list_insert(&shsurf->view->geometry.transformation_list,
2033 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002034 shsurf->saved_rotation_valid = false;
2035 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002036}
2037
Philip Withnallbecb77e2013-11-25 18:01:30 +00002038static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002039set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002040{
2041 struct shell_surface *shsurf;
2042 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002043 struct weston_view *view;
2044
2045 view = get_default_view(surface);
2046 if (!view)
2047 return;
2048
2049 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2050
2051 shsurf = get_shell_surface(surface);
2052 current_ws = get_current_workspace(shsurf->shell);
2053
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002054 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002055 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002056
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002057 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002058 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002059
2060 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002061 weston_view_damage_below(view);
2062}
2063
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002064
Tiago Vignattibe143262012-04-16 17:31:41 +03002065static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002066shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002067{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002068 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002069}
2070
Pekka Paalanen8274d902014-08-06 19:36:51 +03002071static int
2072black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2073{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002074 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002075 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002076 int n;
2077 int rem;
2078 int ret;
2079
2080 n = snprintf(buf, len, "black background surface for ");
2081 if (n < 0)
2082 return n;
2083
2084 rem = (int)len - n;
2085 if (rem < 0)
2086 rem = 0;
2087
2088 if (fs_surface->get_label)
2089 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2090 else
2091 ret = snprintf(buf + n, rem, "<unknown>");
2092
2093 if (ret < 0)
2094 return n;
2095
2096 return n + ret;
2097}
2098
Alex Wu21858432012-04-01 20:13:08 +08002099static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002100black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002103create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002104 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002105 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002106{
2107 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002109
2110 surface = weston_surface_create(ec);
2111 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002112 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002113 return NULL;
2114 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 view = weston_view_create(surface);
2116 if (surface == NULL) {
2117 weston_log("no memory\n");
2118 weston_surface_destroy(surface);
2119 return NULL;
2120 }
Alex Wu4539b082012-03-01 12:57:46 +08002121
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002122 surface->committed = black_surface_committed;
2123 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002124 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002125 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002126 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002127 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002128 pixman_region32_fini(&surface->input);
2129 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002130
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002131 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002132 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133
2134 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002135}
2136
Philip Withnalled8f1a92013-11-25 18:01:43 +00002137static void
2138shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2139{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002140 struct weston_surface *surface =
2141 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002142 struct weston_output *output = shsurf->fullscreen_output;
2143
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002144 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002145
2146 if (!shsurf->fullscreen.black_view)
2147 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002148 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002149 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002150 output->x, output->y,
2151 output->width,
2152 output->height);
2153
2154 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002155 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2156 weston_layer_entry_insert(&shsurf->view->layer_link,
2157 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002158 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002159 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002160
Armin Krezović4663aca2016-06-30 06:04:29 +02002161 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002162 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002163}
2164
Alex Wu4539b082012-03-01 12:57:46 +08002165/* Create black surface and append it to the associated fullscreen surface.
2166 * Handle size dismatch and positioning according to the method. */
2167static void
2168shell_configure_fullscreen(struct shell_surface *shsurf)
2169{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002170 struct weston_surface *surface =
2171 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002172 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002173
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002174 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002175 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002176 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2177 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002178
Philip Withnalled8f1a92013-11-25 18:01:43 +00002179 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002180
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002181 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002182 &surf_width, &surf_height);
2183
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002184 if (surface->buffer_ref.buffer)
2185 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002186}
2187
Alex Wu4539b082012-03-01 12:57:46 +08002188static void
2189shell_map_fullscreen(struct shell_surface *shsurf)
2190{
Alex Wubd3354b2012-04-17 17:20:49 +08002191 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002192}
2193
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002194static struct weston_output *
2195get_focused_output(struct weston_compositor *compositor)
2196{
2197 struct weston_seat *seat;
2198 struct weston_output *output = NULL;
2199
2200 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002201 struct weston_touch *touch = weston_seat_get_touch(seat);
2202 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2203 struct weston_keyboard *keyboard =
2204 weston_seat_get_keyboard(seat);
2205
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002206 /* Priority has touch focus, then pointer and
2207 * then keyboard focus. We should probably have
2208 * three for loops and check frist for touch,
2209 * then for pointer, etc. but unless somebody has some
2210 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002211 if (touch && touch->focus)
2212 output = touch->focus->output;
2213 else if (pointer && pointer->focus)
2214 output = pointer->focus->output;
2215 else if (keyboard && keyboard->focus)
2216 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002217
2218 if (output)
2219 break;
2220 }
2221
2222 return output;
2223}
2224
2225static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002226destroy_shell_seat(struct wl_listener *listener, void *data)
2227{
2228 struct shell_seat *shseat =
2229 container_of(listener,
2230 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002231
2232 wl_list_remove(&shseat->seat_destroy_listener.link);
2233 free(shseat);
2234}
2235
Jason Ekstrand024177c2014-04-21 19:42:58 -05002236static void
2237shell_seat_caps_changed(struct wl_listener *listener, void *data)
2238{
Derek Foreman1281a362015-07-31 16:55:32 -05002239 struct weston_keyboard *keyboard;
2240 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002241 struct shell_seat *seat;
2242
2243 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002244 keyboard = weston_seat_get_keyboard(seat->seat);
2245 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002246
Derek Foreman1281a362015-07-31 16:55:32 -05002247 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002248 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002249 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002251 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002252 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002253 wl_list_init(&seat->keyboard_focus_listener.link);
2254 }
2255
Derek Foreman1281a362015-07-31 16:55:32 -05002256 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002257 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002258 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002259 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002260 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002261 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002262 wl_list_init(&seat->pointer_focus_listener.link);
2263 }
2264}
2265
Giulio Camuffo5085a752013-03-25 21:42:45 +01002266static struct shell_seat *
2267create_shell_seat(struct weston_seat *seat)
2268{
2269 struct shell_seat *shseat;
2270
2271 shseat = calloc(1, sizeof *shseat);
2272 if (!shseat) {
2273 weston_log("no memory to allocate shell seat\n");
2274 return NULL;
2275 }
2276
2277 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002278
2279 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2280 wl_signal_add(&seat->destroy_signal,
2281 &shseat->seat_destroy_listener);
2282
Jason Ekstrand024177c2014-04-21 19:42:58 -05002283 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2284 wl_list_init(&shseat->keyboard_focus_listener.link);
2285
2286 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2287 wl_list_init(&shseat->pointer_focus_listener.link);
2288
2289 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2290 wl_signal_add(&seat->updated_caps_signal,
2291 &shseat->caps_changed_listener);
2292 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2293
Giulio Camuffo5085a752013-03-25 21:42:45 +01002294 return shseat;
2295}
2296
2297static struct shell_seat *
2298get_shell_seat(struct weston_seat *seat)
2299{
2300 struct wl_listener *listener;
2301
2302 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002303 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002304
2305 return container_of(listener,
2306 struct shell_seat, seat_destroy_listener);
2307}
2308
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002309static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002310fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002311{
2312 struct shell_surface *shsurf = data;
2313
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002314 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002315
2316 if (shsurf->output_destroy_listener.notify) {
2317 wl_list_remove(&shsurf->output_destroy_listener.link);
2318 shsurf->output_destroy_listener.notify = NULL;
2319 }
2320
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002321 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002322}
2323
2324static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002325fade_out_done(struct weston_view_animation *animation, void *data)
2326{
2327 struct shell_surface *shsurf = data;
2328 struct wl_event_loop *loop;
2329
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002330 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002331
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002332 if (weston_view_is_mapped(shsurf->view)) {
2333 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002334 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002335 }
2336}
2337
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002338struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002339get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002340{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002341 if (weston_surface_is_desktop_surface(surface)) {
2342 struct weston_desktop_surface *desktop_surface =
2343 weston_surface_get_desktop_surface(surface);
2344 return weston_desktop_surface_get_user_data(desktop_surface);
2345 }
2346 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002347}
2348
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002349/*
2350 * libweston-desktop
2351 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002352
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002353static void
2354desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2355 void *shell)
2356{
2357 struct weston_desktop_client *client =
2358 weston_desktop_surface_get_client(desktop_surface);
2359 struct wl_client *wl_client =
2360 weston_desktop_client_get_client(client);
2361 struct weston_view *view;
2362 struct shell_surface *shsurf;
2363 struct weston_surface *surface =
2364 weston_desktop_surface_get_surface(desktop_surface);
2365
2366 view = weston_desktop_surface_create_view(desktop_surface);
2367 if (!view)
2368 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002369
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002370 shsurf = calloc(1, sizeof *shsurf);
2371 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002372 if (wl_client)
2373 wl_client_post_no_memory(wl_client);
2374 else
2375 weston_log("no memory to allocate shell surface\n");
2376 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002377 }
2378
Pekka Paalanen8274d902014-08-06 19:36:51 +03002379 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002380
Tiago Vignattibc052c92012-04-19 16:18:18 +03002381 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002382 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002383 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002384 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002385 shsurf->desktop_surface = desktop_surface;
2386 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002388 wl_list_init(&shsurf->fullscreen.transform.link);
2389
Semi Malinen99f8c082018-05-02 11:10:32 +02002390 shell_surface_set_output(
2391 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002392
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002393 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002394
Pekka Paalanen460099f2012-01-20 16:48:25 +02002395 /* empty when not in use */
2396 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002397 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002398
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002399 wl_list_init(&shsurf->workspace_transform.link);
2400
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002401 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2402 weston_desktop_surface_set_activated(desktop_surface,
2403 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002404}
2405
Tiago Vignattibc052c92012-04-19 16:18:18 +03002406static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002407desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2408 void *shell)
2409{
2410 struct shell_surface *shsurf =
2411 weston_desktop_surface_get_user_data(desktop_surface);
2412 struct weston_surface *surface =
2413 weston_desktop_surface_get_surface(desktop_surface);
2414
2415 if (!shsurf)
2416 return;
2417
2418 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2419
2420 if (shsurf->fullscreen.black_view)
2421 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2422
2423 weston_surface_set_label_func(surface, NULL);
2424 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2425 shsurf->desktop_surface = NULL;
2426
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002427 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002428 if (weston_surface_is_mapped(surface) &&
2429 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2430 pixman_region32_fini(&surface->pending.input);
2431 pixman_region32_init(&surface->pending.input);
2432 pixman_region32_fini(&surface->input);
2433 pixman_region32_init(&surface->input);
2434 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2435 fade_out_done, shsurf);
2436 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002438
2439 if (shsurf->output_destroy_listener.notify) {
2440 wl_list_remove(&shsurf->output_destroy_listener.link);
2441 shsurf->output_destroy_listener.notify = NULL;
2442 }
2443
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002444 free(shsurf);
2445 }
2446}
2447
2448static void
2449set_maximized_position(struct desktop_shell *shell,
2450 struct shell_surface *shsurf)
2451{
2452 pixman_rectangle32_t area;
2453 struct weston_geometry geometry;
2454
2455 get_output_work_area(shell, shsurf->output, &area);
2456 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2457
2458 weston_view_set_position(shsurf->view,
2459 area.x - geometry.x,
2460 area.y - geometry.y);
2461}
2462
2463static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002464set_position_from_xwayland(struct shell_surface *shsurf)
2465{
2466 struct weston_geometry geometry;
2467 float x;
2468 float y;
2469
2470 assert(shsurf->xwayland.is_set);
2471
2472 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2473 x = shsurf->xwayland.x - geometry.x;
2474 y = shsurf->xwayland.y - geometry.y;
2475
2476 weston_view_set_position(shsurf->view, x, y);
2477
2478#ifdef WM_DEBUG
2479 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2480 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2481 geometry.x, geometry.y, x, y);
2482#endif
2483}
2484
2485static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002486map(struct desktop_shell *shell, struct shell_surface *shsurf,
2487 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002488{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002489 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002490 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2491 struct weston_compositor *compositor = shell->compositor;
2492 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002493
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002494 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002495 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002496 center_on_output(shsurf->view, shsurf->fullscreen_output);
2497 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002498 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002499 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002500 } else if (shsurf->xwayland.is_set) {
2501 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002502 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002504 }
2505
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002506 /* Surface stacking order, see also activate(). */
2507 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002508
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002509 weston_view_update_transform(shsurf->view);
2510 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002511 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002512 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002513 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002514 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002515
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002516 if (!shell->locked) {
2517 wl_list_for_each(seat, &compositor->seat_list, link)
2518 activate(shell, shsurf->view, seat,
2519 WESTON_ACTIVATE_FLAG_CONFIGURE);
2520 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002521
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002522 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002523 switch (shell->win_animation_type) {
2524 case ANIMATION_FADE:
2525 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2526 break;
2527 case ANIMATION_ZOOM:
2528 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2529 break;
2530 case ANIMATION_NONE:
2531 default:
2532 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002533 }
2534 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002535}
2536
2537static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002538desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2539 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002540{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002541 struct shell_surface *shsurf =
2542 weston_desktop_surface_get_user_data(desktop_surface);
2543 struct weston_surface *surface =
2544 weston_desktop_surface_get_surface(desktop_surface);
2545 struct weston_view *view = shsurf->view;
2546 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002547 bool was_fullscreen;
2548 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549
2550 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002551 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002553 was_fullscreen = shsurf->state.fullscreen;
2554 was_maximized = shsurf->state.maximized;
2555
2556 shsurf->state.fullscreen =
2557 weston_desktop_surface_get_fullscreen(desktop_surface);
2558 shsurf->state.maximized =
2559 weston_desktop_surface_get_maximized(desktop_surface);
2560
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561 if (!weston_surface_is_mapped(surface)) {
2562 map(shell, shsurf, sx, sy);
2563 surface->is_mapped = true;
2564 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2565 ++surface->ref_count;
2566 return;
2567 }
2568
2569 if (sx == 0 && sy == 0 &&
2570 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002571 shsurf->last_height == surface->height &&
2572 was_fullscreen == shsurf->state.fullscreen &&
2573 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002574 return;
2575
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002576 if (was_fullscreen)
2577 unset_fullscreen(shsurf);
2578 if (was_maximized)
2579 unset_maximized(shsurf);
2580
Quentin Glidic920cf042016-08-16 14:26:20 +02002581 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2582 !shsurf->saved_position_valid) {
2583 shsurf->saved_x = shsurf->view->geometry.x;
2584 shsurf->saved_y = shsurf->view->geometry.y;
2585 shsurf->saved_position_valid = true;
2586
2587 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2588 wl_list_remove(&shsurf->rotation.transform.link);
2589 wl_list_init(&shsurf->rotation.transform.link);
2590 weston_view_geometry_dirty(shsurf->view);
2591 shsurf->saved_rotation_valid = true;
2592 }
2593 }
2594
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002595 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002596 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002597 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002598 set_maximized_position(shell, shsurf);
2599 surface->output = shsurf->output;
2600 } else {
2601 float from_x, from_y;
2602 float to_x, to_y;
2603 float x, y;
2604
2605 if (shsurf->resize_edges) {
2606 sx = 0;
2607 sy = 0;
2608 }
2609
2610 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2611 sx = shsurf->last_width - surface->width;
2612 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2613 sy = shsurf->last_height - surface->height;
2614
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002615 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2616 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2617 x = shsurf->view->geometry.x + to_x - from_x;
2618 y = shsurf->view->geometry.y + to_y - from_y;
2619
2620 weston_view_set_position(shsurf->view, x, y);
2621 }
2622
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002623 shsurf->last_width = surface->width;
2624 shsurf->last_height = surface->height;
2625
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002626 /* XXX: would a fullscreen surface need the same handling? */
2627 if (surface->output) {
2628 wl_list_for_each(view, &surface->views, surface_link)
2629 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002630 }
2631}
2632
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002633static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002634get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2635{
2636 struct desktop_shell *shell;
2637 pixman_rectangle32_t area;
2638
2639 shell = shell_surface_get_shell(shsurf);
2640 get_output_work_area(shell, shsurf->output, &area);
2641
2642 *width = area.width;
2643 *height = area.height;
2644}
2645
2646static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002647set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2648 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002649{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002650 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2651 struct weston_surface *surface =
2652 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2653 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002654
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002655 if (fullscreen) {
2656 /* handle clients launching in fullscreen */
2657 if (output == NULL && !weston_surface_is_mapped(surface)) {
2658 /* Set the output to the one that has focus currently. */
2659 output = get_focused_output(surface->compositor);
2660 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002661
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002662 shell_surface_set_output(shsurf, output);
2663 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002664
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002665 width = shsurf->output->width;
2666 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002667 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2668 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002669 }
2670 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2671 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002672}
2673
2674static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002675desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2676 struct weston_seat *seat, uint32_t serial, void *shell)
2677{
2678 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2679 struct weston_touch *touch = weston_seat_get_touch(seat);
2680 struct shell_surface *shsurf =
2681 weston_desktop_surface_get_user_data(desktop_surface);
2682 struct weston_surface *surface =
2683 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2684 struct wl_resource *resource = surface->resource;
2685 struct weston_surface *focus;
2686
2687 if (pointer &&
2688 pointer->focus &&
2689 pointer->button_count > 0 &&
2690 pointer->grab_serial == serial) {
2691 focus = weston_surface_get_main_surface(pointer->focus->surface);
2692 if ((focus == surface) &&
2693 (surface_move(shsurf, pointer, true) < 0))
2694 wl_resource_post_no_memory(resource);
2695 } else if (touch &&
2696 touch->focus &&
2697 touch->grab_serial == serial) {
2698 focus = weston_surface_get_main_surface(touch->focus->surface);
2699 if ((focus == surface) &&
2700 (surface_touch_move(shsurf, touch) < 0))
2701 wl_resource_post_no_memory(resource);
2702 }
2703}
2704
2705static void
2706desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2707 struct weston_seat *seat, uint32_t serial,
2708 enum weston_desktop_surface_edge edges, void *shell)
2709{
2710 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2711 struct shell_surface *shsurf =
2712 weston_desktop_surface_get_user_data(desktop_surface);
2713 struct weston_surface *surface =
2714 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2715 struct wl_resource *resource = surface->resource;
2716 struct weston_surface *focus;
2717
2718 if (!pointer ||
2719 pointer->button_count == 0 ||
2720 pointer->grab_serial != serial ||
2721 pointer->focus == NULL)
2722 return;
2723
2724 focus = weston_surface_get_main_surface(pointer->focus->surface);
2725 if (focus != surface)
2726 return;
2727
2728 if (surface_resize(shsurf, pointer, edges) < 0)
2729 wl_resource_post_no_memory(resource);
2730}
2731
2732static void
2733desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2734 bool fullscreen,
2735 struct weston_output *output, void *shell)
2736{
2737 struct shell_surface *shsurf =
2738 weston_desktop_surface_get_user_data(desktop_surface);
2739
2740 set_fullscreen(shsurf, fullscreen, output);
2741}
2742
2743static void
2744set_maximized(struct shell_surface *shsurf, bool maximized)
2745{
2746 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2747 struct weston_surface *surface =
2748 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2749 int32_t width = 0, height = 0;
2750
2751 if (maximized) {
2752 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002753
2754 if (!weston_surface_is_mapped(surface))
2755 output = get_focused_output(surface->compositor);
2756 else
2757 output = surface->output;
2758
2759 shell_surface_set_output(shsurf, output);
2760
Derek Foreman927d9e22017-10-06 14:37:44 -05002761 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002762 }
2763 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2764 weston_desktop_surface_set_size(desktop_surface, width, height);
2765}
2766
2767static void
2768desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2769 bool maximized, void *shell)
2770{
2771 struct shell_surface *shsurf =
2772 weston_desktop_surface_get_user_data(desktop_surface);
2773
2774 set_maximized(shsurf, maximized);
2775}
2776
2777static void
2778desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2779 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002780{
2781 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002782 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002783
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784 /* apply compositor's own minimization logic (hide) */
2785 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002786}
2787
Rafael Antognollie2a34552013-12-03 15:35:45 -02002788static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002789set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002790{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002791 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002792
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002793 if (pointer->grab->interface == &busy_cursor_grab_interface)
2794 return;
2795
2796 grab = malloc(sizeof *grab);
2797 if (!grab)
2798 return;
2799
2800 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2801 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2802 /* Mark the shsurf as ungrabbed so that button binding is able
2803 * to move it. */
2804 shsurf->grabbed = 0;
2805}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002806
2807static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002808end_busy_cursor(struct weston_compositor *compositor,
2809 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002810{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002811 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002812 struct shell_grab *grab;
2813 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002814
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002815 wl_list_for_each(seat, &compositor->seat_list, link) {
2816 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2817 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002818
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002819 if (!pointer)
2820 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002821
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822 if (pointer->grab->interface != &busy_cursor_grab_interface)
2823 continue;
2824
2825 grab = (struct shell_grab *) pointer->grab;
2826 shsurf = grab->shsurf;
2827 if (!shsurf)
2828 continue;
2829
2830 grab_client =
2831 weston_desktop_surface_get_client(shsurf->desktop_surface);
2832 if (grab_client == desktop_client) {
2833 shell_grab_end(grab);
2834 free(grab);
2835 }
2836 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002837}
2838
2839static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002840desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2841 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002842{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002843 struct shell_surface *shsurf =
2844 weston_desktop_surface_get_user_data(desktop_surface);
2845 bool *unresponsive = user_data;
2846
2847 shsurf->unresponsive = *unresponsive;
2848}
2849
2850static void
2851desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2852 void *shell_)
2853{
2854 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002855 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002856 struct weston_seat *seat;
2857 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002858
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002859 weston_desktop_client_for_each_surface(desktop_client,
2860 desktop_surface_set_unresponsive,
2861 &unresponsive);
2862
2863
2864 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2865 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2866 struct weston_desktop_client *grab_client;
2867
2868 if (!pointer || !pointer->focus)
2869 continue;
2870
2871 shsurf = get_shell_surface(pointer->focus->surface);
2872 if (!shsurf)
2873 continue;
2874
2875 grab_client =
2876 weston_desktop_surface_get_client(shsurf->desktop_surface);
2877 if (grab_client == desktop_client)
2878 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002879 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002880}
2881
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002882static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002883desktop_surface_pong(struct weston_desktop_client *desktop_client,
2884 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002885{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002886 struct desktop_shell *shell = shell_;
2887 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002888
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002889 weston_desktop_client_for_each_surface(desktop_client,
2890 desktop_surface_set_unresponsive,
2891 &unresponsive);
2892 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002893}
2894
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002895static void
2896desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2897 int32_t x, int32_t y, void *shell_)
2898{
2899 struct shell_surface *shsurf =
2900 weston_desktop_surface_get_user_data(surface);
2901
2902 shsurf->xwayland.x = x;
2903 shsurf->xwayland.y = y;
2904 shsurf->xwayland.is_set = true;
2905}
2906
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002907static const struct weston_desktop_api shell_desktop_api = {
2908 .struct_size = sizeof(struct weston_desktop_api),
2909 .surface_added = desktop_surface_added,
2910 .surface_removed = desktop_surface_removed,
2911 .committed = desktop_surface_committed,
2912 .move = desktop_surface_move,
2913 .resize = desktop_surface_resize,
2914 .fullscreen_requested = desktop_surface_fullscreen_requested,
2915 .maximized_requested = desktop_surface_maximized_requested,
2916 .minimized_requested = desktop_surface_minimized_requested,
2917 .ping_timeout = desktop_surface_ping_timeout,
2918 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002919 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002920};
2921
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002922/* ************************ *
2923 * end of libweston-desktop *
2924 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002925static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002926configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002927{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002929
Semi Malinene7a52fb2018-04-26 11:08:10 +02002930 if (!ev->output)
2931 return;
2932
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002933 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 if (v->output == ev->output && v != ev) {
2935 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002936 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002937 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002938 }
2939 }
2940
Quentin Glidice8bf9592016-06-23 18:55:20 +02002941 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002942 ev->surface->is_mapped = true;
2943 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002944
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002945 if (wl_list_empty(&ev->layer_link.link)) {
2946 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002947 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002948 }
2949}
2950
David Fort50d962f2016-06-09 17:55:52 +02002951
2952static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002953find_shell_output_from_weston_output(struct desktop_shell *shell,
2954 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002955{
2956 struct shell_output *shell_output;
2957
2958 wl_list_for_each(shell_output, &shell->output_list, link) {
2959 if (shell_output->output == output)
2960 return shell_output;
2961 }
2962
2963 return NULL;
2964}
2965
Pekka Paalanen8274d902014-08-06 19:36:51 +03002966static int
2967background_get_label(struct weston_surface *surface, char *buf, size_t len)
2968{
2969 return snprintf(buf, len, "background for output %s",
2970 surface->output->name);
2971}
2972
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002973static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002974background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002975{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002976 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002978
Jason Ekstranda7af7042013-10-12 22:38:11 -05002979 view = container_of(es->views.next, struct weston_view, surface_link);
2980
Quentin Glidice8bf9592016-06-23 18:55:20 +02002981 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002982}
2983
2984static void
David Fort50d962f2016-06-09 17:55:52 +02002985handle_background_surface_destroy(struct wl_listener *listener, void *data)
2986{
2987 struct shell_output *output =
2988 container_of(listener, struct shell_output, background_surface_listener);
2989
2990 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002991 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002992 output->background_surface = NULL;
2993}
2994
2995static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002996desktop_shell_set_background(struct wl_client *client,
2997 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002998 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002999 struct wl_resource *surface_resource)
3000{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003001 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003002 struct weston_surface *surface =
3003 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003004 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003006
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003007 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003008 wl_resource_post_error(surface_resource,
3009 WL_DISPLAY_ERROR_INVALID_OBJECT,
3010 "surface role already assigned");
3011 return;
3012 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003013
Jason Ekstranda7af7042013-10-12 22:38:11 -05003014 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3015 weston_view_destroy(view);
3016 view = weston_view_create(surface);
3017
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003018 surface->committed = background_committed;
3019 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003020 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003021 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003022 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003023
3024 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003025 if (sh_output->background_surface) {
3026 /* The output already has a background, tell our helper
3027 * there is no need for another one. */
3028 weston_desktop_shell_send_configure(resource, 0,
3029 surface_resource,
3030 0, 0);
3031 } else {
3032 weston_desktop_shell_send_configure(resource, 0,
3033 surface_resource,
3034 surface->output->width,
3035 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003036
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003037 sh_output->background_surface = surface;
3038
3039 sh_output->background_surface_listener.notify =
3040 handle_background_surface_destroy;
3041 wl_signal_add(&surface->destroy_signal,
3042 &sh_output->background_surface_listener);
3043 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003044}
3045
Pekka Paalanen8274d902014-08-06 19:36:51 +03003046static int
3047panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3048{
3049 return snprintf(buf, len, "panel for output %s",
3050 surface->output->name);
3051}
3052
Kristian Høgsberg75840622011-09-06 13:48:16 -04003053static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003054panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003055{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003056 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003057 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003058 int width, height;
3059 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003060
Jason Ekstranda7af7042013-10-12 22:38:11 -05003061 view = container_of(es->views.next, struct weston_view, surface_link);
3062
Quentin Glidice8bf9592016-06-23 18:55:20 +02003063 get_panel_size(shell, view, &width, &height);
3064 switch (shell->panel_position) {
3065 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3066 break;
3067 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3068 y = view->output->height - height;
3069 break;
3070 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3071 break;
3072 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3073 x = view->output->width - width;
3074 break;
3075 }
3076
3077 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003078}
3079
3080static void
David Fort50d962f2016-06-09 17:55:52 +02003081handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3082{
3083 struct shell_output *output =
3084 container_of(listener, struct shell_output, panel_surface_listener);
3085
3086 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003087 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003088 output->panel_surface = NULL;
3089}
3090
3091
3092static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003093desktop_shell_set_panel(struct wl_client *client,
3094 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003095 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003096 struct wl_resource *surface_resource)
3097{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003098 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003099 struct weston_surface *surface =
3100 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003101 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003102 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003103
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003104 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003105 wl_resource_post_error(surface_resource,
3106 WL_DISPLAY_ERROR_INVALID_OBJECT,
3107 "surface role already assigned");
3108 return;
3109 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003110
Jason Ekstranda7af7042013-10-12 22:38:11 -05003111 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3112 weston_view_destroy(view);
3113 view = weston_view_create(surface);
3114
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003115 surface->committed = panel_committed;
3116 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003117 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003118 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003119 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003120
3121 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003122 if (sh_output->panel_surface) {
3123 /* The output already has a panel, tell our helper
3124 * there is no need for another one. */
3125 weston_desktop_shell_send_configure(resource, 0,
3126 surface_resource,
3127 0, 0);
3128 } else {
3129 weston_desktop_shell_send_configure(resource, 0,
3130 surface_resource,
3131 surface->output->width,
3132 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003133
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003134 sh_output->panel_surface = surface;
3135
3136 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3137 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3138 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003139}
3140
Pekka Paalanen8274d902014-08-06 19:36:51 +03003141static int
3142lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3143{
3144 return snprintf(buf, len, "lock window");
3145}
3146
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003147static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003148lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003149{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003150 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003151 struct weston_view *view;
3152
3153 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003154
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003155 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003156 return;
3157
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003159
3160 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003161 weston_layer_entry_insert(&shell->lock_layer.view_list,
3162 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003163 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003164 surface->is_mapped = true;
3165 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003166 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003167 }
3168}
3169
3170static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003171handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003172{
Tiago Vignattibe143262012-04-16 17:31:41 +03003173 struct desktop_shell *shell =
3174 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003175
Martin Minarik6d118362012-06-07 18:01:59 +02003176 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003177 shell->lock_surface = NULL;
3178}
3179
3180static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003181desktop_shell_set_lock_surface(struct wl_client *client,
3182 struct wl_resource *resource,
3183 struct wl_resource *surface_resource)
3184{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003185 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003186 struct weston_surface *surface =
3187 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003188
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003189 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003190
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003191 if (!shell->locked)
3192 return;
3193
Pekka Paalanen98262232011-12-01 10:42:22 +02003194 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003195
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003196 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003197 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003198 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003199
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003200 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003201 surface->committed = lock_surface_committed;
3202 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003203 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003204}
3205
3206static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003207resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003208{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003209 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003210
Quentin Glidic82681572016-12-17 13:40:51 +01003211 weston_layer_unset_position(&shell->lock_layer);
3212
3213 if (shell->showing_input_panels)
3214 weston_layer_set_position(&shell->input_panel_layer,
3215 WESTON_LAYER_POSITION_TOP_UI);
3216 weston_layer_set_position(&shell->fullscreen_layer,
3217 WESTON_LAYER_POSITION_FULLSCREEN);
3218 weston_layer_set_position(&shell->panel_layer,
3219 WESTON_LAYER_POSITION_UI);
3220 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003221
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003222 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003223
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003224 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003225 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003226 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003227}
3228
3229static void
3230desktop_shell_unlock(struct wl_client *client,
3231 struct wl_resource *resource)
3232{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003233 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003234
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003235 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003236
3237 if (shell->locked)
3238 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003239}
3240
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003241static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003242desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003243 struct wl_resource *resource,
3244 struct wl_resource *surface_resource)
3245{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003246 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003247
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003248 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003249 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003250}
3251
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003252static void
3253desktop_shell_desktop_ready(struct wl_client *client,
3254 struct wl_resource *resource)
3255{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003256 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003257
3258 shell_fade_startup(shell);
3259}
3260
Jonny Lamb765760d2014-08-20 15:53:19 +02003261static void
3262desktop_shell_set_panel_position(struct wl_client *client,
3263 struct wl_resource *resource,
3264 uint32_t position)
3265{
3266 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3267
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003268 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3269 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3270 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3271 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003272 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003273 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003274 "bad position argument");
3275 return;
3276 }
3277
3278 shell->panel_position = position;
3279}
3280
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003281static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003282 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003283 desktop_shell_set_panel,
3284 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003285 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003286 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003287 desktop_shell_desktop_ready,
3288 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003289};
3290
3291static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003292move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003293 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003294{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003295 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003296 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003297 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003298
Derek Foreman8ae2db52015-07-15 13:00:36 -05003299 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003300 return;
3301
Derek Foreman8ae2db52015-07-15 13:00:36 -05003302 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003303
Pekka Paalanen01388e22013-04-25 13:57:44 +03003304 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003305 if (surface == NULL)
3306 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003307
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003308 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003309 if (shsurf == NULL ||
3310 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3311 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003312 return;
3313
Derek Foreman794fa0e2015-07-15 13:00:38 -05003314 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003315}
3316
3317static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003318maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003319 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003320{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003321 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003322 struct weston_surface *surface;
3323 struct shell_surface *shsurf;
3324
3325 surface = weston_surface_get_main_surface(focus);
3326 if (surface == NULL)
3327 return;
3328
3329 shsurf = get_shell_surface(surface);
3330 if (shsurf == NULL)
3331 return;
3332
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003333 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003334}
3335
3336static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003337fullscreen_binding(struct weston_keyboard *keyboard,
3338 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003339{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003340 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003341 struct weston_surface *surface;
3342 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003343 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003344
3345 surface = weston_surface_get_main_surface(focus);
3346 if (surface == NULL)
3347 return;
3348
3349 shsurf = get_shell_surface(surface);
3350 if (shsurf == NULL)
3351 return;
3352
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003353 fullscreen =
3354 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003355
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003356 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003357}
3358
3359static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003360touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003361{
Derek Foreman362656b2014-09-04 10:23:05 -05003362 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003363 struct weston_surface *surface;
3364 struct shell_surface *shsurf;
3365
Derek Foreman8ae2db52015-07-15 13:00:36 -05003366 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003367 return;
3368
Derek Foreman8ae2db52015-07-15 13:00:36 -05003369 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003370 surface = weston_surface_get_main_surface(focus);
3371 if (surface == NULL)
3372 return;
3373
3374 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003375 if (shsurf == NULL ||
3376 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3377 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003378 return;
3379
Derek Foremanb7674ae2015-07-15 13:00:37 -05003380 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003381}
3382
3383static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003384resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003385 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003386{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003387 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003388 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003389 uint32_t edges = 0;
3390 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003391 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003392
Derek Foreman8ae2db52015-07-15 13:00:36 -05003393 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003394 return;
3395
Derek Foreman8ae2db52015-07-15 13:00:36 -05003396 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003397
Pekka Paalanen01388e22013-04-25 13:57:44 +03003398 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003399 if (surface == NULL)
3400 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003401
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003402 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003403 if (shsurf == NULL ||
3404 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3405 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003406 return;
3407
Jason Ekstranda7af7042013-10-12 22:38:11 -05003408 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003409 wl_fixed_to_int(pointer->grab_x),
3410 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003411 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003412
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003413 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003414 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003415 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003416 edges |= 0;
3417 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003418 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003420 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003421 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003422 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003423 edges |= 0;
3424 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003425 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003426
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003427 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003428}
3429
3430static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003431surface_opacity_binding(struct weston_pointer *pointer,
3432 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003433 struct weston_pointer_axis_event *event,
3434 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003435{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003436 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003437 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003438 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003439 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003440
Pekka Paalanen01388e22013-04-25 13:57:44 +03003441 /* XXX: broken for windows containing sub-surfaces */
3442 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003443 if (surface == NULL)
3444 return;
3445
3446 shsurf = get_shell_surface(surface);
3447 if (!shsurf)
3448 return;
3449
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003450 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003451
Jason Ekstranda7af7042013-10-12 22:38:11 -05003452 if (shsurf->view->alpha > 1.0)
3453 shsurf->view->alpha = 1.0;
3454 if (shsurf->view->alpha < step)
3455 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003456
Jason Ekstranda7af7042013-10-12 22:38:11 -05003457 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003458 weston_surface_damage(surface);
3459}
3460
3461static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003462do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3463 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003464{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003465 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003466 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003467 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003468 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003469
Derek Foreman1281a362015-07-31 16:55:32 -05003470 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003471 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3472 return;
3473 }
3474
Scott Moreauccbf29d2012-02-22 14:21:41 -07003475 wl_list_for_each(output, &compositor->output_list, link) {
3476 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003477 wl_fixed_to_double(pointer->x),
3478 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003479 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003480 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003481 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003482 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003483 increment = -output->zoom.increment;
3484 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003485 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003486 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003487 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003488 else
3489 increment = 0;
3490
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003491 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003492
Scott Moreaue6603982012-06-11 13:07:51 -06003493 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003494 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003495 else if (output->zoom.level > output->zoom.max_level)
3496 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003497
3498 if (!output->zoom.active) {
3499 if (output->zoom.level <= 0.0)
3500 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003501 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003502 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003503
Scott Moreaue6603982012-06-11 13:07:51 -06003504 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003505
Jason Ekstranda7af7042013-10-12 22:38:11 -05003506 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003507 }
3508 }
3509}
3510
Scott Moreauccbf29d2012-02-22 14:21:41 -07003511static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003512zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003513 struct weston_pointer_axis_event *event,
3514 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003515{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003516 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003517}
3518
3519static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003520zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003521 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003522{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003523 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003524}
3525
3526static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003527terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003528 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003530 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003531
Daniel Stone325fc2d2012-05-30 16:31:58 +01003532 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003533}
3534
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003535static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003536rotate_grab_motion(struct weston_pointer_grab *grab,
3537 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003538 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003539{
3540 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003541 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003542 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003543 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003544 struct weston_surface *surface =
3545 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003546 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003547
Jonas Ådahld2510102014-10-05 21:39:14 +02003548 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003549
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003550 if (!shsurf)
3551 return;
3552
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003553 cx = 0.5f * surface->width;
3554 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003555
Daniel Stone37816df2012-05-16 18:45:18 +01003556 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3557 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558 r = sqrtf(dx * dx + dy * dy);
3559
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003560 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003561 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003562
3563 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003564 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003565 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003566
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003567 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003568 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003569
3570 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003571 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003572 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003573 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003574 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003575
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003576 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003578 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003579 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003580 wl_list_init(&shsurf->rotation.transform.link);
3581 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003582 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003583 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003584
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003585 /* We need to adjust the position of the surface
3586 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003587 cposx = shsurf->view->geometry.x + cx;
3588 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003589 dposx = rotate->center.x - cposx;
3590 dposy = rotate->center.y - cposy;
3591 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 weston_view_set_position(shsurf->view,
3593 shsurf->view->geometry.x + dposx,
3594 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003595 }
3596
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003597 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003598 * lazily applies the damage due to rotation update.
3599 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003600 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003601}
3602
3603static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003604rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003605 const struct timespec *time,
3606 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003607{
3608 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003609 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003610 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003611 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003612 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003613
Daniel Stone4dbadb12012-05-30 16:31:51 +01003614 if (pointer->button_count == 0 &&
3615 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003616 if (shsurf)
3617 weston_matrix_multiply(&shsurf->rotation.rotation,
3618 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003619 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620 free(rotate);
3621 }
3622}
3623
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003624static void
3625rotate_grab_cancel(struct weston_pointer_grab *grab)
3626{
3627 struct rotate_grab *rotate =
3628 container_of(grab, struct rotate_grab, base.grab);
3629
3630 shell_grab_end(&rotate->base);
3631 free(rotate);
3632}
3633
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003634static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003635 noop_grab_focus,
3636 rotate_grab_motion,
3637 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003638 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003639 noop_grab_axis_source,
3640 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003641 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642};
3643
3644static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003645surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003646{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003647 struct weston_surface *surface =
3648 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003649 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003650 float dx, dy;
3651 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003652
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653 rotate = malloc(sizeof *rotate);
3654 if (!rotate)
3655 return;
3656
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003657 weston_view_to_global_float(shsurf->view,
3658 surface->width * 0.5f,
3659 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003661
Derek Foreman74de4692015-07-15 13:00:39 -05003662 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3663 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003664 r = sqrtf(dx * dx + dy * dy);
3665 if (r > 20.0f) {
3666 struct weston_matrix inverse;
3667
3668 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003669 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003670 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003671
3672 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003673 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003674 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003675 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003676 weston_matrix_init(&rotate->rotation);
3677 }
3678
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003679 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003680 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003681}
3682
3683static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003684rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3685 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003686{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003687 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003688 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003689 struct shell_surface *surface;
3690
Derek Foreman8ae2db52015-07-15 13:00:36 -05003691 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003692 return;
3693
Derek Foreman8ae2db52015-07-15 13:00:36 -05003694 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003695
Pekka Paalanen01388e22013-04-25 13:57:44 +03003696 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003697 if (base_surface == NULL)
3698 return;
3699
3700 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003701 if (surface == NULL ||
3702 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3703 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003704 return;
3705
Derek Foreman74de4692015-07-15 13:00:39 -05003706 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003707}
3708
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003709/* Move all fullscreen layers down to the current workspace and hide their
3710 * black views. The surfaces' state is set to both fullscreen and lowered,
3711 * and this is reversed when such a surface is re-configured, see
3712 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3713 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003714 * lowering_output = NULL - Lower on all outputs, else only lower on the
3715 * specified output.
3716 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003717 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003718 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003719void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003720lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003721 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003722{
3723 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003725
3726 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003727 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003728 &shell->fullscreen_layer.view_list.link,
3729 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003730 struct shell_surface *shsurf = get_shell_surface(view->surface);
3731
3732 if (!shsurf)
3733 continue;
3734
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003735 /* Only lower surfaces which have lowering_output as their fullscreen
3736 * output, unless a NULL output asks for lowering on all outputs.
3737 */
3738 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3739 continue;
3740
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003741 /* We can have a non-fullscreen popup for a fullscreen surface
3742 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003743 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003744 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003745 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3746 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003747 weston_view_damage_below(shsurf->fullscreen.black_view);
3748
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003749 }
3750
3751 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003752 weston_layer_entry_remove(&view->layer_link);
3753 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003754 weston_view_damage_below(view);
3755 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003756
3757 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003758 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003759}
3760
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003761void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003762activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003763 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003764{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003765 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003766 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003767 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003768 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003769 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003770 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003771
Pekka Paalanen01388e22013-04-25 13:57:44 +03003772 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003773 shsurf = get_shell_surface(main_surface);
3774 assert(shsurf);
3775
3776 /* Only demote fullscreen surfaces on the output of activated shsurf.
3777 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003778 if (shsurf->output)
3779 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003780
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003781 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782
Jonas Ådahl8538b222012-08-29 22:13:03 +02003783 state = ensure_focus_state(shell, seat);
3784 if (state == NULL)
3785 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003786
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003787 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003788 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003789
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003790 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3791 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003792 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003793
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003794 /* Update the surface’s layer. This brings it to the top of the stacking
3795 * order as appropriate. */
3796 shell_surface_update_layer(shsurf);
3797
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003798 if (shell->focus_animation_type != ANIMATION_NONE) {
3799 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003800 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003801 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003802}
3803
Alex Wu21858432012-04-01 20:13:08 +08003804/* no-op func for checking black surface */
3805static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003806black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003807{
3808}
3809
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003810static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003811is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003812{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003813 struct weston_surface *surface = view->surface;
3814
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003815 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003816 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003817 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003818 return true;
3819 }
3820 return false;
3821}
3822
Kristian Høgsberg75840622011-09-06 13:48:16 -04003823static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003824activate_binding(struct weston_seat *seat,
3825 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003826 struct weston_view *focus_view,
3827 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003828{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003829 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003830 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003831
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003832 if (!focus_view)
3833 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003834
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003835 if (is_black_surface_view(focus_view, &main_view))
3836 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003837
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003838 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003839 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003840 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003841
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003842 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003843}
3844
3845static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003846click_to_activate_binding(struct weston_pointer *pointer,
3847 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003848 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003849{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003850 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003851 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003852 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003853 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003854
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003855 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003856 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003857 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003858}
3859
3860static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003861touch_to_activate_binding(struct weston_touch *touch,
3862 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003863 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003864{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003865 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003866 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003867 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003868 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003869
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003870 activate_binding(touch->seat, data, touch->focus,
3871 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003872}
3873
3874static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003875unfocus_all_seats(struct desktop_shell *shell)
3876{
3877 struct weston_seat *seat, *next;
3878
3879 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003880 struct weston_keyboard *keyboard =
3881 weston_seat_get_keyboard(seat);
3882
3883 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003884 continue;
3885
Derek Foreman1281a362015-07-31 16:55:32 -05003886 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003887 }
3888}
3889
3890static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003891lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003892{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003893 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003894
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003895 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003896 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003897 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003898 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003899
3900 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003902 /* Hide all surfaces by removing the fullscreen, panel and
3903 * toplevel layers. This way nothing else can show or receive
3904 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003905
Quentin Glidic82681572016-12-17 13:40:51 +01003906 weston_layer_unset_position(&shell->panel_layer);
3907 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003908 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003909 weston_layer_unset_position(&shell->input_panel_layer);
3910 weston_layer_unset_position(&ws->layer);
3911
3912 weston_layer_set_position(&shell->lock_layer,
3913 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003914
Derek Foreman004b4a12015-07-20 16:28:13 -05003915 weston_compositor_sleep(shell->compositor);
3916
Neil Robertsb4a91702014-04-09 16:33:32 +01003917 /* Remove the keyboard focus on all seats. This will be
3918 * restored to the workspace's saved state via
3919 * restore_focus_state when the compositor is unlocked */
3920 unfocus_all_seats(shell);
3921
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003922 /* TODO: disable bindings that should not work while locked. */
3923
3924 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003925}
3926
3927static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003928unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003929{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003930 struct wl_resource *shell_resource;
3931
Pekka Paalanend81c2162011-11-16 13:47:34 +02003932 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003933 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003934 return;
3935 }
3936
3937 /* If desktop-shell client has gone away, unlock immediately. */
3938 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003939 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003940 return;
3941 }
3942
3943 if (shell->prepare_event_sent)
3944 return;
3945
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003946 shell_resource = shell->child.desktop_shell;
3947 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003948 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003949}
3950
3951static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003952shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003953{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003954 struct shell_output *shell_output = data;
3955 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003956
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003957 shell_output->fade.animation = NULL;
3958 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003959 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003960 weston_surface_destroy(shell_output->fade.view->surface);
3961 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003962 break;
3963 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003964 lock(shell);
3965 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003966 default:
3967 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003968 }
3969}
3970
Jason Ekstranda7af7042013-10-12 22:38:11 -05003971static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003972shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003973{
3974 struct weston_compositor *compositor = shell->compositor;
3975 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003976 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003977
3978 surface = weston_surface_create(compositor);
3979 if (!surface)
3980 return NULL;
3981
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 view = weston_view_create(surface);
3983 if (!view) {
3984 weston_surface_destroy(surface);
3985 return NULL;
3986 }
3987
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003988 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3989 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003990 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003991 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3992 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003993 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003994 surface->is_mapped = true;
3995 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003996
Jason Ekstranda7af7042013-10-12 22:38:11 -05003997 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003998}
3999
4000static void
4001shell_fade(struct desktop_shell *shell, enum fade_type type)
4002{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004003 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004004 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004005
4006 switch (type) {
4007 case FADE_IN:
4008 tint = 0.0;
4009 break;
4010 case FADE_OUT:
4011 tint = 1.0;
4012 break;
4013 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004014 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004015 return;
4016 }
4017
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004018 /* Create a separate fade surface for each output */
4019 wl_list_for_each(shell_output, &shell->output_list, link) {
4020 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004021
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004022 if (shell_output->fade.view == NULL) {
4023 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4024 if (!shell_output->fade.view)
4025 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004026
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004027 shell_output->fade.view->alpha = 1.0 - tint;
4028 weston_view_update_transform(shell_output->fade.view);
4029 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004030
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004031 if (shell_output->fade.view->output == NULL) {
4032 /* If the black view gets a NULL output, we lost the
4033 * last output and we'll just cancel the fade. This
4034 * happens when you close the last window under the
4035 * X11 or Wayland backends. */
4036 shell->locked = false;
4037 weston_surface_destroy(shell_output->fade.view->surface);
4038 shell_output->fade.view = NULL;
4039 } else if (shell_output->fade.animation) {
4040 weston_fade_update(shell_output->fade.animation, tint);
4041 } else {
4042 shell_output->fade.animation =
4043 weston_fade_run(shell_output->fade.view,
4044 1.0 - tint, tint, 300.0,
4045 shell_fade_done_for_output, shell_output);
4046 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004047 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004048}
4049
4050static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004051do_shell_fade_startup(void *data)
4052{
4053 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004054 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004055
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004056 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004057 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004058 } else {
4059 weston_log("desktop shell: "
4060 "unexpected fade-in animation type %d\n",
4061 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004062 wl_list_for_each(shell_output, &shell->output_list, link) {
4063 weston_surface_destroy(shell_output->fade.view->surface);
4064 shell_output->fade.view = NULL;
4065 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004066 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004067}
4068
4069static void
4070shell_fade_startup(struct desktop_shell *shell)
4071{
4072 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004073 struct shell_output *shell_output;
4074 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004075
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004076 wl_list_for_each(shell_output, &shell->output_list, link) {
4077 if (!shell_output->fade.startup_timer)
4078 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004079
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004080 wl_event_source_remove(shell_output->fade.startup_timer);
4081 shell_output->fade.startup_timer = NULL;
4082 has_fade = true;
4083 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004084
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004085 if (has_fade) {
4086 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4087 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4088 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004089}
4090
4091static int
4092fade_startup_timeout(void *data)
4093{
4094 struct desktop_shell *shell = data;
4095
4096 shell_fade_startup(shell);
4097 return 0;
4098}
4099
4100static void
4101shell_fade_init(struct desktop_shell *shell)
4102{
4103 /* Make compositor output all black, and wait for the desktop-shell
4104 * client to signal it is ready, then fade in. The timer triggers a
4105 * fade-in, in case the desktop-shell client takes too long.
4106 */
4107
4108 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004109 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004110
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004111 if (shell->startup_animation_type == ANIMATION_NONE)
4112 return;
4113
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004114 wl_list_for_each(shell_output, &shell->output_list, link) {
4115 if (shell_output->fade.view != NULL) {
4116 weston_log("%s: warning: fade surface already exists\n",
4117 __func__);
4118 continue;
4119 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004120
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004121 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4122 if (!shell_output->fade.view)
4123 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004124
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004125 weston_view_update_transform(shell_output->fade.view);
4126 weston_surface_damage(shell_output->fade.view->surface);
4127
4128 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4129 shell_output->fade.startup_timer =
4130 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4131 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4132 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004133}
4134
4135static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004136idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004137{
4138 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004139 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004140
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004141 struct weston_seat *seat;
4142
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004143 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4144 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004145
4146 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004147 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004148}
4149
4150static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004151wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004152{
4153 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004154 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004155
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004156 unlock(shell);
4157}
4158
4159static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004160transform_handler(struct wl_listener *listener, void *data)
4161{
4162 struct weston_surface *surface = data;
4163 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004164 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004165 int x, y;
4166
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004167 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004168 return;
4169
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004170 api = shsurf->shell->xwayland_surface_api;
4171 if (!api) {
4172 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4173 shsurf->shell->xwayland_surface_api = api;
4174 }
4175
4176 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004177 return;
4178
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004179 if (!weston_view_is_mapped(shsurf->view))
4180 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004181
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004182 x = shsurf->view->geometry.x;
4183 y = shsurf->view->geometry.y;
4184
4185 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004186}
4187
4188static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004189center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004190{
Giulio Camuffob8366642013-04-25 13:57:46 +03004191 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004192 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004193
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004194 if (!output) {
4195 weston_view_set_position(view, 0, 0);
4196 return;
4197 }
4198
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004200
4201 x = output->x + (output->width - width) / 2 - surf_x / 2;
4202 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004203
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004204 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004205}
4206
4207static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004208weston_view_set_initial_position(struct weston_view *view,
4209 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004210{
4211 struct weston_compositor *compositor = shell->compositor;
4212 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004213 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004214 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004215 struct weston_output *output, *target_output = NULL;
4216 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004217 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004218
4219 /* As a heuristic place the new window on the same output as the
4220 * pointer. Falling back to the output containing 0, 0.
4221 *
4222 * TODO: Do something clever for touch too?
4223 */
4224 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004225 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4226
4227 if (pointer) {
4228 ix = wl_fixed_to_int(pointer->x);
4229 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004230 break;
4231 }
4232 }
4233
4234 wl_list_for_each(output, &compositor->output_list, link) {
4235 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4236 target_output = output;
4237 break;
4238 }
4239 }
4240
4241 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 weston_view_set_position(view, 10 + random() % 400,
4243 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004244 return;
4245 }
4246
4247 /* Valid range within output where the surface will still be onscreen.
4248 * If this is negative it means that the surface is bigger than
4249 * output.
4250 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004251 get_output_work_area(shell, target_output, &area);
4252
Derek Foremanf814c5d2015-04-15 12:23:54 -05004253 x = area.x;
4254 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004255 range_x = area.width - view->surface->width;
4256 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004257
Rob Bradford4cb88c72012-08-13 15:18:44 +01004258 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004259 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004260
4261 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004262 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004263
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004265}
4266
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004267static bool
4268check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4269{
4270 struct timespec now;
4271
4272 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4273 return false;
4274
4275 /*
4276 * If the shell helper client dies before the session has been
4277 * up for roughly 30 seconds, better just make Weston shut down,
4278 * because the user likely has no way to interact with the desktop
4279 * anyway.
4280 */
4281 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4282 weston_log("Error: %s apparently cannot run at all.\n",
4283 shell->client);
4284 weston_log_continue(STAMP_SPACE "Quitting...");
4285 wl_display_terminate(shell->compositor->wl_display);
4286
4287 return true;
4288 }
4289
4290 return false;
4291}
4292
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004293static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004294
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004295static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004296respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004297{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004298 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004299
4300 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004301 weston_compositor_get_time(&time);
4302 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004303 shell->child.deathstamp = time;
4304 shell->child.deathcount = 0;
4305 }
4306
4307 shell->child.deathcount++;
4308 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004309 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004310 return;
4311 }
4312
Pekka Paalanen826dc142014-08-27 12:11:53 +03004313 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004314 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004315}
4316
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004317static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004318desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4319{
4320 struct desktop_shell *shell;
4321
4322 shell = container_of(listener, struct desktop_shell,
4323 child.client_destroy_listener);
4324
Pekka Paalanen826dc142014-08-27 12:11:53 +03004325 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004326 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004327 /*
4328 * unbind_desktop_shell() will reset shell->child.desktop_shell
4329 * before the respawned process has a chance to create a new
4330 * desktop_shell object, because we are being called from the
4331 * wl_client destructor which destroys all wl_resources before
4332 * returning.
4333 */
4334
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004335 if (!check_desktop_shell_crash_too_early(shell))
4336 respawn_desktop_shell_process(shell);
4337
Pekka Paalanen826dc142014-08-27 12:11:53 +03004338 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004339}
4340
4341static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004342launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004343{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004344 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004345
Pekka Paalanen826dc142014-08-27 12:11:53 +03004346 shell->child.client = weston_client_start(shell->compositor,
4347 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004348
Arnaud Vrac42631192014-08-25 20:56:46 +02004349 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004350 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004351 return;
4352 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004353
4354 shell->child.client_destroy_listener.notify =
4355 desktop_shell_client_destroy;
4356 wl_client_add_destroy_listener(shell->child.client,
4357 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004358}
4359
4360static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004361unbind_desktop_shell(struct wl_resource *resource)
4362{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004363 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004364
4365 if (shell->locked)
4366 resume_desktop(shell);
4367
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004368 shell->child.desktop_shell = NULL;
4369 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004370}
4371
4372static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004373bind_desktop_shell(struct wl_client *client,
4374 void *data, uint32_t version, uint32_t id)
4375{
Tiago Vignattibe143262012-04-16 17:31:41 +03004376 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004377 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004378
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004379 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4380 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004381
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004382 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004383 wl_resource_set_implementation(resource,
4384 &desktop_shell_implementation,
4385 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004386 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004387 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004388 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004389
4390 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4391 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004392}
4393
Kristian Høgsberg07045392012-02-19 18:52:44 -05004394struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004395 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004396 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004397 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004398 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004399 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004400};
4401
4402static void
4403switcher_next(struct switcher *switcher)
4404{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004406 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004407 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004408 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004409
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004410 /* temporary re-display minimized surfaces */
4411 struct weston_view *tmp;
4412 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004413 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4414 weston_layer_entry_remove(&view->layer_link);
4415 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004416 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4417 *minimized = view;
4418 }
4419
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004420 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004421 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004422 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004423 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004424 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004425 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004426 next = view;
4427 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004428 view->alpha = 0.25;
4429 weston_view_geometry_dirty(view);
4430 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004431 }
Alex Wu1659daa2012-04-01 20:13:09 +08004432
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004433 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004434 view->alpha = 0.25;
4435 weston_view_geometry_dirty(view);
4436 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004437 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004438 }
4439
4440 if (next == NULL)
4441 next = first;
4442
Alex Wu07b26062012-03-12 16:06:01 +08004443 if (next == NULL)
4444 return;
4445
Kristian Høgsberg07045392012-02-19 18:52:44 -05004446 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004447 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004448
4449 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004450 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004452
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004453 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004454 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004456}
4457
4458static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004459switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004460{
4461 struct switcher *switcher =
4462 container_of(listener, struct switcher, listener);
4463
4464 switcher_next(switcher);
4465}
4466
4467static void
Daniel Stone351eb612012-05-31 15:27:47 -04004468switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004469{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004471 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004472 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004473
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004474 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004475 if (is_focus_view(view))
4476 continue;
4477
Jason Ekstranda7af7042013-10-12 22:38:11 -05004478 view->alpha = 1.0;
4479 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004480 }
4481
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004482 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004483 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004484 keyboard->seat,
4485 WESTON_ACTIVATE_FLAG_CONFIGURE);
4486 }
4487
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004489 weston_keyboard_end_grab(keyboard);
4490 if (keyboard->input_method_resource)
4491 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004492
4493 /* re-hide surfaces that were temporary shown during the switch */
4494 struct weston_view **minimized;
4495 wl_array_for_each(minimized, &switcher->minimized_array) {
4496 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004497 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004498 weston_layer_entry_remove(&(*minimized)->layer_link);
4499 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004500 weston_view_damage_below(*minimized);
4501 }
4502 }
4503 wl_array_release(&switcher->minimized_array);
4504
Kristian Høgsberg07045392012-02-19 18:52:44 -05004505 free(switcher);
4506}
4507
4508static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004509switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004510 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004511{
4512 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004513 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004514
Daniel Stonec9785ea2012-05-30 16:31:52 +01004515 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004516 switcher_next(switcher);
4517}
4518
4519static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004520switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004521 uint32_t mods_depressed, uint32_t mods_latched,
4522 uint32_t mods_locked, uint32_t group)
4523{
4524 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004525 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004526
Daniel Stone351eb612012-05-31 15:27:47 -04004527 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4528 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004529}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004530
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004531static void
4532switcher_cancel(struct weston_keyboard_grab *grab)
4533{
4534 struct switcher *switcher = container_of(grab, struct switcher, grab);
4535
4536 switcher_destroy(switcher);
4537}
4538
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004539static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004540 switcher_key,
4541 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004542 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004543};
4544
4545static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004546switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004547 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004548{
Tiago Vignattibe143262012-04-16 17:31:41 +03004549 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004550 struct switcher *switcher;
4551
4552 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004553 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004554 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004555 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004556 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004557 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004558
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004559 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004560 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004561 weston_keyboard_start_grab(keyboard, &switcher->grab);
4562 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563 switcher_next(switcher);
4564}
4565
Pekka Paalanen3c647232011-12-22 13:43:43 +02004566static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004567backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004568 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004569{
4570 struct weston_compositor *compositor = data;
4571 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004572 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004573
4574 /* TODO: we're limiting to simple use cases, where we assume just
4575 * control on the primary display. We'd have to extend later if we
4576 * ever get support for setting backlights on random desktop LCD
4577 * panels though */
4578 output = get_default_output(compositor);
4579 if (!output)
4580 return;
4581
4582 if (!output->set_backlight)
4583 return;
4584
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004585 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4586 backlight_new = output->backlight_current - 25;
4587 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4588 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004589
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004590 if (backlight_new < 5)
4591 backlight_new = 5;
4592 if (backlight_new > 255)
4593 backlight_new = 255;
4594
4595 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004596 output->set_backlight(output, output->backlight_current);
4597}
4598
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004599static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004600force_kill_binding(struct weston_keyboard *keyboard,
4601 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004602{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004603 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004604 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004605 struct desktop_shell *shell = data;
4606 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004607 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004608
Derek Foreman8ae2db52015-07-15 13:00:36 -05004609 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004610 if (!focus_surface)
4611 return;
4612
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004613 wl_signal_emit(&compositor->kill_signal, focus_surface);
4614
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004615 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004616 wl_client_get_credentials(client, &pid, NULL, NULL);
4617
4618 /* Skip clients that we launched ourselves (the credentials of
4619 * the socketpair is ours) */
4620 if (pid == getpid())
4621 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004622
Daniel Stone325fc2d2012-05-30 16:31:58 +01004623 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004624}
4625
4626static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004627workspace_up_binding(struct weston_keyboard *keyboard,
4628 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004629{
4630 struct desktop_shell *shell = data;
4631 unsigned int new_index = shell->workspaces.current;
4632
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004633 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004634 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004635 if (new_index != 0)
4636 new_index--;
4637
4638 change_workspace(shell, new_index);
4639}
4640
4641static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004642workspace_down_binding(struct weston_keyboard *keyboard,
4643 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004644{
4645 struct desktop_shell *shell = data;
4646 unsigned int new_index = shell->workspaces.current;
4647
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004648 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004649 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004650 if (new_index < shell->workspaces.num - 1)
4651 new_index++;
4652
4653 change_workspace(shell, new_index);
4654}
4655
4656static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004657workspace_f_binding(struct weston_keyboard *keyboard,
4658 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004659{
4660 struct desktop_shell *shell = data;
4661 unsigned int new_index;
4662
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004663 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004664 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004665 new_index = key - KEY_F1;
4666 if (new_index >= shell->workspaces.num)
4667 new_index = shell->workspaces.num - 1;
4668
4669 change_workspace(shell, new_index);
4670}
4671
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004672static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004673workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004674 const struct timespec *time, uint32_t key,
4675 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004676{
4677 struct desktop_shell *shell = data;
4678 unsigned int new_index = shell->workspaces.current;
4679
4680 if (shell->locked)
4681 return;
4682
4683 if (new_index != 0)
4684 new_index--;
4685
Derek Foreman8ae2db52015-07-15 13:00:36 -05004686 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004687}
4688
4689static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004690workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004691 const struct timespec *time, uint32_t key,
4692 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004693{
4694 struct desktop_shell *shell = data;
4695 unsigned int new_index = shell->workspaces.current;
4696
4697 if (shell->locked)
4698 return;
4699
4700 if (new_index < shell->workspaces.num - 1)
4701 new_index++;
4702
Derek Foreman8ae2db52015-07-15 13:00:36 -05004703 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004704}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004705
4706static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004707shell_reposition_view_on_output_destroy(struct weston_view *view)
4708{
4709 struct weston_output *output, *first_output;
4710 struct weston_compositor *ec = view->surface->compositor;
4711 struct shell_surface *shsurf;
4712 float x, y;
4713 int visible;
4714
4715 x = view->geometry.x;
4716 y = view->geometry.y;
4717
4718 /* At this point the destroyed output is not in the list anymore.
4719 * If the view is still visible somewhere, we leave where it is,
4720 * otherwise, move it to the first output. */
4721 visible = 0;
4722 wl_list_for_each(output, &ec->output_list, link) {
4723 if (pixman_region32_contains_point(&output->region,
4724 x, y, NULL)) {
4725 visible = 1;
4726 break;
4727 }
4728 }
4729
4730 if (!visible) {
4731 first_output = container_of(ec->output_list.next,
4732 struct weston_output, link);
4733
4734 x = first_output->x + first_output->width / 4;
4735 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004736
4737 weston_view_set_position(view, x, y);
4738 } else {
4739 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004740 }
4741
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004742
4743 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004744 if (!shsurf)
4745 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004746
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004747 shsurf->saved_position_valid = false;
4748 set_maximized(shsurf, false);
4749 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004750}
4751
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004752void
4753shell_for_each_layer(struct desktop_shell *shell,
4754 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004755{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004756 struct workspace **ws;
4757
4758 func(shell, &shell->fullscreen_layer, data);
4759 func(shell, &shell->panel_layer, data);
4760 func(shell, &shell->background_layer, data);
4761 func(shell, &shell->lock_layer, data);
4762 func(shell, &shell->input_panel_layer, data);
4763
4764 wl_array_for_each(ws, &shell->workspaces.array)
4765 func(shell, &(*ws)->layer, data);
4766}
4767
4768static void
4769shell_output_destroy_move_layer(struct desktop_shell *shell,
4770 struct weston_layer *layer,
4771 void *data)
4772{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004773 struct weston_view *view;
4774
Marius Vladea40d6d2018-02-08 18:46:42 +02004775 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004776 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004777}
4778
4779static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004780handle_output_destroy(struct wl_listener *listener, void *data)
4781{
4782 struct shell_output *output_listener =
4783 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004784 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004785
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004786 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004787
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004788 if (output_listener->panel_surface)
4789 wl_list_remove(&output_listener->panel_surface_listener.link);
4790 if (output_listener->background_surface)
4791 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004792 wl_list_remove(&output_listener->destroy_listener.link);
4793 wl_list_remove(&output_listener->link);
4794 free(output_listener);
4795}
4796
4797static void
David Fort50d962f2016-06-09 17:55:52 +02004798shell_resize_surface_to_output(struct desktop_shell *shell,
4799 struct weston_surface *surface,
4800 const struct weston_output *output)
4801{
4802 if (!surface)
4803 return;
4804
4805 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4806 surface->resource,
4807 output->width,
4808 output->height);
4809}
4810
4811
4812static void
4813handle_output_resized(struct wl_listener *listener, void *data)
4814{
4815 struct desktop_shell *shell =
4816 container_of(listener, struct desktop_shell, resized_listener);
4817 struct weston_output *output = (struct weston_output *)data;
4818 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4819
4820 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4821 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4822}
4823
4824static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004825create_shell_output(struct desktop_shell *shell,
4826 struct weston_output *output)
4827{
4828 struct shell_output *shell_output;
4829
4830 shell_output = zalloc(sizeof *shell_output);
4831 if (shell_output == NULL)
4832 return;
4833
4834 shell_output->output = output;
4835 shell_output->shell = shell;
4836 shell_output->destroy_listener.notify = handle_output_destroy;
4837 wl_signal_add(&output->destroy_signal,
4838 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004839 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004840}
4841
4842static void
4843handle_output_create(struct wl_listener *listener, void *data)
4844{
4845 struct desktop_shell *shell =
4846 container_of(listener, struct desktop_shell, output_create_listener);
4847 struct weston_output *output = (struct weston_output *)data;
4848
4849 create_shell_output(shell, output);
4850}
4851
4852static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004853handle_output_move_layer(struct desktop_shell *shell,
4854 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004855{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004856 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004857 struct weston_view *view;
4858 float x, y;
4859
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004860 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004861 if (view->output != output)
4862 continue;
4863
4864 x = view->geometry.x + output->move_x;
4865 y = view->geometry.y + output->move_y;
4866 weston_view_set_position(view, x, y);
4867 }
4868}
4869
4870static void
4871handle_output_move(struct wl_listener *listener, void *data)
4872{
4873 struct desktop_shell *shell;
4874
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004875 shell = container_of(listener, struct desktop_shell,
4876 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004877
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004878 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004879}
4880
4881static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004882setup_output_destroy_handler(struct weston_compositor *ec,
4883 struct desktop_shell *shell)
4884{
4885 struct weston_output *output;
4886
4887 wl_list_init(&shell->output_list);
4888 wl_list_for_each(output, &ec->output_list, link)
4889 create_shell_output(shell, output);
4890
4891 shell->output_create_listener.notify = handle_output_create;
4892 wl_signal_add(&ec->output_created_signal,
4893 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004894
4895 shell->output_move_listener.notify = handle_output_move;
4896 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004897}
4898
4899static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004900shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004901{
Tiago Vignattibe143262012-04-16 17:31:41 +03004902 struct desktop_shell *shell =
4903 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004904 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004905 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004906
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004907 /* Force state to unlocked so we don't try to fade */
4908 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004909
4910 if (shell->child.client) {
4911 /* disable respawn */
4912 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004913 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004914 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004915
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004916 wl_list_remove(&shell->idle_listener.link);
4917 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004918 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004919
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004920 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004921 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004922
Xiong Zhang6b481422013-10-23 13:58:32 +08004923 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4924 wl_list_remove(&shell_output->destroy_listener.link);
4925 wl_list_remove(&shell_output->link);
4926 free(shell_output);
4927 }
4928
4929 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004930 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004931 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004932
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004933 wl_array_for_each(ws, &shell->workspaces.array)
4934 workspace_destroy(*ws);
4935 wl_array_release(&shell->workspaces.array);
4936
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004937 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004938 free(shell);
4939}
4940
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004941static void
4942shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4943{
4944 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004945 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004946
Bob Ham744e6532016-01-12 10:21:48 +00004947 if (shell->allow_zap)
4948 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4949 MODIFIER_CTRL | MODIFIER_ALT,
4950 terminate_binding, ec);
4951
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004952 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004953 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4954 click_to_activate_binding,
4955 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004956 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4957 click_to_activate_binding,
4958 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004959 weston_compositor_add_touch_binding(ec, 0,
4960 touch_to_activate_binding,
4961 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004962 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4963 backlight_binding, ec);
4964 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4965 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004966
4967 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004968 if (shell->exposay_modifier)
4969 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4970 exposay_binding, shell);
4971
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004972 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004973 if (!mod)
4974 return;
4975
Ian Ray13404c42017-09-18 15:22:01 +03004976 /* This binding is not configurable, but is only enabled if there is a
4977 * valid binding modifier. */
4978 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4979 MODIFIER_SUPER | MODIFIER_ALT,
4980 surface_opacity_binding, NULL);
4981
Ian Rayab7c0b62017-09-18 15:22:00 +03004982 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4983 mod, zoom_axis_binding,
4984 NULL);
4985
Daniel Stone325fc2d2012-05-30 16:31:58 +01004986 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4987 zoom_key_binding, NULL);
4988 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4989 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004990 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4991 maximize_binding, NULL);
4992 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4993 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004994 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4995 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004996 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004997 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004998 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004999 weston_compositor_add_button_binding(ec, BTN_LEFT,
5000 mod | MODIFIER_SHIFT,
5001 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005002
5003 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005004 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005005 rotate_binding, NULL);
5006
Daniel Stone325fc2d2012-05-30 16:31:58 +01005007 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5008 shell);
5009 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5010 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005011 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5012 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005013 weston_compositor_add_key_binding(ec, KEY_K, mod,
5014 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005015 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5016 workspace_up_binding, shell);
5017 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5018 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005019 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5020 workspace_move_surface_up_binding,
5021 shell);
5022 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5023 workspace_move_surface_down_binding,
5024 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005025
5026 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5027 if (shell->workspaces.num > 1) {
5028 num_workspace_bindings = shell->workspaces.num;
5029 if (num_workspace_bindings > 6)
5030 num_workspace_bindings = 6;
5031 for (i = 0; i < num_workspace_bindings; i++)
5032 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5033 workspace_f_binding,
5034 shell);
5035 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005036
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005037 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005038}
5039
Jason Ekstrand024177c2014-04-21 19:42:58 -05005040static void
5041handle_seat_created(struct wl_listener *listener, void *data)
5042{
5043 struct weston_seat *seat = data;
5044
5045 create_shell_seat(seat);
5046}
5047
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005048WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005049wet_shell_init(struct weston_compositor *ec,
5050 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005051{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005052 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005053 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005054 struct workspace **pws;
5055 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005056 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005057
Peter Huttererf3d62272013-08-08 11:57:05 +10005058 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005059 if (shell == NULL)
5060 return -1;
5061
Kristian Høgsberg75840622011-09-06 13:48:16 -04005062 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005063
5064 shell->destroy_listener.notify = shell_destroy;
5065 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005066 shell->idle_listener.notify = idle_handler;
5067 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5068 shell->wake_listener.notify = wake_handler;
5069 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005070 shell->transform_listener.notify = transform_handler;
5071 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005072
Quentin Glidic82681572016-12-17 13:40:51 +01005073 weston_layer_init(&shell->fullscreen_layer, ec);
5074 weston_layer_init(&shell->panel_layer, ec);
5075 weston_layer_init(&shell->background_layer, ec);
5076 weston_layer_init(&shell->lock_layer, ec);
5077 weston_layer_init(&shell->input_panel_layer, ec);
5078
5079 weston_layer_set_position(&shell->fullscreen_layer,
5080 WESTON_LAYER_POSITION_FULLSCREEN);
5081 weston_layer_set_position(&shell->panel_layer,
5082 WESTON_LAYER_POSITION_UI);
5083 weston_layer_set_position(&shell->background_layer,
5084 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005085
5086 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005087 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005088
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005089 if (input_panel_setup(shell) < 0)
5090 return -1;
5091
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005092 shell->text_backend = text_backend_init(ec);
5093 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005094 return -1;
5095
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005096 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005097
Daniel Stonedf8133b2013-11-19 11:37:14 +01005098 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5099 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5100
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005101 for (i = 0; i < shell->workspaces.num; i++) {
5102 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5103 if (pws == NULL)
5104 return -1;
5105
Quentin Glidic82681572016-12-17 13:40:51 +01005106 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005107 if (*pws == NULL)
5108 return -1;
5109 }
5110 activate_workspace(shell, 0);
5111
Quentin Glidic82681572016-12-17 13:40:51 +01005112 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005113
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005114 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005115 wl_list_init(&shell->workspaces.animation.link);
5116 shell->workspaces.animation.frame = animate_workspace_change_frame;
5117
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005118 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5119 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005120 return -1;
5121
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005122 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005123 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005124 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005125 return -1;
5126
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005127 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005128
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005129 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005130
Xiong Zhang6b481422013-10-23 13:58:32 +08005131 setup_output_destroy_handler(ec, shell);
5132
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005133 loop = wl_display_get_event_loop(ec->wl_display);
5134 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005135
Jason Ekstrand024177c2014-04-21 19:42:58 -05005136 wl_list_for_each(seat, &ec->seat_list, link)
5137 handle_seat_created(NULL, seat);
5138 shell->seat_create_listener.notify = handle_seat_created;
5139 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005140
David Fort50d962f2016-06-09 17:55:52 +02005141 shell->resized_listener.notify = handle_output_resized;
5142 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5143
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005144 screenshooter_create(ec);
5145
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005146 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005147
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005148 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005149
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005150 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5151
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005152 return 0;
5153}