blob: 62dfa4508799abf37f7733ba654270ad610e687e [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,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001434 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001435{
1436}
1437
1438static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001439noop_grab_axis_source(struct weston_pointer_grab *grab,
1440 uint32_t source)
1441{
1442}
1443
1444static void
1445noop_grab_frame(struct weston_pointer_grab *grab)
1446{
1447}
1448
1449static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001450constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1451{
1452 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001453 struct weston_surface *surface =
1454 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001456 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001457 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001458 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001459 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001460
1461 x = wl_fixed_to_int(pointer->x + move->dx);
1462 y = wl_fixed_to_int(pointer->y + move->dy);
1463
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001464 if (shsurf->shell->panel_position ==
1465 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001466 get_output_work_area(shsurf->shell, surface->output, &area);
1467 geometry =
1468 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001469
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001470 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001471 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001472 y = area.y + safety - geometry.height
1473 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001474
1475 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001476 y + geometry.y < area.y)
1477 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001478 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001479
1480 *cx = x;
1481 *cy = y;
1482}
1483
1484static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001485move_grab_motion(struct weston_pointer_grab *grab,
1486 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001487 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001488{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001489 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001490 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001491 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001492 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001493 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001494
Jonas Ådahld2510102014-10-05 21:39:14 +02001495 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001496 if (!shsurf)
1497 return;
1498
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001499 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1500
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001501 constrain_position(move, &cx, &cy);
1502
1503 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001504
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001505 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001506}
1507
1508static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001509move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001510 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001512 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1513 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001514 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001515 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516
Daniel Stone4dbadb12012-05-30 16:31:51 +01001517 if (pointer->button_count == 0 &&
1518 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001519 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001520 free(grab);
1521 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522}
1523
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001524static void
1525move_grab_cancel(struct weston_pointer_grab *grab)
1526{
1527 struct shell_grab *shell_grab =
1528 container_of(grab, struct shell_grab, grab);
1529
1530 shell_grab_end(shell_grab);
1531 free(grab);
1532}
1533
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001534static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001535 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536 move_grab_motion,
1537 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001538 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001539 noop_grab_axis_source,
1540 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001541 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001542};
1543
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001545surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001546 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547{
1548 struct weston_move_grab *move;
1549
1550 if (!shsurf)
1551 return -1;
1552
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001553 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001554 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1555 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556 return 0;
1557
1558 move = malloc(sizeof *move);
1559 if (!move)
1560 return -1;
1561
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001563 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001565 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001566 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567
1568 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001569 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570
1571 return 0;
1572}
1573
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574struct weston_resize_grab {
1575 struct shell_grab base;
1576 uint32_t edges;
1577 int32_t width, height;
1578};
1579
1580static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001581resize_grab_motion(struct weston_pointer_grab *grab,
1582 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001583 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584{
1585 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001586 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587 struct shell_surface *shsurf = resize->base.shsurf;
1588 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001589 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590 wl_fixed_t from_x, from_y;
1591 wl_fixed_t to_x, to_y;
1592
Jonas Ådahld2510102014-10-05 21:39:14 +02001593 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001594
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001595 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001596 return;
1597
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 weston_view_from_global_fixed(shsurf->view,
1599 pointer->grab_x, pointer->grab_y,
1600 &from_x, &from_y);
1601 weston_view_from_global_fixed(shsurf->view,
1602 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001603
1604 width = resize->width;
1605 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1606 width += wl_fixed_to_int(from_x - to_x);
1607 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1608 width += wl_fixed_to_int(to_x - from_x);
1609 }
1610
1611 height = resize->height;
1612 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1613 height += wl_fixed_to_int(from_y - to_y);
1614 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1615 height += wl_fixed_to_int(to_y - from_y);
1616 }
1617
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001618 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1619 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1620
1621 min_size.width = MAX(1, min_size.width);
1622 min_size.height = MAX(1, min_size.height);
1623
1624 if (width < min_size.width)
1625 width = min_size.width;
1626 else if (max_size.width > 0 && width > max_size.width)
1627 width = max_size.width;
1628 if (height < min_size.height)
1629 height = min_size.height;
1630 else if (max_size.width > 0 && width > max_size.width)
1631 width = max_size.width;
1632 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633}
1634
1635static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001636resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637 uint32_t time, uint32_t button, uint32_t state_w)
1638{
1639 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001640 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001642 struct weston_desktop_surface *desktop_surface =
1643 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644
1645 if (pointer->button_count == 0 &&
1646 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001647 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648 shell_grab_end(&resize->base);
1649 free(grab);
1650 }
1651}
1652
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001653static void
1654resize_grab_cancel(struct weston_pointer_grab *grab)
1655{
1656 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001657 struct weston_desktop_surface *desktop_surface =
1658 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001659
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001660 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001661 shell_grab_end(&resize->base);
1662 free(grab);
1663}
1664
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001665static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001666 noop_grab_focus,
1667 resize_grab_motion,
1668 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001669 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001670 noop_grab_axis_source,
1671 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001672 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673};
1674
Giulio Camuffob8366642013-04-25 13:57:46 +03001675/*
1676 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001677 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001678static void
1679surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1680 int32_t *y, int32_t *w, int32_t *h) {
1681 pixman_region32_t region;
1682 pixman_box32_t *box;
1683 struct weston_subsurface *subsurface;
1684
1685 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 surface->width,
1687 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001688
1689 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1690 pixman_region32_union_rect(&region, &region,
1691 subsurface->position.x,
1692 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 subsurface->surface->width,
1694 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001695 }
1696
1697 box = pixman_region32_extents(&region);
1698 if (x)
1699 *x = box->x1;
1700 if (y)
1701 *y = box->y1;
1702 if (w)
1703 *w = box->x2 - box->x1;
1704 if (h)
1705 *h = box->y2 - box->y1;
1706
1707 pixman_region32_fini(&region);
1708}
1709
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710static int
1711surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001712 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713{
1714 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001715 const unsigned resize_topbottom =
1716 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1717 const unsigned resize_leftright =
1718 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1719 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001720 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001722 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001723 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1724 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725 return 0;
1726
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001727 /* Check for invalid edge combinations. */
1728 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1729 (edges & resize_topbottom) == resize_topbottom ||
1730 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 return 0;
1732
1733 resize = malloc(sizeof *resize);
1734 if (!resize)
1735 return -1;
1736
1737 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001739 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1740 resize->width = geometry.width;
1741 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001742
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001743 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001744 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001746 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747
1748 return 0;
1749}
1750
1751static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001752busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001755 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001756 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001758 wl_fixed_t sx, sy;
1759
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760 view = weston_compositor_pick_view(pointer->seat->compositor,
1761 pointer->x, pointer->y,
1762 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001763 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001765 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001766 shell_grab_end(grab);
1767 free(grab);
1768 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001769}
1770
1771static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001772busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1773 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001774 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001775{
Jonas Ådahld2510102014-10-05 21:39:14 +02001776 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001777}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001778
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001779static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001780busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001781 uint32_t time, uint32_t button, uint32_t state)
1782{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001783 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001784 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001785 struct weston_pointer *pointer = grab->grab.pointer;
1786 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001787
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001788 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001789 activate(shsurf->shell, shsurf->view, seat,
1790 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001791 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001792 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001793 activate(shsurf->shell, shsurf->view, seat,
1794 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001795 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001796 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797}
1798
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001799static void
1800busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1801{
1802 struct shell_grab *grab = (struct shell_grab *) base;
1803
1804 shell_grab_end(grab);
1805 free(grab);
1806}
1807
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001808static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809 busy_cursor_grab_focus,
1810 busy_cursor_grab_motion,
1811 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001812 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001813 noop_grab_axis_source,
1814 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001815 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816};
1817
1818static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819handle_pointer_focus(struct wl_listener *listener, void *data)
1820{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001821 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001823 struct shell_surface *shsurf;
1824 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827 return;
1828
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001829 shsurf = get_shell_surface(view->surface);
1830 if (!shsurf)
1831 return;
1832
1833 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1834
1835 if (shsurf->unresponsive)
1836 set_busy_cursor(shsurf, pointer);
1837 else
1838 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839}
1840
1841static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001842shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1843{
1844 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001845 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001846}
1847
1848static void
1849shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1850{
1851 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001852 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001853}
1854
1855static void
1856handle_keyboard_focus(struct wl_listener *listener, void *data)
1857{
1858 struct weston_keyboard *keyboard = data;
1859 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1860
1861 if (seat->focused_surface) {
1862 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1863 if (shsurf)
1864 shell_surface_lose_keyboard_focus(shsurf);
1865 }
1866
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001867 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001868
1869 if (seat->focused_surface) {
1870 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1871 if (shsurf)
1872 shell_surface_gain_keyboard_focus(shsurf);
1873 }
1874}
1875
Philip Withnall07926d92013-11-25 18:01:40 +00001876/* The surface will be inserted into the list immediately after the link
1877 * returned by this function (i.e. will be stacked immediately above the
1878 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001879static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001880shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1881{
1882 struct workspace *ws;
1883
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001884 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1885 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001886 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001887 }
1888
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001889 /* Move the surface to a normal workspace layer so that surfaces
1890 * which were previously fullscreen or transient are no longer
1891 * rendered on top. */
1892 ws = get_current_workspace(shsurf->shell);
1893 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001894}
1895
Philip Withnall648a4dd2013-11-25 18:01:44 +00001896static void
1897shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1898{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001899 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001900}
1901
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001902/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001903 * geometry to ensure the changes are redrawn.
1904 *
1905 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1906 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001907static void
1908shell_surface_update_layer(struct shell_surface *shsurf)
1909{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001910 struct weston_surface *surface =
1911 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001912 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001913
1914 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1915
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001916 if (new_layer_link == NULL)
1917 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001918 if (new_layer_link == &shsurf->view->layer_link)
1919 return;
1920
1921 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001922 weston_layer_entry_remove(&shsurf->view->layer_link);
1923 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001924 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001925 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001926
1927 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001928}
1929
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001930static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001931shell_surface_set_output(struct shell_surface *shsurf,
1932 struct weston_output *output)
1933{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001934 struct weston_surface *es =
1935 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001936
1937 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001938 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001939 if (output)
1940 shsurf->output = output;
1941 else if (es->output)
1942 shsurf->output = es->output;
1943 else
1944 shsurf->output = get_default_output(es->compositor);
1945}
1946
1947static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001948weston_view_set_initial_position(struct weston_view *view,
1949 struct desktop_shell *shell);
1950
1951static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001952unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001953{
Philip Withnallf85fe842013-11-25 18:01:37 +00001954 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001955 wl_list_remove(&shsurf->fullscreen.transform.link);
1956 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001957
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958 if (shsurf->fullscreen.black_view)
1959 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1960 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001961
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001962 if (shsurf->saved_position_valid)
1963 weston_view_set_position(shsurf->view,
1964 shsurf->saved_x, shsurf->saved_y);
1965 else
1966 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001967 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001968
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001969 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001971 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001972 shsurf->saved_rotation_valid = false;
1973 }
Alex Wu4539b082012-03-01 12:57:46 +08001974}
1975
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001976static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001977unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001978{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001979 struct weston_surface *surface =
1980 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1981
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001982 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001983 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001984
1985 if (shsurf->saved_position_valid)
1986 weston_view_set_position(shsurf->view,
1987 shsurf->saved_x, shsurf->saved_y);
1988 else
1989 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001990 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001991
1992 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001993 wl_list_insert(&shsurf->view->geometry.transformation_list,
1994 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001995 shsurf->saved_rotation_valid = false;
1996 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001997}
1998
Philip Withnallbecb77e2013-11-25 18:01:30 +00001999static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002000set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002001{
2002 struct shell_surface *shsurf;
2003 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002004 struct weston_view *view;
2005
2006 view = get_default_view(surface);
2007 if (!view)
2008 return;
2009
2010 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2011
2012 shsurf = get_shell_surface(surface);
2013 current_ws = get_current_workspace(shsurf->shell);
2014
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002015 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002016 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002017
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002018 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002019 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002020
2021 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002022 weston_view_damage_below(view);
2023}
2024
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002025
Tiago Vignattibe143262012-04-16 17:31:41 +03002026static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002027shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002028{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002029 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002030}
2031
Pekka Paalanen8274d902014-08-06 19:36:51 +03002032static int
2033black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2034{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002035 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002036 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002037 int n;
2038 int rem;
2039 int ret;
2040
2041 n = snprintf(buf, len, "black background surface for ");
2042 if (n < 0)
2043 return n;
2044
2045 rem = (int)len - n;
2046 if (rem < 0)
2047 rem = 0;
2048
2049 if (fs_surface->get_label)
2050 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2051 else
2052 ret = snprintf(buf + n, rem, "<unknown>");
2053
2054 if (ret < 0)
2055 return n;
2056
2057 return n + ret;
2058}
2059
Alex Wu21858432012-04-01 20:13:08 +08002060static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002061black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002062
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002064create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002065 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002066 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002067{
2068 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002070
2071 surface = weston_surface_create(ec);
2072 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002073 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002074 return NULL;
2075 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 view = weston_view_create(surface);
2077 if (surface == NULL) {
2078 weston_log("no memory\n");
2079 weston_surface_destroy(surface);
2080 return NULL;
2081 }
Alex Wu4539b082012-03-01 12:57:46 +08002082
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002083 surface->committed = black_surface_committed;
2084 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002085 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002086 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002087 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002088 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002089 pixman_region32_fini(&surface->input);
2090 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002091
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002092 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002093 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094
2095 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002096}
2097
Philip Withnalled8f1a92013-11-25 18:01:43 +00002098static void
2099shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2100{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002101 struct weston_surface *surface =
2102 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002103 struct weston_output *output = shsurf->fullscreen_output;
2104
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002105 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002106
2107 if (!shsurf->fullscreen.black_view)
2108 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002109 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002110 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002111 output->x, output->y,
2112 output->width,
2113 output->height);
2114
2115 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002116 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2117 weston_layer_entry_insert(&shsurf->view->layer_link,
2118 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002119 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002120 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002121
Armin Krezović4663aca2016-06-30 06:04:29 +02002122 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002123 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002124}
2125
Alex Wu4539b082012-03-01 12:57:46 +08002126/* Create black surface and append it to the associated fullscreen surface.
2127 * Handle size dismatch and positioning according to the method. */
2128static void
2129shell_configure_fullscreen(struct shell_surface *shsurf)
2130{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002131 struct weston_surface *surface =
2132 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002133 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002134
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002135 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002136 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002137 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2138 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002139
Philip Withnalled8f1a92013-11-25 18:01:43 +00002140 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002141
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002142 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002143 &surf_width, &surf_height);
2144
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002145 if (surface->buffer_ref.buffer)
2146 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002147}
2148
Alex Wu4539b082012-03-01 12:57:46 +08002149static void
2150shell_map_fullscreen(struct shell_surface *shsurf)
2151{
Alex Wubd3354b2012-04-17 17:20:49 +08002152 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002153}
2154
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002155static struct weston_output *
2156get_focused_output(struct weston_compositor *compositor)
2157{
2158 struct weston_seat *seat;
2159 struct weston_output *output = NULL;
2160
2161 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002162 struct weston_touch *touch = weston_seat_get_touch(seat);
2163 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2164 struct weston_keyboard *keyboard =
2165 weston_seat_get_keyboard(seat);
2166
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002167 /* Priority has touch focus, then pointer and
2168 * then keyboard focus. We should probably have
2169 * three for loops and check frist for touch,
2170 * then for pointer, etc. but unless somebody has some
2171 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002172 if (touch && touch->focus)
2173 output = touch->focus->output;
2174 else if (pointer && pointer->focus)
2175 output = pointer->focus->output;
2176 else if (keyboard && keyboard->focus)
2177 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002178
2179 if (output)
2180 break;
2181 }
2182
2183 return output;
2184}
2185
2186static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002187destroy_shell_seat(struct wl_listener *listener, void *data)
2188{
2189 struct shell_seat *shseat =
2190 container_of(listener,
2191 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002192
2193 wl_list_remove(&shseat->seat_destroy_listener.link);
2194 free(shseat);
2195}
2196
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197static void
2198shell_seat_caps_changed(struct wl_listener *listener, void *data)
2199{
Derek Foreman1281a362015-07-31 16:55:32 -05002200 struct weston_keyboard *keyboard;
2201 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002202 struct shell_seat *seat;
2203
2204 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002205 keyboard = weston_seat_get_keyboard(seat->seat);
2206 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002207
Derek Foreman1281a362015-07-31 16:55:32 -05002208 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002209 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002210 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002211 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002212 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002213 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002214 wl_list_init(&seat->keyboard_focus_listener.link);
2215 }
2216
Derek Foreman1281a362015-07-31 16:55:32 -05002217 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002218 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002219 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002220 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002221 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002222 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002223 wl_list_init(&seat->pointer_focus_listener.link);
2224 }
2225}
2226
Giulio Camuffo5085a752013-03-25 21:42:45 +01002227static struct shell_seat *
2228create_shell_seat(struct weston_seat *seat)
2229{
2230 struct shell_seat *shseat;
2231
2232 shseat = calloc(1, sizeof *shseat);
2233 if (!shseat) {
2234 weston_log("no memory to allocate shell seat\n");
2235 return NULL;
2236 }
2237
2238 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002239
2240 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2241 wl_signal_add(&seat->destroy_signal,
2242 &shseat->seat_destroy_listener);
2243
Jason Ekstrand024177c2014-04-21 19:42:58 -05002244 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2245 wl_list_init(&shseat->keyboard_focus_listener.link);
2246
2247 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2248 wl_list_init(&shseat->pointer_focus_listener.link);
2249
2250 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2251 wl_signal_add(&seat->updated_caps_signal,
2252 &shseat->caps_changed_listener);
2253 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2254
Giulio Camuffo5085a752013-03-25 21:42:45 +01002255 return shseat;
2256}
2257
2258static struct shell_seat *
2259get_shell_seat(struct weston_seat *seat)
2260{
2261 struct wl_listener *listener;
2262
2263 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002264 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002265
2266 return container_of(listener,
2267 struct shell_seat, seat_destroy_listener);
2268}
2269
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002270static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002271fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002272{
2273 struct shell_surface *shsurf = data;
2274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002275 weston_surface_destroy(shsurf->view->surface);
2276 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002277}
2278
2279static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002280fade_out_done(struct weston_view_animation *animation, void *data)
2281{
2282 struct shell_surface *shsurf = data;
2283 struct wl_event_loop *loop;
2284
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002285 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002286
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002287 if (weston_view_is_mapped(shsurf->view)) {
2288 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002289 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002290 }
2291}
2292
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002293struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002294get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002295{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002296 if (weston_surface_is_desktop_surface(surface)) {
2297 struct weston_desktop_surface *desktop_surface =
2298 weston_surface_get_desktop_surface(surface);
2299 return weston_desktop_surface_get_user_data(desktop_surface);
2300 }
2301 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002302}
2303
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002304/*
2305 * libweston-desktop
2306 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002307
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002308static void
2309desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2310 void *shell)
2311{
2312 struct weston_desktop_client *client =
2313 weston_desktop_surface_get_client(desktop_surface);
2314 struct wl_client *wl_client =
2315 weston_desktop_client_get_client(client);
2316 struct weston_view *view;
2317 struct shell_surface *shsurf;
2318 struct weston_surface *surface =
2319 weston_desktop_surface_get_surface(desktop_surface);
2320
2321 view = weston_desktop_surface_create_view(desktop_surface);
2322 if (!view)
2323 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002324
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002325 shsurf = calloc(1, sizeof *shsurf);
2326 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002327 if (wl_client)
2328 wl_client_post_no_memory(wl_client);
2329 else
2330 weston_log("no memory to allocate shell surface\n");
2331 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002332 }
2333
Pekka Paalanen8274d902014-08-06 19:36:51 +03002334 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002335
Tiago Vignattibc052c92012-04-19 16:18:18 +03002336 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002337 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002338 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002339 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002340 shsurf->desktop_surface = desktop_surface;
2341 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002343 wl_list_init(&shsurf->fullscreen.transform.link);
2344
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002345 shsurf->output = get_default_output(shsurf->shell->compositor);
2346
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002347 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002348
Pekka Paalanen460099f2012-01-20 16:48:25 +02002349 /* empty when not in use */
2350 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002351 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002352
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002353 wl_list_init(&shsurf->workspace_transform.link);
2354
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002355 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2356 weston_desktop_surface_set_activated(desktop_surface,
2357 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002358}
2359
Tiago Vignattibc052c92012-04-19 16:18:18 +03002360static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002361desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2362 void *shell)
2363{
2364 struct shell_surface *shsurf =
2365 weston_desktop_surface_get_user_data(desktop_surface);
2366 struct weston_surface *surface =
2367 weston_desktop_surface_get_surface(desktop_surface);
2368
2369 if (!shsurf)
2370 return;
2371
2372 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2373
2374 if (shsurf->fullscreen.black_view)
2375 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2376
2377 weston_surface_set_label_func(surface, NULL);
2378 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2379 shsurf->desktop_surface = NULL;
2380
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002381 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002382 if (weston_surface_is_mapped(surface) &&
2383 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2384 pixman_region32_fini(&surface->pending.input);
2385 pixman_region32_init(&surface->pending.input);
2386 pixman_region32_fini(&surface->input);
2387 pixman_region32_init(&surface->input);
2388 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2389 fade_out_done, shsurf);
2390 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002391 weston_view_destroy(shsurf->view);
2392 free(shsurf);
2393 }
2394}
2395
2396static void
2397set_maximized_position(struct desktop_shell *shell,
2398 struct shell_surface *shsurf)
2399{
2400 pixman_rectangle32_t area;
2401 struct weston_geometry geometry;
2402
2403 get_output_work_area(shell, shsurf->output, &area);
2404 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2405
2406 weston_view_set_position(shsurf->view,
2407 area.x - geometry.x,
2408 area.y - geometry.y);
2409}
2410
2411static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002412set_position_from_xwayland(struct shell_surface *shsurf)
2413{
2414 struct weston_geometry geometry;
2415 float x;
2416 float y;
2417
2418 assert(shsurf->xwayland.is_set);
2419
2420 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2421 x = shsurf->xwayland.x - geometry.x;
2422 y = shsurf->xwayland.y - geometry.y;
2423
2424 weston_view_set_position(shsurf->view, x, y);
2425
2426#ifdef WM_DEBUG
2427 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2428 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2429 geometry.x, geometry.y, x, y);
2430#endif
2431}
2432
2433static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002434map(struct desktop_shell *shell, struct shell_surface *shsurf,
2435 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002436{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002437 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002438 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2439 struct weston_compositor *compositor = shell->compositor;
2440 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002441
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002443 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002444 center_on_output(shsurf->view, shsurf->fullscreen_output);
2445 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002446 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002447 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002448 } else if (shsurf->xwayland.is_set) {
2449 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002450 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002452 }
2453
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002454 /* Surface stacking order, see also activate(). */
2455 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002456
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002457 weston_view_update_transform(shsurf->view);
2458 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002459 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002460 surface->output = shsurf->output;
2461 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002462 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002463
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002464 if (!shell->locked) {
2465 wl_list_for_each(seat, &compositor->seat_list, link)
2466 activate(shell, shsurf->view, seat,
2467 WESTON_ACTIVATE_FLAG_CONFIGURE);
2468 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002469
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002470 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002471 switch (shell->win_animation_type) {
2472 case ANIMATION_FADE:
2473 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2474 break;
2475 case ANIMATION_ZOOM:
2476 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2477 break;
2478 case ANIMATION_NONE:
2479 default:
2480 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002481 }
2482 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002483}
2484
2485static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002486desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2487 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002488{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002489 struct shell_surface *shsurf =
2490 weston_desktop_surface_get_user_data(desktop_surface);
2491 struct weston_surface *surface =
2492 weston_desktop_surface_get_surface(desktop_surface);
2493 struct weston_view *view = shsurf->view;
2494 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002495 bool was_fullscreen;
2496 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002497
2498 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002499 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002501 was_fullscreen = shsurf->state.fullscreen;
2502 was_maximized = shsurf->state.maximized;
2503
2504 shsurf->state.fullscreen =
2505 weston_desktop_surface_get_fullscreen(desktop_surface);
2506 shsurf->state.maximized =
2507 weston_desktop_surface_get_maximized(desktop_surface);
2508
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002509 if (!weston_surface_is_mapped(surface)) {
2510 map(shell, shsurf, sx, sy);
2511 surface->is_mapped = true;
2512 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2513 ++surface->ref_count;
2514 return;
2515 }
2516
2517 if (sx == 0 && sy == 0 &&
2518 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002519 shsurf->last_height == surface->height &&
2520 was_fullscreen == shsurf->state.fullscreen &&
2521 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002522 return;
2523
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002524 if (was_fullscreen)
2525 unset_fullscreen(shsurf);
2526 if (was_maximized)
2527 unset_maximized(shsurf);
2528
Quentin Glidic920cf042016-08-16 14:26:20 +02002529 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2530 !shsurf->saved_position_valid) {
2531 shsurf->saved_x = shsurf->view->geometry.x;
2532 shsurf->saved_y = shsurf->view->geometry.y;
2533 shsurf->saved_position_valid = true;
2534
2535 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2536 wl_list_remove(&shsurf->rotation.transform.link);
2537 wl_list_init(&shsurf->rotation.transform.link);
2538 weston_view_geometry_dirty(shsurf->view);
2539 shsurf->saved_rotation_valid = true;
2540 }
2541 }
2542
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002543 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002544 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002545 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002546 set_maximized_position(shell, shsurf);
2547 surface->output = shsurf->output;
2548 } else {
2549 float from_x, from_y;
2550 float to_x, to_y;
2551 float x, y;
2552
2553 if (shsurf->resize_edges) {
2554 sx = 0;
2555 sy = 0;
2556 }
2557
2558 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2559 sx = shsurf->last_width - surface->width;
2560 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2561 sy = shsurf->last_height - surface->height;
2562
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002563 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2564 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2565 x = shsurf->view->geometry.x + to_x - from_x;
2566 y = shsurf->view->geometry.y + to_y - from_y;
2567
2568 weston_view_set_position(shsurf->view, x, y);
2569 }
2570
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002571 shsurf->last_width = surface->width;
2572 shsurf->last_height = surface->height;
2573
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002574 /* XXX: would a fullscreen surface need the same handling? */
2575 if (surface->output) {
2576 wl_list_for_each(view, &surface->views, surface_link)
2577 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002578 }
2579}
2580
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002581static void
2582set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2583 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002584{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2586 struct weston_surface *surface =
2587 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2588 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002589
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002590 if (fullscreen) {
2591 /* handle clients launching in fullscreen */
2592 if (output == NULL && !weston_surface_is_mapped(surface)) {
2593 /* Set the output to the one that has focus currently. */
2594 output = get_focused_output(surface->compositor);
2595 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002596
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002597 shell_surface_set_output(shsurf, output);
2598 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002599
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002600 width = shsurf->output->width;
2601 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002602 }
2603 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2604 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002605}
2606
2607static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002608desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2609 struct weston_seat *seat, uint32_t serial, void *shell)
2610{
2611 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2612 struct weston_touch *touch = weston_seat_get_touch(seat);
2613 struct shell_surface *shsurf =
2614 weston_desktop_surface_get_user_data(desktop_surface);
2615 struct weston_surface *surface =
2616 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2617 struct wl_resource *resource = surface->resource;
2618 struct weston_surface *focus;
2619
2620 if (pointer &&
2621 pointer->focus &&
2622 pointer->button_count > 0 &&
2623 pointer->grab_serial == serial) {
2624 focus = weston_surface_get_main_surface(pointer->focus->surface);
2625 if ((focus == surface) &&
2626 (surface_move(shsurf, pointer, true) < 0))
2627 wl_resource_post_no_memory(resource);
2628 } else if (touch &&
2629 touch->focus &&
2630 touch->grab_serial == serial) {
2631 focus = weston_surface_get_main_surface(touch->focus->surface);
2632 if ((focus == surface) &&
2633 (surface_touch_move(shsurf, touch) < 0))
2634 wl_resource_post_no_memory(resource);
2635 }
2636}
2637
2638static void
2639desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2640 struct weston_seat *seat, uint32_t serial,
2641 enum weston_desktop_surface_edge edges, void *shell)
2642{
2643 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2644 struct shell_surface *shsurf =
2645 weston_desktop_surface_get_user_data(desktop_surface);
2646 struct weston_surface *surface =
2647 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2648 struct wl_resource *resource = surface->resource;
2649 struct weston_surface *focus;
2650
2651 if (!pointer ||
2652 pointer->button_count == 0 ||
2653 pointer->grab_serial != serial ||
2654 pointer->focus == NULL)
2655 return;
2656
2657 focus = weston_surface_get_main_surface(pointer->focus->surface);
2658 if (focus != surface)
2659 return;
2660
2661 if (surface_resize(shsurf, pointer, edges) < 0)
2662 wl_resource_post_no_memory(resource);
2663}
2664
2665static void
2666desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2667 bool fullscreen,
2668 struct weston_output *output, void *shell)
2669{
2670 struct shell_surface *shsurf =
2671 weston_desktop_surface_get_user_data(desktop_surface);
2672
2673 set_fullscreen(shsurf, fullscreen, output);
2674}
2675
2676static void
2677set_maximized(struct shell_surface *shsurf, bool maximized)
2678{
2679 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2680 struct weston_surface *surface =
2681 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2682 int32_t width = 0, height = 0;
2683
2684 if (maximized) {
2685 struct weston_output *output;
2686 struct desktop_shell *shell;
2687 pixman_rectangle32_t area;
2688
2689 if (!weston_surface_is_mapped(surface))
2690 output = get_focused_output(surface->compositor);
2691 else
2692 output = surface->output;
2693
2694 shell_surface_set_output(shsurf, output);
2695
2696 shell = shell_surface_get_shell(shsurf);
2697 get_output_work_area(shell, shsurf->output, &area);
2698
2699 width = area.width;
2700 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002701 }
2702 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2703 weston_desktop_surface_set_size(desktop_surface, width, height);
2704}
2705
2706static void
2707desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2708 bool maximized, void *shell)
2709{
2710 struct shell_surface *shsurf =
2711 weston_desktop_surface_get_user_data(desktop_surface);
2712
2713 set_maximized(shsurf, maximized);
2714}
2715
2716static void
2717desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2718 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002719{
2720 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002721 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002722
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002723 /* apply compositor's own minimization logic (hide) */
2724 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002725}
2726
Rafael Antognollie2a34552013-12-03 15:35:45 -02002727static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002728set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002729{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002730 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002731
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002732 if (pointer->grab->interface == &busy_cursor_grab_interface)
2733 return;
2734
2735 grab = malloc(sizeof *grab);
2736 if (!grab)
2737 return;
2738
2739 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2740 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2741 /* Mark the shsurf as ungrabbed so that button binding is able
2742 * to move it. */
2743 shsurf->grabbed = 0;
2744}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002745
2746static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002747end_busy_cursor(struct weston_compositor *compositor,
2748 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002749{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002750 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002751 struct shell_grab *grab;
2752 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002753
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002754 wl_list_for_each(seat, &compositor->seat_list, link) {
2755 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2756 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002757
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002758 if (!pointer)
2759 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002760
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002761 if (pointer->grab->interface != &busy_cursor_grab_interface)
2762 continue;
2763
2764 grab = (struct shell_grab *) pointer->grab;
2765 shsurf = grab->shsurf;
2766 if (!shsurf)
2767 continue;
2768
2769 grab_client =
2770 weston_desktop_surface_get_client(shsurf->desktop_surface);
2771 if (grab_client == desktop_client) {
2772 shell_grab_end(grab);
2773 free(grab);
2774 }
2775 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002776}
2777
2778static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002779desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2780 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002781{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002782 struct shell_surface *shsurf =
2783 weston_desktop_surface_get_user_data(desktop_surface);
2784 bool *unresponsive = user_data;
2785
2786 shsurf->unresponsive = *unresponsive;
2787}
2788
2789static void
2790desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2791 void *shell_)
2792{
2793 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002794 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002795 struct weston_seat *seat;
2796 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002797
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798 weston_desktop_client_for_each_surface(desktop_client,
2799 desktop_surface_set_unresponsive,
2800 &unresponsive);
2801
2802
2803 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2804 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2805 struct weston_desktop_client *grab_client;
2806
2807 if (!pointer || !pointer->focus)
2808 continue;
2809
2810 shsurf = get_shell_surface(pointer->focus->surface);
2811 if (!shsurf)
2812 continue;
2813
2814 grab_client =
2815 weston_desktop_surface_get_client(shsurf->desktop_surface);
2816 if (grab_client == desktop_client)
2817 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002818 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002819}
2820
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002821static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822desktop_surface_pong(struct weston_desktop_client *desktop_client,
2823 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002824{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002825 struct desktop_shell *shell = shell_;
2826 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002827
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002828 weston_desktop_client_for_each_surface(desktop_client,
2829 desktop_surface_set_unresponsive,
2830 &unresponsive);
2831 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002832}
2833
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002834static void
2835desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2836 int32_t x, int32_t y, void *shell_)
2837{
2838 struct shell_surface *shsurf =
2839 weston_desktop_surface_get_user_data(surface);
2840
2841 shsurf->xwayland.x = x;
2842 shsurf->xwayland.y = y;
2843 shsurf->xwayland.is_set = true;
2844}
2845
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002846static const struct weston_desktop_api shell_desktop_api = {
2847 .struct_size = sizeof(struct weston_desktop_api),
2848 .surface_added = desktop_surface_added,
2849 .surface_removed = desktop_surface_removed,
2850 .committed = desktop_surface_committed,
2851 .move = desktop_surface_move,
2852 .resize = desktop_surface_resize,
2853 .fullscreen_requested = desktop_surface_fullscreen_requested,
2854 .maximized_requested = desktop_surface_maximized_requested,
2855 .minimized_requested = desktop_surface_minimized_requested,
2856 .ping_timeout = desktop_surface_ping_timeout,
2857 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002858 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002859};
2860
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002861/* ************************ *
2862 * end of libweston-desktop *
2863 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002864static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002865configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002866{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002867 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002868
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002869 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002870 if (v->output == ev->output && v != ev) {
2871 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002872 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002873 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002874 }
2875 }
2876
Quentin Glidice8bf9592016-06-23 18:55:20 +02002877 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002878 ev->surface->is_mapped = true;
2879 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002880
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002881 if (wl_list_empty(&ev->layer_link.link)) {
2882 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002884 }
2885}
2886
David Fort50d962f2016-06-09 17:55:52 +02002887
2888static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002889find_shell_output_from_weston_output(struct desktop_shell *shell,
2890 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002891{
2892 struct shell_output *shell_output;
2893
2894 wl_list_for_each(shell_output, &shell->output_list, link) {
2895 if (shell_output->output == output)
2896 return shell_output;
2897 }
2898
2899 return NULL;
2900}
2901
Pekka Paalanen8274d902014-08-06 19:36:51 +03002902static int
2903background_get_label(struct weston_surface *surface, char *buf, size_t len)
2904{
2905 return snprintf(buf, len, "background for output %s",
2906 surface->output->name);
2907}
2908
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002909static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002910background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002911{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002912 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002914
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 view = container_of(es->views.next, struct weston_view, surface_link);
2916
Quentin Glidice8bf9592016-06-23 18:55:20 +02002917 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002918}
2919
2920static void
David Fort50d962f2016-06-09 17:55:52 +02002921handle_background_surface_destroy(struct wl_listener *listener, void *data)
2922{
2923 struct shell_output *output =
2924 container_of(listener, struct shell_output, background_surface_listener);
2925
2926 weston_log("background surface gone\n");
2927 output->background_surface = NULL;
2928}
2929
2930static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002931desktop_shell_set_background(struct wl_client *client,
2932 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002933 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002934 struct wl_resource *surface_resource)
2935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002936 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002937 struct weston_surface *surface =
2938 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002939 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002941
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002942 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002943 wl_resource_post_error(surface_resource,
2944 WL_DISPLAY_ERROR_INVALID_OBJECT,
2945 "surface role already assigned");
2946 return;
2947 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002948
Jason Ekstranda7af7042013-10-12 22:38:11 -05002949 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2950 weston_view_destroy(view);
2951 view = weston_view_create(surface);
2952
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002953 surface->committed = background_committed;
2954 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002955 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03002956 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002958 weston_desktop_shell_send_configure(resource, 0,
2959 surface_resource,
2960 surface->output->width,
2961 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002962
2963 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2964 sh_output->background_surface = surface;
2965
2966 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2967 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002968}
2969
Pekka Paalanen8274d902014-08-06 19:36:51 +03002970static int
2971panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2972{
2973 return snprintf(buf, len, "panel for output %s",
2974 surface->output->name);
2975}
2976
Kristian Høgsberg75840622011-09-06 13:48:16 -04002977static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002978panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002979{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002980 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002981 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002982 int width, height;
2983 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002984
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 view = container_of(es->views.next, struct weston_view, surface_link);
2986
Quentin Glidice8bf9592016-06-23 18:55:20 +02002987 get_panel_size(shell, view, &width, &height);
2988 switch (shell->panel_position) {
2989 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2990 break;
2991 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2992 y = view->output->height - height;
2993 break;
2994 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2995 break;
2996 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2997 x = view->output->width - width;
2998 break;
2999 }
3000
3001 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003002}
3003
3004static void
David Fort50d962f2016-06-09 17:55:52 +02003005handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3006{
3007 struct shell_output *output =
3008 container_of(listener, struct shell_output, panel_surface_listener);
3009
3010 weston_log("panel surface gone\n");
3011 output->panel_surface = NULL;
3012}
3013
3014
3015static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003016desktop_shell_set_panel(struct wl_client *client,
3017 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003018 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003019 struct wl_resource *surface_resource)
3020{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003021 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003022 struct weston_surface *surface =
3023 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003024 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003025 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003026
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003027 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003028 wl_resource_post_error(surface_resource,
3029 WL_DISPLAY_ERROR_INVALID_OBJECT,
3030 "surface role already assigned");
3031 return;
3032 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003033
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3035 weston_view_destroy(view);
3036 view = weston_view_create(surface);
3037
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003038 surface->committed = panel_committed;
3039 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003040 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03003041 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003042 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003043 weston_desktop_shell_send_configure(resource, 0,
3044 surface_resource,
3045 surface->output->width,
3046 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003047
3048 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3049 sh_output->panel_surface = surface;
3050
3051 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3052 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003053}
3054
Pekka Paalanen8274d902014-08-06 19:36:51 +03003055static int
3056lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3057{
3058 return snprintf(buf, len, "lock window");
3059}
3060
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003061static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003062lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003063{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003064 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 struct weston_view *view;
3066
3067 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003068
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003069 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003070 return;
3071
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003073
3074 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003075 weston_layer_entry_insert(&shell->lock_layer.view_list,
3076 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003078 surface->is_mapped = true;
3079 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003080 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003081 }
3082}
3083
3084static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003085handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003086{
Tiago Vignattibe143262012-04-16 17:31:41 +03003087 struct desktop_shell *shell =
3088 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003089
Martin Minarik6d118362012-06-07 18:01:59 +02003090 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003091 shell->lock_surface = NULL;
3092}
3093
3094static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003095desktop_shell_set_lock_surface(struct wl_client *client,
3096 struct wl_resource *resource,
3097 struct wl_resource *surface_resource)
3098{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003099 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003100 struct weston_surface *surface =
3101 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003102
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003103 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003104
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003105 if (!shell->locked)
3106 return;
3107
Pekka Paalanen98262232011-12-01 10:42:22 +02003108 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003109
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003110 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003111 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003112 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003113
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003114 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003115 surface->committed = lock_surface_committed;
3116 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003117 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003118}
3119
3120static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003121resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003122{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003123 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003124
Quentin Glidic82681572016-12-17 13:40:51 +01003125 weston_layer_unset_position(&shell->lock_layer);
3126
3127 if (shell->showing_input_panels)
3128 weston_layer_set_position(&shell->input_panel_layer,
3129 WESTON_LAYER_POSITION_TOP_UI);
3130 weston_layer_set_position(&shell->fullscreen_layer,
3131 WESTON_LAYER_POSITION_FULLSCREEN);
3132 weston_layer_set_position(&shell->panel_layer,
3133 WESTON_LAYER_POSITION_UI);
3134 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003135
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003136 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003137
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003138 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003139 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003140 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003141}
3142
3143static void
3144desktop_shell_unlock(struct wl_client *client,
3145 struct wl_resource *resource)
3146{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003147 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003148
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003149 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003150
3151 if (shell->locked)
3152 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003153}
3154
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003155static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003156desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003157 struct wl_resource *resource,
3158 struct wl_resource *surface_resource)
3159{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003160 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003161
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003162 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003163 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003164}
3165
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003166static void
3167desktop_shell_desktop_ready(struct wl_client *client,
3168 struct wl_resource *resource)
3169{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003170 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003171
3172 shell_fade_startup(shell);
3173}
3174
Jonny Lamb765760d2014-08-20 15:53:19 +02003175static void
3176desktop_shell_set_panel_position(struct wl_client *client,
3177 struct wl_resource *resource,
3178 uint32_t position)
3179{
3180 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3181
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003182 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3183 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3184 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3185 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003186 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003187 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003188 "bad position argument");
3189 return;
3190 }
3191
3192 shell->panel_position = position;
3193}
3194
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003195static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003196 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003197 desktop_shell_set_panel,
3198 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003199 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003200 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003201 desktop_shell_desktop_ready,
3202 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003203};
3204
3205static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003206move_binding(struct weston_pointer *pointer, uint32_t time,
3207 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003208{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003209 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003210 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003211 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003212
Derek Foreman8ae2db52015-07-15 13:00:36 -05003213 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003214 return;
3215
Derek Foreman8ae2db52015-07-15 13:00:36 -05003216 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003217
Pekka Paalanen01388e22013-04-25 13:57:44 +03003218 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003219 if (surface == NULL)
3220 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003221
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003222 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003223 if (shsurf == NULL ||
3224 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3225 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003226 return;
3227
Derek Foreman794fa0e2015-07-15 13:00:38 -05003228 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003229}
3230
3231static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003232maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3233 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003234{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003235 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003236 struct weston_surface *surface;
3237 struct shell_surface *shsurf;
3238
3239 surface = weston_surface_get_main_surface(focus);
3240 if (surface == NULL)
3241 return;
3242
3243 shsurf = get_shell_surface(surface);
3244 if (shsurf == NULL)
3245 return;
3246
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003247 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003248}
3249
3250static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003251fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3252 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003253{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003254 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003255 struct weston_surface *surface;
3256 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003257 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003258
3259 surface = weston_surface_get_main_surface(focus);
3260 if (surface == NULL)
3261 return;
3262
3263 shsurf = get_shell_surface(surface);
3264 if (shsurf == NULL)
3265 return;
3266
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003267 fullscreen =
3268 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003270 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003271}
3272
3273static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003274touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003275{
Derek Foreman362656b2014-09-04 10:23:05 -05003276 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003277 struct weston_surface *surface;
3278 struct shell_surface *shsurf;
3279
Derek Foreman8ae2db52015-07-15 13:00:36 -05003280 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003281 return;
3282
Derek Foreman8ae2db52015-07-15 13:00:36 -05003283 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003284 surface = weston_surface_get_main_surface(focus);
3285 if (surface == NULL)
3286 return;
3287
3288 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003289 if (shsurf == NULL ||
3290 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3291 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003292 return;
3293
Derek Foremanb7674ae2015-07-15 13:00:37 -05003294 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003295}
3296
3297static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003298resize_binding(struct weston_pointer *pointer, uint32_t time,
3299 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003300{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003301 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003302 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003303 uint32_t edges = 0;
3304 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003305 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003306
Derek Foreman8ae2db52015-07-15 13:00:36 -05003307 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003308 return;
3309
Derek Foreman8ae2db52015-07-15 13:00:36 -05003310 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003311
Pekka Paalanen01388e22013-04-25 13:57:44 +03003312 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003313 if (surface == NULL)
3314 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003315
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003316 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003317 if (shsurf == NULL ||
3318 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3319 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003320 return;
3321
Jason Ekstranda7af7042013-10-12 22:38:11 -05003322 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003323 wl_fixed_to_int(pointer->grab_x),
3324 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003326
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003327 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003328 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003329 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003330 edges |= 0;
3331 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003332 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003333
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003334 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003335 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003336 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003337 edges |= 0;
3338 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003339 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003340
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003341 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003342}
3343
3344static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003345surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003346 struct weston_pointer_axis_event *event,
3347 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003348{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003349 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003350 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003351 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003352 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003353
Pekka Paalanen01388e22013-04-25 13:57:44 +03003354 /* XXX: broken for windows containing sub-surfaces */
3355 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003356 if (surface == NULL)
3357 return;
3358
3359 shsurf = get_shell_surface(surface);
3360 if (!shsurf)
3361 return;
3362
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003363 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003364
Jason Ekstranda7af7042013-10-12 22:38:11 -05003365 if (shsurf->view->alpha > 1.0)
3366 shsurf->view->alpha = 1.0;
3367 if (shsurf->view->alpha < step)
3368 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003369
Jason Ekstranda7af7042013-10-12 22:38:11 -05003370 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003371 weston_surface_damage(surface);
3372}
3373
3374static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003375do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003376 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003377{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003378 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003379 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003380 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003381 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003382
Derek Foreman1281a362015-07-31 16:55:32 -05003383 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003384 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3385 return;
3386 }
3387
Scott Moreauccbf29d2012-02-22 14:21:41 -07003388 wl_list_for_each(output, &compositor->output_list, link) {
3389 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003390 wl_fixed_to_double(pointer->x),
3391 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003392 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003393 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003394 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003395 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003396 increment = -output->zoom.increment;
3397 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003398 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003399 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003400 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003401 else
3402 increment = 0;
3403
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003404 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003405
Scott Moreaue6603982012-06-11 13:07:51 -06003406 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003407 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003408 else if (output->zoom.level > output->zoom.max_level)
3409 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003410
3411 if (!output->zoom.active) {
3412 if (output->zoom.level <= 0.0)
3413 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003414 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003415 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003416
Scott Moreaue6603982012-06-11 13:07:51 -06003417 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003418
Jason Ekstranda7af7042013-10-12 22:38:11 -05003419 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003420 }
3421 }
3422}
3423
Scott Moreauccbf29d2012-02-22 14:21:41 -07003424static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003425zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003426 struct weston_pointer_axis_event *event,
3427 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003428{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003429 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003430}
3431
3432static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003433zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3434 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003435{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003436 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003437}
3438
3439static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003440terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3441 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003443 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003444
Daniel Stone325fc2d2012-05-30 16:31:58 +01003445 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003446}
3447
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003448static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003449rotate_grab_motion(struct weston_pointer_grab *grab,
3450 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003451 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003452{
3453 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003454 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003455 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003456 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003457 struct weston_surface *surface =
3458 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003459 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003460
Jonas Ådahld2510102014-10-05 21:39:14 +02003461 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003462
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003463 if (!shsurf)
3464 return;
3465
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003466 cx = 0.5f * surface->width;
3467 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003468
Daniel Stone37816df2012-05-16 18:45:18 +01003469 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3470 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003471 r = sqrtf(dx * dx + dy * dy);
3472
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003473 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003474 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003475
3476 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003477 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003478 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003479
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003480 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003481 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003482
3483 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003484 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003485 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003486 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003487 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003488
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003489 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003490 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003491 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003492 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003493 wl_list_init(&shsurf->rotation.transform.link);
3494 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003495 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003496 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003497
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003498 /* We need to adjust the position of the surface
3499 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 cposx = shsurf->view->geometry.x + cx;
3501 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003502 dposx = rotate->center.x - cposx;
3503 dposy = rotate->center.y - cposy;
3504 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003505 weston_view_set_position(shsurf->view,
3506 shsurf->view->geometry.x + dposx,
3507 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003508 }
3509
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003510 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003511 * lazily applies the damage due to rotation update.
3512 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003513 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003514}
3515
3516static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003517rotate_grab_button(struct weston_pointer_grab *grab,
3518 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003519{
3520 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003521 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003522 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003523 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003524 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003525
Daniel Stone4dbadb12012-05-30 16:31:51 +01003526 if (pointer->button_count == 0 &&
3527 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003528 if (shsurf)
3529 weston_matrix_multiply(&shsurf->rotation.rotation,
3530 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003531 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003532 free(rotate);
3533 }
3534}
3535
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003536static void
3537rotate_grab_cancel(struct weston_pointer_grab *grab)
3538{
3539 struct rotate_grab *rotate =
3540 container_of(grab, struct rotate_grab, base.grab);
3541
3542 shell_grab_end(&rotate->base);
3543 free(rotate);
3544}
3545
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003546static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003547 noop_grab_focus,
3548 rotate_grab_motion,
3549 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003550 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003551 noop_grab_axis_source,
3552 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003553 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003554};
3555
3556static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003557surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003559 struct weston_surface *surface =
3560 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003561 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003562 float dx, dy;
3563 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003564
Pekka Paalanen460099f2012-01-20 16:48:25 +02003565 rotate = malloc(sizeof *rotate);
3566 if (!rotate)
3567 return;
3568
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003569 weston_view_to_global_float(shsurf->view,
3570 surface->width * 0.5f,
3571 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003573
Derek Foreman74de4692015-07-15 13:00:39 -05003574 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3575 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003576 r = sqrtf(dx * dx + dy * dy);
3577 if (r > 20.0f) {
3578 struct weston_matrix inverse;
3579
3580 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003581 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003582 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003583
3584 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003585 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003586 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003587 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003588 weston_matrix_init(&rotate->rotation);
3589 }
3590
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003591 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003592 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003593}
3594
3595static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003596rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003597 void *data)
3598{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003599 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003600 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003601 struct shell_surface *surface;
3602
Derek Foreman8ae2db52015-07-15 13:00:36 -05003603 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003604 return;
3605
Derek Foreman8ae2db52015-07-15 13:00:36 -05003606 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003607
Pekka Paalanen01388e22013-04-25 13:57:44 +03003608 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003609 if (base_surface == NULL)
3610 return;
3611
3612 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003613 if (surface == NULL ||
3614 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3615 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003616 return;
3617
Derek Foreman74de4692015-07-15 13:00:39 -05003618 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003619}
3620
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003621/* Move all fullscreen layers down to the current workspace and hide their
3622 * black views. The surfaces' state is set to both fullscreen and lowered,
3623 * and this is reversed when such a surface is re-configured, see
3624 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3625 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003626 * lowering_output = NULL - Lower on all outputs, else only lower on the
3627 * specified output.
3628 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003629 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003630 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003631void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003632lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003633 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003634{
3635 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003637
3638 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003640 &shell->fullscreen_layer.view_list.link,
3641 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003642 struct shell_surface *shsurf = get_shell_surface(view->surface);
3643
3644 if (!shsurf)
3645 continue;
3646
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003647 /* Only lower surfaces which have lowering_output as their fullscreen
3648 * output, unless a NULL output asks for lowering on all outputs.
3649 */
3650 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3651 continue;
3652
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003653 /* We can have a non-fullscreen popup for a fullscreen surface
3654 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003655 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003656 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003657 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3658 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003659 weston_view_damage_below(shsurf->fullscreen.black_view);
3660
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003661 }
3662
3663 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003664 weston_layer_entry_remove(&view->layer_link);
3665 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003666 weston_view_damage_below(view);
3667 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003668
3669 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003670 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003671}
3672
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003673void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003674activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003675 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003676{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003677 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003678 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003679 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003680 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003681 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003682 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003683
Pekka Paalanen01388e22013-04-25 13:57:44 +03003684 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003685 shsurf = get_shell_surface(main_surface);
3686 assert(shsurf);
3687
3688 /* Only demote fullscreen surfaces on the output of activated shsurf.
3689 * Leave fullscreen surfaces on unrelated outputs alone. */
3690 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003691
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003692 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003693
Jonas Ådahl8538b222012-08-29 22:13:03 +02003694 state = ensure_focus_state(shell, seat);
3695 if (state == NULL)
3696 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003697
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003698 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003699 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003700
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003701 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3702 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003703 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003704
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003705 /* Update the surface’s layer. This brings it to the top of the stacking
3706 * order as appropriate. */
3707 shell_surface_update_layer(shsurf);
3708
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003709 if (shell->focus_animation_type != ANIMATION_NONE) {
3710 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003711 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003712 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003713}
3714
Alex Wu21858432012-04-01 20:13:08 +08003715/* no-op func for checking black surface */
3716static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003717black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003718{
3719}
3720
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003721static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003722is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003723{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003724 struct weston_surface *surface = view->surface;
3725
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003726 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003727 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003728 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003729 return true;
3730 }
3731 return false;
3732}
3733
Kristian Høgsberg75840622011-09-06 13:48:16 -04003734static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003735activate_binding(struct weston_seat *seat,
3736 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003737 struct weston_view *focus_view,
3738 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003739{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003740 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003741 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003742
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003743 if (!focus_view)
3744 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003745
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003746 if (is_black_surface_view(focus_view, &main_view))
3747 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003748
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003749 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003750 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003751 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003752
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003753 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003754}
3755
3756static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003757click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3758 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003759{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003760 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003761 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003762 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003763 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003764
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003765 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003766 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003767 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003768}
3769
3770static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003771touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3772 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003773{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003774 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003775 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003776 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003777 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003778
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003779 activate_binding(touch->seat, data, touch->focus,
3780 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003781}
3782
3783static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003784unfocus_all_seats(struct desktop_shell *shell)
3785{
3786 struct weston_seat *seat, *next;
3787
3788 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003789 struct weston_keyboard *keyboard =
3790 weston_seat_get_keyboard(seat);
3791
3792 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003793 continue;
3794
Derek Foreman1281a362015-07-31 16:55:32 -05003795 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003796 }
3797}
3798
3799static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003800lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003801{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003802 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003803
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003804 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003805 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003806 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003807 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003808
3809 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003810
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003811 /* Hide all surfaces by removing the fullscreen, panel and
3812 * toplevel layers. This way nothing else can show or receive
3813 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003814
Quentin Glidic82681572016-12-17 13:40:51 +01003815 weston_layer_unset_position(&shell->panel_layer);
3816 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003817 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003818 weston_layer_unset_position(&shell->input_panel_layer);
3819 weston_layer_unset_position(&ws->layer);
3820
3821 weston_layer_set_position(&shell->lock_layer,
3822 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003823
Derek Foreman004b4a12015-07-20 16:28:13 -05003824 weston_compositor_sleep(shell->compositor);
3825
Neil Robertsb4a91702014-04-09 16:33:32 +01003826 /* Remove the keyboard focus on all seats. This will be
3827 * restored to the workspace's saved state via
3828 * restore_focus_state when the compositor is unlocked */
3829 unfocus_all_seats(shell);
3830
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003831 /* TODO: disable bindings that should not work while locked. */
3832
3833 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003834}
3835
3836static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003837unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003838{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003839 struct wl_resource *shell_resource;
3840
Pekka Paalanend81c2162011-11-16 13:47:34 +02003841 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003842 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003843 return;
3844 }
3845
3846 /* If desktop-shell client has gone away, unlock immediately. */
3847 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003848 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003849 return;
3850 }
3851
3852 if (shell->prepare_event_sent)
3853 return;
3854
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003855 shell_resource = shell->child.desktop_shell;
3856 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003857 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003858}
3859
3860static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003861shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003862{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003863 struct shell_output *shell_output = data;
3864 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003865
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003866 shell_output->fade.animation = NULL;
3867 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003868 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003869 weston_surface_destroy(shell_output->fade.view->surface);
3870 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003871 break;
3872 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003873 lock(shell);
3874 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003875 default:
3876 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003877 }
3878}
3879
Jason Ekstranda7af7042013-10-12 22:38:11 -05003880static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003881shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003882{
3883 struct weston_compositor *compositor = shell->compositor;
3884 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003885 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003886
3887 surface = weston_surface_create(compositor);
3888 if (!surface)
3889 return NULL;
3890
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 view = weston_view_create(surface);
3892 if (!view) {
3893 weston_surface_destroy(surface);
3894 return NULL;
3895 }
3896
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003897 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3898 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003899 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003900 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3901 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003902 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003903 surface->is_mapped = true;
3904 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003905
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003907}
3908
3909static void
3910shell_fade(struct desktop_shell *shell, enum fade_type type)
3911{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003912 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003913 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003914
3915 switch (type) {
3916 case FADE_IN:
3917 tint = 0.0;
3918 break;
3919 case FADE_OUT:
3920 tint = 1.0;
3921 break;
3922 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003923 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003924 return;
3925 }
3926
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003927 /* Create a separate fade surface for each output */
3928 wl_list_for_each(shell_output, &shell->output_list, link) {
3929 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003930
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003931 if (shell_output->fade.view == NULL) {
3932 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3933 if (!shell_output->fade.view)
3934 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003935
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003936 shell_output->fade.view->alpha = 1.0 - tint;
3937 weston_view_update_transform(shell_output->fade.view);
3938 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003939
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003940 if (shell_output->fade.view->output == NULL) {
3941 /* If the black view gets a NULL output, we lost the
3942 * last output and we'll just cancel the fade. This
3943 * happens when you close the last window under the
3944 * X11 or Wayland backends. */
3945 shell->locked = false;
3946 weston_surface_destroy(shell_output->fade.view->surface);
3947 shell_output->fade.view = NULL;
3948 } else if (shell_output->fade.animation) {
3949 weston_fade_update(shell_output->fade.animation, tint);
3950 } else {
3951 shell_output->fade.animation =
3952 weston_fade_run(shell_output->fade.view,
3953 1.0 - tint, tint, 300.0,
3954 shell_fade_done_for_output, shell_output);
3955 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003956 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003957}
3958
3959static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003960do_shell_fade_startup(void *data)
3961{
3962 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003963 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003964
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003965 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003966 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003967 } else {
3968 weston_log("desktop shell: "
3969 "unexpected fade-in animation type %d\n",
3970 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003971 wl_list_for_each(shell_output, &shell->output_list, link) {
3972 weston_surface_destroy(shell_output->fade.view->surface);
3973 shell_output->fade.view = NULL;
3974 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003975 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003976}
3977
3978static void
3979shell_fade_startup(struct desktop_shell *shell)
3980{
3981 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003982 struct shell_output *shell_output;
3983 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003984
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003985 wl_list_for_each(shell_output, &shell->output_list, link) {
3986 if (!shell_output->fade.startup_timer)
3987 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003988
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003989 wl_event_source_remove(shell_output->fade.startup_timer);
3990 shell_output->fade.startup_timer = NULL;
3991 has_fade = true;
3992 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003993
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003994 if (has_fade) {
3995 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3996 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3997 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003998}
3999
4000static int
4001fade_startup_timeout(void *data)
4002{
4003 struct desktop_shell *shell = data;
4004
4005 shell_fade_startup(shell);
4006 return 0;
4007}
4008
4009static void
4010shell_fade_init(struct desktop_shell *shell)
4011{
4012 /* Make compositor output all black, and wait for the desktop-shell
4013 * client to signal it is ready, then fade in. The timer triggers a
4014 * fade-in, in case the desktop-shell client takes too long.
4015 */
4016
4017 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004018 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004019
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004020 if (shell->startup_animation_type == ANIMATION_NONE)
4021 return;
4022
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004023 wl_list_for_each(shell_output, &shell->output_list, link) {
4024 if (shell_output->fade.view != NULL) {
4025 weston_log("%s: warning: fade surface already exists\n",
4026 __func__);
4027 continue;
4028 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004029
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004030 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4031 if (!shell_output->fade.view)
4032 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004033
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004034 weston_view_update_transform(shell_output->fade.view);
4035 weston_surface_damage(shell_output->fade.view->surface);
4036
4037 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4038 shell_output->fade.startup_timer =
4039 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4040 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4041 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004042}
4043
4044static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004045idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004046{
4047 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004048 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004049
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004050 struct weston_seat *seat;
4051
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004052 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4053 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004054
4055 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004056 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004057}
4058
4059static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004060wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004061{
4062 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004063 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004064
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004065 unlock(shell);
4066}
4067
4068static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004069transform_handler(struct wl_listener *listener, void *data)
4070{
4071 struct weston_surface *surface = data;
4072 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004073 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004074 int x, y;
4075
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004076 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004077 return;
4078
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004079 api = shsurf->shell->xwayland_surface_api;
4080 if (!api) {
4081 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4082 shsurf->shell->xwayland_surface_api = api;
4083 }
4084
4085 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004086 return;
4087
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004088 if (!weston_view_is_mapped(shsurf->view))
4089 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004090
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004091 x = shsurf->view->geometry.x;
4092 y = shsurf->view->geometry.y;
4093
4094 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004095}
4096
4097static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004098center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004099{
Giulio Camuffob8366642013-04-25 13:57:46 +03004100 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004101 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004102
Jason Ekstranda7af7042013-10-12 22:38:11 -05004103 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004104
4105 x = output->x + (output->width - width) / 2 - surf_x / 2;
4106 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004107
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004108 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004109}
4110
4111static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112weston_view_set_initial_position(struct weston_view *view,
4113 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004114{
4115 struct weston_compositor *compositor = shell->compositor;
4116 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004117 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004118 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004119 struct weston_output *output, *target_output = NULL;
4120 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004121 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004122
4123 /* As a heuristic place the new window on the same output as the
4124 * pointer. Falling back to the output containing 0, 0.
4125 *
4126 * TODO: Do something clever for touch too?
4127 */
4128 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004129 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4130
4131 if (pointer) {
4132 ix = wl_fixed_to_int(pointer->x);
4133 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004134 break;
4135 }
4136 }
4137
4138 wl_list_for_each(output, &compositor->output_list, link) {
4139 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4140 target_output = output;
4141 break;
4142 }
4143 }
4144
4145 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004146 weston_view_set_position(view, 10 + random() % 400,
4147 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004148 return;
4149 }
4150
4151 /* Valid range within output where the surface will still be onscreen.
4152 * If this is negative it means that the surface is bigger than
4153 * output.
4154 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004155 get_output_work_area(shell, target_output, &area);
4156
Derek Foremanf814c5d2015-04-15 12:23:54 -05004157 x = area.x;
4158 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004159 range_x = area.width - view->surface->width;
4160 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004161
Rob Bradford4cb88c72012-08-13 15:18:44 +01004162 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004163 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004164
4165 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004166 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004167
Jason Ekstranda7af7042013-10-12 22:38:11 -05004168 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004169}
4170
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004171static bool
4172check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4173{
4174 struct timespec now;
4175
4176 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4177 return false;
4178
4179 /*
4180 * If the shell helper client dies before the session has been
4181 * up for roughly 30 seconds, better just make Weston shut down,
4182 * because the user likely has no way to interact with the desktop
4183 * anyway.
4184 */
4185 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4186 weston_log("Error: %s apparently cannot run at all.\n",
4187 shell->client);
4188 weston_log_continue(STAMP_SPACE "Quitting...");
4189 wl_display_terminate(shell->compositor->wl_display);
4190
4191 return true;
4192 }
4193
4194 return false;
4195}
4196
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004197static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004198
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004199static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004200respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004201{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004202 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004203
4204 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4205 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004206 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004207 shell->child.deathstamp = time;
4208 shell->child.deathcount = 0;
4209 }
4210
4211 shell->child.deathcount++;
4212 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004213 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004214 return;
4215 }
4216
Pekka Paalanen826dc142014-08-27 12:11:53 +03004217 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004218 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004219}
4220
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004221static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004222desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4223{
4224 struct desktop_shell *shell;
4225
4226 shell = container_of(listener, struct desktop_shell,
4227 child.client_destroy_listener);
4228
Pekka Paalanen826dc142014-08-27 12:11:53 +03004229 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004230 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004231 /*
4232 * unbind_desktop_shell() will reset shell->child.desktop_shell
4233 * before the respawned process has a chance to create a new
4234 * desktop_shell object, because we are being called from the
4235 * wl_client destructor which destroys all wl_resources before
4236 * returning.
4237 */
4238
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004239 if (!check_desktop_shell_crash_too_early(shell))
4240 respawn_desktop_shell_process(shell);
4241
Pekka Paalanen826dc142014-08-27 12:11:53 +03004242 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004243}
4244
4245static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004246launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004247{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004248 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004249
Pekka Paalanen826dc142014-08-27 12:11:53 +03004250 shell->child.client = weston_client_start(shell->compositor,
4251 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004252
Arnaud Vrac42631192014-08-25 20:56:46 +02004253 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004254 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004255 return;
4256 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004257
4258 shell->child.client_destroy_listener.notify =
4259 desktop_shell_client_destroy;
4260 wl_client_add_destroy_listener(shell->child.client,
4261 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004262}
4263
4264static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004265unbind_desktop_shell(struct wl_resource *resource)
4266{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004267 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004268
4269 if (shell->locked)
4270 resume_desktop(shell);
4271
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004272 shell->child.desktop_shell = NULL;
4273 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004274}
4275
4276static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004277bind_desktop_shell(struct wl_client *client,
4278 void *data, uint32_t version, uint32_t id)
4279{
Tiago Vignattibe143262012-04-16 17:31:41 +03004280 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004281 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004282
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004283 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4284 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004285
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004286 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004287 wl_resource_set_implementation(resource,
4288 &desktop_shell_implementation,
4289 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004290 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004291 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004292 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004293
4294 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4295 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004296}
4297
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004299 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004300 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004301 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004302 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004303 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004304};
4305
4306static void
4307switcher_next(struct switcher *switcher)
4308{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004310 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004311 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004312 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004313
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004314 /* temporary re-display minimized surfaces */
4315 struct weston_view *tmp;
4316 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004317 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4318 weston_layer_entry_remove(&view->layer_link);
4319 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004320 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4321 *minimized = view;
4322 }
4323
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004324 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004325 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004326 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004327 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004328 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004329 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004330 next = view;
4331 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004332 view->alpha = 0.25;
4333 weston_view_geometry_dirty(view);
4334 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004335 }
Alex Wu1659daa2012-04-01 20:13:09 +08004336
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004337 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004338 view->alpha = 0.25;
4339 weston_view_geometry_dirty(view);
4340 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004341 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004342 }
4343
4344 if (next == NULL)
4345 next = first;
4346
Alex Wu07b26062012-03-12 16:06:01 +08004347 if (next == NULL)
4348 return;
4349
Kristian Høgsberg07045392012-02-19 18:52:44 -05004350 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004351 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004352
4353 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004354 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004355 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004356
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004357 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004358 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004359 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004360}
4361
4362static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004363switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004364{
4365 struct switcher *switcher =
4366 container_of(listener, struct switcher, listener);
4367
4368 switcher_next(switcher);
4369}
4370
4371static void
Daniel Stone351eb612012-05-31 15:27:47 -04004372switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004375 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004376 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004377
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004378 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004379 if (is_focus_view(view))
4380 continue;
4381
Jason Ekstranda7af7042013-10-12 22:38:11 -05004382 view->alpha = 1.0;
4383 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004384 }
4385
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004386 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004387 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004388 keyboard->seat,
4389 WESTON_ACTIVATE_FLAG_CONFIGURE);
4390 }
4391
Kristian Høgsberg07045392012-02-19 18:52:44 -05004392 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004393 weston_keyboard_end_grab(keyboard);
4394 if (keyboard->input_method_resource)
4395 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004396
4397 /* re-hide surfaces that were temporary shown during the switch */
4398 struct weston_view **minimized;
4399 wl_array_for_each(minimized, &switcher->minimized_array) {
4400 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004401 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004402 weston_layer_entry_remove(&(*minimized)->layer_link);
4403 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004404 weston_view_damage_below(*minimized);
4405 }
4406 }
4407 wl_array_release(&switcher->minimized_array);
4408
Kristian Høgsberg07045392012-02-19 18:52:44 -05004409 free(switcher);
4410}
4411
4412static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004413switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004414 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004415{
4416 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004417 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004418
Daniel Stonec9785ea2012-05-30 16:31:52 +01004419 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004420 switcher_next(switcher);
4421}
4422
4423static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004424switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004425 uint32_t mods_depressed, uint32_t mods_latched,
4426 uint32_t mods_locked, uint32_t group)
4427{
4428 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004429 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430
Daniel Stone351eb612012-05-31 15:27:47 -04004431 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4432 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004433}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004434
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004435static void
4436switcher_cancel(struct weston_keyboard_grab *grab)
4437{
4438 struct switcher *switcher = container_of(grab, struct switcher, grab);
4439
4440 switcher_destroy(switcher);
4441}
4442
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004443static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004444 switcher_key,
4445 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004446 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004447};
4448
4449static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004450switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4451 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004452{
Tiago Vignattibe143262012-04-16 17:31:41 +03004453 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004454 struct switcher *switcher;
4455
4456 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004457 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004458 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004459 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004460 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004461 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004462
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004463 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004464 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004465 weston_keyboard_start_grab(keyboard, &switcher->grab);
4466 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004467 switcher_next(switcher);
4468}
4469
Pekka Paalanen3c647232011-12-22 13:43:43 +02004470static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004471backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4472 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004473{
4474 struct weston_compositor *compositor = data;
4475 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004476 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004477
4478 /* TODO: we're limiting to simple use cases, where we assume just
4479 * control on the primary display. We'd have to extend later if we
4480 * ever get support for setting backlights on random desktop LCD
4481 * panels though */
4482 output = get_default_output(compositor);
4483 if (!output)
4484 return;
4485
4486 if (!output->set_backlight)
4487 return;
4488
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004489 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4490 backlight_new = output->backlight_current - 25;
4491 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4492 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004493
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004494 if (backlight_new < 5)
4495 backlight_new = 5;
4496 if (backlight_new > 255)
4497 backlight_new = 255;
4498
4499 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004500 output->set_backlight(output, output->backlight_current);
4501}
4502
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004503static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004504force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4505 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004506{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004507 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004508 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004509 struct desktop_shell *shell = data;
4510 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004511 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004512
Derek Foreman8ae2db52015-07-15 13:00:36 -05004513 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004514 if (!focus_surface)
4515 return;
4516
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004517 wl_signal_emit(&compositor->kill_signal, focus_surface);
4518
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004519 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004520 wl_client_get_credentials(client, &pid, NULL, NULL);
4521
4522 /* Skip clients that we launched ourselves (the credentials of
4523 * the socketpair is ours) */
4524 if (pid == getpid())
4525 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004526
Daniel Stone325fc2d2012-05-30 16:31:58 +01004527 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004528}
4529
4530static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004531workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004532 uint32_t key, void *data)
4533{
4534 struct desktop_shell *shell = data;
4535 unsigned int new_index = shell->workspaces.current;
4536
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004537 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004538 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004539 if (new_index != 0)
4540 new_index--;
4541
4542 change_workspace(shell, new_index);
4543}
4544
4545static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004546workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004547 uint32_t key, void *data)
4548{
4549 struct desktop_shell *shell = data;
4550 unsigned int new_index = shell->workspaces.current;
4551
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004552 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004553 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004554 if (new_index < shell->workspaces.num - 1)
4555 new_index++;
4556
4557 change_workspace(shell, new_index);
4558}
4559
4560static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004561workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004562 uint32_t key, void *data)
4563{
4564 struct desktop_shell *shell = data;
4565 unsigned int new_index;
4566
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004567 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004568 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004569 new_index = key - KEY_F1;
4570 if (new_index >= shell->workspaces.num)
4571 new_index = shell->workspaces.num - 1;
4572
4573 change_workspace(shell, new_index);
4574}
4575
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004576static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004577workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4578 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004579{
4580 struct desktop_shell *shell = data;
4581 unsigned int new_index = shell->workspaces.current;
4582
4583 if (shell->locked)
4584 return;
4585
4586 if (new_index != 0)
4587 new_index--;
4588
Derek Foreman8ae2db52015-07-15 13:00:36 -05004589 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004590}
4591
4592static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004593workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4594 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004595{
4596 struct desktop_shell *shell = data;
4597 unsigned int new_index = shell->workspaces.current;
4598
4599 if (shell->locked)
4600 return;
4601
4602 if (new_index < shell->workspaces.num - 1)
4603 new_index++;
4604
Derek Foreman8ae2db52015-07-15 13:00:36 -05004605 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004606}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004607
4608static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004609shell_reposition_view_on_output_destroy(struct weston_view *view)
4610{
4611 struct weston_output *output, *first_output;
4612 struct weston_compositor *ec = view->surface->compositor;
4613 struct shell_surface *shsurf;
4614 float x, y;
4615 int visible;
4616
4617 x = view->geometry.x;
4618 y = view->geometry.y;
4619
4620 /* At this point the destroyed output is not in the list anymore.
4621 * If the view is still visible somewhere, we leave where it is,
4622 * otherwise, move it to the first output. */
4623 visible = 0;
4624 wl_list_for_each(output, &ec->output_list, link) {
4625 if (pixman_region32_contains_point(&output->region,
4626 x, y, NULL)) {
4627 visible = 1;
4628 break;
4629 }
4630 }
4631
4632 if (!visible) {
4633 first_output = container_of(ec->output_list.next,
4634 struct weston_output, link);
4635
4636 x = first_output->x + first_output->width / 4;
4637 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004638
4639 weston_view_set_position(view, x, y);
4640 } else {
4641 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004642 }
4643
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004644
4645 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004646 if (!shsurf)
4647 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004648
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004649 shsurf->saved_position_valid = false;
4650 set_maximized(shsurf, false);
4651 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004652}
4653
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004654void
4655shell_for_each_layer(struct desktop_shell *shell,
4656 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004657{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004658 struct workspace **ws;
4659
4660 func(shell, &shell->fullscreen_layer, data);
4661 func(shell, &shell->panel_layer, data);
4662 func(shell, &shell->background_layer, data);
4663 func(shell, &shell->lock_layer, data);
4664 func(shell, &shell->input_panel_layer, data);
4665
4666 wl_array_for_each(ws, &shell->workspaces.array)
4667 func(shell, &(*ws)->layer, data);
4668}
4669
4670static void
4671shell_output_destroy_move_layer(struct desktop_shell *shell,
4672 struct weston_layer *layer,
4673 void *data)
4674{
4675 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004676 struct weston_view *view;
4677
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004678 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004679 if (view->output != output)
4680 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004681
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004682 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004683 }
4684}
4685
4686static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004687handle_output_destroy(struct wl_listener *listener, void *data)
4688{
4689 struct shell_output *output_listener =
4690 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004691 struct weston_output *output = output_listener->output;
4692 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004693
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004694 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004695
Quentin Glidic561201c2016-08-20 18:19:51 +02004696 wl_list_remove(&output_listener->panel_surface_listener.link);
4697 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004698 wl_list_remove(&output_listener->destroy_listener.link);
4699 wl_list_remove(&output_listener->link);
4700 free(output_listener);
4701}
4702
4703static void
David Fort50d962f2016-06-09 17:55:52 +02004704shell_resize_surface_to_output(struct desktop_shell *shell,
4705 struct weston_surface *surface,
4706 const struct weston_output *output)
4707{
4708 if (!surface)
4709 return;
4710
4711 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4712 surface->resource,
4713 output->width,
4714 output->height);
4715}
4716
4717
4718static void
4719handle_output_resized(struct wl_listener *listener, void *data)
4720{
4721 struct desktop_shell *shell =
4722 container_of(listener, struct desktop_shell, resized_listener);
4723 struct weston_output *output = (struct weston_output *)data;
4724 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4725
4726 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4727 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4728}
4729
4730static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004731create_shell_output(struct desktop_shell *shell,
4732 struct weston_output *output)
4733{
4734 struct shell_output *shell_output;
4735
4736 shell_output = zalloc(sizeof *shell_output);
4737 if (shell_output == NULL)
4738 return;
4739
4740 shell_output->output = output;
4741 shell_output->shell = shell;
4742 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004743 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004744 wl_signal_add(&output->destroy_signal,
4745 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004746 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004747}
4748
4749static void
4750handle_output_create(struct wl_listener *listener, void *data)
4751{
4752 struct desktop_shell *shell =
4753 container_of(listener, struct desktop_shell, output_create_listener);
4754 struct weston_output *output = (struct weston_output *)data;
4755
4756 create_shell_output(shell, output);
4757}
4758
4759static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004760handle_output_move_layer(struct desktop_shell *shell,
4761 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004762{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004763 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004764 struct weston_view *view;
4765 float x, y;
4766
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004767 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004768 if (view->output != output)
4769 continue;
4770
4771 x = view->geometry.x + output->move_x;
4772 y = view->geometry.y + output->move_y;
4773 weston_view_set_position(view, x, y);
4774 }
4775}
4776
4777static void
4778handle_output_move(struct wl_listener *listener, void *data)
4779{
4780 struct desktop_shell *shell;
4781
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004782 shell = container_of(listener, struct desktop_shell,
4783 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004784
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004785 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004786}
4787
4788static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004789setup_output_destroy_handler(struct weston_compositor *ec,
4790 struct desktop_shell *shell)
4791{
4792 struct weston_output *output;
4793
4794 wl_list_init(&shell->output_list);
4795 wl_list_for_each(output, &ec->output_list, link)
4796 create_shell_output(shell, output);
4797
4798 shell->output_create_listener.notify = handle_output_create;
4799 wl_signal_add(&ec->output_created_signal,
4800 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004801
4802 shell->output_move_listener.notify = handle_output_move;
4803 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004804}
4805
4806static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004807shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004808{
Tiago Vignattibe143262012-04-16 17:31:41 +03004809 struct desktop_shell *shell =
4810 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004811 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004812 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004813
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004814 /* Force state to unlocked so we don't try to fade */
4815 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004816
4817 if (shell->child.client) {
4818 /* disable respawn */
4819 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004820 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004821 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004822
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004823 wl_list_remove(&shell->idle_listener.link);
4824 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004825 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004826
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004827 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004828 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004829
Xiong Zhang6b481422013-10-23 13:58:32 +08004830 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4831 wl_list_remove(&shell_output->destroy_listener.link);
4832 wl_list_remove(&shell_output->link);
4833 free(shell_output);
4834 }
4835
4836 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004837 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004838 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004839
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004840 wl_array_for_each(ws, &shell->workspaces.array)
4841 workspace_destroy(*ws);
4842 wl_array_release(&shell->workspaces.array);
4843
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004844 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004845 free(shell);
4846}
4847
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004848static void
4849shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4850{
4851 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004852 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004853
Bob Ham744e6532016-01-12 10:21:48 +00004854 if (shell->allow_zap)
4855 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4856 MODIFIER_CTRL | MODIFIER_ALT,
4857 terminate_binding, ec);
4858
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004859 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004860 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4861 click_to_activate_binding,
4862 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004863 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4864 click_to_activate_binding,
4865 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004866 weston_compositor_add_touch_binding(ec, 0,
4867 touch_to_activate_binding,
4868 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004869 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4870 backlight_binding, ec);
4871 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4872 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004873
4874 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004875 if (shell->exposay_modifier)
4876 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4877 exposay_binding, shell);
4878
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004879 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004880 if (!mod)
4881 return;
4882
Ian Ray13404c42017-09-18 15:22:01 +03004883 /* This binding is not configurable, but is only enabled if there is a
4884 * valid binding modifier. */
4885 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4886 MODIFIER_SUPER | MODIFIER_ALT,
4887 surface_opacity_binding, NULL);
4888
Ian Rayab7c0b62017-09-18 15:22:00 +03004889 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4890 mod, zoom_axis_binding,
4891 NULL);
4892
Daniel Stone325fc2d2012-05-30 16:31:58 +01004893 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4894 zoom_key_binding, NULL);
4895 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4896 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004897 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4898 maximize_binding, NULL);
4899 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4900 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004901 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4902 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004903 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004904 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004905 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004906 weston_compositor_add_button_binding(ec, BTN_LEFT,
4907 mod | MODIFIER_SHIFT,
4908 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004909
4910 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004911 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004912 rotate_binding, NULL);
4913
Daniel Stone325fc2d2012-05-30 16:31:58 +01004914 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4915 shell);
4916 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4917 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004918 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4919 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004920 weston_compositor_add_key_binding(ec, KEY_K, mod,
4921 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004922 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4923 workspace_up_binding, shell);
4924 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4925 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004926 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4927 workspace_move_surface_up_binding,
4928 shell);
4929 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4930 workspace_move_surface_down_binding,
4931 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004932
4933 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4934 if (shell->workspaces.num > 1) {
4935 num_workspace_bindings = shell->workspaces.num;
4936 if (num_workspace_bindings > 6)
4937 num_workspace_bindings = 6;
4938 for (i = 0; i < num_workspace_bindings; i++)
4939 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4940 workspace_f_binding,
4941 shell);
4942 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004943
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004944 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004945}
4946
Jason Ekstrand024177c2014-04-21 19:42:58 -05004947static void
4948handle_seat_created(struct wl_listener *listener, void *data)
4949{
4950 struct weston_seat *seat = data;
4951
4952 create_shell_seat(seat);
4953}
4954
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004955WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004956wet_shell_init(struct weston_compositor *ec,
4957 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004958{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004959 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004960 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004961 struct workspace **pws;
4962 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004963 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004964
Peter Huttererf3d62272013-08-08 11:57:05 +10004965 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004966 if (shell == NULL)
4967 return -1;
4968
Kristian Høgsberg75840622011-09-06 13:48:16 -04004969 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004970
4971 shell->destroy_listener.notify = shell_destroy;
4972 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004973 shell->idle_listener.notify = idle_handler;
4974 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4975 shell->wake_listener.notify = wake_handler;
4976 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004977 shell->transform_listener.notify = transform_handler;
4978 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004979
Quentin Glidic82681572016-12-17 13:40:51 +01004980 weston_layer_init(&shell->fullscreen_layer, ec);
4981 weston_layer_init(&shell->panel_layer, ec);
4982 weston_layer_init(&shell->background_layer, ec);
4983 weston_layer_init(&shell->lock_layer, ec);
4984 weston_layer_init(&shell->input_panel_layer, ec);
4985
4986 weston_layer_set_position(&shell->fullscreen_layer,
4987 WESTON_LAYER_POSITION_FULLSCREEN);
4988 weston_layer_set_position(&shell->panel_layer,
4989 WESTON_LAYER_POSITION_UI);
4990 weston_layer_set_position(&shell->background_layer,
4991 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004992
4993 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004994 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004995
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004996 if (input_panel_setup(shell) < 0)
4997 return -1;
4998
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004999 shell->text_backend = text_backend_init(ec);
5000 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005001 return -1;
5002
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005003 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005004
Daniel Stonedf8133b2013-11-19 11:37:14 +01005005 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5006 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5007
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005008 for (i = 0; i < shell->workspaces.num; i++) {
5009 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5010 if (pws == NULL)
5011 return -1;
5012
Quentin Glidic82681572016-12-17 13:40:51 +01005013 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005014 if (*pws == NULL)
5015 return -1;
5016 }
5017 activate_workspace(shell, 0);
5018
Quentin Glidic82681572016-12-17 13:40:51 +01005019 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005020
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005021 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005022 wl_list_init(&shell->workspaces.animation.link);
5023 shell->workspaces.animation.frame = animate_workspace_change_frame;
5024
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005025 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5026 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005027 return -1;
5028
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005029 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005030 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005031 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005032 return -1;
5033
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005034 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005035
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005036 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005037
Xiong Zhang6b481422013-10-23 13:58:32 +08005038 setup_output_destroy_handler(ec, shell);
5039
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005040 loop = wl_display_get_event_loop(ec->wl_display);
5041 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005042
Jason Ekstrand024177c2014-04-21 19:42:58 -05005043 wl_list_for_each(seat, &ec->seat_list, link)
5044 handle_seat_created(NULL, seat);
5045 shell->seat_create_listener.notify = handle_seat_created;
5046 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005047
David Fort50d962f2016-06-09 17:55:52 +02005048 shell->resized_listener.notify = handle_output_resized;
5049 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5050
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005051 screenshooter_create(ec);
5052
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005053 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005054
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005055 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005056
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005057 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5058
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005059 return 0;
5060}