blob: b8f00eb12c0245c81daba74d5e26f03249a0ea9a [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;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200131
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700132 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200133 bool fullscreen;
134 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100135 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200136 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700137
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200138 struct {
139 bool is_set;
140 int32_t x;
141 int32_t y;
142 } xwayland;
143
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500144 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500145
146 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200147};
148
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300149struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400150 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300151 struct shell_surface *shsurf;
152 struct wl_listener shsurf_destroy_listener;
153};
154
Rusty Lynch1084da52013-08-15 09:10:08 -0700155struct shell_touch_grab {
156 struct weston_touch_grab grab;
157 struct shell_surface *shsurf;
158 struct wl_listener shsurf_destroy_listener;
159 struct weston_touch *touch;
160};
161
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300162struct weston_move_grab {
163 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100164 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500165 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500166};
167
Rusty Lynch1084da52013-08-15 09:10:08 -0700168struct weston_touch_move_grab {
169 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800170 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700171 wl_fixed_t dx, dy;
172};
173
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500176 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200177 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200178 float x;
179 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200180 } center;
181};
182
Giulio Camuffo5085a752013-03-25 21:42:45 +0100183struct shell_seat {
184 struct weston_seat *seat;
185 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500186 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100187
Jason Ekstrand024177c2014-04-21 19:42:58 -0500188 struct wl_listener caps_changed_listener;
189 struct wl_listener pointer_focus_listener;
190 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100191};
192
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800193
Alex Wubd3354b2012-04-17 17:20:49 +0800194static struct desktop_shell *
195shell_surface_get_shell(struct shell_surface *shsurf);
196
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500197static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200198set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
199
200static void
Derek Foreman74de4692015-07-15 13:00:39 -0500201surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500202
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300203static void
204shell_fade_startup(struct desktop_shell *shell);
205
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700206static void
207shell_fade(struct desktop_shell *shell, enum fade_type type);
208
Philip Withnallbecb77e2013-11-25 18:01:30 +0000209static struct shell_seat *
210get_shell_seat(struct weston_seat *seat);
211
Jonny Lambd73c6942014-08-20 15:53:20 +0200212static void
213get_output_panel_size(struct desktop_shell *shell,
214 struct weston_output *output,
215 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700216
Philip Withnall648a4dd2013-11-25 18:01:44 +0000217static void
218shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
219
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220static int
221shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
222{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200224 struct weston_desktop_surface *desktop_surface =
225 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 t = weston_desktop_surface_get_title(desktop_surface);
228 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229
230 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200231 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300232 t ? " '" : "", t ?: "", t ? "'" : "",
233 c ? " of " : "", c ?: "");
234}
235
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500236static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300238{
239 struct shell_grab *grab;
240
241 grab = container_of(listener, struct shell_grab,
242 shsurf_destroy_listener);
243
244 grab->shsurf = NULL;
245}
246
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800247struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248get_default_view(struct weston_surface *surface)
249{
250 struct shell_surface *shsurf;
251 struct weston_view *view;
252
253 if (!surface || wl_list_empty(&surface->views))
254 return NULL;
255
256 shsurf = get_shell_surface(surface);
257 if (shsurf)
258 return shsurf->view;
259
260 wl_list_for_each(view, &surface->views, surface_link)
261 if (weston_view_is_mapped(view))
262 return view;
263
264 return container_of(surface->views.next, struct weston_view, surface_link);
265}
266
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300268shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400269 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400271 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800272 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300274 struct desktop_shell *shell = shsurf->shell;
275
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200276 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400277
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278 grab->grab.interface = interface;
279 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400280 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500281 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400282 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700284 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400285 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400286 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800287 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 weston_pointer_set_focus(pointer,
290 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400291 wl_fixed_from_int(0),
292 wl_fixed_from_int(0));
293 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294}
295
Jonny Lambd73c6942014-08-20 15:53:20 +0200296static void
Quentin Glidic581df062016-06-23 18:55:19 +0200297get_panel_size(struct desktop_shell *shell,
298 struct weston_view *view,
299 int *width,
300 int *height)
301{
302 float x1, y1;
303 float x2, y2;
304 weston_view_to_global_float(view, 0, 0, &x1, &y1);
305 weston_view_to_global_float(view,
306 view->surface->width,
307 view->surface->height,
308 &x2, &y2);
309 *width = (int)(x2 - x1);
310 *height = (int)(y2 - y1);
311}
312
313static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200314get_output_panel_size(struct desktop_shell *shell,
315 struct weston_output *output,
316 int *width,
317 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700318{
319 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200320
321 *width = 0;
322 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700323
324 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200325 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700326
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300327 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200328 if (view->surface->output == output) {
329 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200330 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700331 }
332 }
333
Jonny Lambd73c6942014-08-20 15:53:20 +0200334 /* the correct view wasn't found */
335}
336
337static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200338get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200339 struct weston_output *output,
340 pixman_rectangle32_t *area)
341{
342 int32_t panel_width = 0, panel_height = 0;
343
Derek Foremanf814c5d2015-04-15 12:23:54 -0500344 area->x = output->x;
345 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200346
347 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200348 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800349 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200350 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500351 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000352 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800353 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200354 area->width = output->width;
355 area->height = output->height - panel_height;
356 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800357 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500358 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000359 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800360 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200361 area->width = output->width - panel_width;
362 area->height = output->height;
363 break;
364 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700365}
366
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400367static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300368shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300369{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400371 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700372 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400373
374 if (grab->shsurf->resize_edges) {
375 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400376 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700377 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700379 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300380}
381
382static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700383shell_touch_grab_start(struct shell_touch_grab *grab,
384 const struct weston_touch_grab_interface *interface,
385 struct shell_surface *shsurf,
386 struct weston_touch *touch)
387{
388 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800389
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200390 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700391
Rusty Lynch1084da52013-08-15 09:10:08 -0700392 grab->grab.interface = interface;
393 grab->shsurf = shsurf;
394 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
395 wl_signal_add(&shsurf->destroy_signal,
396 &grab->shsurf_destroy_listener);
397
398 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700399 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700400
401 weston_touch_start_grab(touch, &grab->grab);
402 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500403 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700405}
406
407static void
408shell_touch_grab_end(struct shell_touch_grab *grab)
409{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700410 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700411 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700412 grab->shsurf->grabbed = 0;
413 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700414
415 weston_touch_end_grab(grab->touch);
416}
417
418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800420 struct weston_output *output);
421
Daniel Stone496ca172012-05-30 16:31:42 +0100422static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300423get_modifier(char *modifier)
424{
425 if (!modifier)
426 return MODIFIER_SUPER;
427
428 if (!strcmp("ctrl", modifier))
429 return MODIFIER_CTRL;
430 else if (!strcmp("alt", modifier))
431 return MODIFIER_ALT;
432 else if (!strcmp("super", modifier))
433 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000434 else if (!strcmp("none", modifier))
435 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300436 else
437 return MODIFIER_SUPER;
438}
439
Juan Zhaoe10d2792012-04-25 19:09:52 +0800440static enum animation_type
441get_animation_type(char *animation)
442{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800443 if (!animation)
444 return ANIMATION_NONE;
445
Juan Zhaoe10d2792012-04-25 19:09:52 +0800446 if (!strcmp("zoom", animation))
447 return ANIMATION_ZOOM;
448 else if (!strcmp("fade", animation))
449 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100450 else if (!strcmp("dim-layer", animation))
451 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800452 else
453 return ANIMATION_NONE;
454}
455
Alex Wu4539b082012-03-01 12:57:46 +0800456static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400457shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200458{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400459 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500460 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300461 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000462 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200463
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300464 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400465 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300466 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
467 WESTON_SHELL_CLIENT);
468 if (ret < 0)
469 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500471 "client", &s, client);
472 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100473 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000474
475 weston_config_section_get_bool(section,
476 "allow-zap", &allow_zap, true);
477 shell->allow_zap = allow_zap;
478
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100479 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400480 "binding-modifier", &s, "super");
481 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200482 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800483
484 weston_config_section_get_string(section,
485 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000486 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800487 free(s);
488
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400489 weston_config_section_get_string(section, "animation", &s, "none");
490 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200491 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200492 weston_config_section_get_string(section, "close-animation", &s, "fade");
493 shell->win_close_animation_type = get_animation_type(s);
494 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700495 weston_config_section_get_string(section,
496 "startup-animation", &s, "fade");
497 shell->startup_animation_type = get_animation_type(s);
498 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700499 if (shell->startup_animation_type == ANIMATION_ZOOM)
500 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100501 weston_config_section_get_string(section, "focus-animation", &s, "none");
502 shell->focus_animation_type = get_animation_type(s);
503 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400504 weston_config_section_get_uint(section, "num-workspaces",
505 &shell->workspaces.num,
506 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200507}
508
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800509struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100510get_default_output(struct weston_compositor *compositor)
511{
Armin Krezović10b06182016-06-23 11:59:30 +0200512 if (wl_list_empty(&compositor->output_list))
513 return NULL;
514
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100515 return container_of(compositor->output_list.next,
516 struct weston_output, link);
517}
518
Pekka Paalanen8274d902014-08-06 19:36:51 +0300519static int
520focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
521{
522 return snprintf(buf, len, "focus highlight effect for output %s",
523 surface->output->name);
524}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525
526/* no-op func for checking focus surface */
527static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200528focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100529{
530}
531
532static struct focus_surface *
533get_focus_surface(struct weston_surface *surface)
534{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200535 if (surface->committed == focus_surface_committed)
536 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100537 else
538 return NULL;
539}
540
541static bool
542is_focus_surface (struct weston_surface *es)
543{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200544 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545}
546
547static bool
548is_focus_view (struct weston_view *view)
549{
550 return is_focus_surface (view->surface);
551}
552
553static struct focus_surface *
554create_focus_surface(struct weston_compositor *ec,
555 struct weston_output *output)
556{
557 struct focus_surface *fsurf = NULL;
558 struct weston_surface *surface = NULL;
559
560 fsurf = malloc(sizeof *fsurf);
561 if (!fsurf)
562 return NULL;
563
564 fsurf->surface = weston_surface_create(ec);
565 surface = fsurf->surface;
566 if (surface == NULL) {
567 free(fsurf);
568 return NULL;
569 }
570
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200571 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200573 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200574 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300575 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800577 fsurf->view = weston_view_create(surface);
578 if (fsurf->view == NULL) {
579 weston_surface_destroy(surface);
580 free(fsurf);
581 return NULL;
582 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100583 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200584 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100585
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600586 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600587 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100588 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
589 pixman_region32_fini(&surface->opaque);
590 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
591 output->width, output->height);
592 pixman_region32_fini(&surface->input);
593 pixman_region32_init(&surface->input);
594
595 wl_list_init(&fsurf->workspace_transform.link);
596
597 return fsurf;
598}
599
600static void
601focus_surface_destroy(struct focus_surface *fsurf)
602{
603 weston_surface_destroy(fsurf->surface);
604 free(fsurf);
605}
606
607static void
608focus_animation_done(struct weston_view_animation *animation, void *data)
609{
610 struct workspace *ws = data;
611
612 ws->focus_animation = NULL;
613}
614
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200615static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200616focus_state_destroy(struct focus_state *state)
617{
618 wl_list_remove(&state->seat_destroy_listener.link);
619 wl_list_remove(&state->surface_destroy_listener.link);
620 free(state);
621}
622
623static void
624focus_state_seat_destroy(struct wl_listener *listener, void *data)
625{
626 struct focus_state *state = container_of(listener,
627 struct focus_state,
628 seat_destroy_listener);
629
630 wl_list_remove(&state->link);
631 focus_state_destroy(state);
632}
633
634static void
635focus_state_surface_destroy(struct wl_listener *listener, void *data)
636{
637 struct focus_state *state = container_of(listener,
638 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400639 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200640 struct weston_surface *main_surface;
641 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500642 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200643
Pekka Paalanen01388e22013-04-25 13:57:44 +0300644 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
645
Kristian Høgsberge3778222012-07-31 17:29:30 -0400646 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300647 wl_list_for_each(view,
648 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400650 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100651 if (is_focus_view(view))
652 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200653 if (!get_shell_surface(view->surface))
654 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400655
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200656 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400657 break;
658 }
659
Pekka Paalanen01388e22013-04-25 13:57:44 +0300660 /* if the focus was a sub-surface, activate its main surface */
661 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200662 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300663
Kristian Høgsberge3778222012-07-31 17:29:30 -0400664 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100665 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200666 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200667 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400668 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200669 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100670 if (state->ws->focus_animation)
671 weston_view_animation_destroy(state->ws->focus_animation);
672
673 state->ws->focus_animation = weston_fade_run(
674 state->ws->fsurf_front->view,
675 state->ws->fsurf_front->view->alpha, 0.0, 300,
676 focus_animation_done, state->ws);
677 }
678
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 wl_list_remove(&state->link);
680 focus_state_destroy(state);
681 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200682}
683
684static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200685focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
686 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200687{
Jonas Ådahl04769742012-06-13 00:01:24 +0200688 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200689
690 state = malloc(sizeof *state);
691 if (state == NULL)
692 return NULL;
693
Quentin Glidicfff39812016-08-15 10:56:52 +0200694 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400696 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400698 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200699
700 state->seat_destroy_listener.notify = focus_state_seat_destroy;
701 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400702 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200703 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400704 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705
706 return state;
707}
708
Jonas Ådahl8538b222012-08-29 22:13:03 +0200709static struct focus_state *
710ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
711{
712 struct workspace *ws = get_current_workspace(shell);
713 struct focus_state *state;
714
715 wl_list_for_each(state, &ws->focus_list, link)
716 if (state->seat == seat)
717 break;
718
719 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200720 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200721
722 return state;
723}
724
Jonas Ådahl04769742012-06-13 00:01:24 +0200725static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800726focus_state_set_focus(struct focus_state *state,
727 struct weston_surface *surface)
728{
729 if (state->keyboard_focus) {
730 wl_list_remove(&state->surface_destroy_listener.link);
731 wl_list_init(&state->surface_destroy_listener.link);
732 }
733
734 state->keyboard_focus = surface;
735 if (surface)
736 wl_signal_add(&surface->destroy_signal,
737 &state->surface_destroy_listener);
738}
739
740static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400741restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200742{
743 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400744 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100745 struct wl_list pending_seat_list;
746 struct weston_seat *seat, *next_seat;
747
748 /* Temporarily steal the list of seats so that we can keep
749 * track of the seats we've already processed */
750 wl_list_init(&pending_seat_list);
751 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
752 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200753
754 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500755 struct weston_keyboard *keyboard =
756 weston_seat_get_keyboard(state->seat);
757
Neil Roberts4237f502014-04-09 16:33:31 +0100758 wl_list_remove(&state->seat->link);
759 wl_list_insert(&shell->compositor->seat_list,
760 &state->seat->link);
761
Derek Foreman1281a362015-07-31 16:55:32 -0500762 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800763 continue;
764
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400765 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200766
Derek Foreman1281a362015-07-31 16:55:32 -0500767 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200768 }
Neil Roberts4237f502014-04-09 16:33:31 +0100769
770 /* For any remaining seats that we don't have a focus state
771 * for we'll reset the keyboard focus to NULL */
772 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500773 struct weston_keyboard *keyboard =
774 weston_seat_get_keyboard(seat);
775
Neil Roberts4237f502014-04-09 16:33:31 +0100776 wl_list_insert(&shell->compositor->seat_list, &seat->link);
777
Derek Foreman1281a362015-07-31 16:55:32 -0500778 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100779 continue;
780
Derek Foreman1281a362015-07-31 16:55:32 -0500781 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783}
784
785static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
787 struct weston_seat *seat)
788{
Derek Foreman1281a362015-07-31 16:55:32 -0500789 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791
792 wl_list_for_each(state, &ws->focus_list, link) {
793 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500794 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200795 return;
796 }
797 }
798}
799
800static void
801drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
802 struct weston_surface *surface)
803{
804 struct focus_state *state;
805
806 wl_list_for_each(state, &ws->focus_list, link)
807 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800808 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809}
810
811static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100812animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
813 struct weston_view *from, struct weston_view *to)
814{
815 struct weston_output *output;
816 bool focus_surface_created = false;
817
818 /* FIXME: Only support dim animation using two layers */
819 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
820 return;
821
822 output = get_default_output(shell->compositor);
823 if (ws->fsurf_front == NULL && (from || to)) {
824 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800825 if (ws->fsurf_front == NULL)
826 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100827 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800828
829 ws->fsurf_back = create_focus_surface(shell->compositor, output);
830 if (ws->fsurf_back == NULL) {
831 focus_surface_destroy(ws->fsurf_front);
832 return;
833 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800835
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 focus_surface_created = true;
837 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300838 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
839 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 }
841
842 if (ws->focus_animation) {
843 weston_view_animation_destroy(ws->focus_animation);
844 ws->focus_animation = NULL;
845 }
846
847 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&to->layer_link,
849 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300851 weston_layer_entry_insert(&ws->layer.view_list,
852 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853
854 if (focus_surface_created) {
855 ws->focus_animation = weston_fade_run(
856 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200857 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 focus_animation_done, ws);
859 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300860 weston_layer_entry_insert(&from->layer_link,
861 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100862 ws->focus_animation = weston_stable_fade_run(
863 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200864 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 focus_animation_done, ws);
866 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300867 weston_layer_entry_insert(&ws->layer.view_list,
868 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100869 ws->focus_animation = weston_stable_fade_run(
870 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200871 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 focus_animation_done, ws);
873 }
874}
875
876static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877workspace_destroy(struct workspace *ws)
878{
Jonas Ådahl04769742012-06-13 00:01:24 +0200879 struct focus_state *state, *next;
880
881 wl_list_for_each_safe(state, next, &ws->focus_list, link)
882 focus_state_destroy(state);
883
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100884 if (ws->fsurf_front)
885 focus_surface_destroy(ws->fsurf_front);
886 if (ws->fsurf_back)
887 focus_surface_destroy(ws->fsurf_back);
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889 free(ws);
890}
891
Jonas Ådahl04769742012-06-13 00:01:24 +0200892static void
893seat_destroyed(struct wl_listener *listener, void *data)
894{
895 struct weston_seat *seat = data;
896 struct focus_state *state, *next;
897 struct workspace *ws = container_of(listener,
898 struct workspace,
899 seat_destroyed_listener);
900
901 wl_list_for_each_safe(state, next, &ws->focus_list, link)
902 if (state->seat == seat)
903 wl_list_remove(&state->link);
904}
905
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200906static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100907workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908{
909 struct workspace *ws = malloc(sizeof *ws);
910 if (ws == NULL)
911 return NULL;
912
Quentin Glidic82681572016-12-17 13:40:51 +0100913 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914
Jonas Ådahl04769742012-06-13 00:01:24 +0200915 wl_list_init(&ws->focus_list);
916 wl_list_init(&ws->seat_destroyed_listener.link);
917 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 ws->fsurf_front = NULL;
919 ws->fsurf_back = NULL;
920 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200921
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200922 return ws;
923}
924
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925static int
926workspace_is_empty(struct workspace *ws)
927{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300928 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200929}
930
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200931static struct workspace *
932get_workspace(struct desktop_shell *shell, unsigned int index)
933{
934 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200935 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200936 pws += index;
937 return *pws;
938}
939
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800940struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941get_current_workspace(struct desktop_shell *shell)
942{
943 return get_workspace(shell, shell->workspaces.current);
944}
945
946static void
947activate_workspace(struct desktop_shell *shell, unsigned int index)
948{
949 struct workspace *ws;
950
951 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100952 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200953
954 shell->workspaces.current = index;
955}
956
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200957static unsigned int
958get_output_height(struct weston_output *output)
959{
960 return abs(output->region.extents.y1 - output->region.extents.y2);
961}
962
963static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200966 struct weston_transform *transform;
967
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 if (is_focus_view(view)) {
969 struct focus_surface *fsurf = get_focus_surface(view->surface);
970 transform = &fsurf->workspace_transform;
971 } else {
972 struct shell_surface *shsurf = get_shell_surface(view->surface);
973 transform = &shsurf->workspace_transform;
974 }
975
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200979
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 weston_matrix_init(&transform->matrix);
981 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984}
985
986static void
987workspace_translate_out(struct workspace *ws, double fraction)
988{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990 unsigned int height;
991 double d;
992
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300993 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995 d = height * fraction;
996
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998 }
999}
1000
1001static void
1002workspace_translate_in(struct workspace *ws, double fraction)
1003{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 unsigned int height;
1006 double d;
1007
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001008 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010
1011 if (fraction > 0)
1012 d = -(height - height * fraction);
1013 else
1014 d = height + height * fraction;
1015
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 }
1018}
1019
1020static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021reverse_workspace_change_animation(struct desktop_shell *shell,
1022 unsigned int index,
1023 struct workspace *from,
1024 struct workspace *to)
1025{
1026 shell->workspaces.current = index;
1027
1028 shell->workspaces.anim_to = to;
1029 shell->workspaces.anim_from = from;
1030 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001031 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032
Quentin Glidic82681572016-12-17 13:40:51 +01001033 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1034 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1035
Scott Moreau4272e632012-08-13 09:58:41 -06001036 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001037}
1038
1039static void
1040workspace_deactivate_transforms(struct workspace *ws)
1041{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001043 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001045 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001046 if (is_focus_view(view)) {
1047 struct focus_surface *fsurf = get_focus_surface(view->surface);
1048 transform = &fsurf->workspace_transform;
1049 } else {
1050 struct shell_surface *shsurf = get_shell_surface(view->surface);
1051 transform = &shsurf->workspace_transform;
1052 }
1053
1054 if (!wl_list_empty(&transform->link)) {
1055 wl_list_remove(&transform->link);
1056 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001057 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059 }
1060}
1061
1062static void
1063finish_workspace_change_animation(struct desktop_shell *shell,
1064 struct workspace *from,
1065 struct workspace *to)
1066{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001067 struct weston_view *view;
1068
Scott Moreau4272e632012-08-13 09:58:41 -06001069 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001071 /* Views that extend past the bottom of the output are still
1072 * visible after the workspace animation ends but before its layer
1073 * is hidden. In that case, we need to damage below those views so
1074 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001075 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001076 weston_view_damage_below(view);
1077
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 wl_list_remove(&shell->workspaces.animation.link);
1079 workspace_deactivate_transforms(from);
1080 workspace_deactivate_transforms(to);
1081 shell->workspaces.anim_to = NULL;
1082
Quentin Glidic82681572016-12-17 13:40:51 +01001083 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001084}
1085
1086static void
1087animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001088 struct weston_output *output,
1089 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090{
1091 struct desktop_shell *shell =
1092 container_of(animation, struct desktop_shell,
1093 workspaces.animation);
1094 struct workspace *from = shell->workspaces.anim_from;
1095 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001096 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 double x, y;
1098
1099 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1100 finish_workspace_change_animation(shell, from, to);
1101 return;
1102 }
1103
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001104 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001106 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001108 timespec_add_msec(&shell->workspaces.anim_timestamp,
1109 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001111 -(asin(1.0 - shell->workspaces.anim_current) *
1112 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1113 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 }
1115
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117
1118 /*
1119 * x = [0, π/2]
1120 * y(x) = sin(x)
1121 */
1122 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1123 y = sin(x);
1124
1125 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001126 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1129 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1130 shell->workspaces.anim_current = y;
1131
Scott Moreau4272e632012-08-13 09:58:41 -06001132 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001134 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136}
1137
1138static void
1139animate_workspace_change(struct desktop_shell *shell,
1140 unsigned int index,
1141 struct workspace *from,
1142 struct workspace *to)
1143{
1144 struct weston_output *output;
1145
1146 int dir;
1147
1148 if (index > shell->workspaces.current)
1149 dir = -1;
1150 else
1151 dir = 1;
1152
1153 shell->workspaces.current = index;
1154
1155 shell->workspaces.anim_dir = dir;
1156 shell->workspaces.anim_from = from;
1157 shell->workspaces.anim_to = to;
1158 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001159 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160
1161 output = container_of(shell->compositor->output_list.next,
1162 struct weston_output, link);
1163 wl_list_insert(&output->animation_list,
1164 &shell->workspaces.animation.link);
1165
Quentin Glidic82681572016-12-17 13:40:51 +01001166 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1167 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168
1169 workspace_translate_in(to, 0);
1170
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001171 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001172
Scott Moreau4272e632012-08-13 09:58:41 -06001173 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174}
1175
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001176static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001177update_workspace(struct desktop_shell *shell, unsigned int index,
1178 struct workspace *from, struct workspace *to)
1179{
1180 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001181 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1182 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001183}
1184
1185static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186change_workspace(struct desktop_shell *shell, unsigned int index)
1187{
1188 struct workspace *from;
1189 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001190 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191
1192 if (index == shell->workspaces.current)
1193 return;
1194
1195 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001196 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001197 return;
1198
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001199 from = get_current_workspace(shell);
1200 to = get_workspace(shell, index);
1201
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 if (shell->workspaces.anim_from == to &&
1203 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001205 reverse_workspace_change_animation(shell, index, from, to);
1206 return;
1207 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001208
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209 if (shell->workspaces.anim_to != NULL)
1210 finish_workspace_change_animation(shell,
1211 shell->workspaces.anim_from,
1212 shell->workspaces.anim_to);
1213
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001215
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 if (shell->focus_animation_type != ANIMATION_NONE) {
1217 wl_list_for_each(state, &from->focus_list, link)
1218 if (state->keyboard_focus)
1219 animate_focus_change(shell, from,
1220 get_default_view(state->keyboard_focus), NULL);
1221
1222 wl_list_for_each(state, &to->focus_list, link)
1223 if (state->keyboard_focus)
1224 animate_focus_change(shell, to,
1225 NULL, get_default_view(state->keyboard_focus));
1226 }
1227
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 if (workspace_is_empty(to) && workspace_is_empty(from))
1229 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001230 else
1231 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001232}
1233
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234static bool
1235workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1236{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001237 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 struct wl_list *e;
1239
1240 if (wl_list_empty(list))
1241 return false;
1242
1243 e = list->next;
1244
1245 if (e->next != list)
1246 return false;
1247
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001248 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249}
1250
1251static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001252surface_keyboard_focus_lost(struct weston_surface *surface)
1253{
1254 struct weston_compositor *compositor = surface->compositor;
1255 struct weston_seat *seat;
1256 struct weston_surface *focus;
1257
1258 wl_list_for_each(seat, &compositor->seat_list, link) {
1259 struct weston_keyboard *keyboard =
1260 weston_seat_get_keyboard(seat);
1261
1262 if (!keyboard)
1263 continue;
1264
1265 focus = weston_surface_get_main_surface(keyboard->focus);
1266 if (focus == surface)
1267 weston_keyboard_set_focus(keyboard, NULL);
1268 }
1269}
1270
1271static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 unsigned int index)
1275{
Derek Foreman1281a362015-07-31 16:55:32 -05001276 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001277 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279 struct shell_surface *shsurf;
1280 struct workspace *from;
1281 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001282 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283
Derek Foreman1281a362015-07-31 16:55:32 -05001284 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 view = get_default_view(surface);
1286 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001287 index == shell->workspaces.current ||
1288 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 return;
1290
1291 from = get_current_workspace(shell);
1292 to = get_workspace(shell, index);
1293
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001294 weston_layer_entry_remove(&view->layer_link);
1295 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296
Philip Withnall659163d2013-11-25 18:01:36 +00001297 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001298 if (shsurf != NULL)
1299 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001300
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302 drop_focus_state(shell, from, surface);
1303
1304 if (shell->workspaces.anim_from == to &&
1305 shell->workspaces.anim_to == from) {
1306 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001307
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 return;
1309 }
1310
1311 if (shell->workspaces.anim_to != NULL)
1312 finish_workspace_change_animation(shell,
1313 shell->workspaces.anim_from,
1314 shell->workspaces.anim_to);
1315
1316 if (workspace_is_empty(from) &&
1317 workspace_has_only(to, surface))
1318 update_workspace(shell, index, from, to);
1319 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001320 if (shsurf != NULL &&
1321 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001322 wl_list_insert(&shell->workspaces.anim_sticky_list,
1323 &shsurf->workspace_transform.link);
1324
1325 animate_workspace_change(shell, index, from, to);
1326 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
Jonas Ådahl8538b222012-08-29 22:13:03 +02001328 state = ensure_focus_state(shell, seat);
1329 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001330 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001331}
1332
1333static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001334touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001335 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001336{
1337}
1338
1339static void
1340touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1341{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001342 struct weston_touch_move_grab *move =
1343 (struct weston_touch_move_grab *) container_of(
1344 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001345
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001346 if (touch_id == 0)
1347 move->active = 0;
1348
Jonas Ådahl9484b692013-12-02 22:05:03 +01001349 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001350 shell_touch_grab_end(&move->base);
1351 free(move);
1352 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001353}
1354
1355static void
1356touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001357 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001358{
1359 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1360 struct shell_surface *shsurf = move->base.shsurf;
1361 struct weston_surface *es;
1362 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1363 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1364
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001365 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001366 return;
1367
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001368 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001370 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001371
1372 weston_compositor_schedule_repaint(es->compositor);
1373}
1374
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001375static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001376touch_move_grab_frame(struct weston_touch_grab *grab)
1377{
1378}
1379
1380static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001381touch_move_grab_cancel(struct weston_touch_grab *grab)
1382{
1383 struct weston_touch_move_grab *move =
1384 (struct weston_touch_move_grab *) container_of(
1385 grab, struct shell_touch_grab, grab);
1386
1387 shell_touch_grab_end(&move->base);
1388 free(move);
1389}
1390
Rusty Lynch1084da52013-08-15 09:10:08 -07001391static const struct weston_touch_grab_interface touch_move_grab_interface = {
1392 touch_move_grab_down,
1393 touch_move_grab_up,
1394 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001395 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001396 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001397};
1398
1399static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001400surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001401{
1402 struct weston_touch_move_grab *move;
1403
1404 if (!shsurf)
1405 return -1;
1406
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001407 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1408 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001409 return 0;
1410
1411 move = malloc(sizeof *move);
1412 if (!move)
1413 return -1;
1414
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001415 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001417 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001419 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001420
1421 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001422 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001423
1424 return 0;
1425}
1426
1427static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001428noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001429{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001430}
1431
1432static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001433noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001434 const struct timespec *time,
1435 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001436{
1437}
1438
1439static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001440noop_grab_axis_source(struct weston_pointer_grab *grab,
1441 uint32_t source)
1442{
1443}
1444
1445static void
1446noop_grab_frame(struct weston_pointer_grab *grab)
1447{
1448}
1449
1450static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001451constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1452{
1453 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001454 struct weston_surface *surface =
1455 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001456 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001457 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001458 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001459 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001460 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001461
1462 x = wl_fixed_to_int(pointer->x + move->dx);
1463 y = wl_fixed_to_int(pointer->y + move->dy);
1464
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001465 if (shsurf->shell->panel_position ==
1466 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 get_output_work_area(shsurf->shell, surface->output, &area);
1468 geometry =
1469 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001470
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001471 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001472 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 y = area.y + safety - geometry.height
1474 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001475
1476 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001477 y + geometry.y < area.y)
1478 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001479 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001480
1481 *cx = x;
1482 *cy = y;
1483}
1484
1485static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001486move_grab_motion(struct weston_pointer_grab *grab,
1487 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001488 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001489{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001491 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001492 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001493 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001494 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001495
Jonas Ådahld2510102014-10-05 21:39:14 +02001496 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001497 if (!shsurf)
1498 return;
1499
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001500 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1501
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001502 constrain_position(move, &cx, &cy);
1503
1504 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001505
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001506 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507}
1508
1509static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001511 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001513 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1514 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001515 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001516 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001517
Daniel Stone4dbadb12012-05-30 16:31:51 +01001518 if (pointer->button_count == 0 &&
1519 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001520 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001521 free(grab);
1522 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001523}
1524
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001525static void
1526move_grab_cancel(struct weston_pointer_grab *grab)
1527{
1528 struct shell_grab *shell_grab =
1529 container_of(grab, struct shell_grab, grab);
1530
1531 shell_grab_end(shell_grab);
1532 free(grab);
1533}
1534
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001535static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001536 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001537 move_grab_motion,
1538 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001539 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001540 noop_grab_axis_source,
1541 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001542 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001543};
1544
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001545static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001546surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001547 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548{
1549 struct weston_move_grab *move;
1550
1551 if (!shsurf)
1552 return -1;
1553
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001554 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001555 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1556 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001557 return 0;
1558
1559 move = malloc(sizeof *move);
1560 if (!move)
1561 return -1;
1562
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001564 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001566 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001567 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568
1569 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001570 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571
1572 return 0;
1573}
1574
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575struct weston_resize_grab {
1576 struct shell_grab base;
1577 uint32_t edges;
1578 int32_t width, height;
1579};
1580
1581static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001582resize_grab_motion(struct weston_pointer_grab *grab,
1583 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001584 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585{
1586 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001587 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588 struct shell_surface *shsurf = resize->base.shsurf;
1589 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001590 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591 wl_fixed_t from_x, from_y;
1592 wl_fixed_t to_x, to_y;
1593
Jonas Ådahld2510102014-10-05 21:39:14 +02001594 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001595
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001596 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 return;
1598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_from_global_fixed(shsurf->view,
1600 pointer->grab_x, pointer->grab_y,
1601 &from_x, &from_y);
1602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604
1605 width = resize->width;
1606 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1607 width += wl_fixed_to_int(from_x - to_x);
1608 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1609 width += wl_fixed_to_int(to_x - from_x);
1610 }
1611
1612 height = resize->height;
1613 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1614 height += wl_fixed_to_int(from_y - to_y);
1615 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1616 height += wl_fixed_to_int(to_y - from_y);
1617 }
1618
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001619 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1620 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1621
1622 min_size.width = MAX(1, min_size.width);
1623 min_size.height = MAX(1, min_size.height);
1624
1625 if (width < min_size.width)
1626 width = min_size.width;
1627 else if (max_size.width > 0 && width > max_size.width)
1628 width = max_size.width;
1629 if (height < min_size.height)
1630 height = min_size.height;
1631 else if (max_size.width > 0 && width > max_size.width)
1632 width = max_size.width;
1633 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634}
1635
1636static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001637resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001638 const struct timespec *time,
1639 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640{
1641 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001642 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001643 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001644 struct weston_desktop_surface *desktop_surface =
1645 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646
1647 if (pointer->button_count == 0 &&
1648 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001649 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001650 shell_grab_end(&resize->base);
1651 free(grab);
1652 }
1653}
1654
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001655static void
1656resize_grab_cancel(struct weston_pointer_grab *grab)
1657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001659 struct weston_desktop_surface *desktop_surface =
1660 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001661
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001662 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001663 shell_grab_end(&resize->base);
1664 free(grab);
1665}
1666
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001667static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668 noop_grab_focus,
1669 resize_grab_motion,
1670 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001671 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001672 noop_grab_axis_source,
1673 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675};
1676
Giulio Camuffob8366642013-04-25 13:57:46 +03001677/*
1678 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001679 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001680static void
1681surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1682 int32_t *y, int32_t *w, int32_t *h) {
1683 pixman_region32_t region;
1684 pixman_box32_t *box;
1685 struct weston_subsurface *subsurface;
1686
1687 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 surface->width,
1689 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001690
1691 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1692 pixman_region32_union_rect(&region, &region,
1693 subsurface->position.x,
1694 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 subsurface->surface->width,
1696 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001697 }
1698
1699 box = pixman_region32_extents(&region);
1700 if (x)
1701 *x = box->x1;
1702 if (y)
1703 *y = box->y1;
1704 if (w)
1705 *w = box->x2 - box->x1;
1706 if (h)
1707 *h = box->y2 - box->y1;
1708
1709 pixman_region32_fini(&region);
1710}
1711
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712static int
1713surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001714 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715{
1716 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001717 const unsigned resize_topbottom =
1718 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1719 const unsigned resize_leftright =
1720 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1721 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001722 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001724 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001725 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1726 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 return 0;
1728
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001729 /* Check for invalid edge combinations. */
1730 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1731 (edges & resize_topbottom) == resize_topbottom ||
1732 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733 return 0;
1734
1735 resize = malloc(sizeof *resize);
1736 if (!resize)
1737 return -1;
1738
1739 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001741 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1742 resize->width = geometry.width;
1743 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001744
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001745 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001746 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001748 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749
1750 return 0;
1751}
1752
1753static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001754busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001757 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001758 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001760 wl_fixed_t sx, sy;
1761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 view = weston_compositor_pick_view(pointer->seat->compositor,
1763 pointer->x, pointer->y,
1764 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001765 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001766
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001767 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001768 shell_grab_end(grab);
1769 free(grab);
1770 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001771}
1772
1773static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001774busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1775 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001776 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001777{
Jonas Ådahld2510102014-10-05 21:39:14 +02001778 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001779}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001781static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001782busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001783 const struct timespec *time,
1784 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001786 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001787 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001788 struct weston_pointer *pointer = grab->grab.pointer;
1789 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001790
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001791 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001792 activate(shsurf->shell, shsurf->view, seat,
1793 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001794 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001795 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001796 activate(shsurf->shell, shsurf->view, seat,
1797 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001798 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800}
1801
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001802static void
1803busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1804{
1805 struct shell_grab *grab = (struct shell_grab *) base;
1806
1807 shell_grab_end(grab);
1808 free(grab);
1809}
1810
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001811static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812 busy_cursor_grab_focus,
1813 busy_cursor_grab_motion,
1814 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001815 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001816 noop_grab_axis_source,
1817 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001818 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819};
1820
1821static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822handle_pointer_focus(struct wl_listener *listener, void *data)
1823{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001826 struct shell_surface *shsurf;
1827 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001830 return;
1831
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001832 shsurf = get_shell_surface(view->surface);
1833 if (!shsurf)
1834 return;
1835
1836 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1837
1838 if (shsurf->unresponsive)
1839 set_busy_cursor(shsurf, pointer);
1840 else
1841 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842}
1843
1844static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001845shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1846{
1847 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001848 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001849}
1850
1851static void
1852shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1853{
1854 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001855 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001856}
1857
1858static void
1859handle_keyboard_focus(struct wl_listener *listener, void *data)
1860{
1861 struct weston_keyboard *keyboard = data;
1862 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1863
1864 if (seat->focused_surface) {
1865 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1866 if (shsurf)
1867 shell_surface_lose_keyboard_focus(shsurf);
1868 }
1869
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001870 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001871
1872 if (seat->focused_surface) {
1873 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1874 if (shsurf)
1875 shell_surface_gain_keyboard_focus(shsurf);
1876 }
1877}
1878
Philip Withnall07926d92013-11-25 18:01:40 +00001879/* The surface will be inserted into the list immediately after the link
1880 * returned by this function (i.e. will be stacked immediately above the
1881 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001882static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001883shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1884{
1885 struct workspace *ws;
1886
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001887 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1888 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001889 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001890 }
1891
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001892 /* Move the surface to a normal workspace layer so that surfaces
1893 * which were previously fullscreen or transient are no longer
1894 * rendered on top. */
1895 ws = get_current_workspace(shsurf->shell);
1896 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001897}
1898
Philip Withnall648a4dd2013-11-25 18:01:44 +00001899static void
1900shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1901{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001902 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001903}
1904
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001905/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001906 * geometry to ensure the changes are redrawn.
1907 *
1908 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1909 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001910static void
1911shell_surface_update_layer(struct shell_surface *shsurf)
1912{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001913 struct weston_surface *surface =
1914 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001915 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001916
1917 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1918
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001919 if (new_layer_link == NULL)
1920 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001921 if (new_layer_link == &shsurf->view->layer_link)
1922 return;
1923
1924 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001925 weston_layer_entry_remove(&shsurf->view->layer_link);
1926 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001927 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001928 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001929
1930 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001931}
1932
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001933static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001934shell_surface_set_output(struct shell_surface *shsurf,
1935 struct weston_output *output)
1936{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001937 struct weston_surface *es =
1938 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001939
1940 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001941 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001942 if (output)
1943 shsurf->output = output;
1944 else if (es->output)
1945 shsurf->output = es->output;
1946 else
1947 shsurf->output = get_default_output(es->compositor);
1948}
1949
1950static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001951weston_view_set_initial_position(struct weston_view *view,
1952 struct desktop_shell *shell);
1953
1954static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001955unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001956{
Philip Withnallf85fe842013-11-25 18:01:37 +00001957 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001958 wl_list_remove(&shsurf->fullscreen.transform.link);
1959 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001960
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961 if (shsurf->fullscreen.black_view)
1962 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1963 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001964
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001965 if (shsurf->saved_position_valid)
1966 weston_view_set_position(shsurf->view,
1967 shsurf->saved_x, shsurf->saved_y);
1968 else
1969 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001970 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001971
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001972 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001974 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001975 shsurf->saved_rotation_valid = false;
1976 }
Alex Wu4539b082012-03-01 12:57:46 +08001977}
1978
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001979static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001980unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001981{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001982 struct weston_surface *surface =
1983 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1984
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001985 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001986 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001987
1988 if (shsurf->saved_position_valid)
1989 weston_view_set_position(shsurf->view,
1990 shsurf->saved_x, shsurf->saved_y);
1991 else
1992 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001993 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001994
1995 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 wl_list_insert(&shsurf->view->geometry.transformation_list,
1997 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001998 shsurf->saved_rotation_valid = false;
1999 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002000}
2001
Philip Withnallbecb77e2013-11-25 18:01:30 +00002002static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002003set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002004{
2005 struct shell_surface *shsurf;
2006 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002007 struct weston_view *view;
2008
2009 view = get_default_view(surface);
2010 if (!view)
2011 return;
2012
2013 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2014
2015 shsurf = get_shell_surface(surface);
2016 current_ws = get_current_workspace(shsurf->shell);
2017
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002018 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002019 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002020
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002021 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002022 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002023
2024 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002025 weston_view_damage_below(view);
2026}
2027
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002028
Tiago Vignattibe143262012-04-16 17:31:41 +03002029static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002030shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002031{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002032 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002033}
2034
Pekka Paalanen8274d902014-08-06 19:36:51 +03002035static int
2036black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2037{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002038 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002039 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002040 int n;
2041 int rem;
2042 int ret;
2043
2044 n = snprintf(buf, len, "black background surface for ");
2045 if (n < 0)
2046 return n;
2047
2048 rem = (int)len - n;
2049 if (rem < 0)
2050 rem = 0;
2051
2052 if (fs_surface->get_label)
2053 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2054 else
2055 ret = snprintf(buf + n, rem, "<unknown>");
2056
2057 if (ret < 0)
2058 return n;
2059
2060 return n + ret;
2061}
2062
Alex Wu21858432012-04-01 20:13:08 +08002063static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002064black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002065
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002067create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002068 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002069 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002070{
2071 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002073
2074 surface = weston_surface_create(ec);
2075 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002076 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002077 return NULL;
2078 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 view = weston_view_create(surface);
2080 if (surface == NULL) {
2081 weston_log("no memory\n");
2082 weston_surface_destroy(surface);
2083 return NULL;
2084 }
Alex Wu4539b082012-03-01 12:57:46 +08002085
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002086 surface->committed = black_surface_committed;
2087 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002088 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002089 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002090 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002091 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002092 pixman_region32_fini(&surface->input);
2093 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002094
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002095 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002096 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097
2098 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002099}
2100
Philip Withnalled8f1a92013-11-25 18:01:43 +00002101static void
2102shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2103{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002104 struct weston_surface *surface =
2105 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002106 struct weston_output *output = shsurf->fullscreen_output;
2107
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002108 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002109
2110 if (!shsurf->fullscreen.black_view)
2111 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002112 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002113 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002114 output->x, output->y,
2115 output->width,
2116 output->height);
2117
2118 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002119 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2120 weston_layer_entry_insert(&shsurf->view->layer_link,
2121 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002122 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002123 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002124
Armin Krezović4663aca2016-06-30 06:04:29 +02002125 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002126 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002127}
2128
Alex Wu4539b082012-03-01 12:57:46 +08002129/* Create black surface and append it to the associated fullscreen surface.
2130 * Handle size dismatch and positioning according to the method. */
2131static void
2132shell_configure_fullscreen(struct shell_surface *shsurf)
2133{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002134 struct weston_surface *surface =
2135 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002136 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002137
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002138 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002139 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002140 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2141 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002142
Philip Withnalled8f1a92013-11-25 18:01:43 +00002143 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002144
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002145 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002146 &surf_width, &surf_height);
2147
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002148 if (surface->buffer_ref.buffer)
2149 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002150}
2151
Alex Wu4539b082012-03-01 12:57:46 +08002152static void
2153shell_map_fullscreen(struct shell_surface *shsurf)
2154{
Alex Wubd3354b2012-04-17 17:20:49 +08002155 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002156}
2157
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002158static struct weston_output *
2159get_focused_output(struct weston_compositor *compositor)
2160{
2161 struct weston_seat *seat;
2162 struct weston_output *output = NULL;
2163
2164 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002165 struct weston_touch *touch = weston_seat_get_touch(seat);
2166 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2167 struct weston_keyboard *keyboard =
2168 weston_seat_get_keyboard(seat);
2169
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002170 /* Priority has touch focus, then pointer and
2171 * then keyboard focus. We should probably have
2172 * three for loops and check frist for touch,
2173 * then for pointer, etc. but unless somebody has some
2174 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002175 if (touch && touch->focus)
2176 output = touch->focus->output;
2177 else if (pointer && pointer->focus)
2178 output = pointer->focus->output;
2179 else if (keyboard && keyboard->focus)
2180 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002181
2182 if (output)
2183 break;
2184 }
2185
2186 return output;
2187}
2188
2189static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002190destroy_shell_seat(struct wl_listener *listener, void *data)
2191{
2192 struct shell_seat *shseat =
2193 container_of(listener,
2194 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002195
2196 wl_list_remove(&shseat->seat_destroy_listener.link);
2197 free(shseat);
2198}
2199
Jason Ekstrand024177c2014-04-21 19:42:58 -05002200static void
2201shell_seat_caps_changed(struct wl_listener *listener, void *data)
2202{
Derek Foreman1281a362015-07-31 16:55:32 -05002203 struct weston_keyboard *keyboard;
2204 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002205 struct shell_seat *seat;
2206
2207 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002208 keyboard = weston_seat_get_keyboard(seat->seat);
2209 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002210
Derek Foreman1281a362015-07-31 16:55:32 -05002211 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002212 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002213 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002214 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002215 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002216 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002217 wl_list_init(&seat->keyboard_focus_listener.link);
2218 }
2219
Derek Foreman1281a362015-07-31 16:55:32 -05002220 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002221 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002222 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002223 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002224 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002225 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002226 wl_list_init(&seat->pointer_focus_listener.link);
2227 }
2228}
2229
Giulio Camuffo5085a752013-03-25 21:42:45 +01002230static struct shell_seat *
2231create_shell_seat(struct weston_seat *seat)
2232{
2233 struct shell_seat *shseat;
2234
2235 shseat = calloc(1, sizeof *shseat);
2236 if (!shseat) {
2237 weston_log("no memory to allocate shell seat\n");
2238 return NULL;
2239 }
2240
2241 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002242
2243 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2244 wl_signal_add(&seat->destroy_signal,
2245 &shseat->seat_destroy_listener);
2246
Jason Ekstrand024177c2014-04-21 19:42:58 -05002247 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2248 wl_list_init(&shseat->keyboard_focus_listener.link);
2249
2250 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2251 wl_list_init(&shseat->pointer_focus_listener.link);
2252
2253 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2254 wl_signal_add(&seat->updated_caps_signal,
2255 &shseat->caps_changed_listener);
2256 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2257
Giulio Camuffo5085a752013-03-25 21:42:45 +01002258 return shseat;
2259}
2260
2261static struct shell_seat *
2262get_shell_seat(struct weston_seat *seat)
2263{
2264 struct wl_listener *listener;
2265
2266 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002267 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002268
2269 return container_of(listener,
2270 struct shell_seat, seat_destroy_listener);
2271}
2272
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002273static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002274fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002275{
2276 struct shell_surface *shsurf = data;
2277
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002278 weston_surface_destroy(shsurf->view->surface);
2279 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002280}
2281
2282static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002283fade_out_done(struct weston_view_animation *animation, void *data)
2284{
2285 struct shell_surface *shsurf = data;
2286 struct wl_event_loop *loop;
2287
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002288 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002289
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002290 if (weston_view_is_mapped(shsurf->view)) {
2291 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002292 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002293 }
2294}
2295
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002296struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002297get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002298{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002299 if (weston_surface_is_desktop_surface(surface)) {
2300 struct weston_desktop_surface *desktop_surface =
2301 weston_surface_get_desktop_surface(surface);
2302 return weston_desktop_surface_get_user_data(desktop_surface);
2303 }
2304 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002305}
2306
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002307/*
2308 * libweston-desktop
2309 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002310
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002311static void
2312desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2313 void *shell)
2314{
2315 struct weston_desktop_client *client =
2316 weston_desktop_surface_get_client(desktop_surface);
2317 struct wl_client *wl_client =
2318 weston_desktop_client_get_client(client);
2319 struct weston_view *view;
2320 struct shell_surface *shsurf;
2321 struct weston_surface *surface =
2322 weston_desktop_surface_get_surface(desktop_surface);
2323
2324 view = weston_desktop_surface_create_view(desktop_surface);
2325 if (!view)
2326 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002327
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002328 shsurf = calloc(1, sizeof *shsurf);
2329 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002330 if (wl_client)
2331 wl_client_post_no_memory(wl_client);
2332 else
2333 weston_log("no memory to allocate shell surface\n");
2334 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002335 }
2336
Pekka Paalanen8274d902014-08-06 19:36:51 +03002337 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002338
Tiago Vignattibc052c92012-04-19 16:18:18 +03002339 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002340 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002341 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002342 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002343 shsurf->desktop_surface = desktop_surface;
2344 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002345 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002346 wl_list_init(&shsurf->fullscreen.transform.link);
2347
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002348 shsurf->output = get_default_output(shsurf->shell->compositor);
2349
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002350 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002351
Pekka Paalanen460099f2012-01-20 16:48:25 +02002352 /* empty when not in use */
2353 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002354 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002355
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002356 wl_list_init(&shsurf->workspace_transform.link);
2357
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002358 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2359 weston_desktop_surface_set_activated(desktop_surface,
2360 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002361}
2362
Tiago Vignattibc052c92012-04-19 16:18:18 +03002363static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002364desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2365 void *shell)
2366{
2367 struct shell_surface *shsurf =
2368 weston_desktop_surface_get_user_data(desktop_surface);
2369 struct weston_surface *surface =
2370 weston_desktop_surface_get_surface(desktop_surface);
2371
2372 if (!shsurf)
2373 return;
2374
2375 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2376
2377 if (shsurf->fullscreen.black_view)
2378 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2379
2380 weston_surface_set_label_func(surface, NULL);
2381 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2382 shsurf->desktop_surface = NULL;
2383
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002384 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002385 if (weston_surface_is_mapped(surface) &&
2386 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2387 pixman_region32_fini(&surface->pending.input);
2388 pixman_region32_init(&surface->pending.input);
2389 pixman_region32_fini(&surface->input);
2390 pixman_region32_init(&surface->input);
2391 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2392 fade_out_done, shsurf);
2393 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002394 weston_view_destroy(shsurf->view);
2395 free(shsurf);
2396 }
2397}
2398
2399static void
2400set_maximized_position(struct desktop_shell *shell,
2401 struct shell_surface *shsurf)
2402{
2403 pixman_rectangle32_t area;
2404 struct weston_geometry geometry;
2405
2406 get_output_work_area(shell, shsurf->output, &area);
2407 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2408
2409 weston_view_set_position(shsurf->view,
2410 area.x - geometry.x,
2411 area.y - geometry.y);
2412}
2413
2414static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002415set_position_from_xwayland(struct shell_surface *shsurf)
2416{
2417 struct weston_geometry geometry;
2418 float x;
2419 float y;
2420
2421 assert(shsurf->xwayland.is_set);
2422
2423 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2424 x = shsurf->xwayland.x - geometry.x;
2425 y = shsurf->xwayland.y - geometry.y;
2426
2427 weston_view_set_position(shsurf->view, x, y);
2428
2429#ifdef WM_DEBUG
2430 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2431 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2432 geometry.x, geometry.y, x, y);
2433#endif
2434}
2435
2436static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437map(struct desktop_shell *shell, struct shell_surface *shsurf,
2438 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002439{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002440 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002441 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2442 struct weston_compositor *compositor = shell->compositor;
2443 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002444
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002445 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002446 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002447 center_on_output(shsurf->view, shsurf->fullscreen_output);
2448 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002449 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002450 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002451 } else if (shsurf->xwayland.is_set) {
2452 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002453 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002454 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002455 }
2456
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002457 /* Surface stacking order, see also activate(). */
2458 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002459
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002460 weston_view_update_transform(shsurf->view);
2461 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002462 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002463 surface->output = shsurf->output;
2464 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002465 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002466
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002467 if (!shell->locked) {
2468 wl_list_for_each(seat, &compositor->seat_list, link)
2469 activate(shell, shsurf->view, seat,
2470 WESTON_ACTIVATE_FLAG_CONFIGURE);
2471 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002472
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002473 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 switch (shell->win_animation_type) {
2475 case ANIMATION_FADE:
2476 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2477 break;
2478 case ANIMATION_ZOOM:
2479 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2480 break;
2481 case ANIMATION_NONE:
2482 default:
2483 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002484 }
2485 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002486}
2487
2488static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002489desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2490 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002491{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002492 struct shell_surface *shsurf =
2493 weston_desktop_surface_get_user_data(desktop_surface);
2494 struct weston_surface *surface =
2495 weston_desktop_surface_get_surface(desktop_surface);
2496 struct weston_view *view = shsurf->view;
2497 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002498 bool was_fullscreen;
2499 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500
2501 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002502 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002504 was_fullscreen = shsurf->state.fullscreen;
2505 was_maximized = shsurf->state.maximized;
2506
2507 shsurf->state.fullscreen =
2508 weston_desktop_surface_get_fullscreen(desktop_surface);
2509 shsurf->state.maximized =
2510 weston_desktop_surface_get_maximized(desktop_surface);
2511
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002512 if (!weston_surface_is_mapped(surface)) {
2513 map(shell, shsurf, sx, sy);
2514 surface->is_mapped = true;
2515 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2516 ++surface->ref_count;
2517 return;
2518 }
2519
2520 if (sx == 0 && sy == 0 &&
2521 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002522 shsurf->last_height == surface->height &&
2523 was_fullscreen == shsurf->state.fullscreen &&
2524 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002525 return;
2526
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002527 if (was_fullscreen)
2528 unset_fullscreen(shsurf);
2529 if (was_maximized)
2530 unset_maximized(shsurf);
2531
Quentin Glidic920cf042016-08-16 14:26:20 +02002532 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2533 !shsurf->saved_position_valid) {
2534 shsurf->saved_x = shsurf->view->geometry.x;
2535 shsurf->saved_y = shsurf->view->geometry.y;
2536 shsurf->saved_position_valid = true;
2537
2538 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2539 wl_list_remove(&shsurf->rotation.transform.link);
2540 wl_list_init(&shsurf->rotation.transform.link);
2541 weston_view_geometry_dirty(shsurf->view);
2542 shsurf->saved_rotation_valid = true;
2543 }
2544 }
2545
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002546 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002547 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002548 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549 set_maximized_position(shell, shsurf);
2550 surface->output = shsurf->output;
2551 } else {
2552 float from_x, from_y;
2553 float to_x, to_y;
2554 float x, y;
2555
2556 if (shsurf->resize_edges) {
2557 sx = 0;
2558 sy = 0;
2559 }
2560
2561 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2562 sx = shsurf->last_width - surface->width;
2563 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2564 sy = shsurf->last_height - surface->height;
2565
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002566 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2567 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2568 x = shsurf->view->geometry.x + to_x - from_x;
2569 y = shsurf->view->geometry.y + to_y - from_y;
2570
2571 weston_view_set_position(shsurf->view, x, y);
2572 }
2573
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002574 shsurf->last_width = surface->width;
2575 shsurf->last_height = surface->height;
2576
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002577 /* XXX: would a fullscreen surface need the same handling? */
2578 if (surface->output) {
2579 wl_list_for_each(view, &surface->views, surface_link)
2580 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002581 }
2582}
2583
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002584static void
2585set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2586 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002587{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002588 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2589 struct weston_surface *surface =
2590 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2591 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002592
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002593 if (fullscreen) {
2594 /* handle clients launching in fullscreen */
2595 if (output == NULL && !weston_surface_is_mapped(surface)) {
2596 /* Set the output to the one that has focus currently. */
2597 output = get_focused_output(surface->compositor);
2598 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002599
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002600 shell_surface_set_output(shsurf, output);
2601 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002602
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002603 width = shsurf->output->width;
2604 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002605 }
2606 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2607 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002608}
2609
2610static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002611desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2612 struct weston_seat *seat, uint32_t serial, void *shell)
2613{
2614 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2615 struct weston_touch *touch = weston_seat_get_touch(seat);
2616 struct shell_surface *shsurf =
2617 weston_desktop_surface_get_user_data(desktop_surface);
2618 struct weston_surface *surface =
2619 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2620 struct wl_resource *resource = surface->resource;
2621 struct weston_surface *focus;
2622
2623 if (pointer &&
2624 pointer->focus &&
2625 pointer->button_count > 0 &&
2626 pointer->grab_serial == serial) {
2627 focus = weston_surface_get_main_surface(pointer->focus->surface);
2628 if ((focus == surface) &&
2629 (surface_move(shsurf, pointer, true) < 0))
2630 wl_resource_post_no_memory(resource);
2631 } else if (touch &&
2632 touch->focus &&
2633 touch->grab_serial == serial) {
2634 focus = weston_surface_get_main_surface(touch->focus->surface);
2635 if ((focus == surface) &&
2636 (surface_touch_move(shsurf, touch) < 0))
2637 wl_resource_post_no_memory(resource);
2638 }
2639}
2640
2641static void
2642desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2643 struct weston_seat *seat, uint32_t serial,
2644 enum weston_desktop_surface_edge edges, void *shell)
2645{
2646 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2647 struct shell_surface *shsurf =
2648 weston_desktop_surface_get_user_data(desktop_surface);
2649 struct weston_surface *surface =
2650 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2651 struct wl_resource *resource = surface->resource;
2652 struct weston_surface *focus;
2653
2654 if (!pointer ||
2655 pointer->button_count == 0 ||
2656 pointer->grab_serial != serial ||
2657 pointer->focus == NULL)
2658 return;
2659
2660 focus = weston_surface_get_main_surface(pointer->focus->surface);
2661 if (focus != surface)
2662 return;
2663
2664 if (surface_resize(shsurf, pointer, edges) < 0)
2665 wl_resource_post_no_memory(resource);
2666}
2667
2668static void
2669desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2670 bool fullscreen,
2671 struct weston_output *output, void *shell)
2672{
2673 struct shell_surface *shsurf =
2674 weston_desktop_surface_get_user_data(desktop_surface);
2675
2676 set_fullscreen(shsurf, fullscreen, output);
2677}
2678
2679static void
2680set_maximized(struct shell_surface *shsurf, bool maximized)
2681{
2682 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2683 struct weston_surface *surface =
2684 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2685 int32_t width = 0, height = 0;
2686
2687 if (maximized) {
2688 struct weston_output *output;
2689 struct desktop_shell *shell;
2690 pixman_rectangle32_t area;
2691
2692 if (!weston_surface_is_mapped(surface))
2693 output = get_focused_output(surface->compositor);
2694 else
2695 output = surface->output;
2696
2697 shell_surface_set_output(shsurf, output);
2698
2699 shell = shell_surface_get_shell(shsurf);
2700 get_output_work_area(shell, shsurf->output, &area);
2701
2702 width = area.width;
2703 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002704 }
2705 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2706 weston_desktop_surface_set_size(desktop_surface, width, height);
2707}
2708
2709static void
2710desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2711 bool maximized, void *shell)
2712{
2713 struct shell_surface *shsurf =
2714 weston_desktop_surface_get_user_data(desktop_surface);
2715
2716 set_maximized(shsurf, maximized);
2717}
2718
2719static void
2720desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2721 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002722{
2723 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002724 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002725
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002726 /* apply compositor's own minimization logic (hide) */
2727 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002728}
2729
Rafael Antognollie2a34552013-12-03 15:35:45 -02002730static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002731set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002732{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002733 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002734
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002735 if (pointer->grab->interface == &busy_cursor_grab_interface)
2736 return;
2737
2738 grab = malloc(sizeof *grab);
2739 if (!grab)
2740 return;
2741
2742 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2743 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2744 /* Mark the shsurf as ungrabbed so that button binding is able
2745 * to move it. */
2746 shsurf->grabbed = 0;
2747}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748
2749static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002750end_busy_cursor(struct weston_compositor *compositor,
2751 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002752{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002753 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002754 struct shell_grab *grab;
2755 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002756
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002757 wl_list_for_each(seat, &compositor->seat_list, link) {
2758 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2759 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002760
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002761 if (!pointer)
2762 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002763
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002764 if (pointer->grab->interface != &busy_cursor_grab_interface)
2765 continue;
2766
2767 grab = (struct shell_grab *) pointer->grab;
2768 shsurf = grab->shsurf;
2769 if (!shsurf)
2770 continue;
2771
2772 grab_client =
2773 weston_desktop_surface_get_client(shsurf->desktop_surface);
2774 if (grab_client == desktop_client) {
2775 shell_grab_end(grab);
2776 free(grab);
2777 }
2778 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002779}
2780
2781static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002782desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2783 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002784{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002785 struct shell_surface *shsurf =
2786 weston_desktop_surface_get_user_data(desktop_surface);
2787 bool *unresponsive = user_data;
2788
2789 shsurf->unresponsive = *unresponsive;
2790}
2791
2792static void
2793desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2794 void *shell_)
2795{
2796 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002797 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798 struct weston_seat *seat;
2799 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002800
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002801 weston_desktop_client_for_each_surface(desktop_client,
2802 desktop_surface_set_unresponsive,
2803 &unresponsive);
2804
2805
2806 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2807 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2808 struct weston_desktop_client *grab_client;
2809
2810 if (!pointer || !pointer->focus)
2811 continue;
2812
2813 shsurf = get_shell_surface(pointer->focus->surface);
2814 if (!shsurf)
2815 continue;
2816
2817 grab_client =
2818 weston_desktop_surface_get_client(shsurf->desktop_surface);
2819 if (grab_client == desktop_client)
2820 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002821 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002822}
2823
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002824static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002825desktop_surface_pong(struct weston_desktop_client *desktop_client,
2826 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002827{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002828 struct desktop_shell *shell = shell_;
2829 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002830
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831 weston_desktop_client_for_each_surface(desktop_client,
2832 desktop_surface_set_unresponsive,
2833 &unresponsive);
2834 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002835}
2836
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002837static void
2838desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2839 int32_t x, int32_t y, void *shell_)
2840{
2841 struct shell_surface *shsurf =
2842 weston_desktop_surface_get_user_data(surface);
2843
2844 shsurf->xwayland.x = x;
2845 shsurf->xwayland.y = y;
2846 shsurf->xwayland.is_set = true;
2847}
2848
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002849static const struct weston_desktop_api shell_desktop_api = {
2850 .struct_size = sizeof(struct weston_desktop_api),
2851 .surface_added = desktop_surface_added,
2852 .surface_removed = desktop_surface_removed,
2853 .committed = desktop_surface_committed,
2854 .move = desktop_surface_move,
2855 .resize = desktop_surface_resize,
2856 .fullscreen_requested = desktop_surface_fullscreen_requested,
2857 .maximized_requested = desktop_surface_maximized_requested,
2858 .minimized_requested = desktop_surface_minimized_requested,
2859 .ping_timeout = desktop_surface_ping_timeout,
2860 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002861 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002862};
2863
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002864/* ************************ *
2865 * end of libweston-desktop *
2866 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002867static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002868configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002869{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002870 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002871
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002872 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002873 if (v->output == ev->output && v != ev) {
2874 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002875 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002876 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002877 }
2878 }
2879
Quentin Glidice8bf9592016-06-23 18:55:20 +02002880 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002881 ev->surface->is_mapped = true;
2882 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002883
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002884 if (wl_list_empty(&ev->layer_link.link)) {
2885 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002887 }
2888}
2889
David Fort50d962f2016-06-09 17:55:52 +02002890
2891static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002892find_shell_output_from_weston_output(struct desktop_shell *shell,
2893 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002894{
2895 struct shell_output *shell_output;
2896
2897 wl_list_for_each(shell_output, &shell->output_list, link) {
2898 if (shell_output->output == output)
2899 return shell_output;
2900 }
2901
2902 return NULL;
2903}
2904
Pekka Paalanen8274d902014-08-06 19:36:51 +03002905static int
2906background_get_label(struct weston_surface *surface, char *buf, size_t len)
2907{
2908 return snprintf(buf, len, "background for output %s",
2909 surface->output->name);
2910}
2911
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002912static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002913background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002914{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002915 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002917
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 view = container_of(es->views.next, struct weston_view, surface_link);
2919
Quentin Glidice8bf9592016-06-23 18:55:20 +02002920 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002921}
2922
2923static void
David Fort50d962f2016-06-09 17:55:52 +02002924handle_background_surface_destroy(struct wl_listener *listener, void *data)
2925{
2926 struct shell_output *output =
2927 container_of(listener, struct shell_output, background_surface_listener);
2928
2929 weston_log("background surface gone\n");
2930 output->background_surface = NULL;
2931}
2932
2933static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002934desktop_shell_set_background(struct wl_client *client,
2935 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002936 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002937 struct wl_resource *surface_resource)
2938{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002939 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002940 struct weston_surface *surface =
2941 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002942 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002944
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002945 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002946 wl_resource_post_error(surface_resource,
2947 WL_DISPLAY_ERROR_INVALID_OBJECT,
2948 "surface role already assigned");
2949 return;
2950 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002951
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2953 weston_view_destroy(view);
2954 view = weston_view_create(surface);
2955
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002956 surface->committed = background_committed;
2957 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002958 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03002959 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002961 weston_desktop_shell_send_configure(resource, 0,
2962 surface_resource,
2963 surface->output->width,
2964 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002965
2966 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2967 sh_output->background_surface = surface;
2968
2969 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2970 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002971}
2972
Pekka Paalanen8274d902014-08-06 19:36:51 +03002973static int
2974panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2975{
2976 return snprintf(buf, len, "panel for output %s",
2977 surface->output->name);
2978}
2979
Kristian Høgsberg75840622011-09-06 13:48:16 -04002980static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002981panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002982{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002983 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002985 int width, height;
2986 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 view = container_of(es->views.next, struct weston_view, surface_link);
2989
Quentin Glidice8bf9592016-06-23 18:55:20 +02002990 get_panel_size(shell, view, &width, &height);
2991 switch (shell->panel_position) {
2992 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2993 break;
2994 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2995 y = view->output->height - height;
2996 break;
2997 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2998 break;
2999 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3000 x = view->output->width - width;
3001 break;
3002 }
3003
3004 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003005}
3006
3007static void
David Fort50d962f2016-06-09 17:55:52 +02003008handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3009{
3010 struct shell_output *output =
3011 container_of(listener, struct shell_output, panel_surface_listener);
3012
3013 weston_log("panel surface gone\n");
3014 output->panel_surface = NULL;
3015}
3016
3017
3018static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003019desktop_shell_set_panel(struct wl_client *client,
3020 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003021 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003022 struct wl_resource *surface_resource)
3023{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003024 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003025 struct weston_surface *surface =
3026 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003028 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003029
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003030 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003031 wl_resource_post_error(surface_resource,
3032 WL_DISPLAY_ERROR_INVALID_OBJECT,
3033 "surface role already assigned");
3034 return;
3035 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003036
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3038 weston_view_destroy(view);
3039 view = weston_view_create(surface);
3040
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003041 surface->committed = panel_committed;
3042 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003043 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03003044 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003046 weston_desktop_shell_send_configure(resource, 0,
3047 surface_resource,
3048 surface->output->width,
3049 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003050
3051 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3052 sh_output->panel_surface = surface;
3053
3054 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3055 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003056}
3057
Pekka Paalanen8274d902014-08-06 19:36:51 +03003058static int
3059lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3060{
3061 return snprintf(buf, len, "lock window");
3062}
3063
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003064static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003065lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003066{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003067 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003068 struct weston_view *view;
3069
3070 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003071
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003072 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003073 return;
3074
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003076
3077 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003078 weston_layer_entry_insert(&shell->lock_layer.view_list,
3079 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003081 surface->is_mapped = true;
3082 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003083 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003084 }
3085}
3086
3087static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003088handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003089{
Tiago Vignattibe143262012-04-16 17:31:41 +03003090 struct desktop_shell *shell =
3091 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003092
Martin Minarik6d118362012-06-07 18:01:59 +02003093 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003094 shell->lock_surface = NULL;
3095}
3096
3097static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003098desktop_shell_set_lock_surface(struct wl_client *client,
3099 struct wl_resource *resource,
3100 struct wl_resource *surface_resource)
3101{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003102 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003103 struct weston_surface *surface =
3104 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003105
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003106 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003107
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003108 if (!shell->locked)
3109 return;
3110
Pekka Paalanen98262232011-12-01 10:42:22 +02003111 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003112
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003113 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003114 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003115 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003116
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003117 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003118 surface->committed = lock_surface_committed;
3119 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003120 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003121}
3122
3123static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003124resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003125{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003126 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003127
Quentin Glidic82681572016-12-17 13:40:51 +01003128 weston_layer_unset_position(&shell->lock_layer);
3129
3130 if (shell->showing_input_panels)
3131 weston_layer_set_position(&shell->input_panel_layer,
3132 WESTON_LAYER_POSITION_TOP_UI);
3133 weston_layer_set_position(&shell->fullscreen_layer,
3134 WESTON_LAYER_POSITION_FULLSCREEN);
3135 weston_layer_set_position(&shell->panel_layer,
3136 WESTON_LAYER_POSITION_UI);
3137 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003138
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003139 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003140
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003141 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003142 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003143 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003144}
3145
3146static void
3147desktop_shell_unlock(struct wl_client *client,
3148 struct wl_resource *resource)
3149{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003150 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003151
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003152 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003153
3154 if (shell->locked)
3155 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003156}
3157
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003158static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003159desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003160 struct wl_resource *resource,
3161 struct wl_resource *surface_resource)
3162{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003163 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003164
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003165 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003166 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003167}
3168
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003169static void
3170desktop_shell_desktop_ready(struct wl_client *client,
3171 struct wl_resource *resource)
3172{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003173 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003174
3175 shell_fade_startup(shell);
3176}
3177
Jonny Lamb765760d2014-08-20 15:53:19 +02003178static void
3179desktop_shell_set_panel_position(struct wl_client *client,
3180 struct wl_resource *resource,
3181 uint32_t position)
3182{
3183 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3184
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003185 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3186 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3187 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3188 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003189 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003190 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003191 "bad position argument");
3192 return;
3193 }
3194
3195 shell->panel_position = position;
3196}
3197
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003198static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003199 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003200 desktop_shell_set_panel,
3201 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003202 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003203 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003204 desktop_shell_desktop_ready,
3205 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003206};
3207
3208static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003209move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003210 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003211{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003212 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003213 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003214 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003215
Derek Foreman8ae2db52015-07-15 13:00:36 -05003216 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003217 return;
3218
Derek Foreman8ae2db52015-07-15 13:00:36 -05003219 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003220
Pekka Paalanen01388e22013-04-25 13:57:44 +03003221 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003222 if (surface == NULL)
3223 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003224
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003225 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003226 if (shsurf == NULL ||
3227 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3228 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003229 return;
3230
Derek Foreman794fa0e2015-07-15 13:00:38 -05003231 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003232}
3233
3234static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003235maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003236 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003237{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003238 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003239 struct weston_surface *surface;
3240 struct shell_surface *shsurf;
3241
3242 surface = weston_surface_get_main_surface(focus);
3243 if (surface == NULL)
3244 return;
3245
3246 shsurf = get_shell_surface(surface);
3247 if (shsurf == NULL)
3248 return;
3249
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003250 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003251}
3252
3253static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003254fullscreen_binding(struct weston_keyboard *keyboard,
3255 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003256{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003257 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003258 struct weston_surface *surface;
3259 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003260 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003261
3262 surface = weston_surface_get_main_surface(focus);
3263 if (surface == NULL)
3264 return;
3265
3266 shsurf = get_shell_surface(surface);
3267 if (shsurf == NULL)
3268 return;
3269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003270 fullscreen =
3271 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003273 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003274}
3275
3276static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003277touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003278{
Derek Foreman362656b2014-09-04 10:23:05 -05003279 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003280 struct weston_surface *surface;
3281 struct shell_surface *shsurf;
3282
Derek Foreman8ae2db52015-07-15 13:00:36 -05003283 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003284 return;
3285
Derek Foreman8ae2db52015-07-15 13:00:36 -05003286 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003287 surface = weston_surface_get_main_surface(focus);
3288 if (surface == NULL)
3289 return;
3290
3291 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003292 if (shsurf == NULL ||
3293 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3294 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003295 return;
3296
Derek Foremanb7674ae2015-07-15 13:00:37 -05003297 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003298}
3299
3300static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003301resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003302 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003303{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003304 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003305 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003306 uint32_t edges = 0;
3307 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003308 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003309
Derek Foreman8ae2db52015-07-15 13:00:36 -05003310 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003311 return;
3312
Derek Foreman8ae2db52015-07-15 13:00:36 -05003313 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003314
Pekka Paalanen01388e22013-04-25 13:57:44 +03003315 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003316 if (surface == NULL)
3317 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003318
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003319 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003320 if (shsurf == NULL ||
3321 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3322 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003323 return;
3324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003326 wl_fixed_to_int(pointer->grab_x),
3327 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003328 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003329
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003330 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003331 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003332 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003333 edges |= 0;
3334 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003335 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003336
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003337 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003338 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003339 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003340 edges |= 0;
3341 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003342 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003343
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003344 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003345}
3346
3347static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003348surface_opacity_binding(struct weston_pointer *pointer,
3349 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003350 struct weston_pointer_axis_event *event,
3351 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003352{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003353 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003354 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003355 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003356 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003357
Pekka Paalanen01388e22013-04-25 13:57:44 +03003358 /* XXX: broken for windows containing sub-surfaces */
3359 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003360 if (surface == NULL)
3361 return;
3362
3363 shsurf = get_shell_surface(surface);
3364 if (!shsurf)
3365 return;
3366
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003367 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003368
Jason Ekstranda7af7042013-10-12 22:38:11 -05003369 if (shsurf->view->alpha > 1.0)
3370 shsurf->view->alpha = 1.0;
3371 if (shsurf->view->alpha < step)
3372 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003373
Jason Ekstranda7af7042013-10-12 22:38:11 -05003374 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003375 weston_surface_damage(surface);
3376}
3377
3378static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003379do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3380 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003381{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003382 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003383 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003384 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003385 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003386
Derek Foreman1281a362015-07-31 16:55:32 -05003387 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003388 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3389 return;
3390 }
3391
Scott Moreauccbf29d2012-02-22 14:21:41 -07003392 wl_list_for_each(output, &compositor->output_list, link) {
3393 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003394 wl_fixed_to_double(pointer->x),
3395 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003396 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003397 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003398 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003399 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003400 increment = -output->zoom.increment;
3401 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003402 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003403 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003404 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003405 else
3406 increment = 0;
3407
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003408 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003409
Scott Moreaue6603982012-06-11 13:07:51 -06003410 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003411 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003412 else if (output->zoom.level > output->zoom.max_level)
3413 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003414
3415 if (!output->zoom.active) {
3416 if (output->zoom.level <= 0.0)
3417 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003418 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003419 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003420
Scott Moreaue6603982012-06-11 13:07:51 -06003421 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003422
Jason Ekstranda7af7042013-10-12 22:38:11 -05003423 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003424 }
3425 }
3426}
3427
Scott Moreauccbf29d2012-02-22 14:21:41 -07003428static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003429zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003430 struct weston_pointer_axis_event *event,
3431 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003432{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003433 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003434}
3435
3436static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003437zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003438 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003439{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003440 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003441}
3442
3443static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003444terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003445 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003446{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003447 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003448
Daniel Stone325fc2d2012-05-30 16:31:58 +01003449 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003450}
3451
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003452static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003453rotate_grab_motion(struct weston_pointer_grab *grab,
3454 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003455 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003456{
3457 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003458 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003459 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003460 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003461 struct weston_surface *surface =
3462 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003463 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003464
Jonas Ådahld2510102014-10-05 21:39:14 +02003465 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003466
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003467 if (!shsurf)
3468 return;
3469
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003470 cx = 0.5f * surface->width;
3471 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003472
Daniel Stone37816df2012-05-16 18:45:18 +01003473 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3474 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003475 r = sqrtf(dx * dx + dy * dy);
3476
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003477 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003478 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003479
3480 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003481 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003482 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003483
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003484 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003485 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003486
3487 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003488 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003489 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003490 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003491 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003492
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003493 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003494 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003495 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003496 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003497 wl_list_init(&shsurf->rotation.transform.link);
3498 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003499 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003500 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003501
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003502 /* We need to adjust the position of the surface
3503 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003504 cposx = shsurf->view->geometry.x + cx;
3505 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003506 dposx = rotate->center.x - cposx;
3507 dposy = rotate->center.y - cposy;
3508 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003509 weston_view_set_position(shsurf->view,
3510 shsurf->view->geometry.x + dposx,
3511 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003512 }
3513
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003514 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003515 * lazily applies the damage due to rotation update.
3516 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003517 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003518}
3519
3520static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003521rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003522 const struct timespec *time,
3523 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003524{
3525 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003526 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003527 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003528 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003529 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003530
Daniel Stone4dbadb12012-05-30 16:31:51 +01003531 if (pointer->button_count == 0 &&
3532 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003533 if (shsurf)
3534 weston_matrix_multiply(&shsurf->rotation.rotation,
3535 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003536 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003537 free(rotate);
3538 }
3539}
3540
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003541static void
3542rotate_grab_cancel(struct weston_pointer_grab *grab)
3543{
3544 struct rotate_grab *rotate =
3545 container_of(grab, struct rotate_grab, base.grab);
3546
3547 shell_grab_end(&rotate->base);
3548 free(rotate);
3549}
3550
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003551static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552 noop_grab_focus,
3553 rotate_grab_motion,
3554 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003555 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003556 noop_grab_axis_source,
3557 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003558 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003559};
3560
3561static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003562surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003563{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003564 struct weston_surface *surface =
3565 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003566 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003567 float dx, dy;
3568 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003569
Pekka Paalanen460099f2012-01-20 16:48:25 +02003570 rotate = malloc(sizeof *rotate);
3571 if (!rotate)
3572 return;
3573
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003574 weston_view_to_global_float(shsurf->view,
3575 surface->width * 0.5f,
3576 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003578
Derek Foreman74de4692015-07-15 13:00:39 -05003579 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3580 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003581 r = sqrtf(dx * dx + dy * dy);
3582 if (r > 20.0f) {
3583 struct weston_matrix inverse;
3584
3585 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003586 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003587 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003588
3589 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003590 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003591 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003592 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003593 weston_matrix_init(&rotate->rotation);
3594 }
3595
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003596 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003597 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003598}
3599
3600static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003601rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3602 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003603{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003604 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003605 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003606 struct shell_surface *surface;
3607
Derek Foreman8ae2db52015-07-15 13:00:36 -05003608 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003609 return;
3610
Derek Foreman8ae2db52015-07-15 13:00:36 -05003611 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003612
Pekka Paalanen01388e22013-04-25 13:57:44 +03003613 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003614 if (base_surface == NULL)
3615 return;
3616
3617 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003618 if (surface == NULL ||
3619 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3620 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003621 return;
3622
Derek Foreman74de4692015-07-15 13:00:39 -05003623 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003624}
3625
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003626/* Move all fullscreen layers down to the current workspace and hide their
3627 * black views. The surfaces' state is set to both fullscreen and lowered,
3628 * and this is reversed when such a surface is re-configured, see
3629 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3630 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003631 * lowering_output = NULL - Lower on all outputs, else only lower on the
3632 * specified output.
3633 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003634 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003635 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003636void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003637lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003638 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003639{
3640 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003641 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003642
3643 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003644 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003645 &shell->fullscreen_layer.view_list.link,
3646 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003647 struct shell_surface *shsurf = get_shell_surface(view->surface);
3648
3649 if (!shsurf)
3650 continue;
3651
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003652 /* Only lower surfaces which have lowering_output as their fullscreen
3653 * output, unless a NULL output asks for lowering on all outputs.
3654 */
3655 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3656 continue;
3657
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003658 /* We can have a non-fullscreen popup for a fullscreen surface
3659 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003660 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003661 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003662 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3663 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003664 weston_view_damage_below(shsurf->fullscreen.black_view);
3665
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003666 }
3667
3668 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003669 weston_layer_entry_remove(&view->layer_link);
3670 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003671 weston_view_damage_below(view);
3672 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003673
3674 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003675 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003676}
3677
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003678void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003679activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003680 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003681{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003682 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003683 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003684 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003685 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003686 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003687 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003688
Pekka Paalanen01388e22013-04-25 13:57:44 +03003689 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003690 shsurf = get_shell_surface(main_surface);
3691 assert(shsurf);
3692
3693 /* Only demote fullscreen surfaces on the output of activated shsurf.
3694 * Leave fullscreen surfaces on unrelated outputs alone. */
3695 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003696
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003697 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003698
Jonas Ådahl8538b222012-08-29 22:13:03 +02003699 state = ensure_focus_state(shell, seat);
3700 if (state == NULL)
3701 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003702
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003703 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003704 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003705
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003706 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3707 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003708 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003709
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003710 /* Update the surface’s layer. This brings it to the top of the stacking
3711 * order as appropriate. */
3712 shell_surface_update_layer(shsurf);
3713
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003714 if (shell->focus_animation_type != ANIMATION_NONE) {
3715 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003716 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003717 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003718}
3719
Alex Wu21858432012-04-01 20:13:08 +08003720/* no-op func for checking black surface */
3721static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003722black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003723{
3724}
3725
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003726static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003727is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003728{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003729 struct weston_surface *surface = view->surface;
3730
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003731 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003732 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003733 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003734 return true;
3735 }
3736 return false;
3737}
3738
Kristian Høgsberg75840622011-09-06 13:48:16 -04003739static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003740activate_binding(struct weston_seat *seat,
3741 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003742 struct weston_view *focus_view,
3743 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003744{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003745 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003746 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003747
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003748 if (!focus_view)
3749 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003750
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003751 if (is_black_surface_view(focus_view, &main_view))
3752 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003753
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003754 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003755 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003756 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003757
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003758 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003759}
3760
3761static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003762click_to_activate_binding(struct weston_pointer *pointer,
3763 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003764 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003765{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003766 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003767 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003768 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003769 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003770
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003771 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003772 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003773 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003774}
3775
3776static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003777touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3778 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003779{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003780 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003781 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003782 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003783 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003784
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003785 activate_binding(touch->seat, data, touch->focus,
3786 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003787}
3788
3789static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003790unfocus_all_seats(struct desktop_shell *shell)
3791{
3792 struct weston_seat *seat, *next;
3793
3794 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003795 struct weston_keyboard *keyboard =
3796 weston_seat_get_keyboard(seat);
3797
3798 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003799 continue;
3800
Derek Foreman1281a362015-07-31 16:55:32 -05003801 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003802 }
3803}
3804
3805static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003806lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003807{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003808 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003809
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003810 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003811 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003812 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003813 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003814
3815 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003816
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003817 /* Hide all surfaces by removing the fullscreen, panel and
3818 * toplevel layers. This way nothing else can show or receive
3819 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003820
Quentin Glidic82681572016-12-17 13:40:51 +01003821 weston_layer_unset_position(&shell->panel_layer);
3822 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003823 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003824 weston_layer_unset_position(&shell->input_panel_layer);
3825 weston_layer_unset_position(&ws->layer);
3826
3827 weston_layer_set_position(&shell->lock_layer,
3828 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003829
Derek Foreman004b4a12015-07-20 16:28:13 -05003830 weston_compositor_sleep(shell->compositor);
3831
Neil Robertsb4a91702014-04-09 16:33:32 +01003832 /* Remove the keyboard focus on all seats. This will be
3833 * restored to the workspace's saved state via
3834 * restore_focus_state when the compositor is unlocked */
3835 unfocus_all_seats(shell);
3836
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003837 /* TODO: disable bindings that should not work while locked. */
3838
3839 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003840}
3841
3842static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003843unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003844{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003845 struct wl_resource *shell_resource;
3846
Pekka Paalanend81c2162011-11-16 13:47:34 +02003847 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003848 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003849 return;
3850 }
3851
3852 /* If desktop-shell client has gone away, unlock immediately. */
3853 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003854 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003855 return;
3856 }
3857
3858 if (shell->prepare_event_sent)
3859 return;
3860
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003861 shell_resource = shell->child.desktop_shell;
3862 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003863 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003864}
3865
3866static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003867shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003868{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003869 struct shell_output *shell_output = data;
3870 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003871
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003872 shell_output->fade.animation = NULL;
3873 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003874 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003875 weston_surface_destroy(shell_output->fade.view->surface);
3876 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003877 break;
3878 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003879 lock(shell);
3880 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003881 default:
3882 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003883 }
3884}
3885
Jason Ekstranda7af7042013-10-12 22:38:11 -05003886static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003887shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003888{
3889 struct weston_compositor *compositor = shell->compositor;
3890 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003892
3893 surface = weston_surface_create(compositor);
3894 if (!surface)
3895 return NULL;
3896
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 view = weston_view_create(surface);
3898 if (!view) {
3899 weston_surface_destroy(surface);
3900 return NULL;
3901 }
3902
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003903 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3904 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003905 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003906 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3907 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003908 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003909 surface->is_mapped = true;
3910 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003911
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003913}
3914
3915static void
3916shell_fade(struct desktop_shell *shell, enum fade_type type)
3917{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003918 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003919 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003920
3921 switch (type) {
3922 case FADE_IN:
3923 tint = 0.0;
3924 break;
3925 case FADE_OUT:
3926 tint = 1.0;
3927 break;
3928 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003929 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003930 return;
3931 }
3932
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003933 /* Create a separate fade surface for each output */
3934 wl_list_for_each(shell_output, &shell->output_list, link) {
3935 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003936
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003937 if (shell_output->fade.view == NULL) {
3938 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3939 if (!shell_output->fade.view)
3940 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003941
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003942 shell_output->fade.view->alpha = 1.0 - tint;
3943 weston_view_update_transform(shell_output->fade.view);
3944 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003945
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003946 if (shell_output->fade.view->output == NULL) {
3947 /* If the black view gets a NULL output, we lost the
3948 * last output and we'll just cancel the fade. This
3949 * happens when you close the last window under the
3950 * X11 or Wayland backends. */
3951 shell->locked = false;
3952 weston_surface_destroy(shell_output->fade.view->surface);
3953 shell_output->fade.view = NULL;
3954 } else if (shell_output->fade.animation) {
3955 weston_fade_update(shell_output->fade.animation, tint);
3956 } else {
3957 shell_output->fade.animation =
3958 weston_fade_run(shell_output->fade.view,
3959 1.0 - tint, tint, 300.0,
3960 shell_fade_done_for_output, shell_output);
3961 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003962 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963}
3964
3965static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003966do_shell_fade_startup(void *data)
3967{
3968 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003969 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003970
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003971 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003972 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003973 } else {
3974 weston_log("desktop shell: "
3975 "unexpected fade-in animation type %d\n",
3976 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003977 wl_list_for_each(shell_output, &shell->output_list, link) {
3978 weston_surface_destroy(shell_output->fade.view->surface);
3979 shell_output->fade.view = NULL;
3980 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003981 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003982}
3983
3984static void
3985shell_fade_startup(struct desktop_shell *shell)
3986{
3987 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003988 struct shell_output *shell_output;
3989 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003990
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003991 wl_list_for_each(shell_output, &shell->output_list, link) {
3992 if (!shell_output->fade.startup_timer)
3993 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003994
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003995 wl_event_source_remove(shell_output->fade.startup_timer);
3996 shell_output->fade.startup_timer = NULL;
3997 has_fade = true;
3998 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003999
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004000 if (has_fade) {
4001 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4002 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4003 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004004}
4005
4006static int
4007fade_startup_timeout(void *data)
4008{
4009 struct desktop_shell *shell = data;
4010
4011 shell_fade_startup(shell);
4012 return 0;
4013}
4014
4015static void
4016shell_fade_init(struct desktop_shell *shell)
4017{
4018 /* Make compositor output all black, and wait for the desktop-shell
4019 * client to signal it is ready, then fade in. The timer triggers a
4020 * fade-in, in case the desktop-shell client takes too long.
4021 */
4022
4023 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004024 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004025
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004026 if (shell->startup_animation_type == ANIMATION_NONE)
4027 return;
4028
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004029 wl_list_for_each(shell_output, &shell->output_list, link) {
4030 if (shell_output->fade.view != NULL) {
4031 weston_log("%s: warning: fade surface already exists\n",
4032 __func__);
4033 continue;
4034 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004035
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004036 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4037 if (!shell_output->fade.view)
4038 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004039
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004040 weston_view_update_transform(shell_output->fade.view);
4041 weston_surface_damage(shell_output->fade.view->surface);
4042
4043 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4044 shell_output->fade.startup_timer =
4045 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4046 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4047 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004048}
4049
4050static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004051idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004052{
4053 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004054 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004055
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004056 struct weston_seat *seat;
4057
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004058 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4059 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004060
4061 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004062 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004063}
4064
4065static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004066wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004067{
4068 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004069 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004070
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004071 unlock(shell);
4072}
4073
4074static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004075transform_handler(struct wl_listener *listener, void *data)
4076{
4077 struct weston_surface *surface = data;
4078 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004079 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004080 int x, y;
4081
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004082 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004083 return;
4084
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004085 api = shsurf->shell->xwayland_surface_api;
4086 if (!api) {
4087 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4088 shsurf->shell->xwayland_surface_api = api;
4089 }
4090
4091 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004092 return;
4093
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004094 if (!weston_view_is_mapped(shsurf->view))
4095 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004096
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004097 x = shsurf->view->geometry.x;
4098 y = shsurf->view->geometry.y;
4099
4100 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004101}
4102
4103static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004104center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004105{
Giulio Camuffob8366642013-04-25 13:57:46 +03004106 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004107 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004108
Jason Ekstranda7af7042013-10-12 22:38:11 -05004109 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004110
4111 x = output->x + (output->width - width) / 2 - surf_x / 2;
4112 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004113
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004114 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004115}
4116
4117static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004118weston_view_set_initial_position(struct weston_view *view,
4119 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004120{
4121 struct weston_compositor *compositor = shell->compositor;
4122 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004123 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004124 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004125 struct weston_output *output, *target_output = NULL;
4126 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004127 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004128
4129 /* As a heuristic place the new window on the same output as the
4130 * pointer. Falling back to the output containing 0, 0.
4131 *
4132 * TODO: Do something clever for touch too?
4133 */
4134 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004135 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4136
4137 if (pointer) {
4138 ix = wl_fixed_to_int(pointer->x);
4139 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004140 break;
4141 }
4142 }
4143
4144 wl_list_for_each(output, &compositor->output_list, link) {
4145 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4146 target_output = output;
4147 break;
4148 }
4149 }
4150
4151 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004152 weston_view_set_position(view, 10 + random() % 400,
4153 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004154 return;
4155 }
4156
4157 /* Valid range within output where the surface will still be onscreen.
4158 * If this is negative it means that the surface is bigger than
4159 * output.
4160 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004161 get_output_work_area(shell, target_output, &area);
4162
Derek Foremanf814c5d2015-04-15 12:23:54 -05004163 x = area.x;
4164 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004165 range_x = area.width - view->surface->width;
4166 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004167
Rob Bradford4cb88c72012-08-13 15:18:44 +01004168 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004169 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004170
4171 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004172 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004173
Jason Ekstranda7af7042013-10-12 22:38:11 -05004174 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004175}
4176
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004177static bool
4178check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4179{
4180 struct timespec now;
4181
4182 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4183 return false;
4184
4185 /*
4186 * If the shell helper client dies before the session has been
4187 * up for roughly 30 seconds, better just make Weston shut down,
4188 * because the user likely has no way to interact with the desktop
4189 * anyway.
4190 */
4191 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4192 weston_log("Error: %s apparently cannot run at all.\n",
4193 shell->client);
4194 weston_log_continue(STAMP_SPACE "Quitting...");
4195 wl_display_terminate(shell->compositor->wl_display);
4196
4197 return true;
4198 }
4199
4200 return false;
4201}
4202
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004203static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004204
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004205static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004206respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004207{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004208 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004209
4210 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4211 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004212 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004213 shell->child.deathstamp = time;
4214 shell->child.deathcount = 0;
4215 }
4216
4217 shell->child.deathcount++;
4218 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004219 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004220 return;
4221 }
4222
Pekka Paalanen826dc142014-08-27 12:11:53 +03004223 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004224 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004225}
4226
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004227static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004228desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4229{
4230 struct desktop_shell *shell;
4231
4232 shell = container_of(listener, struct desktop_shell,
4233 child.client_destroy_listener);
4234
Pekka Paalanen826dc142014-08-27 12:11:53 +03004235 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004236 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004237 /*
4238 * unbind_desktop_shell() will reset shell->child.desktop_shell
4239 * before the respawned process has a chance to create a new
4240 * desktop_shell object, because we are being called from the
4241 * wl_client destructor which destroys all wl_resources before
4242 * returning.
4243 */
4244
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004245 if (!check_desktop_shell_crash_too_early(shell))
4246 respawn_desktop_shell_process(shell);
4247
Pekka Paalanen826dc142014-08-27 12:11:53 +03004248 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004249}
4250
4251static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004252launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004253{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004254 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004255
Pekka Paalanen826dc142014-08-27 12:11:53 +03004256 shell->child.client = weston_client_start(shell->compositor,
4257 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004258
Arnaud Vrac42631192014-08-25 20:56:46 +02004259 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004260 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004261 return;
4262 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004263
4264 shell->child.client_destroy_listener.notify =
4265 desktop_shell_client_destroy;
4266 wl_client_add_destroy_listener(shell->child.client,
4267 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004268}
4269
4270static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004271unbind_desktop_shell(struct wl_resource *resource)
4272{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004273 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004274
4275 if (shell->locked)
4276 resume_desktop(shell);
4277
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004278 shell->child.desktop_shell = NULL;
4279 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004280}
4281
4282static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004283bind_desktop_shell(struct wl_client *client,
4284 void *data, uint32_t version, uint32_t id)
4285{
Tiago Vignattibe143262012-04-16 17:31:41 +03004286 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004287 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004288
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004289 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4290 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004291
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004292 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004293 wl_resource_set_implementation(resource,
4294 &desktop_shell_implementation,
4295 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004296 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004297 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004298 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004299
4300 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4301 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004302}
4303
Kristian Høgsberg07045392012-02-19 18:52:44 -05004304struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004305 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004306 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004307 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004308 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004309 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004310};
4311
4312static void
4313switcher_next(struct switcher *switcher)
4314{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004316 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004317 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004318 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004319
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004320 /* temporary re-display minimized surfaces */
4321 struct weston_view *tmp;
4322 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004323 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4324 weston_layer_entry_remove(&view->layer_link);
4325 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004326 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4327 *minimized = view;
4328 }
4329
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004330 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004331 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004332 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004333 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004334 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004335 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004336 next = view;
4337 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004338 view->alpha = 0.25;
4339 weston_view_geometry_dirty(view);
4340 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004341 }
Alex Wu1659daa2012-04-01 20:13:09 +08004342
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004343 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 view->alpha = 0.25;
4345 weston_view_geometry_dirty(view);
4346 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004347 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004348 }
4349
4350 if (next == NULL)
4351 next = first;
4352
Alex Wu07b26062012-03-12 16:06:01 +08004353 if (next == NULL)
4354 return;
4355
Kristian Høgsberg07045392012-02-19 18:52:44 -05004356 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004357 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004358
4359 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004360 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004362
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004363 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004364 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004366}
4367
4368static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004369switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370{
4371 struct switcher *switcher =
4372 container_of(listener, struct switcher, listener);
4373
4374 switcher_next(switcher);
4375}
4376
4377static void
Daniel Stone351eb612012-05-31 15:27:47 -04004378switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004381 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004382 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004384 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004385 if (is_focus_view(view))
4386 continue;
4387
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 view->alpha = 1.0;
4389 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004390 }
4391
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004392 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004393 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004394 keyboard->seat,
4395 WESTON_ACTIVATE_FLAG_CONFIGURE);
4396 }
4397
Kristian Høgsberg07045392012-02-19 18:52:44 -05004398 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004399 weston_keyboard_end_grab(keyboard);
4400 if (keyboard->input_method_resource)
4401 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004402
4403 /* re-hide surfaces that were temporary shown during the switch */
4404 struct weston_view **minimized;
4405 wl_array_for_each(minimized, &switcher->minimized_array) {
4406 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004407 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004408 weston_layer_entry_remove(&(*minimized)->layer_link);
4409 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004410 weston_view_damage_below(*minimized);
4411 }
4412 }
4413 wl_array_release(&switcher->minimized_array);
4414
Kristian Høgsberg07045392012-02-19 18:52:44 -05004415 free(switcher);
4416}
4417
4418static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004419switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004420 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004421{
4422 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004423 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004424
Daniel Stonec9785ea2012-05-30 16:31:52 +01004425 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004426 switcher_next(switcher);
4427}
4428
4429static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004430switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004431 uint32_t mods_depressed, uint32_t mods_latched,
4432 uint32_t mods_locked, uint32_t group)
4433{
4434 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004435 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004436
Daniel Stone351eb612012-05-31 15:27:47 -04004437 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4438 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004439}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004440
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004441static void
4442switcher_cancel(struct weston_keyboard_grab *grab)
4443{
4444 struct switcher *switcher = container_of(grab, struct switcher, grab);
4445
4446 switcher_destroy(switcher);
4447}
4448
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004449static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004450 switcher_key,
4451 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004452 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004453};
4454
4455static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004456switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004457 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004458{
Tiago Vignattibe143262012-04-16 17:31:41 +03004459 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004460 struct switcher *switcher;
4461
4462 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004463 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004464 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004465 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004466 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004467 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004468
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004469 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004470 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004471 weston_keyboard_start_grab(keyboard, &switcher->grab);
4472 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004473 switcher_next(switcher);
4474}
4475
Pekka Paalanen3c647232011-12-22 13:43:43 +02004476static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004477backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004478 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004479{
4480 struct weston_compositor *compositor = data;
4481 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004482 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004483
4484 /* TODO: we're limiting to simple use cases, where we assume just
4485 * control on the primary display. We'd have to extend later if we
4486 * ever get support for setting backlights on random desktop LCD
4487 * panels though */
4488 output = get_default_output(compositor);
4489 if (!output)
4490 return;
4491
4492 if (!output->set_backlight)
4493 return;
4494
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004495 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4496 backlight_new = output->backlight_current - 25;
4497 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4498 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004499
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004500 if (backlight_new < 5)
4501 backlight_new = 5;
4502 if (backlight_new > 255)
4503 backlight_new = 255;
4504
4505 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004506 output->set_backlight(output, output->backlight_current);
4507}
4508
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004509static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004510force_kill_binding(struct weston_keyboard *keyboard,
4511 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004512{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004513 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004514 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004515 struct desktop_shell *shell = data;
4516 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004517 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004518
Derek Foreman8ae2db52015-07-15 13:00:36 -05004519 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004520 if (!focus_surface)
4521 return;
4522
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004523 wl_signal_emit(&compositor->kill_signal, focus_surface);
4524
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004525 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004526 wl_client_get_credentials(client, &pid, NULL, NULL);
4527
4528 /* Skip clients that we launched ourselves (the credentials of
4529 * the socketpair is ours) */
4530 if (pid == getpid())
4531 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004532
Daniel Stone325fc2d2012-05-30 16:31:58 +01004533 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004534}
4535
4536static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004537workspace_up_binding(struct weston_keyboard *keyboard,
4538 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004539{
4540 struct desktop_shell *shell = data;
4541 unsigned int new_index = shell->workspaces.current;
4542
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004543 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004544 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004545 if (new_index != 0)
4546 new_index--;
4547
4548 change_workspace(shell, new_index);
4549}
4550
4551static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004552workspace_down_binding(struct weston_keyboard *keyboard,
4553 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004554{
4555 struct desktop_shell *shell = data;
4556 unsigned int new_index = shell->workspaces.current;
4557
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004558 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004559 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004560 if (new_index < shell->workspaces.num - 1)
4561 new_index++;
4562
4563 change_workspace(shell, new_index);
4564}
4565
4566static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004567workspace_f_binding(struct weston_keyboard *keyboard,
4568 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004569{
4570 struct desktop_shell *shell = data;
4571 unsigned int new_index;
4572
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004573 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004574 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004575 new_index = key - KEY_F1;
4576 if (new_index >= shell->workspaces.num)
4577 new_index = shell->workspaces.num - 1;
4578
4579 change_workspace(shell, new_index);
4580}
4581
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004582static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004583workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004584 const struct timespec *time, uint32_t key,
4585 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004586{
4587 struct desktop_shell *shell = data;
4588 unsigned int new_index = shell->workspaces.current;
4589
4590 if (shell->locked)
4591 return;
4592
4593 if (new_index != 0)
4594 new_index--;
4595
Derek Foreman8ae2db52015-07-15 13:00:36 -05004596 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004597}
4598
4599static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004600workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004601 const struct timespec *time, uint32_t key,
4602 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004603{
4604 struct desktop_shell *shell = data;
4605 unsigned int new_index = shell->workspaces.current;
4606
4607 if (shell->locked)
4608 return;
4609
4610 if (new_index < shell->workspaces.num - 1)
4611 new_index++;
4612
Derek Foreman8ae2db52015-07-15 13:00:36 -05004613 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004614}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004615
4616static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004617shell_reposition_view_on_output_destroy(struct weston_view *view)
4618{
4619 struct weston_output *output, *first_output;
4620 struct weston_compositor *ec = view->surface->compositor;
4621 struct shell_surface *shsurf;
4622 float x, y;
4623 int visible;
4624
4625 x = view->geometry.x;
4626 y = view->geometry.y;
4627
4628 /* At this point the destroyed output is not in the list anymore.
4629 * If the view is still visible somewhere, we leave where it is,
4630 * otherwise, move it to the first output. */
4631 visible = 0;
4632 wl_list_for_each(output, &ec->output_list, link) {
4633 if (pixman_region32_contains_point(&output->region,
4634 x, y, NULL)) {
4635 visible = 1;
4636 break;
4637 }
4638 }
4639
4640 if (!visible) {
4641 first_output = container_of(ec->output_list.next,
4642 struct weston_output, link);
4643
4644 x = first_output->x + first_output->width / 4;
4645 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004646
4647 weston_view_set_position(view, x, y);
4648 } else {
4649 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004650 }
4651
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004652
4653 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004654 if (!shsurf)
4655 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004656
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004657 shsurf->saved_position_valid = false;
4658 set_maximized(shsurf, false);
4659 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004660}
4661
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004662void
4663shell_for_each_layer(struct desktop_shell *shell,
4664 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004665{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004666 struct workspace **ws;
4667
4668 func(shell, &shell->fullscreen_layer, data);
4669 func(shell, &shell->panel_layer, data);
4670 func(shell, &shell->background_layer, data);
4671 func(shell, &shell->lock_layer, data);
4672 func(shell, &shell->input_panel_layer, data);
4673
4674 wl_array_for_each(ws, &shell->workspaces.array)
4675 func(shell, &(*ws)->layer, data);
4676}
4677
4678static void
4679shell_output_destroy_move_layer(struct desktop_shell *shell,
4680 struct weston_layer *layer,
4681 void *data)
4682{
4683 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004684 struct weston_view *view;
4685
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004686 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004687 if (view->output != output)
4688 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004689
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004690 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004691 }
4692}
4693
4694static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004695handle_output_destroy(struct wl_listener *listener, void *data)
4696{
4697 struct shell_output *output_listener =
4698 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004699 struct weston_output *output = output_listener->output;
4700 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004701
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004702 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004703
Quentin Glidic561201c2016-08-20 18:19:51 +02004704 wl_list_remove(&output_listener->panel_surface_listener.link);
4705 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004706 wl_list_remove(&output_listener->destroy_listener.link);
4707 wl_list_remove(&output_listener->link);
4708 free(output_listener);
4709}
4710
4711static void
David Fort50d962f2016-06-09 17:55:52 +02004712shell_resize_surface_to_output(struct desktop_shell *shell,
4713 struct weston_surface *surface,
4714 const struct weston_output *output)
4715{
4716 if (!surface)
4717 return;
4718
4719 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4720 surface->resource,
4721 output->width,
4722 output->height);
4723}
4724
4725
4726static void
4727handle_output_resized(struct wl_listener *listener, void *data)
4728{
4729 struct desktop_shell *shell =
4730 container_of(listener, struct desktop_shell, resized_listener);
4731 struct weston_output *output = (struct weston_output *)data;
4732 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4733
4734 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4735 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4736}
4737
4738static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004739create_shell_output(struct desktop_shell *shell,
4740 struct weston_output *output)
4741{
4742 struct shell_output *shell_output;
4743
4744 shell_output = zalloc(sizeof *shell_output);
4745 if (shell_output == NULL)
4746 return;
4747
4748 shell_output->output = output;
4749 shell_output->shell = shell;
4750 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004751 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004752 wl_signal_add(&output->destroy_signal,
4753 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004754 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004755}
4756
4757static void
4758handle_output_create(struct wl_listener *listener, void *data)
4759{
4760 struct desktop_shell *shell =
4761 container_of(listener, struct desktop_shell, output_create_listener);
4762 struct weston_output *output = (struct weston_output *)data;
4763
4764 create_shell_output(shell, output);
4765}
4766
4767static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004768handle_output_move_layer(struct desktop_shell *shell,
4769 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004770{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004771 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004772 struct weston_view *view;
4773 float x, y;
4774
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004775 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004776 if (view->output != output)
4777 continue;
4778
4779 x = view->geometry.x + output->move_x;
4780 y = view->geometry.y + output->move_y;
4781 weston_view_set_position(view, x, y);
4782 }
4783}
4784
4785static void
4786handle_output_move(struct wl_listener *listener, void *data)
4787{
4788 struct desktop_shell *shell;
4789
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004790 shell = container_of(listener, struct desktop_shell,
4791 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004792
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004793 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004794}
4795
4796static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004797setup_output_destroy_handler(struct weston_compositor *ec,
4798 struct desktop_shell *shell)
4799{
4800 struct weston_output *output;
4801
4802 wl_list_init(&shell->output_list);
4803 wl_list_for_each(output, &ec->output_list, link)
4804 create_shell_output(shell, output);
4805
4806 shell->output_create_listener.notify = handle_output_create;
4807 wl_signal_add(&ec->output_created_signal,
4808 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004809
4810 shell->output_move_listener.notify = handle_output_move;
4811 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004812}
4813
4814static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004815shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004816{
Tiago Vignattibe143262012-04-16 17:31:41 +03004817 struct desktop_shell *shell =
4818 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004819 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004820 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004821
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004822 /* Force state to unlocked so we don't try to fade */
4823 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004824
4825 if (shell->child.client) {
4826 /* disable respawn */
4827 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004828 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004829 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004830
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004831 wl_list_remove(&shell->idle_listener.link);
4832 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004833 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004834
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004835 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004836 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004837
Xiong Zhang6b481422013-10-23 13:58:32 +08004838 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4839 wl_list_remove(&shell_output->destroy_listener.link);
4840 wl_list_remove(&shell_output->link);
4841 free(shell_output);
4842 }
4843
4844 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004845 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004846 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004847
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004848 wl_array_for_each(ws, &shell->workspaces.array)
4849 workspace_destroy(*ws);
4850 wl_array_release(&shell->workspaces.array);
4851
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004852 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004853 free(shell);
4854}
4855
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004856static void
4857shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4858{
4859 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004860 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004861
Bob Ham744e6532016-01-12 10:21:48 +00004862 if (shell->allow_zap)
4863 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4864 MODIFIER_CTRL | MODIFIER_ALT,
4865 terminate_binding, ec);
4866
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004867 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004868 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4869 click_to_activate_binding,
4870 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004871 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4872 click_to_activate_binding,
4873 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004874 weston_compositor_add_touch_binding(ec, 0,
4875 touch_to_activate_binding,
4876 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004877 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4878 backlight_binding, ec);
4879 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4880 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004881
4882 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004883 if (shell->exposay_modifier)
4884 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4885 exposay_binding, shell);
4886
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004887 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004888 if (!mod)
4889 return;
4890
Ian Ray13404c42017-09-18 15:22:01 +03004891 /* This binding is not configurable, but is only enabled if there is a
4892 * valid binding modifier. */
4893 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4894 MODIFIER_SUPER | MODIFIER_ALT,
4895 surface_opacity_binding, NULL);
4896
Ian Rayab7c0b62017-09-18 15:22:00 +03004897 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4898 mod, zoom_axis_binding,
4899 NULL);
4900
Daniel Stone325fc2d2012-05-30 16:31:58 +01004901 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4902 zoom_key_binding, NULL);
4903 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4904 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004905 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4906 maximize_binding, NULL);
4907 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4908 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004909 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4910 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004911 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004912 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004913 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004914 weston_compositor_add_button_binding(ec, BTN_LEFT,
4915 mod | MODIFIER_SHIFT,
4916 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004917
4918 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004919 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004920 rotate_binding, NULL);
4921
Daniel Stone325fc2d2012-05-30 16:31:58 +01004922 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4923 shell);
4924 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4925 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004926 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4927 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004928 weston_compositor_add_key_binding(ec, KEY_K, mod,
4929 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004930 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4931 workspace_up_binding, shell);
4932 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4933 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004934 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4935 workspace_move_surface_up_binding,
4936 shell);
4937 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4938 workspace_move_surface_down_binding,
4939 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004940
4941 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4942 if (shell->workspaces.num > 1) {
4943 num_workspace_bindings = shell->workspaces.num;
4944 if (num_workspace_bindings > 6)
4945 num_workspace_bindings = 6;
4946 for (i = 0; i < num_workspace_bindings; i++)
4947 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4948 workspace_f_binding,
4949 shell);
4950 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004951
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004952 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004953}
4954
Jason Ekstrand024177c2014-04-21 19:42:58 -05004955static void
4956handle_seat_created(struct wl_listener *listener, void *data)
4957{
4958 struct weston_seat *seat = data;
4959
4960 create_shell_seat(seat);
4961}
4962
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004963WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004964wet_shell_init(struct weston_compositor *ec,
4965 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004966{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004967 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004968 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004969 struct workspace **pws;
4970 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004971 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004972
Peter Huttererf3d62272013-08-08 11:57:05 +10004973 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004974 if (shell == NULL)
4975 return -1;
4976
Kristian Høgsberg75840622011-09-06 13:48:16 -04004977 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004978
4979 shell->destroy_listener.notify = shell_destroy;
4980 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004981 shell->idle_listener.notify = idle_handler;
4982 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4983 shell->wake_listener.notify = wake_handler;
4984 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004985 shell->transform_listener.notify = transform_handler;
4986 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004987
Quentin Glidic82681572016-12-17 13:40:51 +01004988 weston_layer_init(&shell->fullscreen_layer, ec);
4989 weston_layer_init(&shell->panel_layer, ec);
4990 weston_layer_init(&shell->background_layer, ec);
4991 weston_layer_init(&shell->lock_layer, ec);
4992 weston_layer_init(&shell->input_panel_layer, ec);
4993
4994 weston_layer_set_position(&shell->fullscreen_layer,
4995 WESTON_LAYER_POSITION_FULLSCREEN);
4996 weston_layer_set_position(&shell->panel_layer,
4997 WESTON_LAYER_POSITION_UI);
4998 weston_layer_set_position(&shell->background_layer,
4999 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005000
5001 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005002 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005003
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005004 if (input_panel_setup(shell) < 0)
5005 return -1;
5006
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005007 shell->text_backend = text_backend_init(ec);
5008 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005009 return -1;
5010
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005011 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005012
Daniel Stonedf8133b2013-11-19 11:37:14 +01005013 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5014 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5015
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005016 for (i = 0; i < shell->workspaces.num; i++) {
5017 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5018 if (pws == NULL)
5019 return -1;
5020
Quentin Glidic82681572016-12-17 13:40:51 +01005021 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005022 if (*pws == NULL)
5023 return -1;
5024 }
5025 activate_workspace(shell, 0);
5026
Quentin Glidic82681572016-12-17 13:40:51 +01005027 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005028
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005029 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005030 wl_list_init(&shell->workspaces.animation.link);
5031 shell->workspaces.animation.frame = animate_workspace_change_frame;
5032
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005033 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5034 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005035 return -1;
5036
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005037 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005038 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005039 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005040 return -1;
5041
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005042 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005043
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005044 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005045
Xiong Zhang6b481422013-10-23 13:58:32 +08005046 setup_output_destroy_handler(ec, shell);
5047
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005048 loop = wl_display_get_event_loop(ec->wl_display);
5049 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005050
Jason Ekstrand024177c2014-04-21 19:42:58 -05005051 wl_list_for_each(seat, &ec->seat_list, link)
5052 handle_seat_created(NULL, seat);
5053 shell->seat_create_listener.notify = handle_seat_created;
5054 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005055
David Fort50d962f2016-06-09 17:55:52 +02005056 shell->resized_listener.notify = handle_output_resized;
5057 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5058
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005059 screenshooter_create(ec);
5060
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005061 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005062
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005063 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005064
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005065 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5066
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005067 return 0;
5068}