blob: 6b1876d2bb66647e170f4b668f3de3c826a17eb0 [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"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020044#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020054 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040056 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020057 struct weston_surface *keyboard_focus;
58 struct wl_list link;
59 struct wl_listener seat_destroy_listener;
60 struct wl_listener surface_destroy_listener;
61};
62
Philip Withnall648a4dd2013-11-25 18:01:44 +000063/*
64 * Surface stacking and ordering.
65 *
66 * This is handled using several linked lists of surfaces, organised into
67 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
68 * above all of the surfaces in the layer below. The set of layers is static and
69 * in the following order (top-most first):
70 * • Lock layer (only ever displayed on its own)
71 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010072 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Fullscreen layer
74 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000075 * • Workspace layers
76 * • Background layer
77 *
78 * The list of layers may be manipulated to remove whole layers of surfaces from
79 * display. For example, when locking the screen, all layers except the lock
80 * layer are removed.
81 *
82 * A surface’s layer is modified on configuring the surface, in
83 * set_surface_type() (which is only called when the surface’s type change is
84 * _committed_). If a surface’s type changes (e.g. when making a window
85 * fullscreen) its layer changes too.
86 *
87 * In order to allow popup and transient surfaces to be correctly stacked above
88 * their parent surfaces, each surface tracks both its parent surface, and a
89 * linked list of its children. When a surface’s layer is updated, so are the
90 * layers of its children. Note that child surfaces are *not* the same as
91 * subsurfaces — child/parent surfaces are purely for maintaining stacking
92 * order.
93 *
94 * The children_link list of siblings of a surface (i.e. those surfaces which
95 * have the same parent) only contains weston_surfaces which have a
96 * shell_surface. Stacking is not implemented for non-shell_surface
97 * weston_surfaces. This means that the following implication does *not* hold:
98 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
99 */
100
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200101struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500102 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200104 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600106 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700107
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200130
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700131 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200132 bool fullscreen;
133 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100134 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200135 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700136
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200137 struct {
138 bool is_set;
139 int32_t x;
140 int32_t y;
141 } xwayland;
142
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500143 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500144
145 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200146};
147
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300148struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400149 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152};
153
Rusty Lynch1084da52013-08-15 09:10:08 -0700154struct shell_touch_grab {
155 struct weston_touch_grab grab;
156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
158 struct weston_touch *touch;
159};
160
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300161struct weston_move_grab {
162 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100163 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500164 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165};
166
Rusty Lynch1084da52013-08-15 09:10:08 -0700167struct weston_touch_move_grab {
168 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800169 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700170 wl_fixed_t dx, dy;
171};
172
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300174 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500175 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200177 float x;
178 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200179 } center;
180};
181
Giulio Camuffo5085a752013-03-25 21:42:45 +0100182struct shell_seat {
183 struct weston_seat *seat;
184 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500185 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100186
Jason Ekstrand024177c2014-04-21 19:42:58 -0500187 struct wl_listener caps_changed_listener;
188 struct wl_listener pointer_focus_listener;
189 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100190};
191
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800192
Alex Wubd3354b2012-04-17 17:20:49 +0800193static struct desktop_shell *
194shell_surface_get_shell(struct shell_surface *shsurf);
195
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500196static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200197set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
198
199static void
Derek Foreman74de4692015-07-15 13:00:39 -0500200surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500201
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300202static void
203shell_fade_startup(struct desktop_shell *shell);
204
Philip Withnallbecb77e2013-11-25 18:01:30 +0000205static struct shell_seat *
206get_shell_seat(struct weston_seat *seat);
207
Jonny Lambd73c6942014-08-20 15:53:20 +0200208static void
209get_output_panel_size(struct desktop_shell *shell,
210 struct weston_output *output,
211 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700212
Philip Withnall648a4dd2013-11-25 18:01:44 +0000213static void
214shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
215
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216static int
217shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
218{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200220 struct weston_desktop_surface *desktop_surface =
221 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200223 t = weston_desktop_surface_get_title(desktop_surface);
224 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300225
226 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300228 t ? " '" : "", t ?: "", t ? "'" : "",
229 c ? " of " : "", c ?: "");
230}
231
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500232static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400233destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234{
235 struct shell_grab *grab;
236
237 grab = container_of(listener, struct shell_grab,
238 shsurf_destroy_listener);
239
240 grab->shsurf = NULL;
241}
242
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800243struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500244get_default_view(struct weston_surface *surface)
245{
246 struct shell_surface *shsurf;
247 struct weston_view *view;
248
249 if (!surface || wl_list_empty(&surface->views))
250 return NULL;
251
252 shsurf = get_shell_surface(surface);
253 if (shsurf)
254 return shsurf->view;
255
256 wl_list_for_each(view, &surface->views, surface_link)
257 if (weston_view_is_mapped(view))
258 return view;
259
260 return container_of(surface->views.next, struct weston_view, surface_link);
261}
262
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400265 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300266 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400267 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800268 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct desktop_shell *shell = shsurf->shell;
271
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200272 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400273
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274 grab->grab.interface = interface;
275 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500277 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400278 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700280 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400281 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400282 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800283 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400284 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 weston_pointer_set_focus(pointer,
286 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400287 wl_fixed_from_int(0),
288 wl_fixed_from_int(0));
289 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300290}
291
Jonny Lambd73c6942014-08-20 15:53:20 +0200292static void
Quentin Glidic581df062016-06-23 18:55:19 +0200293get_panel_size(struct desktop_shell *shell,
294 struct weston_view *view,
295 int *width,
296 int *height)
297{
298 float x1, y1;
299 float x2, y2;
300 weston_view_to_global_float(view, 0, 0, &x1, &y1);
301 weston_view_to_global_float(view,
302 view->surface->width,
303 view->surface->height,
304 &x2, &y2);
305 *width = (int)(x2 - x1);
306 *height = (int)(y2 - y1);
307}
308
309static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200310get_output_panel_size(struct desktop_shell *shell,
311 struct weston_output *output,
312 int *width,
313 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700314{
315 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200316
317 *width = 0;
318 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700319
320 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200321 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700322
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300323 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200324 if (view->surface->output == output) {
325 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200326 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700327 }
328 }
329
Jonny Lambd73c6942014-08-20 15:53:20 +0200330 /* the correct view wasn't found */
331}
332
333static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200334get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200335 struct weston_output *output,
336 pixman_rectangle32_t *area)
337{
338 int32_t panel_width = 0, panel_height = 0;
339
Derek Foremanf814c5d2015-04-15 12:23:54 -0500340 area->x = output->x;
341 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200342
343 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200344 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800345 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200346 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500347 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000348 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800349 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200350 area->width = output->width;
351 area->height = output->height - panel_height;
352 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800353 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500354 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000355 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200357 area->width = output->width - panel_width;
358 area->height = output->height;
359 break;
360 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700361}
362
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400363static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400367 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400369
370 if (grab->shsurf->resize_edges) {
371 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700373 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700375 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300376}
377
378static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700379shell_touch_grab_start(struct shell_touch_grab *grab,
380 const struct weston_touch_grab_interface *interface,
381 struct shell_surface *shsurf,
382 struct weston_touch *touch)
383{
384 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800385
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200386 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700387
Rusty Lynch1084da52013-08-15 09:10:08 -0700388 grab->grab.interface = interface;
389 grab->shsurf = shsurf;
390 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
391 wl_signal_add(&shsurf->destroy_signal,
392 &grab->shsurf_destroy_listener);
393
394 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700396
397 weston_touch_start_grab(touch, &grab->grab);
398 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500399 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700401}
402
403static void
404shell_touch_grab_end(struct shell_touch_grab *grab)
405{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700406 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700407 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 grab->shsurf->grabbed = 0;
409 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700410
411 weston_touch_end_grab(grab->touch);
412}
413
414static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800416 struct weston_output *output);
417
Daniel Stone496ca172012-05-30 16:31:42 +0100418static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300419get_modifier(char *modifier)
420{
421 if (!modifier)
422 return MODIFIER_SUPER;
423
424 if (!strcmp("ctrl", modifier))
425 return MODIFIER_CTRL;
426 else if (!strcmp("alt", modifier))
427 return MODIFIER_ALT;
428 else if (!strcmp("super", modifier))
429 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000430 else if (!strcmp("none", modifier))
431 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300432 else
433 return MODIFIER_SUPER;
434}
435
Juan Zhaoe10d2792012-04-25 19:09:52 +0800436static enum animation_type
437get_animation_type(char *animation)
438{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800439 if (!animation)
440 return ANIMATION_NONE;
441
Juan Zhaoe10d2792012-04-25 19:09:52 +0800442 if (!strcmp("zoom", animation))
443 return ANIMATION_ZOOM;
444 else if (!strcmp("fade", animation))
445 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100446 else if (!strcmp("dim-layer", animation))
447 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800448 else
449 return ANIMATION_NONE;
450}
451
Alex Wu4539b082012-03-01 12:57:46 +0800452static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400453shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200454{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400455 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500456 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300457 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000458 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200459
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300460 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300462 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
463 WESTON_SHELL_CLIENT);
464 if (ret < 0)
465 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500467 "client", &s, client);
468 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100469 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000470
471 weston_config_section_get_bool(section,
472 "allow-zap", &allow_zap, true);
473 shell->allow_zap = allow_zap;
474
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100475 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400476 "binding-modifier", &s, "super");
477 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200478 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800479
480 weston_config_section_get_string(section,
481 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000482 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800483 free(s);
484
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400485 weston_config_section_get_string(section, "animation", &s, "none");
486 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200487 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200488 weston_config_section_get_string(section, "close-animation", &s, "fade");
489 shell->win_close_animation_type = get_animation_type(s);
490 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700491 weston_config_section_get_string(section,
492 "startup-animation", &s, "fade");
493 shell->startup_animation_type = get_animation_type(s);
494 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700495 if (shell->startup_animation_type == ANIMATION_ZOOM)
496 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100497 weston_config_section_get_string(section, "focus-animation", &s, "none");
498 shell->focus_animation_type = get_animation_type(s);
499 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400500 weston_config_section_get_uint(section, "num-workspaces",
501 &shell->workspaces.num,
502 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200503}
504
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800505struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100506get_default_output(struct weston_compositor *compositor)
507{
Armin Krezović10b06182016-06-23 11:59:30 +0200508 if (wl_list_empty(&compositor->output_list))
509 return NULL;
510
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511 return container_of(compositor->output_list.next,
512 struct weston_output, link);
513}
514
Pekka Paalanen8274d902014-08-06 19:36:51 +0300515static int
516focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
517{
518 return snprintf(buf, len, "focus highlight effect for output %s",
519 surface->output->name);
520}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100521
522/* no-op func for checking focus surface */
523static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200524focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525{
526}
527
528static struct focus_surface *
529get_focus_surface(struct weston_surface *surface)
530{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200531 if (surface->committed == focus_surface_committed)
532 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533 else
534 return NULL;
535}
536
537static bool
538is_focus_surface (struct weston_surface *es)
539{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200540 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100541}
542
543static bool
544is_focus_view (struct weston_view *view)
545{
546 return is_focus_surface (view->surface);
547}
548
549static struct focus_surface *
550create_focus_surface(struct weston_compositor *ec,
551 struct weston_output *output)
552{
553 struct focus_surface *fsurf = NULL;
554 struct weston_surface *surface = NULL;
555
556 fsurf = malloc(sizeof *fsurf);
557 if (!fsurf)
558 return NULL;
559
560 fsurf->surface = weston_surface_create(ec);
561 surface = fsurf->surface;
562 if (surface == NULL) {
563 free(fsurf);
564 return NULL;
565 }
566
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200567 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100568 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200569 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300571 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800573 fsurf->view = weston_view_create(surface);
574 if (fsurf->view == NULL) {
575 weston_surface_destroy(surface);
576 free(fsurf);
577 return NULL;
578 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100579 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200580 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100581
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600582 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600583 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
585 pixman_region32_fini(&surface->opaque);
586 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
587 output->width, output->height);
588 pixman_region32_fini(&surface->input);
589 pixman_region32_init(&surface->input);
590
591 wl_list_init(&fsurf->workspace_transform.link);
592
593 return fsurf;
594}
595
596static void
597focus_surface_destroy(struct focus_surface *fsurf)
598{
599 weston_surface_destroy(fsurf->surface);
600 free(fsurf);
601}
602
603static void
604focus_animation_done(struct weston_view_animation *animation, void *data)
605{
606 struct workspace *ws = data;
607
608 ws->focus_animation = NULL;
609}
610
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200611static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200612focus_state_destroy(struct focus_state *state)
613{
614 wl_list_remove(&state->seat_destroy_listener.link);
615 wl_list_remove(&state->surface_destroy_listener.link);
616 free(state);
617}
618
619static void
620focus_state_seat_destroy(struct wl_listener *listener, void *data)
621{
622 struct focus_state *state = container_of(listener,
623 struct focus_state,
624 seat_destroy_listener);
625
626 wl_list_remove(&state->link);
627 focus_state_destroy(state);
628}
629
630static void
631focus_state_surface_destroy(struct wl_listener *listener, void *data)
632{
633 struct focus_state *state = container_of(listener,
634 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400635 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200636 struct weston_surface *main_surface;
637 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500638 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200639
Pekka Paalanen01388e22013-04-25 13:57:44 +0300640 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
641
Kristian Høgsberge3778222012-07-31 17:29:30 -0400642 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300643 wl_list_for_each(view,
644 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500645 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400646 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 if (is_focus_view(view))
648 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200649 if (!get_shell_surface(view->surface))
650 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400651
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200652 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400653 break;
654 }
655
Pekka Paalanen01388e22013-04-25 13:57:44 +0300656 /* if the focus was a sub-surface, activate its main surface */
657 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200658 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300659
Kristian Høgsberge3778222012-07-31 17:29:30 -0400660 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100661 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200662 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200663 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400664 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200665 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100666 if (state->ws->focus_animation)
667 weston_view_animation_destroy(state->ws->focus_animation);
668
669 state->ws->focus_animation = weston_fade_run(
670 state->ws->fsurf_front->view,
671 state->ws->fsurf_front->view->alpha, 0.0, 300,
672 focus_animation_done, state->ws);
673 }
674
Kristian Høgsberge3778222012-07-31 17:29:30 -0400675 wl_list_remove(&state->link);
676 focus_state_destroy(state);
677 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200678}
679
680static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200681focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
682 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200683{
Jonas Ådahl04769742012-06-13 00:01:24 +0200684 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200685
686 state = malloc(sizeof *state);
687 if (state == NULL)
688 return NULL;
689
Quentin Glidicfff39812016-08-15 10:56:52 +0200690 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100691 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400692 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200693 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400694 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200695
696 state->seat_destroy_listener.notify = focus_state_seat_destroy;
697 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400698 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200699 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400700 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200701
702 return state;
703}
704
Jonas Ådahl8538b222012-08-29 22:13:03 +0200705static struct focus_state *
706ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
707{
708 struct workspace *ws = get_current_workspace(shell);
709 struct focus_state *state;
710
711 wl_list_for_each(state, &ws->focus_list, link)
712 if (state->seat == seat)
713 break;
714
715 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200716 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200717
718 return state;
719}
720
Jonas Ådahl04769742012-06-13 00:01:24 +0200721static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800722focus_state_set_focus(struct focus_state *state,
723 struct weston_surface *surface)
724{
725 if (state->keyboard_focus) {
726 wl_list_remove(&state->surface_destroy_listener.link);
727 wl_list_init(&state->surface_destroy_listener.link);
728 }
729
730 state->keyboard_focus = surface;
731 if (surface)
732 wl_signal_add(&surface->destroy_signal,
733 &state->surface_destroy_listener);
734}
735
736static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400737restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200738{
739 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400740 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100741 struct wl_list pending_seat_list;
742 struct weston_seat *seat, *next_seat;
743
744 /* Temporarily steal the list of seats so that we can keep
745 * track of the seats we've already processed */
746 wl_list_init(&pending_seat_list);
747 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
748 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200749
750 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500751 struct weston_keyboard *keyboard =
752 weston_seat_get_keyboard(state->seat);
753
Neil Roberts4237f502014-04-09 16:33:31 +0100754 wl_list_remove(&state->seat->link);
755 wl_list_insert(&shell->compositor->seat_list,
756 &state->seat->link);
757
Derek Foreman1281a362015-07-31 16:55:32 -0500758 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800759 continue;
760
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400761 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200762
Derek Foreman1281a362015-07-31 16:55:32 -0500763 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764 }
Neil Roberts4237f502014-04-09 16:33:31 +0100765
766 /* For any remaining seats that we don't have a focus state
767 * for we'll reset the keyboard focus to NULL */
768 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500769 struct weston_keyboard *keyboard =
770 weston_seat_get_keyboard(seat);
771
Neil Roberts4237f502014-04-09 16:33:31 +0100772 wl_list_insert(&shell->compositor->seat_list, &seat->link);
773
Derek Foreman1281a362015-07-31 16:55:32 -0500774 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100775 continue;
776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100778 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200779}
780
781static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200782replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
783 struct weston_seat *seat)
784{
Derek Foreman1281a362015-07-31 16:55:32 -0500785 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787
788 wl_list_for_each(state, &ws->focus_list, link) {
789 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500790 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791 return;
792 }
793 }
794}
795
796static void
797drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
798 struct weston_surface *surface)
799{
800 struct focus_state *state;
801
802 wl_list_for_each(state, &ws->focus_list, link)
803 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800804 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805}
806
807static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100808animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
809 struct weston_view *from, struct weston_view *to)
810{
811 struct weston_output *output;
812 bool focus_surface_created = false;
813
814 /* FIXME: Only support dim animation using two layers */
815 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
816 return;
817
818 output = get_default_output(shell->compositor);
819 if (ws->fsurf_front == NULL && (from || to)) {
820 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800821 if (ws->fsurf_front == NULL)
822 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100823 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800824
825 ws->fsurf_back = create_focus_surface(shell->compositor, output);
826 if (ws->fsurf_back == NULL) {
827 focus_surface_destroy(ws->fsurf_front);
828 return;
829 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800831
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 focus_surface_created = true;
833 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300834 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
835 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 }
837
838 if (ws->focus_animation) {
839 weston_view_animation_destroy(ws->focus_animation);
840 ws->focus_animation = NULL;
841 }
842
843 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300844 weston_layer_entry_insert(&to->layer_link,
845 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300847 weston_layer_entry_insert(&ws->layer.view_list,
848 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849
850 if (focus_surface_created) {
851 ws->focus_animation = weston_fade_run(
852 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200853 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854 focus_animation_done, ws);
855 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300856 weston_layer_entry_insert(&from->layer_link,
857 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 ws->focus_animation = weston_stable_fade_run(
859 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200860 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 focus_animation_done, ws);
862 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300863 weston_layer_entry_insert(&ws->layer.view_list,
864 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 ws->focus_animation = weston_stable_fade_run(
866 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200867 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 focus_animation_done, ws);
869 }
870}
871
872static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200873workspace_destroy(struct workspace *ws)
874{
Jonas Ådahl04769742012-06-13 00:01:24 +0200875 struct focus_state *state, *next;
876
877 wl_list_for_each_safe(state, next, &ws->focus_list, link)
878 focus_state_destroy(state);
879
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100880 if (ws->fsurf_front)
881 focus_surface_destroy(ws->fsurf_front);
882 if (ws->fsurf_back)
883 focus_surface_destroy(ws->fsurf_back);
884
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200885 free(ws);
886}
887
Jonas Ådahl04769742012-06-13 00:01:24 +0200888static void
889seat_destroyed(struct wl_listener *listener, void *data)
890{
891 struct weston_seat *seat = data;
892 struct focus_state *state, *next;
893 struct workspace *ws = container_of(listener,
894 struct workspace,
895 seat_destroyed_listener);
896
897 wl_list_for_each_safe(state, next, &ws->focus_list, link)
898 if (state->seat == seat)
899 wl_list_remove(&state->link);
900}
901
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200902static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100903workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200904{
905 struct workspace *ws = malloc(sizeof *ws);
906 if (ws == NULL)
907 return NULL;
908
Quentin Glidic82681572016-12-17 13:40:51 +0100909 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910
Jonas Ådahl04769742012-06-13 00:01:24 +0200911 wl_list_init(&ws->focus_list);
912 wl_list_init(&ws->seat_destroyed_listener.link);
913 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100914 ws->fsurf_front = NULL;
915 ws->fsurf_back = NULL;
916 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200917
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918 return ws;
919}
920
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921static int
922workspace_is_empty(struct workspace *ws)
923{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300924 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925}
926
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200927static struct workspace *
928get_workspace(struct desktop_shell *shell, unsigned int index)
929{
930 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200931 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932 pws += index;
933 return *pws;
934}
935
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800936struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937get_current_workspace(struct desktop_shell *shell)
938{
939 return get_workspace(shell, shell->workspaces.current);
940}
941
942static void
943activate_workspace(struct desktop_shell *shell, unsigned int index)
944{
945 struct workspace *ws;
946
947 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100948 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200949
950 shell->workspaces.current = index;
951}
952
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953static unsigned int
954get_output_height(struct weston_output *output)
955{
956 return abs(output->region.extents.y1 - output->region.extents.y2);
957}
958
959static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200961{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 struct weston_transform *transform;
963
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 if (is_focus_view(view)) {
965 struct focus_surface *fsurf = get_focus_surface(view->surface);
966 transform = &fsurf->workspace_transform;
967 } else {
968 struct shell_surface *shsurf = get_shell_surface(view->surface);
969 transform = &shsurf->workspace_transform;
970 }
971
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 weston_matrix_init(&transform->matrix);
977 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980}
981
982static void
983workspace_translate_out(struct workspace *ws, double fraction)
984{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 unsigned int height;
987 double d;
988
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 d = height * fraction;
992
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 }
995}
996
997static void
998workspace_translate_in(struct workspace *ws, double fraction)
999{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001 unsigned int height;
1002 double d;
1003
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001004 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006
1007 if (fraction > 0)
1008 d = -(height - height * fraction);
1009 else
1010 d = height + height * fraction;
1011
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 }
1014}
1015
1016static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017reverse_workspace_change_animation(struct desktop_shell *shell,
1018 unsigned int index,
1019 struct workspace *from,
1020 struct workspace *to)
1021{
1022 shell->workspaces.current = index;
1023
1024 shell->workspaces.anim_to = to;
1025 shell->workspaces.anim_from = from;
1026 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1027 shell->workspaces.anim_timestamp = 0;
1028
Quentin Glidic82681572016-12-17 13:40:51 +01001029 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1030 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033}
1034
1035static void
1036workspace_deactivate_transforms(struct workspace *ws)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001041 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 if (is_focus_view(view)) {
1043 struct focus_surface *fsurf = get_focus_surface(view->surface);
1044 transform = &fsurf->workspace_transform;
1045 } else {
1046 struct shell_surface *shsurf = get_shell_surface(view->surface);
1047 transform = &shsurf->workspace_transform;
1048 }
1049
1050 if (!wl_list_empty(&transform->link)) {
1051 wl_list_remove(&transform->link);
1052 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001053 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 }
1056}
1057
1058static void
1059finish_workspace_change_animation(struct desktop_shell *shell,
1060 struct workspace *from,
1061 struct workspace *to)
1062{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001063 struct weston_view *view;
1064
Scott Moreau4272e632012-08-13 09:58:41 -06001065 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001067 /* Views that extend past the bottom of the output are still
1068 * visible after the workspace animation ends but before its layer
1069 * is hidden. In that case, we need to damage below those views so
1070 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001071 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001072 weston_view_damage_below(view);
1073
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074 wl_list_remove(&shell->workspaces.animation.link);
1075 workspace_deactivate_transforms(from);
1076 workspace_deactivate_transforms(to);
1077 shell->workspaces.anim_to = NULL;
1078
Quentin Glidic82681572016-12-17 13:40:51 +01001079 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080}
1081
1082static void
1083animate_workspace_change_frame(struct weston_animation *animation,
1084 struct weston_output *output, uint32_t msecs)
1085{
1086 struct desktop_shell *shell =
1087 container_of(animation, struct desktop_shell,
1088 workspaces.animation);
1089 struct workspace *from = shell->workspaces.anim_from;
1090 struct workspace *to = shell->workspaces.anim_to;
1091 uint32_t t;
1092 double x, y;
1093
1094 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1095 finish_workspace_change_animation(shell, from, to);
1096 return;
1097 }
1098
1099 if (shell->workspaces.anim_timestamp == 0) {
1100 if (shell->workspaces.anim_current == 0.0)
1101 shell->workspaces.anim_timestamp = msecs;
1102 else
1103 shell->workspaces.anim_timestamp =
1104 msecs -
1105 /* Invers of movement function 'y' below. */
1106 (asin(1.0 - shell->workspaces.anim_current) *
1107 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1108 M_2_PI);
1109 }
1110
1111 t = msecs - shell->workspaces.anim_timestamp;
1112
1113 /*
1114 * x = [0, π/2]
1115 * y(x) = sin(x)
1116 */
1117 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1118 y = sin(x);
1119
1120 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001121 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122
1123 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1124 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1125 shell->workspaces.anim_current = y;
1126
Scott Moreau4272e632012-08-13 09:58:41 -06001127 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001129 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131}
1132
1133static void
1134animate_workspace_change(struct desktop_shell *shell,
1135 unsigned int index,
1136 struct workspace *from,
1137 struct workspace *to)
1138{
1139 struct weston_output *output;
1140
1141 int dir;
1142
1143 if (index > shell->workspaces.current)
1144 dir = -1;
1145 else
1146 dir = 1;
1147
1148 shell->workspaces.current = index;
1149
1150 shell->workspaces.anim_dir = dir;
1151 shell->workspaces.anim_from = from;
1152 shell->workspaces.anim_to = to;
1153 shell->workspaces.anim_current = 0.0;
1154 shell->workspaces.anim_timestamp = 0;
1155
1156 output = container_of(shell->compositor->output_list.next,
1157 struct weston_output, link);
1158 wl_list_insert(&output->animation_list,
1159 &shell->workspaces.animation.link);
1160
Quentin Glidic82681572016-12-17 13:40:51 +01001161 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1162 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163
1164 workspace_translate_in(to, 0);
1165
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001166 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001167
Scott Moreau4272e632012-08-13 09:58:41 -06001168 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001169}
1170
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001171static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001172update_workspace(struct desktop_shell *shell, unsigned int index,
1173 struct workspace *from, struct workspace *to)
1174{
1175 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001176 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1177 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001178}
1179
1180static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001181change_workspace(struct desktop_shell *shell, unsigned int index)
1182{
1183 struct workspace *from;
1184 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001185 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186
1187 if (index == shell->workspaces.current)
1188 return;
1189
1190 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001191 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192 return;
1193
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001194 from = get_current_workspace(shell);
1195 to = get_workspace(shell, index);
1196
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001197 if (shell->workspaces.anim_from == to &&
1198 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001199 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200 reverse_workspace_change_animation(shell, index, from, to);
1201 return;
1202 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001203
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001204 if (shell->workspaces.anim_to != NULL)
1205 finish_workspace_change_animation(shell,
1206 shell->workspaces.anim_from,
1207 shell->workspaces.anim_to);
1208
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001209 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001210
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001211 if (shell->focus_animation_type != ANIMATION_NONE) {
1212 wl_list_for_each(state, &from->focus_list, link)
1213 if (state->keyboard_focus)
1214 animate_focus_change(shell, from,
1215 get_default_view(state->keyboard_focus), NULL);
1216
1217 wl_list_for_each(state, &to->focus_list, link)
1218 if (state->keyboard_focus)
1219 animate_focus_change(shell, to,
1220 NULL, get_default_view(state->keyboard_focus));
1221 }
1222
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223 if (workspace_is_empty(to) && workspace_is_empty(from))
1224 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001225 else
1226 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001227}
1228
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229static bool
1230workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1231{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001232 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001233 struct wl_list *e;
1234
1235 if (wl_list_empty(list))
1236 return false;
1237
1238 e = list->next;
1239
1240 if (e->next != list)
1241 return false;
1242
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001243 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244}
1245
1246static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001247surface_keyboard_focus_lost(struct weston_surface *surface)
1248{
1249 struct weston_compositor *compositor = surface->compositor;
1250 struct weston_seat *seat;
1251 struct weston_surface *focus;
1252
1253 wl_list_for_each(seat, &compositor->seat_list, link) {
1254 struct weston_keyboard *keyboard =
1255 weston_seat_get_keyboard(seat);
1256
1257 if (!keyboard)
1258 continue;
1259
1260 focus = weston_surface_get_main_surface(keyboard->focus);
1261 if (focus == surface)
1262 weston_keyboard_set_focus(keyboard, NULL);
1263 }
1264}
1265
1266static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001268 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001269 unsigned int index)
1270{
Derek Foreman1281a362015-07-31 16:55:32 -05001271 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001272 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 struct shell_surface *shsurf;
1275 struct workspace *from;
1276 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001277 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278
Derek Foreman1281a362015-07-31 16:55:32 -05001279 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 view = get_default_view(surface);
1281 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001282 index == shell->workspaces.current ||
1283 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284 return;
1285
1286 from = get_current_workspace(shell);
1287 to = get_workspace(shell, index);
1288
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001289 weston_layer_entry_remove(&view->layer_link);
1290 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001291
Philip Withnall659163d2013-11-25 18:01:36 +00001292 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001293 if (shsurf != NULL)
1294 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001295
Jonas Ådahle9d22502012-08-29 22:13:01 +02001296 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297 drop_focus_state(shell, from, surface);
1298
1299 if (shell->workspaces.anim_from == to &&
1300 shell->workspaces.anim_to == from) {
1301 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 return;
1304 }
1305
1306 if (shell->workspaces.anim_to != NULL)
1307 finish_workspace_change_animation(shell,
1308 shell->workspaces.anim_from,
1309 shell->workspaces.anim_to);
1310
1311 if (workspace_is_empty(from) &&
1312 workspace_has_only(to, surface))
1313 update_workspace(shell, index, from, to);
1314 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001315 if (shsurf != NULL &&
1316 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001317 wl_list_insert(&shell->workspaces.anim_sticky_list,
1318 &shsurf->workspace_transform.link);
1319
1320 animate_workspace_change(shell, index, from, to);
1321 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001322
Jonas Ådahl8538b222012-08-29 22:13:03 +02001323 state = ensure_focus_state(shell, seat);
1324 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001325 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001326}
1327
1328static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001329touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001330 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001331{
1332}
1333
1334static void
1335touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1336{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001337 struct weston_touch_move_grab *move =
1338 (struct weston_touch_move_grab *) container_of(
1339 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001340
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001341 if (touch_id == 0)
1342 move->active = 0;
1343
Jonas Ådahl9484b692013-12-02 22:05:03 +01001344 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001345 shell_touch_grab_end(&move->base);
1346 free(move);
1347 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001348}
1349
1350static void
1351touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001352 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
1354 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1355 struct shell_surface *shsurf = move->base.shsurf;
1356 struct weston_surface *es;
1357 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1358 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1359
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001360 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001361 return;
1362
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001363 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001364
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001365 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001366
1367 weston_compositor_schedule_repaint(es->compositor);
1368}
1369
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001370static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001371touch_move_grab_frame(struct weston_touch_grab *grab)
1372{
1373}
1374
1375static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001376touch_move_grab_cancel(struct weston_touch_grab *grab)
1377{
1378 struct weston_touch_move_grab *move =
1379 (struct weston_touch_move_grab *) container_of(
1380 grab, struct shell_touch_grab, grab);
1381
1382 shell_touch_grab_end(&move->base);
1383 free(move);
1384}
1385
Rusty Lynch1084da52013-08-15 09:10:08 -07001386static const struct weston_touch_grab_interface touch_move_grab_interface = {
1387 touch_move_grab_down,
1388 touch_move_grab_up,
1389 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001390 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001391 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001392};
1393
1394static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001395surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001396{
1397 struct weston_touch_move_grab *move;
1398
1399 if (!shsurf)
1400 return -1;
1401
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001402 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1403 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001404 return 0;
1405
1406 move = malloc(sizeof *move);
1407 if (!move)
1408 return -1;
1409
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001410 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001412 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001414 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001415
1416 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001417 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001418
1419 return 0;
1420}
1421
1422static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001423noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001424{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425}
1426
1427static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001428noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001429 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001430{
1431}
1432
1433static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001434noop_grab_axis_source(struct weston_pointer_grab *grab,
1435 uint32_t source)
1436{
1437}
1438
1439static void
1440noop_grab_frame(struct weston_pointer_grab *grab)
1441{
1442}
1443
1444static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001445constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1446{
1447 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001448 struct weston_surface *surface =
1449 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001450 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001451 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001452 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001453 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001454 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455
1456 x = wl_fixed_to_int(pointer->x + move->dx);
1457 y = wl_fixed_to_int(pointer->y + move->dy);
1458
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001459 if (shsurf->shell->panel_position ==
1460 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001461 get_output_work_area(shsurf->shell, surface->output, &area);
1462 geometry =
1463 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001464
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001465 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001466 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 y = area.y + safety - geometry.height
1468 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001469
1470 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001471 y + geometry.y < area.y)
1472 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001473 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001474
1475 *cx = x;
1476 *cy = y;
1477}
1478
1479static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001480move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001481 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001482{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001484 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001485 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001486 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001487 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001488
Jonas Ådahld2510102014-10-05 21:39:14 +02001489 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001490 if (!shsurf)
1491 return;
1492
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001493 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1494
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001495 constrain_position(move, &cx, &cy);
1496
1497 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001499 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500}
1501
1502static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001503move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001504 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001505{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001506 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1507 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001508 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001509 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510
Daniel Stone4dbadb12012-05-30 16:31:51 +01001511 if (pointer->button_count == 0 &&
1512 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001513 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001514 free(grab);
1515 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516}
1517
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001518static void
1519move_grab_cancel(struct weston_pointer_grab *grab)
1520{
1521 struct shell_grab *shell_grab =
1522 container_of(grab, struct shell_grab, grab);
1523
1524 shell_grab_end(shell_grab);
1525 free(grab);
1526}
1527
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001528static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001529 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001530 move_grab_motion,
1531 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001532 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001533 noop_grab_axis_source,
1534 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001535 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536};
1537
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001539surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001540 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541{
1542 struct weston_move_grab *move;
1543
1544 if (!shsurf)
1545 return -1;
1546
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001547 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001548 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1549 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550 return 0;
1551
1552 move = malloc(sizeof *move);
1553 if (!move)
1554 return -1;
1555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001557 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001559 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001560 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561
1562 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001563 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564
1565 return 0;
1566}
1567
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568struct weston_resize_grab {
1569 struct shell_grab base;
1570 uint32_t edges;
1571 int32_t width, height;
1572};
1573
1574static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001575resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001576 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001577{
1578 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001579 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580 struct shell_surface *shsurf = resize->base.shsurf;
1581 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001582 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001583 wl_fixed_t from_x, from_y;
1584 wl_fixed_t to_x, to_y;
1585
Jonas Ådahld2510102014-10-05 21:39:14 +02001586 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001587
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001588 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589 return;
1590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 weston_view_from_global_fixed(shsurf->view,
1592 pointer->grab_x, pointer->grab_y,
1593 &from_x, &from_y);
1594 weston_view_from_global_fixed(shsurf->view,
1595 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001596
1597 width = resize->width;
1598 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1599 width += wl_fixed_to_int(from_x - to_x);
1600 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1601 width += wl_fixed_to_int(to_x - from_x);
1602 }
1603
1604 height = resize->height;
1605 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1606 height += wl_fixed_to_int(from_y - to_y);
1607 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1608 height += wl_fixed_to_int(to_y - from_y);
1609 }
1610
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001611 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1612 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1613
1614 min_size.width = MAX(1, min_size.width);
1615 min_size.height = MAX(1, min_size.height);
1616
1617 if (width < min_size.width)
1618 width = min_size.width;
1619 else if (max_size.width > 0 && width > max_size.width)
1620 width = max_size.width;
1621 if (height < min_size.height)
1622 height = min_size.height;
1623 else if (max_size.width > 0 && width > max_size.width)
1624 width = max_size.width;
1625 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626}
1627
1628static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001629resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630 uint32_t time, uint32_t button, uint32_t state_w)
1631{
1632 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001633 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634 enum wl_pointer_button_state state = state_w;
1635
1636 if (pointer->button_count == 0 &&
1637 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1638 shell_grab_end(&resize->base);
1639 free(grab);
1640 }
1641}
1642
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001643static void
1644resize_grab_cancel(struct weston_pointer_grab *grab)
1645{
1646 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1647
1648 shell_grab_end(&resize->base);
1649 free(grab);
1650}
1651
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001652static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653 noop_grab_focus,
1654 resize_grab_motion,
1655 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001656 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001657 noop_grab_axis_source,
1658 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001659 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660};
1661
Giulio Camuffob8366642013-04-25 13:57:46 +03001662/*
1663 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001664 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001665static void
1666surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1667 int32_t *y, int32_t *w, int32_t *h) {
1668 pixman_region32_t region;
1669 pixman_box32_t *box;
1670 struct weston_subsurface *subsurface;
1671
1672 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 surface->width,
1674 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001675
1676 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1677 pixman_region32_union_rect(&region, &region,
1678 subsurface->position.x,
1679 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 subsurface->surface->width,
1681 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001682 }
1683
1684 box = pixman_region32_extents(&region);
1685 if (x)
1686 *x = box->x1;
1687 if (y)
1688 *y = box->y1;
1689 if (w)
1690 *w = box->x2 - box->x1;
1691 if (h)
1692 *h = box->y2 - box->y1;
1693
1694 pixman_region32_fini(&region);
1695}
1696
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697static int
1698surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001699 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700{
1701 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001702 const unsigned resize_topbottom =
1703 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1704 const unsigned resize_leftright =
1705 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1706 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001707 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001709 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001710 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1711 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712 return 0;
1713
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001714 /* Check for invalid edge combinations. */
1715 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1716 (edges & resize_topbottom) == resize_topbottom ||
1717 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718 return 0;
1719
1720 resize = malloc(sizeof *resize);
1721 if (!resize)
1722 return -1;
1723
1724 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001726 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1727 resize->width = geometry.width;
1728 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001729
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001730 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001732 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733
1734 return 0;
1735}
1736
1737static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001738busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001739{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001741 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001742 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 wl_fixed_t sx, sy;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1748 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001749 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001750
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001751 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001752 shell_grab_end(grab);
1753 free(grab);
1754 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755}
1756
1757static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001758busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001759 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001760{
Jonas Ådahld2510102014-10-05 21:39:14 +02001761 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001764static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001765busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001766 uint32_t time, uint32_t button, uint32_t state)
1767{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001768 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001769 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001770 struct weston_pointer *pointer = grab->grab.pointer;
1771 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001772
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001773 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001774 activate(shsurf->shell, shsurf->view, seat,
1775 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001776 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001777 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001778 activate(shsurf->shell, shsurf->view, seat,
1779 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001780 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001781 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782}
1783
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001784static void
1785busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1786{
1787 struct shell_grab *grab = (struct shell_grab *) base;
1788
1789 shell_grab_end(grab);
1790 free(grab);
1791}
1792
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001793static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794 busy_cursor_grab_focus,
1795 busy_cursor_grab_motion,
1796 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001797 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001798 noop_grab_axis_source,
1799 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001800 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801};
1802
1803static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804handle_pointer_focus(struct wl_listener *listener, void *data)
1805{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001806 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001808 struct shell_surface *shsurf;
1809 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812 return;
1813
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001814 shsurf = get_shell_surface(view->surface);
1815 if (!shsurf)
1816 return;
1817
1818 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1819
1820 if (shsurf->unresponsive)
1821 set_busy_cursor(shsurf, pointer);
1822 else
1823 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001824}
1825
1826static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001827shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1828{
1829 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001830 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001831}
1832
1833static void
1834shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1835{
1836 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001837 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001838}
1839
1840static void
1841handle_keyboard_focus(struct wl_listener *listener, void *data)
1842{
1843 struct weston_keyboard *keyboard = data;
1844 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1845
1846 if (seat->focused_surface) {
1847 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1848 if (shsurf)
1849 shell_surface_lose_keyboard_focus(shsurf);
1850 }
1851
1852 seat->focused_surface = keyboard->focus;
1853
1854 if (seat->focused_surface) {
1855 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1856 if (shsurf)
1857 shell_surface_gain_keyboard_focus(shsurf);
1858 }
1859}
1860
Philip Withnall07926d92013-11-25 18:01:40 +00001861/* The surface will be inserted into the list immediately after the link
1862 * returned by this function (i.e. will be stacked immediately above the
1863 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001864static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001865shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1866{
1867 struct workspace *ws;
1868
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001869 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1870 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001871 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001872 }
1873
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001874 /* Move the surface to a normal workspace layer so that surfaces
1875 * which were previously fullscreen or transient are no longer
1876 * rendered on top. */
1877 ws = get_current_workspace(shsurf->shell);
1878 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001879}
1880
Philip Withnall648a4dd2013-11-25 18:01:44 +00001881static void
1882shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1883{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001884 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001885}
1886
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001887/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001888 * geometry to ensure the changes are redrawn.
1889 *
1890 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1891 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001892static void
1893shell_surface_update_layer(struct shell_surface *shsurf)
1894{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001895 struct weston_surface *surface =
1896 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001897 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001898
1899 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1900
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001901 if (new_layer_link == NULL)
1902 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001903 if (new_layer_link == &shsurf->view->layer_link)
1904 return;
1905
1906 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001907 weston_layer_entry_remove(&shsurf->view->layer_link);
1908 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001909 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001910 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001911
1912 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001913}
1914
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001915static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001916shell_surface_set_output(struct shell_surface *shsurf,
1917 struct weston_output *output)
1918{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001919 struct weston_surface *es =
1920 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001921
1922 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001923 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001924 if (output)
1925 shsurf->output = output;
1926 else if (es->output)
1927 shsurf->output = es->output;
1928 else
1929 shsurf->output = get_default_output(es->compositor);
1930}
1931
1932static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001933weston_view_set_initial_position(struct weston_view *view,
1934 struct desktop_shell *shell);
1935
1936static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001937unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001938{
Philip Withnallf85fe842013-11-25 18:01:37 +00001939 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001940 wl_list_remove(&shsurf->fullscreen.transform.link);
1941 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001942
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943 if (shsurf->fullscreen.black_view)
1944 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1945 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001946
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001947 if (shsurf->saved_position_valid)
1948 weston_view_set_position(shsurf->view,
1949 shsurf->saved_x, shsurf->saved_y);
1950 else
1951 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001952 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001953
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001954 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001956 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001957 shsurf->saved_rotation_valid = false;
1958 }
Alex Wu4539b082012-03-01 12:57:46 +08001959}
1960
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001961static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001962unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001963{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001964 struct weston_surface *surface =
1965 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1966
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001967 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001968 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001969
1970 if (shsurf->saved_position_valid)
1971 weston_view_set_position(shsurf->view,
1972 shsurf->saved_x, shsurf->saved_y);
1973 else
1974 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001975 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001976
1977 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978 wl_list_insert(&shsurf->view->geometry.transformation_list,
1979 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001980 shsurf->saved_rotation_valid = false;
1981 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001982}
1983
Philip Withnallbecb77e2013-11-25 18:01:30 +00001984static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001985set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001986{
1987 struct shell_surface *shsurf;
1988 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001989 struct weston_view *view;
1990
1991 view = get_default_view(surface);
1992 if (!view)
1993 return;
1994
1995 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1996
1997 shsurf = get_shell_surface(surface);
1998 current_ws = get_current_workspace(shsurf->shell);
1999
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002000 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002001 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002002
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002003 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002004 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002005
2006 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002007 weston_view_damage_below(view);
2008}
2009
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002010
Tiago Vignattibe143262012-04-16 17:31:41 +03002011static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002012shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002013{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002014 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002015}
2016
Pekka Paalanen8274d902014-08-06 19:36:51 +03002017static int
2018black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2019{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002020 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002021 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002022 int n;
2023 int rem;
2024 int ret;
2025
2026 n = snprintf(buf, len, "black background surface for ");
2027 if (n < 0)
2028 return n;
2029
2030 rem = (int)len - n;
2031 if (rem < 0)
2032 rem = 0;
2033
2034 if (fs_surface->get_label)
2035 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2036 else
2037 ret = snprintf(buf + n, rem, "<unknown>");
2038
2039 if (ret < 0)
2040 return n;
2041
2042 return n + ret;
2043}
2044
Alex Wu21858432012-04-01 20:13:08 +08002045static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002046black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002047
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002049create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002050 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002051 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002052{
2053 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002055
2056 surface = weston_surface_create(ec);
2057 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002058 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002059 return NULL;
2060 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 view = weston_view_create(surface);
2062 if (surface == NULL) {
2063 weston_log("no memory\n");
2064 weston_surface_destroy(surface);
2065 return NULL;
2066 }
Alex Wu4539b082012-03-01 12:57:46 +08002067
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002068 surface->committed = black_surface_committed;
2069 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002070 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002071 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002072 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002073 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002074 pixman_region32_fini(&surface->input);
2075 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002076
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002077 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002078 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079
2080 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002081}
2082
Philip Withnalled8f1a92013-11-25 18:01:43 +00002083static void
2084shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2085{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002086 struct weston_surface *surface =
2087 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002088 struct weston_output *output = shsurf->fullscreen_output;
2089
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002090 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002091
2092 if (!shsurf->fullscreen.black_view)
2093 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002094 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002095 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002096 output->x, output->y,
2097 output->width,
2098 output->height);
2099
2100 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002101 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2102 weston_layer_entry_insert(&shsurf->view->layer_link,
2103 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002104 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002105 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002106
Armin Krezović4663aca2016-06-30 06:04:29 +02002107 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002108 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002109}
2110
Alex Wu4539b082012-03-01 12:57:46 +08002111/* Create black surface and append it to the associated fullscreen surface.
2112 * Handle size dismatch and positioning according to the method. */
2113static void
2114shell_configure_fullscreen(struct shell_surface *shsurf)
2115{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002116 struct weston_surface *surface =
2117 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002118 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002119
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002120 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002121 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002122 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2123 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002124
Philip Withnalled8f1a92013-11-25 18:01:43 +00002125 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002126
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002127 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002128 &surf_width, &surf_height);
2129
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002130 if (surface->buffer_ref.buffer)
2131 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002132}
2133
Alex Wu4539b082012-03-01 12:57:46 +08002134static void
2135shell_map_fullscreen(struct shell_surface *shsurf)
2136{
Alex Wubd3354b2012-04-17 17:20:49 +08002137 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002138}
2139
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002140static struct weston_output *
2141get_focused_output(struct weston_compositor *compositor)
2142{
2143 struct weston_seat *seat;
2144 struct weston_output *output = NULL;
2145
2146 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002147 struct weston_touch *touch = weston_seat_get_touch(seat);
2148 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2149 struct weston_keyboard *keyboard =
2150 weston_seat_get_keyboard(seat);
2151
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002152 /* Priority has touch focus, then pointer and
2153 * then keyboard focus. We should probably have
2154 * three for loops and check frist for touch,
2155 * then for pointer, etc. but unless somebody has some
2156 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002157 if (touch && touch->focus)
2158 output = touch->focus->output;
2159 else if (pointer && pointer->focus)
2160 output = pointer->focus->output;
2161 else if (keyboard && keyboard->focus)
2162 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002163
2164 if (output)
2165 break;
2166 }
2167
2168 return output;
2169}
2170
2171static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002172destroy_shell_seat(struct wl_listener *listener, void *data)
2173{
2174 struct shell_seat *shseat =
2175 container_of(listener,
2176 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002177
2178 wl_list_remove(&shseat->seat_destroy_listener.link);
2179 free(shseat);
2180}
2181
Jason Ekstrand024177c2014-04-21 19:42:58 -05002182static void
2183shell_seat_caps_changed(struct wl_listener *listener, void *data)
2184{
Derek Foreman1281a362015-07-31 16:55:32 -05002185 struct weston_keyboard *keyboard;
2186 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002187 struct shell_seat *seat;
2188
2189 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002190 keyboard = weston_seat_get_keyboard(seat->seat);
2191 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002192
Derek Foreman1281a362015-07-31 16:55:32 -05002193 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002194 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002195 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002196 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002197 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002198 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002199 wl_list_init(&seat->keyboard_focus_listener.link);
2200 }
2201
Derek Foreman1281a362015-07-31 16:55:32 -05002202 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002203 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002204 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002205 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002206 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002207 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002208 wl_list_init(&seat->pointer_focus_listener.link);
2209 }
2210}
2211
Giulio Camuffo5085a752013-03-25 21:42:45 +01002212static struct shell_seat *
2213create_shell_seat(struct weston_seat *seat)
2214{
2215 struct shell_seat *shseat;
2216
2217 shseat = calloc(1, sizeof *shseat);
2218 if (!shseat) {
2219 weston_log("no memory to allocate shell seat\n");
2220 return NULL;
2221 }
2222
2223 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002224
2225 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2226 wl_signal_add(&seat->destroy_signal,
2227 &shseat->seat_destroy_listener);
2228
Jason Ekstrand024177c2014-04-21 19:42:58 -05002229 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2230 wl_list_init(&shseat->keyboard_focus_listener.link);
2231
2232 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2233 wl_list_init(&shseat->pointer_focus_listener.link);
2234
2235 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2236 wl_signal_add(&seat->updated_caps_signal,
2237 &shseat->caps_changed_listener);
2238 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2239
Giulio Camuffo5085a752013-03-25 21:42:45 +01002240 return shseat;
2241}
2242
2243static struct shell_seat *
2244get_shell_seat(struct weston_seat *seat)
2245{
2246 struct wl_listener *listener;
2247
2248 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002249 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002250
2251 return container_of(listener,
2252 struct shell_seat, seat_destroy_listener);
2253}
2254
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002255static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002256fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002257{
2258 struct shell_surface *shsurf = data;
2259
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002260 weston_surface_destroy(shsurf->view->surface);
2261 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002262}
2263
2264static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002265fade_out_done(struct weston_view_animation *animation, void *data)
2266{
2267 struct shell_surface *shsurf = data;
2268 struct wl_event_loop *loop;
2269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002270 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002271
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002272 if (weston_view_is_mapped(shsurf->view)) {
2273 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002274 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002275 }
2276}
2277
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002278struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002279get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002280{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002281 if (weston_surface_is_desktop_surface(surface)) {
2282 struct weston_desktop_surface *desktop_surface =
2283 weston_surface_get_desktop_surface(surface);
2284 return weston_desktop_surface_get_user_data(desktop_surface);
2285 }
2286 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002287}
2288
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002289/*
2290 * libweston-desktop
2291 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002292
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002293static void
2294desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2295 void *shell)
2296{
2297 struct weston_desktop_client *client =
2298 weston_desktop_surface_get_client(desktop_surface);
2299 struct wl_client *wl_client =
2300 weston_desktop_client_get_client(client);
2301 struct weston_view *view;
2302 struct shell_surface *shsurf;
2303 struct weston_surface *surface =
2304 weston_desktop_surface_get_surface(desktop_surface);
2305
2306 view = weston_desktop_surface_create_view(desktop_surface);
2307 if (!view)
2308 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002309
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002310 shsurf = calloc(1, sizeof *shsurf);
2311 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002312 if (wl_client)
2313 wl_client_post_no_memory(wl_client);
2314 else
2315 weston_log("no memory to allocate shell surface\n");
2316 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002317 }
2318
Pekka Paalanen8274d902014-08-06 19:36:51 +03002319 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002320
Tiago Vignattibc052c92012-04-19 16:18:18 +03002321 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002322 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002323 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002324 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002325 shsurf->desktop_surface = desktop_surface;
2326 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002328 wl_list_init(&shsurf->fullscreen.transform.link);
2329
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002330 shsurf->output = get_default_output(shsurf->shell->compositor);
2331
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002332 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002333
Pekka Paalanen460099f2012-01-20 16:48:25 +02002334 /* empty when not in use */
2335 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002336 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002338 wl_list_init(&shsurf->workspace_transform.link);
2339
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002340 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2341 weston_desktop_surface_set_activated(desktop_surface,
2342 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002343}
2344
Tiago Vignattibc052c92012-04-19 16:18:18 +03002345static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002346desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2347 void *shell)
2348{
2349 struct shell_surface *shsurf =
2350 weston_desktop_surface_get_user_data(desktop_surface);
2351 struct weston_surface *surface =
2352 weston_desktop_surface_get_surface(desktop_surface);
2353
2354 if (!shsurf)
2355 return;
2356
2357 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2358
2359 if (shsurf->fullscreen.black_view)
2360 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2361
2362 weston_surface_set_label_func(surface, NULL);
2363 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2364 shsurf->desktop_surface = NULL;
2365
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002366 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367 if (weston_surface_is_mapped(surface) &&
2368 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2369 pixman_region32_fini(&surface->pending.input);
2370 pixman_region32_init(&surface->pending.input);
2371 pixman_region32_fini(&surface->input);
2372 pixman_region32_init(&surface->input);
2373 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2374 fade_out_done, shsurf);
2375 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002376 weston_view_destroy(shsurf->view);
2377 free(shsurf);
2378 }
2379}
2380
2381static void
2382set_maximized_position(struct desktop_shell *shell,
2383 struct shell_surface *shsurf)
2384{
2385 pixman_rectangle32_t area;
2386 struct weston_geometry geometry;
2387
2388 get_output_work_area(shell, shsurf->output, &area);
2389 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2390
2391 weston_view_set_position(shsurf->view,
2392 area.x - geometry.x,
2393 area.y - geometry.y);
2394}
2395
2396static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002397set_position_from_xwayland(struct shell_surface *shsurf)
2398{
2399 struct weston_geometry geometry;
2400 float x;
2401 float y;
2402
2403 assert(shsurf->xwayland.is_set);
2404
2405 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2406 x = shsurf->xwayland.x - geometry.x;
2407 y = shsurf->xwayland.y - geometry.y;
2408
2409 weston_view_set_position(shsurf->view, x, y);
2410
2411#ifdef WM_DEBUG
2412 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2413 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2414 geometry.x, geometry.y, x, y);
2415#endif
2416}
2417
2418static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419map(struct desktop_shell *shell, struct shell_surface *shsurf,
2420 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002421{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002422 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002423 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2424 struct weston_compositor *compositor = shell->compositor;
2425 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002426
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002427 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002428 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 center_on_output(shsurf->view, shsurf->fullscreen_output);
2430 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002431 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002432 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002433 } else if (shsurf->xwayland.is_set) {
2434 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002435 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002436 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002437 }
2438
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002439 /* Surface stacking order, see also activate(). */
2440 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002441
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 weston_view_update_transform(shsurf->view);
2443 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002444 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002445 surface->output = shsurf->output;
2446 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002447 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002448
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449 if (!shell->locked) {
2450 wl_list_for_each(seat, &compositor->seat_list, link)
2451 activate(shell, shsurf->view, seat,
2452 WESTON_ACTIVATE_FLAG_CONFIGURE);
2453 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002454
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002455 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002456 switch (shell->win_animation_type) {
2457 case ANIMATION_FADE:
2458 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2459 break;
2460 case ANIMATION_ZOOM:
2461 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2462 break;
2463 case ANIMATION_NONE:
2464 default:
2465 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002466 }
2467 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002468}
2469
2470static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002471desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2472 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002473{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 struct shell_surface *shsurf =
2475 weston_desktop_surface_get_user_data(desktop_surface);
2476 struct weston_surface *surface =
2477 weston_desktop_surface_get_surface(desktop_surface);
2478 struct weston_view *view = shsurf->view;
2479 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002480 bool was_fullscreen;
2481 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002482
2483 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002484 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002485
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002486 was_fullscreen = shsurf->state.fullscreen;
2487 was_maximized = shsurf->state.maximized;
2488
2489 shsurf->state.fullscreen =
2490 weston_desktop_surface_get_fullscreen(desktop_surface);
2491 shsurf->state.maximized =
2492 weston_desktop_surface_get_maximized(desktop_surface);
2493
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002494 if (!weston_surface_is_mapped(surface)) {
2495 map(shell, shsurf, sx, sy);
2496 surface->is_mapped = true;
2497 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2498 ++surface->ref_count;
2499 return;
2500 }
2501
2502 if (sx == 0 && sy == 0 &&
2503 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002504 shsurf->last_height == surface->height &&
2505 was_fullscreen == shsurf->state.fullscreen &&
2506 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002507 return;
2508
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002509 if (was_fullscreen)
2510 unset_fullscreen(shsurf);
2511 if (was_maximized)
2512 unset_maximized(shsurf);
2513
Quentin Glidic920cf042016-08-16 14:26:20 +02002514 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2515 !shsurf->saved_position_valid) {
2516 shsurf->saved_x = shsurf->view->geometry.x;
2517 shsurf->saved_y = shsurf->view->geometry.y;
2518 shsurf->saved_position_valid = true;
2519
2520 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2521 wl_list_remove(&shsurf->rotation.transform.link);
2522 wl_list_init(&shsurf->rotation.transform.link);
2523 weston_view_geometry_dirty(shsurf->view);
2524 shsurf->saved_rotation_valid = true;
2525 }
2526 }
2527
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002528 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002530 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531 set_maximized_position(shell, shsurf);
2532 surface->output = shsurf->output;
2533 } else {
2534 float from_x, from_y;
2535 float to_x, to_y;
2536 float x, y;
2537
2538 if (shsurf->resize_edges) {
2539 sx = 0;
2540 sy = 0;
2541 }
2542
2543 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2544 sx = shsurf->last_width - surface->width;
2545 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2546 sy = shsurf->last_height - surface->height;
2547
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002548 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2549 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2550 x = shsurf->view->geometry.x + to_x - from_x;
2551 y = shsurf->view->geometry.y + to_y - from_y;
2552
2553 weston_view_set_position(shsurf->view, x, y);
2554 }
2555
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002556 shsurf->last_width = surface->width;
2557 shsurf->last_height = surface->height;
2558
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002559 /* XXX: would a fullscreen surface need the same handling? */
2560 if (surface->output) {
2561 wl_list_for_each(view, &surface->views, surface_link)
2562 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002563 }
2564}
2565
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002566static void
2567set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2568 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002569{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002570 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2571 struct weston_surface *surface =
2572 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2573 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002574
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002575 if (fullscreen) {
2576 /* handle clients launching in fullscreen */
2577 if (output == NULL && !weston_surface_is_mapped(surface)) {
2578 /* Set the output to the one that has focus currently. */
2579 output = get_focused_output(surface->compositor);
2580 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002581
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002582 shell_surface_set_output(shsurf, output);
2583 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002584
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 width = shsurf->output->width;
2586 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587 }
2588 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2589 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002590}
2591
2592static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002593desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2594 struct weston_seat *seat, uint32_t serial, void *shell)
2595{
2596 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2597 struct weston_touch *touch = weston_seat_get_touch(seat);
2598 struct shell_surface *shsurf =
2599 weston_desktop_surface_get_user_data(desktop_surface);
2600 struct weston_surface *surface =
2601 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2602 struct wl_resource *resource = surface->resource;
2603 struct weston_surface *focus;
2604
2605 if (pointer &&
2606 pointer->focus &&
2607 pointer->button_count > 0 &&
2608 pointer->grab_serial == serial) {
2609 focus = weston_surface_get_main_surface(pointer->focus->surface);
2610 if ((focus == surface) &&
2611 (surface_move(shsurf, pointer, true) < 0))
2612 wl_resource_post_no_memory(resource);
2613 } else if (touch &&
2614 touch->focus &&
2615 touch->grab_serial == serial) {
2616 focus = weston_surface_get_main_surface(touch->focus->surface);
2617 if ((focus == surface) &&
2618 (surface_touch_move(shsurf, touch) < 0))
2619 wl_resource_post_no_memory(resource);
2620 }
2621}
2622
2623static void
2624desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2625 struct weston_seat *seat, uint32_t serial,
2626 enum weston_desktop_surface_edge edges, void *shell)
2627{
2628 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2629 struct shell_surface *shsurf =
2630 weston_desktop_surface_get_user_data(desktop_surface);
2631 struct weston_surface *surface =
2632 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2633 struct wl_resource *resource = surface->resource;
2634 struct weston_surface *focus;
2635
2636 if (!pointer ||
2637 pointer->button_count == 0 ||
2638 pointer->grab_serial != serial ||
2639 pointer->focus == NULL)
2640 return;
2641
2642 focus = weston_surface_get_main_surface(pointer->focus->surface);
2643 if (focus != surface)
2644 return;
2645
2646 if (surface_resize(shsurf, pointer, edges) < 0)
2647 wl_resource_post_no_memory(resource);
2648}
2649
2650static void
2651desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2652 bool fullscreen,
2653 struct weston_output *output, void *shell)
2654{
2655 struct shell_surface *shsurf =
2656 weston_desktop_surface_get_user_data(desktop_surface);
2657
2658 set_fullscreen(shsurf, fullscreen, output);
2659}
2660
2661static void
2662set_maximized(struct shell_surface *shsurf, bool maximized)
2663{
2664 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2665 struct weston_surface *surface =
2666 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2667 int32_t width = 0, height = 0;
2668
2669 if (maximized) {
2670 struct weston_output *output;
2671 struct desktop_shell *shell;
2672 pixman_rectangle32_t area;
2673
2674 if (!weston_surface_is_mapped(surface))
2675 output = get_focused_output(surface->compositor);
2676 else
2677 output = surface->output;
2678
2679 shell_surface_set_output(shsurf, output);
2680
2681 shell = shell_surface_get_shell(shsurf);
2682 get_output_work_area(shell, shsurf->output, &area);
2683
2684 width = area.width;
2685 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002686 }
2687 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2688 weston_desktop_surface_set_size(desktop_surface, width, height);
2689}
2690
2691static void
2692desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2693 bool maximized, void *shell)
2694{
2695 struct shell_surface *shsurf =
2696 weston_desktop_surface_get_user_data(desktop_surface);
2697
2698 set_maximized(shsurf, maximized);
2699}
2700
2701static void
2702desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2703 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002704{
2705 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002706 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002707
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002708 /* apply compositor's own minimization logic (hide) */
2709 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002710}
2711
Rafael Antognollie2a34552013-12-03 15:35:45 -02002712static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002713set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002714{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002715 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002716
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002717 if (pointer->grab->interface == &busy_cursor_grab_interface)
2718 return;
2719
2720 grab = malloc(sizeof *grab);
2721 if (!grab)
2722 return;
2723
2724 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2725 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2726 /* Mark the shsurf as ungrabbed so that button binding is able
2727 * to move it. */
2728 shsurf->grabbed = 0;
2729}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002730
2731static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002732end_busy_cursor(struct weston_compositor *compositor,
2733 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002734{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002735 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002736 struct shell_grab *grab;
2737 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002738
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002739 wl_list_for_each(seat, &compositor->seat_list, link) {
2740 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2741 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002742
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002743 if (!pointer)
2744 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002745
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002746 if (pointer->grab->interface != &busy_cursor_grab_interface)
2747 continue;
2748
2749 grab = (struct shell_grab *) pointer->grab;
2750 shsurf = grab->shsurf;
2751 if (!shsurf)
2752 continue;
2753
2754 grab_client =
2755 weston_desktop_surface_get_client(shsurf->desktop_surface);
2756 if (grab_client == desktop_client) {
2757 shell_grab_end(grab);
2758 free(grab);
2759 }
2760 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002761}
2762
2763static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002764desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2765 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002766{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002767 struct shell_surface *shsurf =
2768 weston_desktop_surface_get_user_data(desktop_surface);
2769 bool *unresponsive = user_data;
2770
2771 shsurf->unresponsive = *unresponsive;
2772}
2773
2774static void
2775desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2776 void *shell_)
2777{
2778 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002779 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780 struct weston_seat *seat;
2781 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002782
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002783 weston_desktop_client_for_each_surface(desktop_client,
2784 desktop_surface_set_unresponsive,
2785 &unresponsive);
2786
2787
2788 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2789 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2790 struct weston_desktop_client *grab_client;
2791
2792 if (!pointer || !pointer->focus)
2793 continue;
2794
2795 shsurf = get_shell_surface(pointer->focus->surface);
2796 if (!shsurf)
2797 continue;
2798
2799 grab_client =
2800 weston_desktop_surface_get_client(shsurf->desktop_surface);
2801 if (grab_client == desktop_client)
2802 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002803 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002804}
2805
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002806static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002807desktop_surface_pong(struct weston_desktop_client *desktop_client,
2808 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002809{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810 struct desktop_shell *shell = shell_;
2811 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002812
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002813 weston_desktop_client_for_each_surface(desktop_client,
2814 desktop_surface_set_unresponsive,
2815 &unresponsive);
2816 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002817}
2818
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002819static void
2820desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2821 int32_t x, int32_t y, void *shell_)
2822{
2823 struct shell_surface *shsurf =
2824 weston_desktop_surface_get_user_data(surface);
2825
2826 shsurf->xwayland.x = x;
2827 shsurf->xwayland.y = y;
2828 shsurf->xwayland.is_set = true;
2829}
2830
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831static const struct weston_desktop_api shell_desktop_api = {
2832 .struct_size = sizeof(struct weston_desktop_api),
2833 .surface_added = desktop_surface_added,
2834 .surface_removed = desktop_surface_removed,
2835 .committed = desktop_surface_committed,
2836 .move = desktop_surface_move,
2837 .resize = desktop_surface_resize,
2838 .fullscreen_requested = desktop_surface_fullscreen_requested,
2839 .maximized_requested = desktop_surface_maximized_requested,
2840 .minimized_requested = desktop_surface_minimized_requested,
2841 .ping_timeout = desktop_surface_ping_timeout,
2842 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002843 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002844};
2845
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002846/* ************************ *
2847 * end of libweston-desktop *
2848 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002849static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002850shell_fade(struct desktop_shell *shell, enum fade_type type);
2851
2852static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002853configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002854{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002856
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002857 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 if (v->output == ev->output && v != ev) {
2859 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002860 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002861 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002862 }
2863 }
2864
Quentin Glidice8bf9592016-06-23 18:55:20 +02002865 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002866 ev->surface->is_mapped = true;
2867 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002868
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002869 if (wl_list_empty(&ev->layer_link.link)) {
2870 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002872 }
2873}
2874
David Fort50d962f2016-06-09 17:55:52 +02002875
2876static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877find_shell_output_from_weston_output(struct desktop_shell *shell,
2878 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002879{
2880 struct shell_output *shell_output;
2881
2882 wl_list_for_each(shell_output, &shell->output_list, link) {
2883 if (shell_output->output == output)
2884 return shell_output;
2885 }
2886
2887 return NULL;
2888}
2889
Pekka Paalanen8274d902014-08-06 19:36:51 +03002890static int
2891background_get_label(struct weston_surface *surface, char *buf, size_t len)
2892{
2893 return snprintf(buf, len, "background for output %s",
2894 surface->output->name);
2895}
2896
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002897static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002898background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002899{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002900 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002902
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 view = container_of(es->views.next, struct weston_view, surface_link);
2904
Quentin Glidice8bf9592016-06-23 18:55:20 +02002905 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002906}
2907
2908static void
David Fort50d962f2016-06-09 17:55:52 +02002909handle_background_surface_destroy(struct wl_listener *listener, void *data)
2910{
2911 struct shell_output *output =
2912 container_of(listener, struct shell_output, background_surface_listener);
2913
2914 weston_log("background surface gone\n");
2915 output->background_surface = NULL;
2916}
2917
2918static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002919desktop_shell_set_background(struct wl_client *client,
2920 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002921 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002922 struct wl_resource *surface_resource)
2923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002924 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002925 struct weston_surface *surface =
2926 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002927 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002930 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002931 wl_resource_post_error(surface_resource,
2932 WL_DISPLAY_ERROR_INVALID_OBJECT,
2933 "surface role already assigned");
2934 return;
2935 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002936
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2938 weston_view_destroy(view);
2939 view = weston_view_create(surface);
2940
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002941 surface->committed = background_committed;
2942 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002943 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002944 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002946 weston_desktop_shell_send_configure(resource, 0,
2947 surface_resource,
2948 surface->output->width,
2949 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002950
2951 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2952 sh_output->background_surface = surface;
2953
2954 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2955 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002956}
2957
Pekka Paalanen8274d902014-08-06 19:36:51 +03002958static int
2959panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2960{
2961 return snprintf(buf, len, "panel for output %s",
2962 surface->output->name);
2963}
2964
Kristian Høgsberg75840622011-09-06 13:48:16 -04002965static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002966panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002967{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002968 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002970 int width, height;
2971 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002972
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 view = container_of(es->views.next, struct weston_view, surface_link);
2974
Quentin Glidice8bf9592016-06-23 18:55:20 +02002975 get_panel_size(shell, view, &width, &height);
2976 switch (shell->panel_position) {
2977 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2978 break;
2979 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2980 y = view->output->height - height;
2981 break;
2982 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2983 break;
2984 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2985 x = view->output->width - width;
2986 break;
2987 }
2988
2989 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002990}
2991
2992static void
David Fort50d962f2016-06-09 17:55:52 +02002993handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2994{
2995 struct shell_output *output =
2996 container_of(listener, struct shell_output, panel_surface_listener);
2997
2998 weston_log("panel surface gone\n");
2999 output->panel_surface = NULL;
3000}
3001
3002
3003static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004desktop_shell_set_panel(struct wl_client *client,
3005 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003006 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003007 struct wl_resource *surface_resource)
3008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003010 struct weston_surface *surface =
3011 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003013 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003014
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003015 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003016 wl_resource_post_error(surface_resource,
3017 WL_DISPLAY_ERROR_INVALID_OBJECT,
3018 "surface role already assigned");
3019 return;
3020 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003021
Jason Ekstranda7af7042013-10-12 22:38:11 -05003022 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3023 weston_view_destroy(view);
3024 view = weston_view_create(surface);
3025
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003026 surface->committed = panel_committed;
3027 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003028 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003029 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003031 weston_desktop_shell_send_configure(resource, 0,
3032 surface_resource,
3033 surface->output->width,
3034 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003035
3036 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3037 sh_output->panel_surface = surface;
3038
3039 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3040 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003041}
3042
Pekka Paalanen8274d902014-08-06 19:36:51 +03003043static int
3044lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3045{
3046 return snprintf(buf, len, "lock window");
3047}
3048
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003049static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003050lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003051{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003052 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003053 struct weston_view *view;
3054
3055 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003056
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003057 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003058 return;
3059
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003061
3062 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003063 weston_layer_entry_insert(&shell->lock_layer.view_list,
3064 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003066 surface->is_mapped = true;
3067 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003068 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003069 }
3070}
3071
3072static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003073handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003074{
Tiago Vignattibe143262012-04-16 17:31:41 +03003075 struct desktop_shell *shell =
3076 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003077
Martin Minarik6d118362012-06-07 18:01:59 +02003078 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003079 shell->lock_surface = NULL;
3080}
3081
3082static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003083desktop_shell_set_lock_surface(struct wl_client *client,
3084 struct wl_resource *resource,
3085 struct wl_resource *surface_resource)
3086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003088 struct weston_surface *surface =
3089 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003090
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003091 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003092
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003093 if (!shell->locked)
3094 return;
3095
Pekka Paalanen98262232011-12-01 10:42:22 +02003096 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003097
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003098 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003099 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003100 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003101
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003102 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003103 surface->committed = lock_surface_committed;
3104 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003105 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003106}
3107
3108static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003109resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003110{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003111 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003112
Quentin Glidic82681572016-12-17 13:40:51 +01003113 weston_layer_unset_position(&shell->lock_layer);
3114
3115 if (shell->showing_input_panels)
3116 weston_layer_set_position(&shell->input_panel_layer,
3117 WESTON_LAYER_POSITION_TOP_UI);
3118 weston_layer_set_position(&shell->fullscreen_layer,
3119 WESTON_LAYER_POSITION_FULLSCREEN);
3120 weston_layer_set_position(&shell->panel_layer,
3121 WESTON_LAYER_POSITION_UI);
3122 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003123
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003124 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003125
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003126 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003127 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003128 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003129}
3130
3131static void
3132desktop_shell_unlock(struct wl_client *client,
3133 struct wl_resource *resource)
3134{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003135 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003136
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003137 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003138
3139 if (shell->locked)
3140 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003141}
3142
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003144desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003145 struct wl_resource *resource,
3146 struct wl_resource *surface_resource)
3147{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003148 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003149
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003150 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003151 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003152}
3153
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003154static void
3155desktop_shell_desktop_ready(struct wl_client *client,
3156 struct wl_resource *resource)
3157{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003158 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003159
3160 shell_fade_startup(shell);
3161}
3162
Jonny Lamb765760d2014-08-20 15:53:19 +02003163static void
3164desktop_shell_set_panel_position(struct wl_client *client,
3165 struct wl_resource *resource,
3166 uint32_t position)
3167{
3168 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3169
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003170 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3171 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3172 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3173 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003174 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003175 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003176 "bad position argument");
3177 return;
3178 }
3179
3180 shell->panel_position = position;
3181}
3182
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003183static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003184 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003185 desktop_shell_set_panel,
3186 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003187 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003188 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003189 desktop_shell_desktop_ready,
3190 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003191};
3192
3193static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003194move_binding(struct weston_pointer *pointer, uint32_t time,
3195 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003196{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003197 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003198 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003199 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003200
Derek Foreman8ae2db52015-07-15 13:00:36 -05003201 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003202 return;
3203
Derek Foreman8ae2db52015-07-15 13:00:36 -05003204 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003205
Pekka Paalanen01388e22013-04-25 13:57:44 +03003206 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003207 if (surface == NULL)
3208 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003209
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003210 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003211 if (shsurf == NULL ||
3212 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3213 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003214 return;
3215
Derek Foreman794fa0e2015-07-15 13:00:38 -05003216 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003217}
3218
3219static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003220maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3221 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003222{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003223 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003224 struct weston_surface *surface;
3225 struct shell_surface *shsurf;
3226
3227 surface = weston_surface_get_main_surface(focus);
3228 if (surface == NULL)
3229 return;
3230
3231 shsurf = get_shell_surface(surface);
3232 if (shsurf == NULL)
3233 return;
3234
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003235 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003236}
3237
3238static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003239fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3240 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003241{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003242 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003243 struct weston_surface *surface;
3244 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003245 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003246
3247 surface = weston_surface_get_main_surface(focus);
3248 if (surface == NULL)
3249 return;
3250
3251 shsurf = get_shell_surface(surface);
3252 if (shsurf == NULL)
3253 return;
3254
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003255 fullscreen =
3256 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003257
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003258 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003259}
3260
3261static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003262touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003263{
Derek Foreman362656b2014-09-04 10:23:05 -05003264 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003265 struct weston_surface *surface;
3266 struct shell_surface *shsurf;
3267
Derek Foreman8ae2db52015-07-15 13:00:36 -05003268 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003269 return;
3270
Derek Foreman8ae2db52015-07-15 13:00:36 -05003271 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003272 surface = weston_surface_get_main_surface(focus);
3273 if (surface == NULL)
3274 return;
3275
3276 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003277 if (shsurf == NULL ||
3278 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3279 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003280 return;
3281
Derek Foremanb7674ae2015-07-15 13:00:37 -05003282 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003283}
3284
3285static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003286resize_binding(struct weston_pointer *pointer, uint32_t time,
3287 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003288{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003289 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003290 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003291 uint32_t edges = 0;
3292 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003293 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003294
Derek Foreman8ae2db52015-07-15 13:00:36 -05003295 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003296 return;
3297
Derek Foreman8ae2db52015-07-15 13:00:36 -05003298 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003299
Pekka Paalanen01388e22013-04-25 13:57:44 +03003300 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003301 if (surface == NULL)
3302 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003303
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003304 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003305 if (shsurf == NULL ||
3306 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3307 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003308 return;
3309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003311 wl_fixed_to_int(pointer->grab_x),
3312 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003314
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003315 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003316 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003317 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003318 edges |= 0;
3319 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003320 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003321
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003323 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003324 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003325 edges |= 0;
3326 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003327 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003328
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003329 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003330}
3331
3332static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003333surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003334 struct weston_pointer_axis_event *event,
3335 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003336{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003337 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003338 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003339 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003340 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003341
Pekka Paalanen01388e22013-04-25 13:57:44 +03003342 /* XXX: broken for windows containing sub-surfaces */
3343 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003344 if (surface == NULL)
3345 return;
3346
3347 shsurf = get_shell_surface(surface);
3348 if (!shsurf)
3349 return;
3350
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003351 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003352
Jason Ekstranda7af7042013-10-12 22:38:11 -05003353 if (shsurf->view->alpha > 1.0)
3354 shsurf->view->alpha = 1.0;
3355 if (shsurf->view->alpha < step)
3356 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003357
Jason Ekstranda7af7042013-10-12 22:38:11 -05003358 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003359 weston_surface_damage(surface);
3360}
3361
3362static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003363do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003364 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003365{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003366 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003367 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003368 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003369 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003370
Derek Foreman1281a362015-07-31 16:55:32 -05003371 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003372 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3373 return;
3374 }
3375
Scott Moreauccbf29d2012-02-22 14:21:41 -07003376 wl_list_for_each(output, &compositor->output_list, link) {
3377 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003378 wl_fixed_to_double(pointer->x),
3379 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003380 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003381 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003382 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003383 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003384 increment = -output->zoom.increment;
3385 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003386 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003387 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003388 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003389 else
3390 increment = 0;
3391
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003392 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003393
Scott Moreaue6603982012-06-11 13:07:51 -06003394 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003395 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003396 else if (output->zoom.level > output->zoom.max_level)
3397 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003398
3399 if (!output->zoom.active) {
3400 if (output->zoom.level <= 0.0)
3401 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003402 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003403 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003404
Scott Moreaue6603982012-06-11 13:07:51 -06003405 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003406
Jason Ekstranda7af7042013-10-12 22:38:11 -05003407 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003408 }
3409 }
3410}
3411
Scott Moreauccbf29d2012-02-22 14:21:41 -07003412static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003413zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003414 struct weston_pointer_axis_event *event,
3415 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003416{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003417 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003418}
3419
3420static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003421zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3422 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003423{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003424 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003425}
3426
3427static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003428terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3429 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003431 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003432
Daniel Stone325fc2d2012-05-30 16:31:58 +01003433 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003434}
3435
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003436static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003437rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003438 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003439{
3440 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003441 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003442 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003443 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003444 struct weston_surface *surface =
3445 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003446 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003447
Jonas Ådahld2510102014-10-05 21:39:14 +02003448 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003449
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003450 if (!shsurf)
3451 return;
3452
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003453 cx = 0.5f * surface->width;
3454 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003455
Daniel Stone37816df2012-05-16 18:45:18 +01003456 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3457 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003458 r = sqrtf(dx * dx + dy * dy);
3459
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003460 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003462
3463 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003464 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003465 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003466
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003467 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003468 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003469
3470 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003471 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003472 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003473 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003474 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003475
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003476 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003477 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003478 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003479 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003480 wl_list_init(&shsurf->rotation.transform.link);
3481 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003482 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003483 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003484
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003485 /* We need to adjust the position of the surface
3486 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 cposx = shsurf->view->geometry.x + cx;
3488 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003489 dposx = rotate->center.x - cposx;
3490 dposy = rotate->center.y - cposy;
3491 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 weston_view_set_position(shsurf->view,
3493 shsurf->view->geometry.x + dposx,
3494 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003495 }
3496
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003497 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003498 * lazily applies the damage due to rotation update.
3499 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003500 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003501}
3502
3503static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003504rotate_grab_button(struct weston_pointer_grab *grab,
3505 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506{
3507 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003508 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003509 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003510 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003511 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003512
Daniel Stone4dbadb12012-05-30 16:31:51 +01003513 if (pointer->button_count == 0 &&
3514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003515 if (shsurf)
3516 weston_matrix_multiply(&shsurf->rotation.rotation,
3517 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003518 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003519 free(rotate);
3520 }
3521}
3522
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003523static void
3524rotate_grab_cancel(struct weston_pointer_grab *grab)
3525{
3526 struct rotate_grab *rotate =
3527 container_of(grab, struct rotate_grab, base.grab);
3528
3529 shell_grab_end(&rotate->base);
3530 free(rotate);
3531}
3532
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003533static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003534 noop_grab_focus,
3535 rotate_grab_motion,
3536 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003537 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003538 noop_grab_axis_source,
3539 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003540 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003541};
3542
3543static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003544surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003545{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003546 struct weston_surface *surface =
3547 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003549 float dx, dy;
3550 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003551
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552 rotate = malloc(sizeof *rotate);
3553 if (!rotate)
3554 return;
3555
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003556 weston_view_to_global_float(shsurf->view,
3557 surface->width * 0.5f,
3558 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003559 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003560
Derek Foreman74de4692015-07-15 13:00:39 -05003561 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3562 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003563 r = sqrtf(dx * dx + dy * dy);
3564 if (r > 20.0f) {
3565 struct weston_matrix inverse;
3566
3567 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003568 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003569 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003570
3571 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003572 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003573 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003574 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003575 weston_matrix_init(&rotate->rotation);
3576 }
3577
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003578 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003579 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580}
3581
3582static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003583rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003584 void *data)
3585{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003586 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003587 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003588 struct shell_surface *surface;
3589
Derek Foreman8ae2db52015-07-15 13:00:36 -05003590 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003591 return;
3592
Derek Foreman8ae2db52015-07-15 13:00:36 -05003593 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003594
Pekka Paalanen01388e22013-04-25 13:57:44 +03003595 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003596 if (base_surface == NULL)
3597 return;
3598
3599 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003600 if (surface == NULL ||
3601 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3602 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003603 return;
3604
Derek Foreman74de4692015-07-15 13:00:39 -05003605 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003606}
3607
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003608/* Move all fullscreen layers down to the current workspace and hide their
3609 * black views. The surfaces' state is set to both fullscreen and lowered,
3610 * and this is reversed when such a surface is re-configured, see
3611 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3612 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003613 * lowering_output = NULL - Lower on all outputs, else only lower on the
3614 * specified output.
3615 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003616 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003617 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003618void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003619lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003620 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003621{
3622 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003624
3625 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003627 &shell->fullscreen_layer.view_list.link,
3628 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003629 struct shell_surface *shsurf = get_shell_surface(view->surface);
3630
3631 if (!shsurf)
3632 continue;
3633
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003634 /* Only lower surfaces which have lowering_output as their fullscreen
3635 * output, unless a NULL output asks for lowering on all outputs.
3636 */
3637 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3638 continue;
3639
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003640 /* We can have a non-fullscreen popup for a fullscreen surface
3641 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003642 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003643 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003644 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3645 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003646 weston_view_damage_below(shsurf->fullscreen.black_view);
3647
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003648 }
3649
3650 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003651 weston_layer_entry_remove(&view->layer_link);
3652 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003653 weston_view_damage_below(view);
3654 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003655
3656 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003657 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003658}
3659
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003660void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003661activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003662 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003663{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003664 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003665 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003666 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003667 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003668 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003669 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003670
Pekka Paalanen01388e22013-04-25 13:57:44 +03003671 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003672 shsurf = get_shell_surface(main_surface);
3673 assert(shsurf);
3674
3675 /* Only demote fullscreen surfaces on the output of activated shsurf.
3676 * Leave fullscreen surfaces on unrelated outputs alone. */
3677 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003678
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003679 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003680
Jonas Ådahl8538b222012-08-29 22:13:03 +02003681 state = ensure_focus_state(shell, seat);
3682 if (state == NULL)
3683 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003684
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003685 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003686 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003687
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003688 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3689 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003690 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003691
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003692 /* Update the surface’s layer. This brings it to the top of the stacking
3693 * order as appropriate. */
3694 shell_surface_update_layer(shsurf);
3695
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003696 if (shell->focus_animation_type != ANIMATION_NONE) {
3697 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003698 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003699 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003700}
3701
Alex Wu21858432012-04-01 20:13:08 +08003702/* no-op func for checking black surface */
3703static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003704black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003705{
3706}
3707
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003708static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003709is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003710{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003711 struct weston_surface *surface = view->surface;
3712
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003713 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003714 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003715 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003716 return true;
3717 }
3718 return false;
3719}
3720
Kristian Høgsberg75840622011-09-06 13:48:16 -04003721static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003722activate_binding(struct weston_seat *seat,
3723 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003724 struct weston_view *focus_view,
3725 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003726{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003727 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003728 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003729
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003730 if (!focus_view)
3731 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003732
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003733 if (is_black_surface_view(focus_view, &main_view))
3734 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003735
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003736 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003737 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003738 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003739
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003740 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003741}
3742
3743static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003744click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3745 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003746{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003747 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003748 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003749 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003750 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003751
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003752 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003753 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003754 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003755}
3756
3757static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003758touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3759 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003760{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003761 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003762 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003763 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003764 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003765
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003766 activate_binding(touch->seat, data, touch->focus,
3767 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003768}
3769
3770static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003771unfocus_all_seats(struct desktop_shell *shell)
3772{
3773 struct weston_seat *seat, *next;
3774
3775 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003776 struct weston_keyboard *keyboard =
3777 weston_seat_get_keyboard(seat);
3778
3779 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003780 continue;
3781
Derek Foreman1281a362015-07-31 16:55:32 -05003782 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003783 }
3784}
3785
3786static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003787lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003788{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003789 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003790
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003791 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003792 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003793 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003794 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003795
3796 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003797
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003798 /* Hide all surfaces by removing the fullscreen, panel and
3799 * toplevel layers. This way nothing else can show or receive
3800 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003801
Quentin Glidic82681572016-12-17 13:40:51 +01003802 weston_layer_unset_position(&shell->panel_layer);
3803 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003804 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003805 weston_layer_unset_position(&shell->input_panel_layer);
3806 weston_layer_unset_position(&ws->layer);
3807
3808 weston_layer_set_position(&shell->lock_layer,
3809 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003810
Derek Foreman004b4a12015-07-20 16:28:13 -05003811 weston_compositor_sleep(shell->compositor);
3812
Neil Robertsb4a91702014-04-09 16:33:32 +01003813 /* Remove the keyboard focus on all seats. This will be
3814 * restored to the workspace's saved state via
3815 * restore_focus_state when the compositor is unlocked */
3816 unfocus_all_seats(shell);
3817
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003818 /* TODO: disable bindings that should not work while locked. */
3819
3820 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003821}
3822
3823static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003824unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003825{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003826 struct wl_resource *shell_resource;
3827
Pekka Paalanend81c2162011-11-16 13:47:34 +02003828 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003829 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003830 return;
3831 }
3832
3833 /* If desktop-shell client has gone away, unlock immediately. */
3834 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003835 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003836 return;
3837 }
3838
3839 if (shell->prepare_event_sent)
3840 return;
3841
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003842 shell_resource = shell->child.desktop_shell;
3843 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003844 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003845}
3846
3847static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003848shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003849{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003850 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003851
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003852 shell->fade.animation = NULL;
3853
3854 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003855 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003856 weston_surface_destroy(shell->fade.view->surface);
3857 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003858 break;
3859 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003860 lock(shell);
3861 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003862 default:
3863 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003864 }
3865}
3866
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003868shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003869{
3870 struct weston_compositor *compositor = shell->compositor;
3871 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003872 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003873
3874 surface = weston_surface_create(compositor);
3875 if (!surface)
3876 return NULL;
3877
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 view = weston_view_create(surface);
3879 if (!view) {
3880 weston_surface_destroy(surface);
3881 return NULL;
3882 }
3883
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003884 weston_surface_set_size(surface, 8192, 8192);
3885 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003886 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003887 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3888 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003889 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003890 surface->is_mapped = true;
3891 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003892
Jason Ekstranda7af7042013-10-12 22:38:11 -05003893 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003894}
3895
3896static void
3897shell_fade(struct desktop_shell *shell, enum fade_type type)
3898{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003899 float tint;
3900
3901 switch (type) {
3902 case FADE_IN:
3903 tint = 0.0;
3904 break;
3905 case FADE_OUT:
3906 tint = 1.0;
3907 break;
3908 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003909 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003910 return;
3911 }
3912
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003913 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003914
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003915 if (shell->fade.view == NULL) {
3916 shell->fade.view = shell_fade_create_surface(shell);
3917 if (!shell->fade.view)
3918 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003919
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003920 shell->fade.view->alpha = 1.0 - tint;
3921 weston_view_update_transform(shell->fade.view);
3922 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003923
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003924 if (shell->fade.view->output == NULL) {
3925 /* If the black view gets a NULL output, we lost the
3926 * last output and we'll just cancel the fade. This
3927 * happens when you close the last window under the
3928 * X11 or Wayland backends. */
3929 shell->locked = false;
3930 weston_surface_destroy(shell->fade.view->surface);
3931 shell->fade.view = NULL;
3932 } else if (shell->fade.animation) {
3933 weston_fade_update(shell->fade.animation, tint);
3934 } else {
3935 shell->fade.animation =
3936 weston_fade_run(shell->fade.view,
3937 1.0 - tint, tint, 300.0,
3938 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003939 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003940}
3941
3942static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003943do_shell_fade_startup(void *data)
3944{
3945 struct desktop_shell *shell = data;
3946
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003947 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003948 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003949 } else {
3950 weston_log("desktop shell: "
3951 "unexpected fade-in animation type %d\n",
3952 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003953 weston_surface_destroy(shell->fade.view->surface);
3954 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003955 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003956}
3957
3958static void
3959shell_fade_startup(struct desktop_shell *shell)
3960{
3961 struct wl_event_loop *loop;
3962
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003963 if (!shell->fade.startup_timer)
3964 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003965
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003966 wl_event_source_remove(shell->fade.startup_timer);
3967 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003968
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003969 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3970 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003971}
3972
3973static int
3974fade_startup_timeout(void *data)
3975{
3976 struct desktop_shell *shell = data;
3977
3978 shell_fade_startup(shell);
3979 return 0;
3980}
3981
3982static void
3983shell_fade_init(struct desktop_shell *shell)
3984{
3985 /* Make compositor output all black, and wait for the desktop-shell
3986 * client to signal it is ready, then fade in. The timer triggers a
3987 * fade-in, in case the desktop-shell client takes too long.
3988 */
3989
3990 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003991
3992 if (shell->fade.view != NULL) {
3993 weston_log("%s: warning: fade surface already exists\n",
3994 __func__);
3995 return;
3996 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003997
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003998 if (shell->startup_animation_type == ANIMATION_NONE)
3999 return;
4000
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004001 shell->fade.view = shell_fade_create_surface(shell);
4002 if (!shell->fade.view)
4003 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004004
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004005 weston_view_update_transform(shell->fade.view);
4006 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004007
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004008 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4009 shell->fade.startup_timer =
4010 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4011 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004012}
4013
4014static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004015idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004016{
4017 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004018 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004019
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004020 struct weston_seat *seat;
4021
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004022 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4023 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004024
4025 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004026 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004027}
4028
4029static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004030wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004031{
4032 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004033 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004034
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004035 unlock(shell);
4036}
4037
4038static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004039transform_handler(struct wl_listener *listener, void *data)
4040{
4041 struct weston_surface *surface = data;
4042 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004043 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004044 int x, y;
4045
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004046 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004047 return;
4048
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004049 api = shsurf->shell->xwayland_surface_api;
4050 if (!api) {
4051 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4052 shsurf->shell->xwayland_surface_api = api;
4053 }
4054
4055 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004056 return;
4057
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004058 if (!weston_view_is_mapped(shsurf->view))
4059 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004060
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004061 x = shsurf->view->geometry.x;
4062 y = shsurf->view->geometry.y;
4063
4064 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004065}
4066
4067static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004068center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004069{
Giulio Camuffob8366642013-04-25 13:57:46 +03004070 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004071 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004072
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004074
4075 x = output->x + (output->width - width) / 2 - surf_x / 2;
4076 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004077
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004078 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004079}
4080
4081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004082weston_view_set_initial_position(struct weston_view *view,
4083 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004084{
4085 struct weston_compositor *compositor = shell->compositor;
4086 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004087 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004088 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004089 struct weston_output *output, *target_output = NULL;
4090 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004091 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004092
4093 /* As a heuristic place the new window on the same output as the
4094 * pointer. Falling back to the output containing 0, 0.
4095 *
4096 * TODO: Do something clever for touch too?
4097 */
4098 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004099 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4100
4101 if (pointer) {
4102 ix = wl_fixed_to_int(pointer->x);
4103 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004104 break;
4105 }
4106 }
4107
4108 wl_list_for_each(output, &compositor->output_list, link) {
4109 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4110 target_output = output;
4111 break;
4112 }
4113 }
4114
4115 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004116 weston_view_set_position(view, 10 + random() % 400,
4117 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004118 return;
4119 }
4120
4121 /* Valid range within output where the surface will still be onscreen.
4122 * If this is negative it means that the surface is bigger than
4123 * output.
4124 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004125 get_output_work_area(shell, target_output, &area);
4126
Derek Foremanf814c5d2015-04-15 12:23:54 -05004127 x = area.x;
4128 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004129 range_x = area.width - view->surface->width;
4130 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004131
Rob Bradford4cb88c72012-08-13 15:18:44 +01004132 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004133 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004134
4135 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004136 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004137
Jason Ekstranda7af7042013-10-12 22:38:11 -05004138 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004139}
4140
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004141static bool
4142check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4143{
4144 struct timespec now;
4145
4146 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4147 return false;
4148
4149 /*
4150 * If the shell helper client dies before the session has been
4151 * up for roughly 30 seconds, better just make Weston shut down,
4152 * because the user likely has no way to interact with the desktop
4153 * anyway.
4154 */
4155 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4156 weston_log("Error: %s apparently cannot run at all.\n",
4157 shell->client);
4158 weston_log_continue(STAMP_SPACE "Quitting...");
4159 wl_display_terminate(shell->compositor->wl_display);
4160
4161 return true;
4162 }
4163
4164 return false;
4165}
4166
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004167static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004168
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004169static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004170respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004171{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004172 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004173
4174 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4175 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004176 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004177 shell->child.deathstamp = time;
4178 shell->child.deathcount = 0;
4179 }
4180
4181 shell->child.deathcount++;
4182 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004183 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004184 return;
4185 }
4186
Pekka Paalanen826dc142014-08-27 12:11:53 +03004187 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004188 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004189}
4190
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004191static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004192desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4193{
4194 struct desktop_shell *shell;
4195
4196 shell = container_of(listener, struct desktop_shell,
4197 child.client_destroy_listener);
4198
Pekka Paalanen826dc142014-08-27 12:11:53 +03004199 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004200 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004201 /*
4202 * unbind_desktop_shell() will reset shell->child.desktop_shell
4203 * before the respawned process has a chance to create a new
4204 * desktop_shell object, because we are being called from the
4205 * wl_client destructor which destroys all wl_resources before
4206 * returning.
4207 */
4208
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004209 if (!check_desktop_shell_crash_too_early(shell))
4210 respawn_desktop_shell_process(shell);
4211
Pekka Paalanen826dc142014-08-27 12:11:53 +03004212 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004213}
4214
4215static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004216launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004217{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004218 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004219
Pekka Paalanen826dc142014-08-27 12:11:53 +03004220 shell->child.client = weston_client_start(shell->compositor,
4221 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004222
Arnaud Vrac42631192014-08-25 20:56:46 +02004223 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004224 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004225 return;
4226 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004227
4228 shell->child.client_destroy_listener.notify =
4229 desktop_shell_client_destroy;
4230 wl_client_add_destroy_listener(shell->child.client,
4231 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004232}
4233
4234static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004235unbind_desktop_shell(struct wl_resource *resource)
4236{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004237 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004238
4239 if (shell->locked)
4240 resume_desktop(shell);
4241
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004242 shell->child.desktop_shell = NULL;
4243 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004244}
4245
4246static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004247bind_desktop_shell(struct wl_client *client,
4248 void *data, uint32_t version, uint32_t id)
4249{
Tiago Vignattibe143262012-04-16 17:31:41 +03004250 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004251 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004252
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004253 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4254 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004255
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004256 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004257 wl_resource_set_implementation(resource,
4258 &desktop_shell_implementation,
4259 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004260 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004261 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004262 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004263
4264 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4265 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004266}
4267
Kristian Høgsberg07045392012-02-19 18:52:44 -05004268struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004269 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004270 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004271 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004272 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004273 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004274};
4275
4276static void
4277switcher_next(struct switcher *switcher)
4278{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004279 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004280 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004281 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004282 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004283
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004284 /* temporary re-display minimized surfaces */
4285 struct weston_view *tmp;
4286 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004287 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4288 weston_layer_entry_remove(&view->layer_link);
4289 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004290 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4291 *minimized = view;
4292 }
4293
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004294 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004295 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004296 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004297 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004298 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004299 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004300 next = view;
4301 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 view->alpha = 0.25;
4303 weston_view_geometry_dirty(view);
4304 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004305 }
Alex Wu1659daa2012-04-01 20:13:09 +08004306
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004307 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004308 view->alpha = 0.25;
4309 weston_view_geometry_dirty(view);
4310 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004311 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004312 }
4313
4314 if (next == NULL)
4315 next = first;
4316
Alex Wu07b26062012-03-12 16:06:01 +08004317 if (next == NULL)
4318 return;
4319
Kristian Høgsberg07045392012-02-19 18:52:44 -05004320 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004321 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004322
4323 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004324 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004326
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004327 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004328 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004330}
4331
4332static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004333switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004334{
4335 struct switcher *switcher =
4336 container_of(listener, struct switcher, listener);
4337
4338 switcher_next(switcher);
4339}
4340
4341static void
Daniel Stone351eb612012-05-31 15:27:47 -04004342switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004343{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004345 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004346 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004348 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004349 if (is_focus_view(view))
4350 continue;
4351
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 view->alpha = 1.0;
4353 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004354 }
4355
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004356 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004357 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004358 keyboard->seat,
4359 WESTON_ACTIVATE_FLAG_CONFIGURE);
4360 }
4361
Kristian Høgsberg07045392012-02-19 18:52:44 -05004362 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004363 weston_keyboard_end_grab(keyboard);
4364 if (keyboard->input_method_resource)
4365 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004366
4367 /* re-hide surfaces that were temporary shown during the switch */
4368 struct weston_view **minimized;
4369 wl_array_for_each(minimized, &switcher->minimized_array) {
4370 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004371 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004372 weston_layer_entry_remove(&(*minimized)->layer_link);
4373 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004374 weston_view_damage_below(*minimized);
4375 }
4376 }
4377 wl_array_release(&switcher->minimized_array);
4378
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379 free(switcher);
4380}
4381
4382static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004383switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004384 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004385{
4386 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004387 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004388
Daniel Stonec9785ea2012-05-30 16:31:52 +01004389 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004390 switcher_next(switcher);
4391}
4392
4393static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004394switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004395 uint32_t mods_depressed, uint32_t mods_latched,
4396 uint32_t mods_locked, uint32_t group)
4397{
4398 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004399 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004400
Daniel Stone351eb612012-05-31 15:27:47 -04004401 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4402 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004403}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004404
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004405static void
4406switcher_cancel(struct weston_keyboard_grab *grab)
4407{
4408 struct switcher *switcher = container_of(grab, struct switcher, grab);
4409
4410 switcher_destroy(switcher);
4411}
4412
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004413static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004414 switcher_key,
4415 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004416 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004417};
4418
4419static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004420switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4421 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004422{
Tiago Vignattibe143262012-04-16 17:31:41 +03004423 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004424 struct switcher *switcher;
4425
4426 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004427 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004428 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004429 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004431 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004432
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004433 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004434 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004435 weston_keyboard_start_grab(keyboard, &switcher->grab);
4436 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004437 switcher_next(switcher);
4438}
4439
Pekka Paalanen3c647232011-12-22 13:43:43 +02004440static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004441backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4442 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004443{
4444 struct weston_compositor *compositor = data;
4445 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004446 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004447
4448 /* TODO: we're limiting to simple use cases, where we assume just
4449 * control on the primary display. We'd have to extend later if we
4450 * ever get support for setting backlights on random desktop LCD
4451 * panels though */
4452 output = get_default_output(compositor);
4453 if (!output)
4454 return;
4455
4456 if (!output->set_backlight)
4457 return;
4458
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004459 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4460 backlight_new = output->backlight_current - 25;
4461 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4462 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004463
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004464 if (backlight_new < 5)
4465 backlight_new = 5;
4466 if (backlight_new > 255)
4467 backlight_new = 255;
4468
4469 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004470 output->set_backlight(output, output->backlight_current);
4471}
4472
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004473static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004474force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4475 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004476{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004477 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004478 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004479 struct desktop_shell *shell = data;
4480 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004481 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004482
Derek Foreman8ae2db52015-07-15 13:00:36 -05004483 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004484 if (!focus_surface)
4485 return;
4486
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004487 wl_signal_emit(&compositor->kill_signal, focus_surface);
4488
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004489 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004490 wl_client_get_credentials(client, &pid, NULL, NULL);
4491
4492 /* Skip clients that we launched ourselves (the credentials of
4493 * the socketpair is ours) */
4494 if (pid == getpid())
4495 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004496
Daniel Stone325fc2d2012-05-30 16:31:58 +01004497 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004498}
4499
4500static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004501workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004502 uint32_t key, void *data)
4503{
4504 struct desktop_shell *shell = data;
4505 unsigned int new_index = shell->workspaces.current;
4506
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004507 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004508 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004509 if (new_index != 0)
4510 new_index--;
4511
4512 change_workspace(shell, new_index);
4513}
4514
4515static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004516workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004517 uint32_t key, void *data)
4518{
4519 struct desktop_shell *shell = data;
4520 unsigned int new_index = shell->workspaces.current;
4521
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004522 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004523 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004524 if (new_index < shell->workspaces.num - 1)
4525 new_index++;
4526
4527 change_workspace(shell, new_index);
4528}
4529
4530static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004531workspace_f_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;
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 new_index = key - KEY_F1;
4540 if (new_index >= shell->workspaces.num)
4541 new_index = shell->workspaces.num - 1;
4542
4543 change_workspace(shell, new_index);
4544}
4545
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004546static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004547workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4548 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004549{
4550 struct desktop_shell *shell = data;
4551 unsigned int new_index = shell->workspaces.current;
4552
4553 if (shell->locked)
4554 return;
4555
4556 if (new_index != 0)
4557 new_index--;
4558
Derek Foreman8ae2db52015-07-15 13:00:36 -05004559 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004560}
4561
4562static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004563workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4564 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004565{
4566 struct desktop_shell *shell = data;
4567 unsigned int new_index = shell->workspaces.current;
4568
4569 if (shell->locked)
4570 return;
4571
4572 if (new_index < shell->workspaces.num - 1)
4573 new_index++;
4574
Derek Foreman8ae2db52015-07-15 13:00:36 -05004575 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004576}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004577
4578static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004579shell_reposition_view_on_output_destroy(struct weston_view *view)
4580{
4581 struct weston_output *output, *first_output;
4582 struct weston_compositor *ec = view->surface->compositor;
4583 struct shell_surface *shsurf;
4584 float x, y;
4585 int visible;
4586
4587 x = view->geometry.x;
4588 y = view->geometry.y;
4589
4590 /* At this point the destroyed output is not in the list anymore.
4591 * If the view is still visible somewhere, we leave where it is,
4592 * otherwise, move it to the first output. */
4593 visible = 0;
4594 wl_list_for_each(output, &ec->output_list, link) {
4595 if (pixman_region32_contains_point(&output->region,
4596 x, y, NULL)) {
4597 visible = 1;
4598 break;
4599 }
4600 }
4601
4602 if (!visible) {
4603 first_output = container_of(ec->output_list.next,
4604 struct weston_output, link);
4605
4606 x = first_output->x + first_output->width / 4;
4607 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004608
4609 weston_view_set_position(view, x, y);
4610 } else {
4611 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004612 }
4613
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004614
4615 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004616 if (!shsurf)
4617 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004618
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004619 shsurf->saved_position_valid = false;
4620 set_maximized(shsurf, false);
4621 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004622}
4623
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004624void
4625shell_for_each_layer(struct desktop_shell *shell,
4626 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004627{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004628 struct workspace **ws;
4629
4630 func(shell, &shell->fullscreen_layer, data);
4631 func(shell, &shell->panel_layer, data);
4632 func(shell, &shell->background_layer, data);
4633 func(shell, &shell->lock_layer, data);
4634 func(shell, &shell->input_panel_layer, data);
4635
4636 wl_array_for_each(ws, &shell->workspaces.array)
4637 func(shell, &(*ws)->layer, data);
4638}
4639
4640static void
4641shell_output_destroy_move_layer(struct desktop_shell *shell,
4642 struct weston_layer *layer,
4643 void *data)
4644{
4645 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004646 struct weston_view *view;
4647
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004648 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004649 if (view->output != output)
4650 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004651
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004652 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004653 }
4654}
4655
4656static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004657handle_output_destroy(struct wl_listener *listener, void *data)
4658{
4659 struct shell_output *output_listener =
4660 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004661 struct weston_output *output = output_listener->output;
4662 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004663
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004664 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004665
Quentin Glidic561201c2016-08-20 18:19:51 +02004666 wl_list_remove(&output_listener->panel_surface_listener.link);
4667 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004668 wl_list_remove(&output_listener->destroy_listener.link);
4669 wl_list_remove(&output_listener->link);
4670 free(output_listener);
4671}
4672
4673static void
David Fort50d962f2016-06-09 17:55:52 +02004674shell_resize_surface_to_output(struct desktop_shell *shell,
4675 struct weston_surface *surface,
4676 const struct weston_output *output)
4677{
4678 if (!surface)
4679 return;
4680
4681 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4682 surface->resource,
4683 output->width,
4684 output->height);
4685}
4686
4687
4688static void
4689handle_output_resized(struct wl_listener *listener, void *data)
4690{
4691 struct desktop_shell *shell =
4692 container_of(listener, struct desktop_shell, resized_listener);
4693 struct weston_output *output = (struct weston_output *)data;
4694 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4695
4696 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4697 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4698}
4699
4700static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004701create_shell_output(struct desktop_shell *shell,
4702 struct weston_output *output)
4703{
4704 struct shell_output *shell_output;
4705
4706 shell_output = zalloc(sizeof *shell_output);
4707 if (shell_output == NULL)
4708 return;
4709
4710 shell_output->output = output;
4711 shell_output->shell = shell;
4712 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004713 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004714 wl_signal_add(&output->destroy_signal,
4715 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004716 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004717}
4718
4719static void
4720handle_output_create(struct wl_listener *listener, void *data)
4721{
4722 struct desktop_shell *shell =
4723 container_of(listener, struct desktop_shell, output_create_listener);
4724 struct weston_output *output = (struct weston_output *)data;
4725
4726 create_shell_output(shell, output);
4727}
4728
4729static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004730handle_output_move_layer(struct desktop_shell *shell,
4731 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004732{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004733 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004734 struct weston_view *view;
4735 float x, y;
4736
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004737 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004738 if (view->output != output)
4739 continue;
4740
4741 x = view->geometry.x + output->move_x;
4742 y = view->geometry.y + output->move_y;
4743 weston_view_set_position(view, x, y);
4744 }
4745}
4746
4747static void
4748handle_output_move(struct wl_listener *listener, void *data)
4749{
4750 struct desktop_shell *shell;
4751
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004752 shell = container_of(listener, struct desktop_shell,
4753 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004754
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004755 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004756}
4757
4758static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004759setup_output_destroy_handler(struct weston_compositor *ec,
4760 struct desktop_shell *shell)
4761{
4762 struct weston_output *output;
4763
4764 wl_list_init(&shell->output_list);
4765 wl_list_for_each(output, &ec->output_list, link)
4766 create_shell_output(shell, output);
4767
4768 shell->output_create_listener.notify = handle_output_create;
4769 wl_signal_add(&ec->output_created_signal,
4770 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004771
4772 shell->output_move_listener.notify = handle_output_move;
4773 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004774}
4775
4776static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004777shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004778{
Tiago Vignattibe143262012-04-16 17:31:41 +03004779 struct desktop_shell *shell =
4780 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004781 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004782 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004783
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004784 /* Force state to unlocked so we don't try to fade */
4785 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004786
4787 if (shell->child.client) {
4788 /* disable respawn */
4789 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004790 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004791 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004792
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004793 wl_list_remove(&shell->idle_listener.link);
4794 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004795 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004796
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004797 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004798 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004799
Xiong Zhang6b481422013-10-23 13:58:32 +08004800 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4801 wl_list_remove(&shell_output->destroy_listener.link);
4802 wl_list_remove(&shell_output->link);
4803 free(shell_output);
4804 }
4805
4806 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004807 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004808 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004809
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004810 wl_array_for_each(ws, &shell->workspaces.array)
4811 workspace_destroy(*ws);
4812 wl_array_release(&shell->workspaces.array);
4813
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004814 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004815 free(shell);
4816}
4817
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004818static void
4819shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4820{
4821 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004822 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004823
Bob Ham744e6532016-01-12 10:21:48 +00004824 if (shell->allow_zap)
4825 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4826 MODIFIER_CTRL | MODIFIER_ALT,
4827 terminate_binding, ec);
4828
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004829 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004830 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4831 click_to_activate_binding,
4832 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004833 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4834 click_to_activate_binding,
4835 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004836 weston_compositor_add_touch_binding(ec, 0,
4837 touch_to_activate_binding,
4838 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004839 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4840 MODIFIER_SUPER | MODIFIER_ALT,
4841 surface_opacity_binding, NULL);
4842 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4843 MODIFIER_SUPER, zoom_axis_binding,
4844 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004845 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4846 backlight_binding, ec);
4847 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4848 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004849
4850 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004851 if (shell->exposay_modifier)
4852 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4853 exposay_binding, shell);
4854
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004855 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004856 if (!mod)
4857 return;
4858
Daniel Stone325fc2d2012-05-30 16:31:58 +01004859 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4860 zoom_key_binding, NULL);
4861 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4862 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004863 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4864 maximize_binding, NULL);
4865 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4866 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004867 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4868 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004869 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004870 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004871 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004872 weston_compositor_add_button_binding(ec, BTN_LEFT,
4873 mod | MODIFIER_SHIFT,
4874 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004875
4876 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004877 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004878 rotate_binding, NULL);
4879
Daniel Stone325fc2d2012-05-30 16:31:58 +01004880 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4881 shell);
4882 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4883 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004884 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4885 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004886 weston_compositor_add_key_binding(ec, KEY_K, mod,
4887 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004888 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4889 workspace_up_binding, shell);
4890 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4891 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004892 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4893 workspace_move_surface_up_binding,
4894 shell);
4895 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4896 workspace_move_surface_down_binding,
4897 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004898
4899 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4900 if (shell->workspaces.num > 1) {
4901 num_workspace_bindings = shell->workspaces.num;
4902 if (num_workspace_bindings > 6)
4903 num_workspace_bindings = 6;
4904 for (i = 0; i < num_workspace_bindings; i++)
4905 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4906 workspace_f_binding,
4907 shell);
4908 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004909
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004910 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004911}
4912
Jason Ekstrand024177c2014-04-21 19:42:58 -05004913static void
4914handle_seat_created(struct wl_listener *listener, void *data)
4915{
4916 struct weston_seat *seat = data;
4917
4918 create_shell_seat(seat);
4919}
4920
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004922wet_shell_init(struct weston_compositor *ec,
4923 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004924{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004925 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004926 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004927 struct workspace **pws;
4928 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004929 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004930
Peter Huttererf3d62272013-08-08 11:57:05 +10004931 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004932 if (shell == NULL)
4933 return -1;
4934
Kristian Høgsberg75840622011-09-06 13:48:16 -04004935 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004936
4937 shell->destroy_listener.notify = shell_destroy;
4938 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004939 shell->idle_listener.notify = idle_handler;
4940 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4941 shell->wake_listener.notify = wake_handler;
4942 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004943 shell->transform_listener.notify = transform_handler;
4944 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004945
Quentin Glidic82681572016-12-17 13:40:51 +01004946 weston_layer_init(&shell->fullscreen_layer, ec);
4947 weston_layer_init(&shell->panel_layer, ec);
4948 weston_layer_init(&shell->background_layer, ec);
4949 weston_layer_init(&shell->lock_layer, ec);
4950 weston_layer_init(&shell->input_panel_layer, ec);
4951
4952 weston_layer_set_position(&shell->fullscreen_layer,
4953 WESTON_LAYER_POSITION_FULLSCREEN);
4954 weston_layer_set_position(&shell->panel_layer,
4955 WESTON_LAYER_POSITION_UI);
4956 weston_layer_set_position(&shell->background_layer,
4957 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004958
4959 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004960 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004961
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004962 if (input_panel_setup(shell) < 0)
4963 return -1;
4964
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004965 shell->text_backend = text_backend_init(ec);
4966 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004967 return -1;
4968
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004969 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004970
Daniel Stonedf8133b2013-11-19 11:37:14 +01004971 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4972 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4973
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004974 for (i = 0; i < shell->workspaces.num; i++) {
4975 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4976 if (pws == NULL)
4977 return -1;
4978
Quentin Glidic82681572016-12-17 13:40:51 +01004979 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004980 if (*pws == NULL)
4981 return -1;
4982 }
4983 activate_workspace(shell, 0);
4984
Quentin Glidic82681572016-12-17 13:40:51 +01004985 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004986
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004987 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004988 wl_list_init(&shell->workspaces.animation.link);
4989 shell->workspaces.animation.frame = animate_workspace_change_frame;
4990
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004991 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4992 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004993 return -1;
4994
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004995 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004996 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004997 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004998 return -1;
4999
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005000 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005001
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005002 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005003
Xiong Zhang6b481422013-10-23 13:58:32 +08005004 setup_output_destroy_handler(ec, shell);
5005
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005006 loop = wl_display_get_event_loop(ec->wl_display);
5007 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005008
Jason Ekstrand024177c2014-04-21 19:42:58 -05005009 wl_list_for_each(seat, &ec->seat_list, link)
5010 handle_seat_created(NULL, seat);
5011 shell->seat_create_listener.notify = handle_seat_created;
5012 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005013
David Fort50d962f2016-06-09 17:55:52 +02005014 shell->resized_listener.notify = handle_output_resized;
5015 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5016
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005017 screenshooter_create(ec);
5018
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005019 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005020
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005021 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005022
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005023 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5024
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005025 return 0;
5026}