blob: 1f99efe3ff18fd6df5c834e1bd6684b03c2a7e3a [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
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700205static void
206shell_fade(struct desktop_shell *shell, enum fade_type type);
207
Philip Withnallbecb77e2013-11-25 18:01:30 +0000208static struct shell_seat *
209get_shell_seat(struct weston_seat *seat);
210
Jonny Lambd73c6942014-08-20 15:53:20 +0200211static void
212get_output_panel_size(struct desktop_shell *shell,
213 struct weston_output *output,
214 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700215
Philip Withnall648a4dd2013-11-25 18:01:44 +0000216static void
217shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
218
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219static int
220shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
221{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200223 struct weston_desktop_surface *desktop_surface =
224 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300225
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200226 t = weston_desktop_surface_get_title(desktop_surface);
227 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300228
229 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200230 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300231 t ? " '" : "", t ?: "", t ? "'" : "",
232 c ? " of " : "", c ?: "");
233}
234
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500235static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400236destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237{
238 struct shell_grab *grab;
239
240 grab = container_of(listener, struct shell_grab,
241 shsurf_destroy_listener);
242
243 grab->shsurf = NULL;
244}
245
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800246struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500247get_default_view(struct weston_surface *surface)
248{
249 struct shell_surface *shsurf;
250 struct weston_view *view;
251
252 if (!surface || wl_list_empty(&surface->views))
253 return NULL;
254
255 shsurf = get_shell_surface(surface);
256 if (shsurf)
257 return shsurf->view;
258
259 wl_list_for_each(view, &surface->views, surface_link)
260 if (weston_view_is_mapped(view))
261 return view;
262
263 return container_of(surface->views.next, struct weston_view, surface_link);
264}
265
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400268 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300269 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400270 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800271 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300273 struct desktop_shell *shell = shsurf->shell;
274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200275 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400276
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277 grab->grab.interface = interface;
278 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500280 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400281 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700283 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400284 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400285 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800286 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400287 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 weston_pointer_set_focus(pointer,
289 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400290 wl_fixed_from_int(0),
291 wl_fixed_from_int(0));
292 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293}
294
Jonny Lambd73c6942014-08-20 15:53:20 +0200295static void
Quentin Glidic581df062016-06-23 18:55:19 +0200296get_panel_size(struct desktop_shell *shell,
297 struct weston_view *view,
298 int *width,
299 int *height)
300{
301 float x1, y1;
302 float x2, y2;
303 weston_view_to_global_float(view, 0, 0, &x1, &y1);
304 weston_view_to_global_float(view,
305 view->surface->width,
306 view->surface->height,
307 &x2, &y2);
308 *width = (int)(x2 - x1);
309 *height = (int)(y2 - y1);
310}
311
312static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200313get_output_panel_size(struct desktop_shell *shell,
314 struct weston_output *output,
315 int *width,
316 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700317{
318 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200319
320 *width = 0;
321 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700322
323 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200324 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700325
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300326 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200327 if (view->surface->output == output) {
328 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200329 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700330 }
331 }
332
Jonny Lambd73c6942014-08-20 15:53:20 +0200333 /* the correct view wasn't found */
334}
335
336static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200337get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200338 struct weston_output *output,
339 pixman_rectangle32_t *area)
340{
341 int32_t panel_width = 0, panel_height = 0;
342
Derek Foremanf814c5d2015-04-15 12:23:54 -0500343 area->x = output->x;
344 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200345
346 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200347 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800348 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500350 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000351 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800352 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200353 area->width = output->width;
354 area->height = output->height - panel_height;
355 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000358 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 area->width = output->width - panel_width;
361 area->height = output->height;
362 break;
363 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700364}
365
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400366static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300367shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700369 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400370 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372
373 if (grab->shsurf->resize_edges) {
374 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400375 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300377
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700378 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379}
380
381static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700382shell_touch_grab_start(struct shell_touch_grab *grab,
383 const struct weston_touch_grab_interface *interface,
384 struct shell_surface *shsurf,
385 struct weston_touch *touch)
386{
387 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800388
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200389 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700390
Rusty Lynch1084da52013-08-15 09:10:08 -0700391 grab->grab.interface = interface;
392 grab->shsurf = shsurf;
393 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
394 wl_signal_add(&shsurf->destroy_signal,
395 &grab->shsurf_destroy_listener);
396
397 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_start_grab(touch, &grab->grab);
401 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500402 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700404}
405
406static void
407shell_touch_grab_end(struct shell_touch_grab *grab)
408{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700409 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700410 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 grab->shsurf->grabbed = 0;
412 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700413
414 weston_touch_end_grab(grab->touch);
415}
416
417static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800419 struct weston_output *output);
420
Daniel Stone496ca172012-05-30 16:31:42 +0100421static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300422get_modifier(char *modifier)
423{
424 if (!modifier)
425 return MODIFIER_SUPER;
426
427 if (!strcmp("ctrl", modifier))
428 return MODIFIER_CTRL;
429 else if (!strcmp("alt", modifier))
430 return MODIFIER_ALT;
431 else if (!strcmp("super", modifier))
432 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000433 else if (!strcmp("none", modifier))
434 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300435 else
436 return MODIFIER_SUPER;
437}
438
Juan Zhaoe10d2792012-04-25 19:09:52 +0800439static enum animation_type
440get_animation_type(char *animation)
441{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800442 if (!animation)
443 return ANIMATION_NONE;
444
Juan Zhaoe10d2792012-04-25 19:09:52 +0800445 if (!strcmp("zoom", animation))
446 return ANIMATION_ZOOM;
447 else if (!strcmp("fade", animation))
448 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100449 else if (!strcmp("dim-layer", animation))
450 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800451 else
452 return ANIMATION_NONE;
453}
454
Alex Wu4539b082012-03-01 12:57:46 +0800455static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400456shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200457{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500459 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300460 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000461 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200462
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300463 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400464 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300465 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
466 WESTON_SHELL_CLIENT);
467 if (ret < 0)
468 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500470 "client", &s, client);
471 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100472 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000473
474 weston_config_section_get_bool(section,
475 "allow-zap", &allow_zap, true);
476 shell->allow_zap = allow_zap;
477
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100478 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479 "binding-modifier", &s, "super");
480 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200481 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800482
483 weston_config_section_get_string(section,
484 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000485 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800486 free(s);
487
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400488 weston_config_section_get_string(section, "animation", &s, "none");
489 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200490 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200491 weston_config_section_get_string(section, "close-animation", &s, "fade");
492 shell->win_close_animation_type = get_animation_type(s);
493 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700494 weston_config_section_get_string(section,
495 "startup-animation", &s, "fade");
496 shell->startup_animation_type = get_animation_type(s);
497 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700498 if (shell->startup_animation_type == ANIMATION_ZOOM)
499 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100500 weston_config_section_get_string(section, "focus-animation", &s, "none");
501 shell->focus_animation_type = get_animation_type(s);
502 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400503 weston_config_section_get_uint(section, "num-workspaces",
504 &shell->workspaces.num,
505 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200506}
507
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800508struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100509get_default_output(struct weston_compositor *compositor)
510{
Armin Krezović10b06182016-06-23 11:59:30 +0200511 if (wl_list_empty(&compositor->output_list))
512 return NULL;
513
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514 return container_of(compositor->output_list.next,
515 struct weston_output, link);
516}
517
Pekka Paalanen8274d902014-08-06 19:36:51 +0300518static int
519focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
520{
521 return snprintf(buf, len, "focus highlight effect for output %s",
522 surface->output->name);
523}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100524
525/* no-op func for checking focus surface */
526static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200527focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100528{
529}
530
531static struct focus_surface *
532get_focus_surface(struct weston_surface *surface)
533{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200534 if (surface->committed == focus_surface_committed)
535 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100536 else
537 return NULL;
538}
539
540static bool
541is_focus_surface (struct weston_surface *es)
542{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200543 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544}
545
546static bool
547is_focus_view (struct weston_view *view)
548{
549 return is_focus_surface (view->surface);
550}
551
552static struct focus_surface *
553create_focus_surface(struct weston_compositor *ec,
554 struct weston_output *output)
555{
556 struct focus_surface *fsurf = NULL;
557 struct weston_surface *surface = NULL;
558
559 fsurf = malloc(sizeof *fsurf);
560 if (!fsurf)
561 return NULL;
562
563 fsurf->surface = weston_surface_create(ec);
564 surface = fsurf->surface;
565 if (surface == NULL) {
566 free(fsurf);
567 return NULL;
568 }
569
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100571 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200573 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300574 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100575
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800576 fsurf->view = weston_view_create(surface);
577 if (fsurf->view == NULL) {
578 weston_surface_destroy(surface);
579 free(fsurf);
580 return NULL;
581 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100582 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200583 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600585 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600586 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100587 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
588 pixman_region32_fini(&surface->opaque);
589 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
590 output->width, output->height);
591 pixman_region32_fini(&surface->input);
592 pixman_region32_init(&surface->input);
593
594 wl_list_init(&fsurf->workspace_transform.link);
595
596 return fsurf;
597}
598
599static void
600focus_surface_destroy(struct focus_surface *fsurf)
601{
602 weston_surface_destroy(fsurf->surface);
603 free(fsurf);
604}
605
606static void
607focus_animation_done(struct weston_view_animation *animation, void *data)
608{
609 struct workspace *ws = data;
610
611 ws->focus_animation = NULL;
612}
613
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200615focus_state_destroy(struct focus_state *state)
616{
617 wl_list_remove(&state->seat_destroy_listener.link);
618 wl_list_remove(&state->surface_destroy_listener.link);
619 free(state);
620}
621
622static void
623focus_state_seat_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
627 seat_destroy_listener);
628
629 wl_list_remove(&state->link);
630 focus_state_destroy(state);
631}
632
633static void
634focus_state_surface_destroy(struct wl_listener *listener, void *data)
635{
636 struct focus_state *state = container_of(listener,
637 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400638 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200639 struct weston_surface *main_surface;
640 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
Pekka Paalanen01388e22013-04-25 13:57:44 +0300643 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
644
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300646 wl_list_for_each(view,
647 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100650 if (is_focus_view(view))
651 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200652 if (!get_shell_surface(view->surface))
653 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200655 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 break;
657 }
658
Pekka Paalanen01388e22013-04-25 13:57:44 +0300659 /* if the focus was a sub-surface, activate its main surface */
660 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200661 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300662
Kristian Høgsberge3778222012-07-31 17:29:30 -0400663 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100664 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200665 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200666 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200668 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669 if (state->ws->focus_animation)
670 weston_view_animation_destroy(state->ws->focus_animation);
671
672 state->ws->focus_animation = weston_fade_run(
673 state->ws->fsurf_front->view,
674 state->ws->fsurf_front->view->alpha, 0.0, 300,
675 focus_animation_done, state->ws);
676 }
677
Kristian Høgsberge3778222012-07-31 17:29:30 -0400678 wl_list_remove(&state->link);
679 focus_state_destroy(state);
680 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200681}
682
683static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200684focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
685 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200686{
Jonas Ådahl04769742012-06-13 00:01:24 +0200687 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200688
689 state = malloc(sizeof *state);
690 if (state == NULL)
691 return NULL;
692
Quentin Glidicfff39812016-08-15 10:56:52 +0200693 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400695 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200696 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200698
699 state->seat_destroy_listener.notify = focus_state_seat_destroy;
700 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400701 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200702 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200704
705 return state;
706}
707
Jonas Ådahl8538b222012-08-29 22:13:03 +0200708static struct focus_state *
709ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
710{
711 struct workspace *ws = get_current_workspace(shell);
712 struct focus_state *state;
713
714 wl_list_for_each(state, &ws->focus_list, link)
715 if (state->seat == seat)
716 break;
717
718 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200719 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200720
721 return state;
722}
723
Jonas Ådahl04769742012-06-13 00:01:24 +0200724static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800725focus_state_set_focus(struct focus_state *state,
726 struct weston_surface *surface)
727{
728 if (state->keyboard_focus) {
729 wl_list_remove(&state->surface_destroy_listener.link);
730 wl_list_init(&state->surface_destroy_listener.link);
731 }
732
733 state->keyboard_focus = surface;
734 if (surface)
735 wl_signal_add(&surface->destroy_signal,
736 &state->surface_destroy_listener);
737}
738
739static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400740restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200741{
742 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400743 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100744 struct wl_list pending_seat_list;
745 struct weston_seat *seat, *next_seat;
746
747 /* Temporarily steal the list of seats so that we can keep
748 * track of the seats we've already processed */
749 wl_list_init(&pending_seat_list);
750 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
751 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200752
753 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500754 struct weston_keyboard *keyboard =
755 weston_seat_get_keyboard(state->seat);
756
Neil Roberts4237f502014-04-09 16:33:31 +0100757 wl_list_remove(&state->seat->link);
758 wl_list_insert(&shell->compositor->seat_list,
759 &state->seat->link);
760
Derek Foreman1281a362015-07-31 16:55:32 -0500761 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800762 continue;
763
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400764 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200765
Derek Foreman1281a362015-07-31 16:55:32 -0500766 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200767 }
Neil Roberts4237f502014-04-09 16:33:31 +0100768
769 /* For any remaining seats that we don't have a focus state
770 * for we'll reset the keyboard focus to NULL */
771 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500772 struct weston_keyboard *keyboard =
773 weston_seat_get_keyboard(seat);
774
Neil Roberts4237f502014-04-09 16:33:31 +0100775 wl_list_insert(&shell->compositor->seat_list, &seat->link);
776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100778 continue;
779
Derek Foreman1281a362015-07-31 16:55:32 -0500780 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100781 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200782}
783
784static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200785replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
786 struct weston_seat *seat)
787{
Derek Foreman1281a362015-07-31 16:55:32 -0500788 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790
791 wl_list_for_each(state, &ws->focus_list, link) {
792 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500793 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794 return;
795 }
796 }
797}
798
799static void
800drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_surface *surface)
802{
803 struct focus_state *state;
804
805 wl_list_for_each(state, &ws->focus_list, link)
806 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800807 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200808}
809
810static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100811animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
812 struct weston_view *from, struct weston_view *to)
813{
814 struct weston_output *output;
815 bool focus_surface_created = false;
816
817 /* FIXME: Only support dim animation using two layers */
818 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
819 return;
820
821 output = get_default_output(shell->compositor);
822 if (ws->fsurf_front == NULL && (from || to)) {
823 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800824 if (ws->fsurf_front == NULL)
825 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100826 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800827
828 ws->fsurf_back = create_focus_surface(shell->compositor, output);
829 if (ws->fsurf_back == NULL) {
830 focus_surface_destroy(ws->fsurf_front);
831 return;
832 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 focus_surface_created = true;
836 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300837 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
838 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 }
840
841 if (ws->focus_animation) {
842 weston_view_animation_destroy(ws->focus_animation);
843 ws->focus_animation = NULL;
844 }
845
846 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300847 weston_layer_entry_insert(&to->layer_link,
848 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300850 weston_layer_entry_insert(&ws->layer.view_list,
851 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100852
853 if (focus_surface_created) {
854 ws->focus_animation = weston_fade_run(
855 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200856 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 focus_animation_done, ws);
858 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300859 weston_layer_entry_insert(&from->layer_link,
860 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 ws->focus_animation = weston_stable_fade_run(
862 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200863 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 focus_animation_done, ws);
865 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300866 weston_layer_entry_insert(&ws->layer.view_list,
867 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 ws->focus_animation = weston_stable_fade_run(
869 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200870 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100871 focus_animation_done, ws);
872 }
873}
874
875static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876workspace_destroy(struct workspace *ws)
877{
Jonas Ådahl04769742012-06-13 00:01:24 +0200878 struct focus_state *state, *next;
879
880 wl_list_for_each_safe(state, next, &ws->focus_list, link)
881 focus_state_destroy(state);
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 if (ws->fsurf_front)
884 focus_surface_destroy(ws->fsurf_front);
885 if (ws->fsurf_back)
886 focus_surface_destroy(ws->fsurf_back);
887
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200888 free(ws);
889}
890
Jonas Ådahl04769742012-06-13 00:01:24 +0200891static void
892seat_destroyed(struct wl_listener *listener, void *data)
893{
894 struct weston_seat *seat = data;
895 struct focus_state *state, *next;
896 struct workspace *ws = container_of(listener,
897 struct workspace,
898 seat_destroyed_listener);
899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link)
901 if (state->seat == seat)
902 wl_list_remove(&state->link);
903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100906workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907{
908 struct workspace *ws = malloc(sizeof *ws);
909 if (ws == NULL)
910 return NULL;
911
Quentin Glidic82681572016-12-17 13:40:51 +0100912 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913
Jonas Ådahl04769742012-06-13 00:01:24 +0200914 wl_list_init(&ws->focus_list);
915 wl_list_init(&ws->seat_destroyed_listener.link);
916 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100917 ws->fsurf_front = NULL;
918 ws->fsurf_back = NULL;
919 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200920
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921 return ws;
922}
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924static int
925workspace_is_empty(struct workspace *ws)
926{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300927 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928}
929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930static struct workspace *
931get_workspace(struct desktop_shell *shell, unsigned int index)
932{
933 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200934 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935 pws += index;
936 return *pws;
937}
938
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800939struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940get_current_workspace(struct desktop_shell *shell)
941{
942 return get_workspace(shell, shell->workspaces.current);
943}
944
945static void
946activate_workspace(struct desktop_shell *shell, unsigned int index)
947{
948 struct workspace *ws;
949
950 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100951 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200952
953 shell->workspaces.current = index;
954}
955
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956static unsigned int
957get_output_height(struct weston_output *output)
958{
959 return abs(output->region.extents.y1 - output->region.extents.y2);
960}
961
962static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 struct weston_transform *transform;
966
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 if (is_focus_view(view)) {
968 struct focus_surface *fsurf = get_focus_surface(view->surface);
969 transform = &fsurf->workspace_transform;
970 } else {
971 struct shell_surface *shsurf = get_shell_surface(view->surface);
972 transform = &shsurf->workspace_transform;
973 }
974
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 weston_matrix_init(&transform->matrix);
980 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983}
984
985static void
986workspace_translate_out(struct workspace *ws, double fraction)
987{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 unsigned int height;
990 double d;
991
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 d = height * fraction;
995
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 }
998}
999
1000static void
1001workspace_translate_in(struct workspace *ws, double fraction)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 unsigned int height;
1005 double d;
1006
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009
1010 if (fraction > 0)
1011 d = -(height - height * fraction);
1012 else
1013 d = height + height * fraction;
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
1017}
1018
1019static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020reverse_workspace_change_animation(struct desktop_shell *shell,
1021 unsigned int index,
1022 struct workspace *from,
1023 struct workspace *to)
1024{
1025 shell->workspaces.current = index;
1026
1027 shell->workspaces.anim_to = to;
1028 shell->workspaces.anim_from = from;
1029 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1030 shell->workspaces.anim_timestamp = 0;
1031
Quentin Glidic82681572016-12-17 13:40:51 +01001032 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1033 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1034
Scott Moreau4272e632012-08-13 09:58:41 -06001035 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036}
1037
1038static void
1039workspace_deactivate_transforms(struct workspace *ws)
1040{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001043
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001044 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045 if (is_focus_view(view)) {
1046 struct focus_surface *fsurf = get_focus_surface(view->surface);
1047 transform = &fsurf->workspace_transform;
1048 } else {
1049 struct shell_surface *shsurf = get_shell_surface(view->surface);
1050 transform = &shsurf->workspace_transform;
1051 }
1052
1053 if (!wl_list_empty(&transform->link)) {
1054 wl_list_remove(&transform->link);
1055 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001056 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058 }
1059}
1060
1061static void
1062finish_workspace_change_animation(struct desktop_shell *shell,
1063 struct workspace *from,
1064 struct workspace *to)
1065{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001066 struct weston_view *view;
1067
Scott Moreau4272e632012-08-13 09:58:41 -06001068 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001070 /* Views that extend past the bottom of the output are still
1071 * visible after the workspace animation ends but before its layer
1072 * is hidden. In that case, we need to damage below those views so
1073 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001074 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001075 weston_view_damage_below(view);
1076
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077 wl_list_remove(&shell->workspaces.animation.link);
1078 workspace_deactivate_transforms(from);
1079 workspace_deactivate_transforms(to);
1080 shell->workspaces.anim_to = NULL;
1081
Quentin Glidic82681572016-12-17 13:40:51 +01001082 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083}
1084
1085static void
1086animate_workspace_change_frame(struct weston_animation *animation,
1087 struct weston_output *output, uint32_t msecs)
1088{
1089 struct desktop_shell *shell =
1090 container_of(animation, struct desktop_shell,
1091 workspaces.animation);
1092 struct workspace *from = shell->workspaces.anim_from;
1093 struct workspace *to = shell->workspaces.anim_to;
1094 uint32_t t;
1095 double x, y;
1096
1097 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1098 finish_workspace_change_animation(shell, from, to);
1099 return;
1100 }
1101
1102 if (shell->workspaces.anim_timestamp == 0) {
1103 if (shell->workspaces.anim_current == 0.0)
1104 shell->workspaces.anim_timestamp = msecs;
1105 else
1106 shell->workspaces.anim_timestamp =
1107 msecs -
1108 /* Invers of movement function 'y' below. */
1109 (asin(1.0 - shell->workspaces.anim_current) *
1110 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1111 M_2_PI);
1112 }
1113
1114 t = msecs - shell->workspaces.anim_timestamp;
1115
1116 /*
1117 * x = [0, π/2]
1118 * y(x) = sin(x)
1119 */
1120 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1121 y = sin(x);
1122
1123 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001124 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125
1126 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1127 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1128 shell->workspaces.anim_current = y;
1129
Scott Moreau4272e632012-08-13 09:58:41 -06001130 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001132 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134}
1135
1136static void
1137animate_workspace_change(struct desktop_shell *shell,
1138 unsigned int index,
1139 struct workspace *from,
1140 struct workspace *to)
1141{
1142 struct weston_output *output;
1143
1144 int dir;
1145
1146 if (index > shell->workspaces.current)
1147 dir = -1;
1148 else
1149 dir = 1;
1150
1151 shell->workspaces.current = index;
1152
1153 shell->workspaces.anim_dir = dir;
1154 shell->workspaces.anim_from = from;
1155 shell->workspaces.anim_to = to;
1156 shell->workspaces.anim_current = 0.0;
1157 shell->workspaces.anim_timestamp = 0;
1158
1159 output = container_of(shell->compositor->output_list.next,
1160 struct weston_output, link);
1161 wl_list_insert(&output->animation_list,
1162 &shell->workspaces.animation.link);
1163
Quentin Glidic82681572016-12-17 13:40:51 +01001164 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1165 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166
1167 workspace_translate_in(to, 0);
1168
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001169 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001170
Scott Moreau4272e632012-08-13 09:58:41 -06001171 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001172}
1173
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175update_workspace(struct desktop_shell *shell, unsigned int index,
1176 struct workspace *from, struct workspace *to)
1177{
1178 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001179 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1180 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181}
1182
1183static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184change_workspace(struct desktop_shell *shell, unsigned int index)
1185{
1186 struct workspace *from;
1187 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001188 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001189
1190 if (index == shell->workspaces.current)
1191 return;
1192
1193 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001194 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001195 return;
1196
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001197 from = get_current_workspace(shell);
1198 to = get_workspace(shell, index);
1199
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200 if (shell->workspaces.anim_from == to &&
1201 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203 reverse_workspace_change_animation(shell, index, from, to);
1204 return;
1205 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001206
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001207 if (shell->workspaces.anim_to != NULL)
1208 finish_workspace_change_animation(shell,
1209 shell->workspaces.anim_from,
1210 shell->workspaces.anim_to);
1211
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001213
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001214 if (shell->focus_animation_type != ANIMATION_NONE) {
1215 wl_list_for_each(state, &from->focus_list, link)
1216 if (state->keyboard_focus)
1217 animate_focus_change(shell, from,
1218 get_default_view(state->keyboard_focus), NULL);
1219
1220 wl_list_for_each(state, &to->focus_list, link)
1221 if (state->keyboard_focus)
1222 animate_focus_change(shell, to,
1223 NULL, get_default_view(state->keyboard_focus));
1224 }
1225
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001226 if (workspace_is_empty(to) && workspace_is_empty(from))
1227 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 else
1229 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001230}
1231
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232static bool
1233workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1234{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001235 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001236 struct wl_list *e;
1237
1238 if (wl_list_empty(list))
1239 return false;
1240
1241 e = list->next;
1242
1243 if (e->next != list)
1244 return false;
1245
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001246 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247}
1248
1249static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001250surface_keyboard_focus_lost(struct weston_surface *surface)
1251{
1252 struct weston_compositor *compositor = surface->compositor;
1253 struct weston_seat *seat;
1254 struct weston_surface *focus;
1255
1256 wl_list_for_each(seat, &compositor->seat_list, link) {
1257 struct weston_keyboard *keyboard =
1258 weston_seat_get_keyboard(seat);
1259
1260 if (!keyboard)
1261 continue;
1262
1263 focus = weston_surface_get_main_surface(keyboard->focus);
1264 if (focus == surface)
1265 weston_keyboard_set_focus(keyboard, NULL);
1266 }
1267}
1268
1269static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001271 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 unsigned int index)
1273{
Derek Foreman1281a362015-07-31 16:55:32 -05001274 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001275 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 struct shell_surface *shsurf;
1278 struct workspace *from;
1279 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001280 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281
Derek Foreman1281a362015-07-31 16:55:32 -05001282 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 view = get_default_view(surface);
1284 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001285 index == shell->workspaces.current ||
1286 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287 return;
1288
1289 from = get_current_workspace(shell);
1290 to = get_workspace(shell, index);
1291
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001292 weston_layer_entry_remove(&view->layer_link);
1293 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294
Philip Withnall659163d2013-11-25 18:01:36 +00001295 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001296 if (shsurf != NULL)
1297 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001298
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 drop_focus_state(shell, from, surface);
1301
1302 if (shell->workspaces.anim_from == to &&
1303 shell->workspaces.anim_to == from) {
1304 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306 return;
1307 }
1308
1309 if (shell->workspaces.anim_to != NULL)
1310 finish_workspace_change_animation(shell,
1311 shell->workspaces.anim_from,
1312 shell->workspaces.anim_to);
1313
1314 if (workspace_is_empty(from) &&
1315 workspace_has_only(to, surface))
1316 update_workspace(shell, index, from, to);
1317 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001318 if (shsurf != NULL &&
1319 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001320 wl_list_insert(&shell->workspaces.anim_sticky_list,
1321 &shsurf->workspace_transform.link);
1322
1323 animate_workspace_change(shell, index, from, to);
1324 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325
Jonas Ådahl8538b222012-08-29 22:13:03 +02001326 state = ensure_focus_state(shell, seat);
1327 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001328 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001329}
1330
1331static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001332touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001333 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001334{
1335}
1336
1337static void
1338touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1339{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001340 struct weston_touch_move_grab *move =
1341 (struct weston_touch_move_grab *) container_of(
1342 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001343
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001344 if (touch_id == 0)
1345 move->active = 0;
1346
Jonas Ådahl9484b692013-12-02 22:05:03 +01001347 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001348 shell_touch_grab_end(&move->base);
1349 free(move);
1350 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001351}
1352
1353static void
1354touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001355 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001356{
1357 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1358 struct shell_surface *shsurf = move->base.shsurf;
1359 struct weston_surface *es;
1360 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1361 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1362
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001363 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001364 return;
1365
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001366 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001367
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001368 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001369
1370 weston_compositor_schedule_repaint(es->compositor);
1371}
1372
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001373static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001374touch_move_grab_frame(struct weston_touch_grab *grab)
1375{
1376}
1377
1378static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001379touch_move_grab_cancel(struct weston_touch_grab *grab)
1380{
1381 struct weston_touch_move_grab *move =
1382 (struct weston_touch_move_grab *) container_of(
1383 grab, struct shell_touch_grab, grab);
1384
1385 shell_touch_grab_end(&move->base);
1386 free(move);
1387}
1388
Rusty Lynch1084da52013-08-15 09:10:08 -07001389static const struct weston_touch_grab_interface touch_move_grab_interface = {
1390 touch_move_grab_down,
1391 touch_move_grab_up,
1392 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001393 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001395};
1396
1397static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001398surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001399{
1400 struct weston_touch_move_grab *move;
1401
1402 if (!shsurf)
1403 return -1;
1404
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001405 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1406 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001407 return 0;
1408
1409 move = malloc(sizeof *move);
1410 if (!move)
1411 return -1;
1412
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001413 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001415 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001417 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001418
1419 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001420 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001421
1422 return 0;
1423}
1424
1425static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001426noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001427{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001428}
1429
1430static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001431noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001432 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001433{
1434}
1435
1436static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001437noop_grab_axis_source(struct weston_pointer_grab *grab,
1438 uint32_t source)
1439{
1440}
1441
1442static void
1443noop_grab_frame(struct weston_pointer_grab *grab)
1444{
1445}
1446
1447static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001448constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1449{
1450 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001451 struct weston_surface *surface =
1452 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001454 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001456 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001457 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001458
1459 x = wl_fixed_to_int(pointer->x + move->dx);
1460 y = wl_fixed_to_int(pointer->y + move->dy);
1461
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001462 if (shsurf->shell->panel_position ==
1463 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001464 get_output_work_area(shsurf->shell, surface->output, &area);
1465 geometry =
1466 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001467
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001468 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001469 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001470 y = area.y + safety - geometry.height
1471 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001472
1473 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001474 y + geometry.y < area.y)
1475 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001476 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001477
1478 *cx = x;
1479 *cy = y;
1480}
1481
1482static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001483move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001484 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001487 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001488 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001489 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001490 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001491
Jonas Ådahld2510102014-10-05 21:39:14 +02001492 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001493 if (!shsurf)
1494 return;
1495
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001496 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1497
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001498 constrain_position(move, &cx, &cy);
1499
1500 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001502 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503}
1504
1505static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001506move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001507 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001509 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1510 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001512 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513
Daniel Stone4dbadb12012-05-30 16:31:51 +01001514 if (pointer->button_count == 0 &&
1515 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001516 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001517 free(grab);
1518 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001519}
1520
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001521static void
1522move_grab_cancel(struct weston_pointer_grab *grab)
1523{
1524 struct shell_grab *shell_grab =
1525 container_of(grab, struct shell_grab, grab);
1526
1527 shell_grab_end(shell_grab);
1528 free(grab);
1529}
1530
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001531static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001532 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001533 move_grab_motion,
1534 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001535 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001536 noop_grab_axis_source,
1537 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001539};
1540
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001542surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001543 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544{
1545 struct weston_move_grab *move;
1546
1547 if (!shsurf)
1548 return -1;
1549
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001550 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001551 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1552 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553 return 0;
1554
1555 move = malloc(sizeof *move);
1556 if (!move)
1557 return -1;
1558
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001560 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001562 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001563 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564
1565 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001566 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567
1568 return 0;
1569}
1570
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571struct weston_resize_grab {
1572 struct shell_grab base;
1573 uint32_t edges;
1574 int32_t width, height;
1575};
1576
1577static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001578resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001579 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580{
1581 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001582 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001583 struct shell_surface *shsurf = resize->base.shsurf;
1584 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001585 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586 wl_fixed_t from_x, from_y;
1587 wl_fixed_t to_x, to_y;
1588
Jonas Ådahld2510102014-10-05 21:39:14 +02001589 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001590
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001591 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 return;
1593
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 weston_view_from_global_fixed(shsurf->view,
1595 pointer->grab_x, pointer->grab_y,
1596 &from_x, &from_y);
1597 weston_view_from_global_fixed(shsurf->view,
1598 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599
1600 width = resize->width;
1601 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1602 width += wl_fixed_to_int(from_x - to_x);
1603 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1604 width += wl_fixed_to_int(to_x - from_x);
1605 }
1606
1607 height = resize->height;
1608 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1609 height += wl_fixed_to_int(from_y - to_y);
1610 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1611 height += wl_fixed_to_int(to_y - from_y);
1612 }
1613
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001614 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1615 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1616
1617 min_size.width = MAX(1, min_size.width);
1618 min_size.height = MAX(1, min_size.height);
1619
1620 if (width < min_size.width)
1621 width = min_size.width;
1622 else if (max_size.width > 0 && width > max_size.width)
1623 width = max_size.width;
1624 if (height < min_size.height)
1625 height = min_size.height;
1626 else if (max_size.width > 0 && width > max_size.width)
1627 width = max_size.width;
1628 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629}
1630
1631static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 uint32_t time, uint32_t button, uint32_t state_w)
1634{
1635 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001636 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001638 struct weston_desktop_surface *desktop_surface =
1639 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640
1641 if (pointer->button_count == 0 &&
1642 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001643 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644 shell_grab_end(&resize->base);
1645 free(grab);
1646 }
1647}
1648
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001649static void
1650resize_grab_cancel(struct weston_pointer_grab *grab)
1651{
1652 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001653 struct weston_desktop_surface *desktop_surface =
1654 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001655
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001656 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001657 shell_grab_end(&resize->base);
1658 free(grab);
1659}
1660
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001661static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662 noop_grab_focus,
1663 resize_grab_motion,
1664 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001665 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001666 noop_grab_axis_source,
1667 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669};
1670
Giulio Camuffob8366642013-04-25 13:57:46 +03001671/*
1672 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001673 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001674static void
1675surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1676 int32_t *y, int32_t *w, int32_t *h) {
1677 pixman_region32_t region;
1678 pixman_box32_t *box;
1679 struct weston_subsurface *subsurface;
1680
1681 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 surface->width,
1683 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001684
1685 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1686 pixman_region32_union_rect(&region, &region,
1687 subsurface->position.x,
1688 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 subsurface->surface->width,
1690 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001691 }
1692
1693 box = pixman_region32_extents(&region);
1694 if (x)
1695 *x = box->x1;
1696 if (y)
1697 *y = box->y1;
1698 if (w)
1699 *w = box->x2 - box->x1;
1700 if (h)
1701 *h = box->y2 - box->y1;
1702
1703 pixman_region32_fini(&region);
1704}
1705
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706static int
1707surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001708 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
1710 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001711 const unsigned resize_topbottom =
1712 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1713 const unsigned resize_leftright =
1714 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1715 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001716 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001718 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001719 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1720 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721 return 0;
1722
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001723 /* Check for invalid edge combinations. */
1724 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1725 (edges & resize_topbottom) == resize_topbottom ||
1726 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 return 0;
1728
1729 resize = malloc(sizeof *resize);
1730 if (!resize)
1731 return -1;
1732
1733 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001735 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1736 resize->width = geometry.width;
1737 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001738
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001739 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001740 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001742 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743
1744 return 0;
1745}
1746
1747static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001748busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001750 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001751 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001752 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001754 wl_fixed_t sx, sy;
1755
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 view = weston_compositor_pick_view(pointer->seat->compositor,
1757 pointer->x, pointer->y,
1758 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001759 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001760
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001761 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001762 shell_grab_end(grab);
1763 free(grab);
1764 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765}
1766
1767static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001768busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001769 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770{
Jonas Ådahld2510102014-10-05 21:39:14 +02001771 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001773
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001775busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001776 uint32_t time, uint32_t button, uint32_t state)
1777{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001778 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001779 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001780 struct weston_pointer *pointer = grab->grab.pointer;
1781 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001782
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001783 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001784 activate(shsurf->shell, shsurf->view, seat,
1785 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001786 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001787 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001788 activate(shsurf->shell, shsurf->view, seat,
1789 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001790 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001791 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
1793
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001794static void
1795busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1796{
1797 struct shell_grab *grab = (struct shell_grab *) base;
1798
1799 shell_grab_end(grab);
1800 free(grab);
1801}
1802
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001803static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 busy_cursor_grab_focus,
1805 busy_cursor_grab_motion,
1806 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001807 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001808 noop_grab_axis_source,
1809 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001810 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811};
1812
1813static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814handle_pointer_focus(struct wl_listener *listener, void *data)
1815{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001816 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001818 struct shell_surface *shsurf;
1819 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822 return;
1823
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001824 shsurf = get_shell_surface(view->surface);
1825 if (!shsurf)
1826 return;
1827
1828 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1829
1830 if (shsurf->unresponsive)
1831 set_busy_cursor(shsurf, pointer);
1832 else
1833 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834}
1835
1836static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001837shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1838{
1839 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001840 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001841}
1842
1843static void
1844shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1845{
1846 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001847 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001848}
1849
1850static void
1851handle_keyboard_focus(struct wl_listener *listener, void *data)
1852{
1853 struct weston_keyboard *keyboard = data;
1854 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1855
1856 if (seat->focused_surface) {
1857 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1858 if (shsurf)
1859 shell_surface_lose_keyboard_focus(shsurf);
1860 }
1861
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001862 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001863
1864 if (seat->focused_surface) {
1865 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1866 if (shsurf)
1867 shell_surface_gain_keyboard_focus(shsurf);
1868 }
1869}
1870
Philip Withnall07926d92013-11-25 18:01:40 +00001871/* The surface will be inserted into the list immediately after the link
1872 * returned by this function (i.e. will be stacked immediately above the
1873 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001874static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001875shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1876{
1877 struct workspace *ws;
1878
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001879 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1880 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001881 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001882 }
1883
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001884 /* Move the surface to a normal workspace layer so that surfaces
1885 * which were previously fullscreen or transient are no longer
1886 * rendered on top. */
1887 ws = get_current_workspace(shsurf->shell);
1888 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001889}
1890
Philip Withnall648a4dd2013-11-25 18:01:44 +00001891static void
1892shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1893{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001894 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001895}
1896
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001897/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001898 * geometry to ensure the changes are redrawn.
1899 *
1900 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1901 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001902static void
1903shell_surface_update_layer(struct shell_surface *shsurf)
1904{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001905 struct weston_surface *surface =
1906 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001907 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001908
1909 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1910
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001911 if (new_layer_link == NULL)
1912 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001913 if (new_layer_link == &shsurf->view->layer_link)
1914 return;
1915
1916 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001917 weston_layer_entry_remove(&shsurf->view->layer_link);
1918 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001919 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001920 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001921
1922 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001923}
1924
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001925static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001926shell_surface_set_output(struct shell_surface *shsurf,
1927 struct weston_output *output)
1928{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001929 struct weston_surface *es =
1930 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001931
1932 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001933 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001934 if (output)
1935 shsurf->output = output;
1936 else if (es->output)
1937 shsurf->output = es->output;
1938 else
1939 shsurf->output = get_default_output(es->compositor);
1940}
1941
1942static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001943weston_view_set_initial_position(struct weston_view *view,
1944 struct desktop_shell *shell);
1945
1946static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001947unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001948{
Philip Withnallf85fe842013-11-25 18:01:37 +00001949 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001950 wl_list_remove(&shsurf->fullscreen.transform.link);
1951 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001952
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953 if (shsurf->fullscreen.black_view)
1954 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1955 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001956
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001957 if (shsurf->saved_position_valid)
1958 weston_view_set_position(shsurf->view,
1959 shsurf->saved_x, shsurf->saved_y);
1960 else
1961 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001962 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001963
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001964 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001966 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001967 shsurf->saved_rotation_valid = false;
1968 }
Alex Wu4539b082012-03-01 12:57:46 +08001969}
1970
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001971static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001972unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001973{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001974 struct weston_surface *surface =
1975 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1976
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001977 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001978 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001979
1980 if (shsurf->saved_position_valid)
1981 weston_view_set_position(shsurf->view,
1982 shsurf->saved_x, shsurf->saved_y);
1983 else
1984 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001985 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001986
1987 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 wl_list_insert(&shsurf->view->geometry.transformation_list,
1989 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001990 shsurf->saved_rotation_valid = false;
1991 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001992}
1993
Philip Withnallbecb77e2013-11-25 18:01:30 +00001994static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001995set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001996{
1997 struct shell_surface *shsurf;
1998 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001999 struct weston_view *view;
2000
2001 view = get_default_view(surface);
2002 if (!view)
2003 return;
2004
2005 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2006
2007 shsurf = get_shell_surface(surface);
2008 current_ws = get_current_workspace(shsurf->shell);
2009
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002010 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002011 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002012
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002013 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002014 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002015
2016 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002017 weston_view_damage_below(view);
2018}
2019
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002020
Tiago Vignattibe143262012-04-16 17:31:41 +03002021static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002022shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002023{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002024 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002025}
2026
Pekka Paalanen8274d902014-08-06 19:36:51 +03002027static int
2028black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2029{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002030 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002031 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002032 int n;
2033 int rem;
2034 int ret;
2035
2036 n = snprintf(buf, len, "black background surface for ");
2037 if (n < 0)
2038 return n;
2039
2040 rem = (int)len - n;
2041 if (rem < 0)
2042 rem = 0;
2043
2044 if (fs_surface->get_label)
2045 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2046 else
2047 ret = snprintf(buf + n, rem, "<unknown>");
2048
2049 if (ret < 0)
2050 return n;
2051
2052 return n + ret;
2053}
2054
Alex Wu21858432012-04-01 20:13:08 +08002055static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002056black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002059create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002060 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002061 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002062{
2063 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002065
2066 surface = weston_surface_create(ec);
2067 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002068 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002069 return NULL;
2070 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 view = weston_view_create(surface);
2072 if (surface == NULL) {
2073 weston_log("no memory\n");
2074 weston_surface_destroy(surface);
2075 return NULL;
2076 }
Alex Wu4539b082012-03-01 12:57:46 +08002077
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002078 surface->committed = black_surface_committed;
2079 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002080 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002081 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002082 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002083 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002084 pixman_region32_fini(&surface->input);
2085 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002086
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002087 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002088 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089
2090 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002091}
2092
Philip Withnalled8f1a92013-11-25 18:01:43 +00002093static void
2094shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2095{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002096 struct weston_surface *surface =
2097 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002098 struct weston_output *output = shsurf->fullscreen_output;
2099
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002100 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002101
2102 if (!shsurf->fullscreen.black_view)
2103 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002104 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002105 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002106 output->x, output->y,
2107 output->width,
2108 output->height);
2109
2110 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002111 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2112 weston_layer_entry_insert(&shsurf->view->layer_link,
2113 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002114 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002115 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002116
Armin Krezović4663aca2016-06-30 06:04:29 +02002117 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002118 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002119}
2120
Alex Wu4539b082012-03-01 12:57:46 +08002121/* Create black surface and append it to the associated fullscreen surface.
2122 * Handle size dismatch and positioning according to the method. */
2123static void
2124shell_configure_fullscreen(struct shell_surface *shsurf)
2125{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002126 struct weston_surface *surface =
2127 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002128 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002129
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002130 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002131 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002132 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2133 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002134
Philip Withnalled8f1a92013-11-25 18:01:43 +00002135 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002136
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002137 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002138 &surf_width, &surf_height);
2139
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002140 if (surface->buffer_ref.buffer)
2141 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002142}
2143
Alex Wu4539b082012-03-01 12:57:46 +08002144static void
2145shell_map_fullscreen(struct shell_surface *shsurf)
2146{
Alex Wubd3354b2012-04-17 17:20:49 +08002147 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002148}
2149
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002150static struct weston_output *
2151get_focused_output(struct weston_compositor *compositor)
2152{
2153 struct weston_seat *seat;
2154 struct weston_output *output = NULL;
2155
2156 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002157 struct weston_touch *touch = weston_seat_get_touch(seat);
2158 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2159 struct weston_keyboard *keyboard =
2160 weston_seat_get_keyboard(seat);
2161
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002162 /* Priority has touch focus, then pointer and
2163 * then keyboard focus. We should probably have
2164 * three for loops and check frist for touch,
2165 * then for pointer, etc. but unless somebody has some
2166 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002167 if (touch && touch->focus)
2168 output = touch->focus->output;
2169 else if (pointer && pointer->focus)
2170 output = pointer->focus->output;
2171 else if (keyboard && keyboard->focus)
2172 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002173
2174 if (output)
2175 break;
2176 }
2177
2178 return output;
2179}
2180
2181static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002182destroy_shell_seat(struct wl_listener *listener, void *data)
2183{
2184 struct shell_seat *shseat =
2185 container_of(listener,
2186 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002187
2188 wl_list_remove(&shseat->seat_destroy_listener.link);
2189 free(shseat);
2190}
2191
Jason Ekstrand024177c2014-04-21 19:42:58 -05002192static void
2193shell_seat_caps_changed(struct wl_listener *listener, void *data)
2194{
Derek Foreman1281a362015-07-31 16:55:32 -05002195 struct weston_keyboard *keyboard;
2196 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 struct shell_seat *seat;
2198
2199 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002200 keyboard = weston_seat_get_keyboard(seat->seat);
2201 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002202
Derek Foreman1281a362015-07-31 16:55:32 -05002203 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002204 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002205 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002207 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002208 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002209 wl_list_init(&seat->keyboard_focus_listener.link);
2210 }
2211
Derek Foreman1281a362015-07-31 16:55:32 -05002212 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002213 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002214 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002215 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002216 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002217 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002218 wl_list_init(&seat->pointer_focus_listener.link);
2219 }
2220}
2221
Giulio Camuffo5085a752013-03-25 21:42:45 +01002222static struct shell_seat *
2223create_shell_seat(struct weston_seat *seat)
2224{
2225 struct shell_seat *shseat;
2226
2227 shseat = calloc(1, sizeof *shseat);
2228 if (!shseat) {
2229 weston_log("no memory to allocate shell seat\n");
2230 return NULL;
2231 }
2232
2233 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002234
2235 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2236 wl_signal_add(&seat->destroy_signal,
2237 &shseat->seat_destroy_listener);
2238
Jason Ekstrand024177c2014-04-21 19:42:58 -05002239 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2240 wl_list_init(&shseat->keyboard_focus_listener.link);
2241
2242 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2243 wl_list_init(&shseat->pointer_focus_listener.link);
2244
2245 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2246 wl_signal_add(&seat->updated_caps_signal,
2247 &shseat->caps_changed_listener);
2248 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2249
Giulio Camuffo5085a752013-03-25 21:42:45 +01002250 return shseat;
2251}
2252
2253static struct shell_seat *
2254get_shell_seat(struct weston_seat *seat)
2255{
2256 struct wl_listener *listener;
2257
2258 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002259 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002260
2261 return container_of(listener,
2262 struct shell_seat, seat_destroy_listener);
2263}
2264
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002265static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002266fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002267{
2268 struct shell_surface *shsurf = data;
2269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002270 weston_surface_destroy(shsurf->view->surface);
2271 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002272}
2273
2274static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002275fade_out_done(struct weston_view_animation *animation, void *data)
2276{
2277 struct shell_surface *shsurf = data;
2278 struct wl_event_loop *loop;
2279
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002280 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002281
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002282 if (weston_view_is_mapped(shsurf->view)) {
2283 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002284 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002285 }
2286}
2287
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002288struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002289get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002290{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002291 if (weston_surface_is_desktop_surface(surface)) {
2292 struct weston_desktop_surface *desktop_surface =
2293 weston_surface_get_desktop_surface(surface);
2294 return weston_desktop_surface_get_user_data(desktop_surface);
2295 }
2296 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002297}
2298
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002299/*
2300 * libweston-desktop
2301 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002302
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002303static void
2304desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2305 void *shell)
2306{
2307 struct weston_desktop_client *client =
2308 weston_desktop_surface_get_client(desktop_surface);
2309 struct wl_client *wl_client =
2310 weston_desktop_client_get_client(client);
2311 struct weston_view *view;
2312 struct shell_surface *shsurf;
2313 struct weston_surface *surface =
2314 weston_desktop_surface_get_surface(desktop_surface);
2315
2316 view = weston_desktop_surface_create_view(desktop_surface);
2317 if (!view)
2318 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002319
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002320 shsurf = calloc(1, sizeof *shsurf);
2321 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002322 if (wl_client)
2323 wl_client_post_no_memory(wl_client);
2324 else
2325 weston_log("no memory to allocate shell surface\n");
2326 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002327 }
2328
Pekka Paalanen8274d902014-08-06 19:36:51 +03002329 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002330
Tiago Vignattibc052c92012-04-19 16:18:18 +03002331 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002332 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002333 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002334 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002335 shsurf->desktop_surface = desktop_surface;
2336 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002338 wl_list_init(&shsurf->fullscreen.transform.link);
2339
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002340 shsurf->output = get_default_output(shsurf->shell->compositor);
2341
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002342 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002343
Pekka Paalanen460099f2012-01-20 16:48:25 +02002344 /* empty when not in use */
2345 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002346 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002347
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002348 wl_list_init(&shsurf->workspace_transform.link);
2349
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002350 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2351 weston_desktop_surface_set_activated(desktop_surface,
2352 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002353}
2354
Tiago Vignattibc052c92012-04-19 16:18:18 +03002355static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002356desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2357 void *shell)
2358{
2359 struct shell_surface *shsurf =
2360 weston_desktop_surface_get_user_data(desktop_surface);
2361 struct weston_surface *surface =
2362 weston_desktop_surface_get_surface(desktop_surface);
2363
2364 if (!shsurf)
2365 return;
2366
2367 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2368
2369 if (shsurf->fullscreen.black_view)
2370 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2371
2372 weston_surface_set_label_func(surface, NULL);
2373 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2374 shsurf->desktop_surface = NULL;
2375
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002376 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002377 if (weston_surface_is_mapped(surface) &&
2378 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2379 pixman_region32_fini(&surface->pending.input);
2380 pixman_region32_init(&surface->pending.input);
2381 pixman_region32_fini(&surface->input);
2382 pixman_region32_init(&surface->input);
2383 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2384 fade_out_done, shsurf);
2385 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002386 weston_view_destroy(shsurf->view);
2387 free(shsurf);
2388 }
2389}
2390
2391static void
2392set_maximized_position(struct desktop_shell *shell,
2393 struct shell_surface *shsurf)
2394{
2395 pixman_rectangle32_t area;
2396 struct weston_geometry geometry;
2397
2398 get_output_work_area(shell, shsurf->output, &area);
2399 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2400
2401 weston_view_set_position(shsurf->view,
2402 area.x - geometry.x,
2403 area.y - geometry.y);
2404}
2405
2406static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002407set_position_from_xwayland(struct shell_surface *shsurf)
2408{
2409 struct weston_geometry geometry;
2410 float x;
2411 float y;
2412
2413 assert(shsurf->xwayland.is_set);
2414
2415 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2416 x = shsurf->xwayland.x - geometry.x;
2417 y = shsurf->xwayland.y - geometry.y;
2418
2419 weston_view_set_position(shsurf->view, x, y);
2420
2421#ifdef WM_DEBUG
2422 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2423 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2424 geometry.x, geometry.y, x, y);
2425#endif
2426}
2427
2428static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429map(struct desktop_shell *shell, struct shell_surface *shsurf,
2430 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002431{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002432 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002433 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2434 struct weston_compositor *compositor = shell->compositor;
2435 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002436
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002438 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002439 center_on_output(shsurf->view, shsurf->fullscreen_output);
2440 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002441 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002443 } else if (shsurf->xwayland.is_set) {
2444 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002445 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002446 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002447 }
2448
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449 /* Surface stacking order, see also activate(). */
2450 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002451
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452 weston_view_update_transform(shsurf->view);
2453 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002454 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 surface->output = shsurf->output;
2456 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002457 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002458
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002459 if (!shell->locked) {
2460 wl_list_for_each(seat, &compositor->seat_list, link)
2461 activate(shell, shsurf->view, seat,
2462 WESTON_ACTIVATE_FLAG_CONFIGURE);
2463 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002464
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002465 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002466 switch (shell->win_animation_type) {
2467 case ANIMATION_FADE:
2468 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2469 break;
2470 case ANIMATION_ZOOM:
2471 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2472 break;
2473 case ANIMATION_NONE:
2474 default:
2475 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002476 }
2477 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002478}
2479
2480static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002481desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2482 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002483{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002484 struct shell_surface *shsurf =
2485 weston_desktop_surface_get_user_data(desktop_surface);
2486 struct weston_surface *surface =
2487 weston_desktop_surface_get_surface(desktop_surface);
2488 struct weston_view *view = shsurf->view;
2489 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002490 bool was_fullscreen;
2491 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002492
2493 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002494 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002495
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002496 was_fullscreen = shsurf->state.fullscreen;
2497 was_maximized = shsurf->state.maximized;
2498
2499 shsurf->state.fullscreen =
2500 weston_desktop_surface_get_fullscreen(desktop_surface);
2501 shsurf->state.maximized =
2502 weston_desktop_surface_get_maximized(desktop_surface);
2503
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002504 if (!weston_surface_is_mapped(surface)) {
2505 map(shell, shsurf, sx, sy);
2506 surface->is_mapped = true;
2507 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2508 ++surface->ref_count;
2509 return;
2510 }
2511
2512 if (sx == 0 && sy == 0 &&
2513 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002514 shsurf->last_height == surface->height &&
2515 was_fullscreen == shsurf->state.fullscreen &&
2516 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517 return;
2518
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002519 if (was_fullscreen)
2520 unset_fullscreen(shsurf);
2521 if (was_maximized)
2522 unset_maximized(shsurf);
2523
Quentin Glidic920cf042016-08-16 14:26:20 +02002524 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2525 !shsurf->saved_position_valid) {
2526 shsurf->saved_x = shsurf->view->geometry.x;
2527 shsurf->saved_y = shsurf->view->geometry.y;
2528 shsurf->saved_position_valid = true;
2529
2530 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2531 wl_list_remove(&shsurf->rotation.transform.link);
2532 wl_list_init(&shsurf->rotation.transform.link);
2533 weston_view_geometry_dirty(shsurf->view);
2534 shsurf->saved_rotation_valid = true;
2535 }
2536 }
2537
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002538 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002539 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002540 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002541 set_maximized_position(shell, shsurf);
2542 surface->output = shsurf->output;
2543 } else {
2544 float from_x, from_y;
2545 float to_x, to_y;
2546 float x, y;
2547
2548 if (shsurf->resize_edges) {
2549 sx = 0;
2550 sy = 0;
2551 }
2552
2553 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2554 sx = shsurf->last_width - surface->width;
2555 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2556 sy = shsurf->last_height - surface->height;
2557
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2559 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2560 x = shsurf->view->geometry.x + to_x - from_x;
2561 y = shsurf->view->geometry.y + to_y - from_y;
2562
2563 weston_view_set_position(shsurf->view, x, y);
2564 }
2565
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002566 shsurf->last_width = surface->width;
2567 shsurf->last_height = surface->height;
2568
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002569 /* XXX: would a fullscreen surface need the same handling? */
2570 if (surface->output) {
2571 wl_list_for_each(view, &surface->views, surface_link)
2572 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002573 }
2574}
2575
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002576static void
2577set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2578 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002579{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002580 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2581 struct weston_surface *surface =
2582 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2583 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002584
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 if (fullscreen) {
2586 /* handle clients launching in fullscreen */
2587 if (output == NULL && !weston_surface_is_mapped(surface)) {
2588 /* Set the output to the one that has focus currently. */
2589 output = get_focused_output(surface->compositor);
2590 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002591
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002592 shell_surface_set_output(shsurf, output);
2593 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002594
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002595 width = shsurf->output->width;
2596 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002597 }
2598 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2599 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002600}
2601
2602static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002603desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2604 struct weston_seat *seat, uint32_t serial, void *shell)
2605{
2606 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2607 struct weston_touch *touch = weston_seat_get_touch(seat);
2608 struct shell_surface *shsurf =
2609 weston_desktop_surface_get_user_data(desktop_surface);
2610 struct weston_surface *surface =
2611 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2612 struct wl_resource *resource = surface->resource;
2613 struct weston_surface *focus;
2614
2615 if (pointer &&
2616 pointer->focus &&
2617 pointer->button_count > 0 &&
2618 pointer->grab_serial == serial) {
2619 focus = weston_surface_get_main_surface(pointer->focus->surface);
2620 if ((focus == surface) &&
2621 (surface_move(shsurf, pointer, true) < 0))
2622 wl_resource_post_no_memory(resource);
2623 } else if (touch &&
2624 touch->focus &&
2625 touch->grab_serial == serial) {
2626 focus = weston_surface_get_main_surface(touch->focus->surface);
2627 if ((focus == surface) &&
2628 (surface_touch_move(shsurf, touch) < 0))
2629 wl_resource_post_no_memory(resource);
2630 }
2631}
2632
2633static void
2634desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2635 struct weston_seat *seat, uint32_t serial,
2636 enum weston_desktop_surface_edge edges, void *shell)
2637{
2638 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2639 struct shell_surface *shsurf =
2640 weston_desktop_surface_get_user_data(desktop_surface);
2641 struct weston_surface *surface =
2642 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2643 struct wl_resource *resource = surface->resource;
2644 struct weston_surface *focus;
2645
2646 if (!pointer ||
2647 pointer->button_count == 0 ||
2648 pointer->grab_serial != serial ||
2649 pointer->focus == NULL)
2650 return;
2651
2652 focus = weston_surface_get_main_surface(pointer->focus->surface);
2653 if (focus != surface)
2654 return;
2655
2656 if (surface_resize(shsurf, pointer, edges) < 0)
2657 wl_resource_post_no_memory(resource);
2658}
2659
2660static void
2661desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2662 bool fullscreen,
2663 struct weston_output *output, void *shell)
2664{
2665 struct shell_surface *shsurf =
2666 weston_desktop_surface_get_user_data(desktop_surface);
2667
2668 set_fullscreen(shsurf, fullscreen, output);
2669}
2670
2671static void
2672set_maximized(struct shell_surface *shsurf, bool maximized)
2673{
2674 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2675 struct weston_surface *surface =
2676 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2677 int32_t width = 0, height = 0;
2678
2679 if (maximized) {
2680 struct weston_output *output;
2681 struct desktop_shell *shell;
2682 pixman_rectangle32_t area;
2683
2684 if (!weston_surface_is_mapped(surface))
2685 output = get_focused_output(surface->compositor);
2686 else
2687 output = surface->output;
2688
2689 shell_surface_set_output(shsurf, output);
2690
2691 shell = shell_surface_get_shell(shsurf);
2692 get_output_work_area(shell, shsurf->output, &area);
2693
2694 width = area.width;
2695 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002696 }
2697 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2698 weston_desktop_surface_set_size(desktop_surface, width, height);
2699}
2700
2701static void
2702desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2703 bool maximized, void *shell)
2704{
2705 struct shell_surface *shsurf =
2706 weston_desktop_surface_get_user_data(desktop_surface);
2707
2708 set_maximized(shsurf, maximized);
2709}
2710
2711static void
2712desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2713 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002714{
2715 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002716 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002717
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002718 /* apply compositor's own minimization logic (hide) */
2719 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002720}
2721
Rafael Antognollie2a34552013-12-03 15:35:45 -02002722static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002723set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002724{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002725 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002726
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002727 if (pointer->grab->interface == &busy_cursor_grab_interface)
2728 return;
2729
2730 grab = malloc(sizeof *grab);
2731 if (!grab)
2732 return;
2733
2734 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2735 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2736 /* Mark the shsurf as ungrabbed so that button binding is able
2737 * to move it. */
2738 shsurf->grabbed = 0;
2739}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002740
2741static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002742end_busy_cursor(struct weston_compositor *compositor,
2743 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002744{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002745 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002746 struct shell_grab *grab;
2747 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002748
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002749 wl_list_for_each(seat, &compositor->seat_list, link) {
2750 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2751 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002752
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002753 if (!pointer)
2754 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002755
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002756 if (pointer->grab->interface != &busy_cursor_grab_interface)
2757 continue;
2758
2759 grab = (struct shell_grab *) pointer->grab;
2760 shsurf = grab->shsurf;
2761 if (!shsurf)
2762 continue;
2763
2764 grab_client =
2765 weston_desktop_surface_get_client(shsurf->desktop_surface);
2766 if (grab_client == desktop_client) {
2767 shell_grab_end(grab);
2768 free(grab);
2769 }
2770 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771}
2772
2773static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002774desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2775 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002776{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777 struct shell_surface *shsurf =
2778 weston_desktop_surface_get_user_data(desktop_surface);
2779 bool *unresponsive = user_data;
2780
2781 shsurf->unresponsive = *unresponsive;
2782}
2783
2784static void
2785desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2786 void *shell_)
2787{
2788 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002789 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002790 struct weston_seat *seat;
2791 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002792
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002793 weston_desktop_client_for_each_surface(desktop_client,
2794 desktop_surface_set_unresponsive,
2795 &unresponsive);
2796
2797
2798 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2799 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2800 struct weston_desktop_client *grab_client;
2801
2802 if (!pointer || !pointer->focus)
2803 continue;
2804
2805 shsurf = get_shell_surface(pointer->focus->surface);
2806 if (!shsurf)
2807 continue;
2808
2809 grab_client =
2810 weston_desktop_surface_get_client(shsurf->desktop_surface);
2811 if (grab_client == desktop_client)
2812 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002813 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002814}
2815
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002816static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002817desktop_surface_pong(struct weston_desktop_client *desktop_client,
2818 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002819{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002820 struct desktop_shell *shell = shell_;
2821 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002822
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002823 weston_desktop_client_for_each_surface(desktop_client,
2824 desktop_surface_set_unresponsive,
2825 &unresponsive);
2826 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002827}
2828
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002829static void
2830desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2831 int32_t x, int32_t y, void *shell_)
2832{
2833 struct shell_surface *shsurf =
2834 weston_desktop_surface_get_user_data(surface);
2835
2836 shsurf->xwayland.x = x;
2837 shsurf->xwayland.y = y;
2838 shsurf->xwayland.is_set = true;
2839}
2840
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002841static const struct weston_desktop_api shell_desktop_api = {
2842 .struct_size = sizeof(struct weston_desktop_api),
2843 .surface_added = desktop_surface_added,
2844 .surface_removed = desktop_surface_removed,
2845 .committed = desktop_surface_committed,
2846 .move = desktop_surface_move,
2847 .resize = desktop_surface_resize,
2848 .fullscreen_requested = desktop_surface_fullscreen_requested,
2849 .maximized_requested = desktop_surface_maximized_requested,
2850 .minimized_requested = desktop_surface_minimized_requested,
2851 .ping_timeout = desktop_surface_ping_timeout,
2852 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002853 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002854};
2855
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002856/* ************************ *
2857 * end of libweston-desktop *
2858 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002859static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002860configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002861{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002862 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002863
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002864 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002865 if (v->output == ev->output && v != ev) {
2866 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002867 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002868 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002869 }
2870 }
2871
Quentin Glidice8bf9592016-06-23 18:55:20 +02002872 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002873 ev->surface->is_mapped = true;
2874 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002875
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002876 if (wl_list_empty(&ev->layer_link.link)) {
2877 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002878 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002879 }
2880}
2881
David Fort50d962f2016-06-09 17:55:52 +02002882
2883static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002884find_shell_output_from_weston_output(struct desktop_shell *shell,
2885 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002886{
2887 struct shell_output *shell_output;
2888
2889 wl_list_for_each(shell_output, &shell->output_list, link) {
2890 if (shell_output->output == output)
2891 return shell_output;
2892 }
2893
2894 return NULL;
2895}
2896
Pekka Paalanen8274d902014-08-06 19:36:51 +03002897static int
2898background_get_label(struct weston_surface *surface, char *buf, size_t len)
2899{
2900 return snprintf(buf, len, "background for output %s",
2901 surface->output->name);
2902}
2903
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002904static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002905background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002906{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002907 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002909
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 view = container_of(es->views.next, struct weston_view, surface_link);
2911
Quentin Glidice8bf9592016-06-23 18:55:20 +02002912 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002913}
2914
2915static void
David Fort50d962f2016-06-09 17:55:52 +02002916handle_background_surface_destroy(struct wl_listener *listener, void *data)
2917{
2918 struct shell_output *output =
2919 container_of(listener, struct shell_output, background_surface_listener);
2920
2921 weston_log("background surface gone\n");
2922 output->background_surface = NULL;
2923}
2924
2925static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002926desktop_shell_set_background(struct wl_client *client,
2927 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002928 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929 struct wl_resource *surface_resource)
2930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002931 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002932 struct weston_surface *surface =
2933 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002934 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002936
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002937 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002938 wl_resource_post_error(surface_resource,
2939 WL_DISPLAY_ERROR_INVALID_OBJECT,
2940 "surface role already assigned");
2941 return;
2942 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002943
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2945 weston_view_destroy(view);
2946 view = weston_view_create(surface);
2947
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002948 surface->committed = background_committed;
2949 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002950 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03002951 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002953 weston_desktop_shell_send_configure(resource, 0,
2954 surface_resource,
2955 surface->output->width,
2956 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002957
2958 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2959 sh_output->background_surface = surface;
2960
2961 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2962 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002963}
2964
Pekka Paalanen8274d902014-08-06 19:36:51 +03002965static int
2966panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2967{
2968 return snprintf(buf, len, "panel for output %s",
2969 surface->output->name);
2970}
2971
Kristian Høgsberg75840622011-09-06 13:48:16 -04002972static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002973panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002974{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002975 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002976 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002977 int width, height;
2978 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002979
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 view = container_of(es->views.next, struct weston_view, surface_link);
2981
Quentin Glidice8bf9592016-06-23 18:55:20 +02002982 get_panel_size(shell, view, &width, &height);
2983 switch (shell->panel_position) {
2984 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2985 break;
2986 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2987 y = view->output->height - height;
2988 break;
2989 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2990 break;
2991 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2992 x = view->output->width - width;
2993 break;
2994 }
2995
2996 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002997}
2998
2999static void
David Fort50d962f2016-06-09 17:55:52 +02003000handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3001{
3002 struct shell_output *output =
3003 container_of(listener, struct shell_output, panel_surface_listener);
3004
3005 weston_log("panel surface gone\n");
3006 output->panel_surface = NULL;
3007}
3008
3009
3010static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003011desktop_shell_set_panel(struct wl_client *client,
3012 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003013 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003014 struct wl_resource *surface_resource)
3015{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003016 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003017 struct weston_surface *surface =
3018 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003020 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003021
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003022 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003023 wl_resource_post_error(surface_resource,
3024 WL_DISPLAY_ERROR_INVALID_OBJECT,
3025 "surface role already assigned");
3026 return;
3027 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003028
Jason Ekstranda7af7042013-10-12 22:38:11 -05003029 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3030 weston_view_destroy(view);
3031 view = weston_view_create(surface);
3032
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003033 surface->committed = panel_committed;
3034 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003035 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03003036 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003038 weston_desktop_shell_send_configure(resource, 0,
3039 surface_resource,
3040 surface->output->width,
3041 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003042
3043 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3044 sh_output->panel_surface = surface;
3045
3046 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3047 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003048}
3049
Pekka Paalanen8274d902014-08-06 19:36:51 +03003050static int
3051lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3052{
3053 return snprintf(buf, len, "lock window");
3054}
3055
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003056static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003057lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003058{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003059 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 struct weston_view *view;
3061
3062 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003063
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003064 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003065 return;
3066
Jason Ekstranda7af7042013-10-12 22:38:11 -05003067 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003068
3069 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003070 weston_layer_entry_insert(&shell->lock_layer.view_list,
3071 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003073 surface->is_mapped = true;
3074 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003075 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003076 }
3077}
3078
3079static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003080handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003081{
Tiago Vignattibe143262012-04-16 17:31:41 +03003082 struct desktop_shell *shell =
3083 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003084
Martin Minarik6d118362012-06-07 18:01:59 +02003085 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003086 shell->lock_surface = NULL;
3087}
3088
3089static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003090desktop_shell_set_lock_surface(struct wl_client *client,
3091 struct wl_resource *resource,
3092 struct wl_resource *surface_resource)
3093{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003094 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003095 struct weston_surface *surface =
3096 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003097
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003098 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003099
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003100 if (!shell->locked)
3101 return;
3102
Pekka Paalanen98262232011-12-01 10:42:22 +02003103 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003104
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003105 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003106 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003107 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003108
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003109 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003110 surface->committed = lock_surface_committed;
3111 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003112 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003113}
3114
3115static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003116resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003117{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003118 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003119
Quentin Glidic82681572016-12-17 13:40:51 +01003120 weston_layer_unset_position(&shell->lock_layer);
3121
3122 if (shell->showing_input_panels)
3123 weston_layer_set_position(&shell->input_panel_layer,
3124 WESTON_LAYER_POSITION_TOP_UI);
3125 weston_layer_set_position(&shell->fullscreen_layer,
3126 WESTON_LAYER_POSITION_FULLSCREEN);
3127 weston_layer_set_position(&shell->panel_layer,
3128 WESTON_LAYER_POSITION_UI);
3129 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003130
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003131 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003132
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003133 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003134 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003135 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003136}
3137
3138static void
3139desktop_shell_unlock(struct wl_client *client,
3140 struct wl_resource *resource)
3141{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003142 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003143
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003144 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003145
3146 if (shell->locked)
3147 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003148}
3149
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003150static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003151desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003152 struct wl_resource *resource,
3153 struct wl_resource *surface_resource)
3154{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003155 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003156
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003157 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003158 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003159}
3160
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003161static void
3162desktop_shell_desktop_ready(struct wl_client *client,
3163 struct wl_resource *resource)
3164{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003165 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003166
3167 shell_fade_startup(shell);
3168}
3169
Jonny Lamb765760d2014-08-20 15:53:19 +02003170static void
3171desktop_shell_set_panel_position(struct wl_client *client,
3172 struct wl_resource *resource,
3173 uint32_t position)
3174{
3175 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3176
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003177 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3178 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3179 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3180 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003181 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003182 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003183 "bad position argument");
3184 return;
3185 }
3186
3187 shell->panel_position = position;
3188}
3189
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003190static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003191 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003192 desktop_shell_set_panel,
3193 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003194 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003195 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003196 desktop_shell_desktop_ready,
3197 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003198};
3199
3200static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003201move_binding(struct weston_pointer *pointer, uint32_t time,
3202 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003203{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003204 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003205 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003206 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003207
Derek Foreman8ae2db52015-07-15 13:00:36 -05003208 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003209 return;
3210
Derek Foreman8ae2db52015-07-15 13:00:36 -05003211 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003212
Pekka Paalanen01388e22013-04-25 13:57:44 +03003213 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003214 if (surface == NULL)
3215 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003216
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003217 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003218 if (shsurf == NULL ||
3219 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3220 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003221 return;
3222
Derek Foreman794fa0e2015-07-15 13:00:38 -05003223 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003224}
3225
3226static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003227maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3228 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003229{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003230 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003231 struct weston_surface *surface;
3232 struct shell_surface *shsurf;
3233
3234 surface = weston_surface_get_main_surface(focus);
3235 if (surface == NULL)
3236 return;
3237
3238 shsurf = get_shell_surface(surface);
3239 if (shsurf == NULL)
3240 return;
3241
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003242 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003243}
3244
3245static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003246fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3247 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003248{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003249 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003250 struct weston_surface *surface;
3251 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003252 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003253
3254 surface = weston_surface_get_main_surface(focus);
3255 if (surface == NULL)
3256 return;
3257
3258 shsurf = get_shell_surface(surface);
3259 if (shsurf == NULL)
3260 return;
3261
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003262 fullscreen =
3263 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003264
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003265 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003266}
3267
3268static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003269touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003270{
Derek Foreman362656b2014-09-04 10:23:05 -05003271 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003272 struct weston_surface *surface;
3273 struct shell_surface *shsurf;
3274
Derek Foreman8ae2db52015-07-15 13:00:36 -05003275 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003276 return;
3277
Derek Foreman8ae2db52015-07-15 13:00:36 -05003278 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003279 surface = weston_surface_get_main_surface(focus);
3280 if (surface == NULL)
3281 return;
3282
3283 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003284 if (shsurf == NULL ||
3285 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3286 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003287 return;
3288
Derek Foremanb7674ae2015-07-15 13:00:37 -05003289 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003290}
3291
3292static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003293resize_binding(struct weston_pointer *pointer, uint32_t time,
3294 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003295{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003296 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003297 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003298 uint32_t edges = 0;
3299 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003300 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003301
Derek Foreman8ae2db52015-07-15 13:00:36 -05003302 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003303 return;
3304
Derek Foreman8ae2db52015-07-15 13:00:36 -05003305 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003306
Pekka Paalanen01388e22013-04-25 13:57:44 +03003307 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003308 if (surface == NULL)
3309 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003310
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003311 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003312 if (shsurf == NULL ||
3313 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3314 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003315 return;
3316
Jason Ekstranda7af7042013-10-12 22:38:11 -05003317 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003318 wl_fixed_to_int(pointer->grab_x),
3319 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003320 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003321
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003323 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003324 else if (x < 2 * surface->width / 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_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003328
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003329 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003330 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003331 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003332 edges |= 0;
3333 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003334 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003335
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003336 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003337}
3338
3339static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003340surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003341 struct weston_pointer_axis_event *event,
3342 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003343{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003344 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003345 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003346 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003347 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003348
Pekka Paalanen01388e22013-04-25 13:57:44 +03003349 /* XXX: broken for windows containing sub-surfaces */
3350 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003351 if (surface == NULL)
3352 return;
3353
3354 shsurf = get_shell_surface(surface);
3355 if (!shsurf)
3356 return;
3357
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003358 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003359
Jason Ekstranda7af7042013-10-12 22:38:11 -05003360 if (shsurf->view->alpha > 1.0)
3361 shsurf->view->alpha = 1.0;
3362 if (shsurf->view->alpha < step)
3363 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003364
Jason Ekstranda7af7042013-10-12 22:38:11 -05003365 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003366 weston_surface_damage(surface);
3367}
3368
3369static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003370do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003371 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003372{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003373 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003374 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003375 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003376 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003377
Derek Foreman1281a362015-07-31 16:55:32 -05003378 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003379 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3380 return;
3381 }
3382
Scott Moreauccbf29d2012-02-22 14:21:41 -07003383 wl_list_for_each(output, &compositor->output_list, link) {
3384 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003385 wl_fixed_to_double(pointer->x),
3386 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003387 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003388 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003389 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003390 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003391 increment = -output->zoom.increment;
3392 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003393 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003394 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003395 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003396 else
3397 increment = 0;
3398
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003399 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003400
Scott Moreaue6603982012-06-11 13:07:51 -06003401 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003402 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003403 else if (output->zoom.level > output->zoom.max_level)
3404 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003405
3406 if (!output->zoom.active) {
3407 if (output->zoom.level <= 0.0)
3408 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003409 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003410 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003411
Scott Moreaue6603982012-06-11 13:07:51 -06003412 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003413
Jason Ekstranda7af7042013-10-12 22:38:11 -05003414 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003415 }
3416 }
3417}
3418
Scott Moreauccbf29d2012-02-22 14:21:41 -07003419static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003420zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003421 struct weston_pointer_axis_event *event,
3422 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003423{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003424 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003425}
3426
3427static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003428zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3429 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003430{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003431 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003432}
3433
3434static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003435terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3436 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003438 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003439
Daniel Stone325fc2d2012-05-30 16:31:58 +01003440 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003441}
3442
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003443static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003444rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003445 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003446{
3447 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003448 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003449 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003450 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003451 struct weston_surface *surface =
3452 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003453 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003454
Jonas Ådahld2510102014-10-05 21:39:14 +02003455 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003456
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003457 if (!shsurf)
3458 return;
3459
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003460 cx = 0.5f * surface->width;
3461 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003462
Daniel Stone37816df2012-05-16 18:45:18 +01003463 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3464 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003465 r = sqrtf(dx * dx + dy * dy);
3466
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003467 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003468 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003469
3470 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003471 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003472 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003473
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003474 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003475 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003476
3477 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003478 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003479 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003480 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003481 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003482
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003483 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003484 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003485 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003486 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003487 wl_list_init(&shsurf->rotation.transform.link);
3488 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003489 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003490 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003491
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003492 /* We need to adjust the position of the surface
3493 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003494 cposx = shsurf->view->geometry.x + cx;
3495 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003496 dposx = rotate->center.x - cposx;
3497 dposy = rotate->center.y - cposy;
3498 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003499 weston_view_set_position(shsurf->view,
3500 shsurf->view->geometry.x + dposx,
3501 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003502 }
3503
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003504 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003505 * lazily applies the damage due to rotation update.
3506 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003507 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003508}
3509
3510static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003511rotate_grab_button(struct weston_pointer_grab *grab,
3512 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003513{
3514 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003515 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003516 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003517 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003518 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003519
Daniel Stone4dbadb12012-05-30 16:31:51 +01003520 if (pointer->button_count == 0 &&
3521 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003522 if (shsurf)
3523 weston_matrix_multiply(&shsurf->rotation.rotation,
3524 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003525 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003526 free(rotate);
3527 }
3528}
3529
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003530static void
3531rotate_grab_cancel(struct weston_pointer_grab *grab)
3532{
3533 struct rotate_grab *rotate =
3534 container_of(grab, struct rotate_grab, base.grab);
3535
3536 shell_grab_end(&rotate->base);
3537 free(rotate);
3538}
3539
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003540static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003541 noop_grab_focus,
3542 rotate_grab_motion,
3543 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003544 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003545 noop_grab_axis_source,
3546 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003547 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548};
3549
3550static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003551surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003553 struct weston_surface *surface =
3554 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003555 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003556 float dx, dy;
3557 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558
Pekka Paalanen460099f2012-01-20 16:48:25 +02003559 rotate = malloc(sizeof *rotate);
3560 if (!rotate)
3561 return;
3562
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003563 weston_view_to_global_float(shsurf->view,
3564 surface->width * 0.5f,
3565 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003566 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003567
Derek Foreman74de4692015-07-15 13:00:39 -05003568 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3569 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003570 r = sqrtf(dx * dx + dy * dy);
3571 if (r > 20.0f) {
3572 struct weston_matrix inverse;
3573
3574 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003575 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003576 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003577
3578 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003579 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003580 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003581 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003582 weston_matrix_init(&rotate->rotation);
3583 }
3584
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003585 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003586 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003587}
3588
3589static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003590rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003591 void *data)
3592{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003593 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003594 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003595 struct shell_surface *surface;
3596
Derek Foreman8ae2db52015-07-15 13:00:36 -05003597 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003598 return;
3599
Derek Foreman8ae2db52015-07-15 13:00:36 -05003600 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003601
Pekka Paalanen01388e22013-04-25 13:57:44 +03003602 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003603 if (base_surface == NULL)
3604 return;
3605
3606 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003607 if (surface == NULL ||
3608 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3609 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003610 return;
3611
Derek Foreman74de4692015-07-15 13:00:39 -05003612 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003613}
3614
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003615/* Move all fullscreen layers down to the current workspace and hide their
3616 * black views. The surfaces' state is set to both fullscreen and lowered,
3617 * and this is reversed when such a surface is re-configured, see
3618 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3619 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003620 * lowering_output = NULL - Lower on all outputs, else only lower on the
3621 * specified output.
3622 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003623 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003624 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003625void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003626lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003627 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003628{
3629 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003631
3632 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003634 &shell->fullscreen_layer.view_list.link,
3635 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003636 struct shell_surface *shsurf = get_shell_surface(view->surface);
3637
3638 if (!shsurf)
3639 continue;
3640
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003641 /* Only lower surfaces which have lowering_output as their fullscreen
3642 * output, unless a NULL output asks for lowering on all outputs.
3643 */
3644 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3645 continue;
3646
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003647 /* We can have a non-fullscreen popup for a fullscreen surface
3648 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003649 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003650 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003651 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3652 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003653 weston_view_damage_below(shsurf->fullscreen.black_view);
3654
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003655 }
3656
3657 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003658 weston_layer_entry_remove(&view->layer_link);
3659 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003660 weston_view_damage_below(view);
3661 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003662
3663 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003664 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003665}
3666
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003667void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003668activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003669 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003670{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003671 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003672 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003673 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003674 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003675 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003676 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003677
Pekka Paalanen01388e22013-04-25 13:57:44 +03003678 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003679 shsurf = get_shell_surface(main_surface);
3680 assert(shsurf);
3681
3682 /* Only demote fullscreen surfaces on the output of activated shsurf.
3683 * Leave fullscreen surfaces on unrelated outputs alone. */
3684 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003685
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003686 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003687
Jonas Ådahl8538b222012-08-29 22:13:03 +02003688 state = ensure_focus_state(shell, seat);
3689 if (state == NULL)
3690 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003691
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003692 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003693 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003694
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003695 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3696 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003697 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003698
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003699 /* Update the surface’s layer. This brings it to the top of the stacking
3700 * order as appropriate. */
3701 shell_surface_update_layer(shsurf);
3702
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003703 if (shell->focus_animation_type != ANIMATION_NONE) {
3704 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003705 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003706 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003707}
3708
Alex Wu21858432012-04-01 20:13:08 +08003709/* no-op func for checking black surface */
3710static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003711black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003712{
3713}
3714
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003715static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003716is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003717{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003718 struct weston_surface *surface = view->surface;
3719
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003720 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003721 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003722 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003723 return true;
3724 }
3725 return false;
3726}
3727
Kristian Høgsberg75840622011-09-06 13:48:16 -04003728static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003729activate_binding(struct weston_seat *seat,
3730 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003731 struct weston_view *focus_view,
3732 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003733{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003734 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003735 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003736
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003737 if (!focus_view)
3738 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003739
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003740 if (is_black_surface_view(focus_view, &main_view))
3741 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003742
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003743 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003744 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003745 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003746
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003747 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003748}
3749
3750static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003751click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3752 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003753{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003754 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003755 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003756 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003757 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003758
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003759 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003760 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003761 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003762}
3763
3764static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003765touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3766 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003767{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003768 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003769 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003770 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003771 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003772
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003773 activate_binding(touch->seat, data, touch->focus,
3774 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003775}
3776
3777static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003778unfocus_all_seats(struct desktop_shell *shell)
3779{
3780 struct weston_seat *seat, *next;
3781
3782 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003783 struct weston_keyboard *keyboard =
3784 weston_seat_get_keyboard(seat);
3785
3786 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003787 continue;
3788
Derek Foreman1281a362015-07-31 16:55:32 -05003789 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003790 }
3791}
3792
3793static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003794lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003795{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003796 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003797
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003798 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003799 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003800 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003801 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003802
3803 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003804
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003805 /* Hide all surfaces by removing the fullscreen, panel and
3806 * toplevel layers. This way nothing else can show or receive
3807 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003808
Quentin Glidic82681572016-12-17 13:40:51 +01003809 weston_layer_unset_position(&shell->panel_layer);
3810 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003811 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003812 weston_layer_unset_position(&shell->input_panel_layer);
3813 weston_layer_unset_position(&ws->layer);
3814
3815 weston_layer_set_position(&shell->lock_layer,
3816 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003817
Derek Foreman004b4a12015-07-20 16:28:13 -05003818 weston_compositor_sleep(shell->compositor);
3819
Neil Robertsb4a91702014-04-09 16:33:32 +01003820 /* Remove the keyboard focus on all seats. This will be
3821 * restored to the workspace's saved state via
3822 * restore_focus_state when the compositor is unlocked */
3823 unfocus_all_seats(shell);
3824
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003825 /* TODO: disable bindings that should not work while locked. */
3826
3827 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003828}
3829
3830static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003831unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003832{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003833 struct wl_resource *shell_resource;
3834
Pekka Paalanend81c2162011-11-16 13:47:34 +02003835 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003836 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003837 return;
3838 }
3839
3840 /* If desktop-shell client has gone away, unlock immediately. */
3841 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003842 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003843 return;
3844 }
3845
3846 if (shell->prepare_event_sent)
3847 return;
3848
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003849 shell_resource = shell->child.desktop_shell;
3850 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003851 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003852}
3853
3854static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003855shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003856{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003857 struct shell_output *shell_output = data;
3858 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003859
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003860 shell_output->fade.animation = NULL;
3861 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003862 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003863 weston_surface_destroy(shell_output->fade.view->surface);
3864 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003865 break;
3866 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003867 lock(shell);
3868 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003869 default:
3870 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003871 }
3872}
3873
Jason Ekstranda7af7042013-10-12 22:38:11 -05003874static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003875shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003876{
3877 struct weston_compositor *compositor = shell->compositor;
3878 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003879 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003880
3881 surface = weston_surface_create(compositor);
3882 if (!surface)
3883 return NULL;
3884
Jason Ekstranda7af7042013-10-12 22:38:11 -05003885 view = weston_view_create(surface);
3886 if (!view) {
3887 weston_surface_destroy(surface);
3888 return NULL;
3889 }
3890
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003891 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3892 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003893 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003894 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3895 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003896 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003897 surface->is_mapped = true;
3898 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003899
Jason Ekstranda7af7042013-10-12 22:38:11 -05003900 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003901}
3902
3903static void
3904shell_fade(struct desktop_shell *shell, enum fade_type type)
3905{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003906 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003907 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003908
3909 switch (type) {
3910 case FADE_IN:
3911 tint = 0.0;
3912 break;
3913 case FADE_OUT:
3914 tint = 1.0;
3915 break;
3916 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003917 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003918 return;
3919 }
3920
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003921 /* Create a separate fade surface for each output */
3922 wl_list_for_each(shell_output, &shell->output_list, link) {
3923 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003924
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003925 if (shell_output->fade.view == NULL) {
3926 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3927 if (!shell_output->fade.view)
3928 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003929
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003930 shell_output->fade.view->alpha = 1.0 - tint;
3931 weston_view_update_transform(shell_output->fade.view);
3932 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003933
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003934 if (shell_output->fade.view->output == NULL) {
3935 /* If the black view gets a NULL output, we lost the
3936 * last output and we'll just cancel the fade. This
3937 * happens when you close the last window under the
3938 * X11 or Wayland backends. */
3939 shell->locked = false;
3940 weston_surface_destroy(shell_output->fade.view->surface);
3941 shell_output->fade.view = NULL;
3942 } else if (shell_output->fade.animation) {
3943 weston_fade_update(shell_output->fade.animation, tint);
3944 } else {
3945 shell_output->fade.animation =
3946 weston_fade_run(shell_output->fade.view,
3947 1.0 - tint, tint, 300.0,
3948 shell_fade_done_for_output, shell_output);
3949 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003950 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003951}
3952
3953static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003954do_shell_fade_startup(void *data)
3955{
3956 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003957 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003958
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003959 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003960 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003961 } else {
3962 weston_log("desktop shell: "
3963 "unexpected fade-in animation type %d\n",
3964 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003965 wl_list_for_each(shell_output, &shell->output_list, link) {
3966 weston_surface_destroy(shell_output->fade.view->surface);
3967 shell_output->fade.view = NULL;
3968 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003969 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003970}
3971
3972static void
3973shell_fade_startup(struct desktop_shell *shell)
3974{
3975 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003976 struct shell_output *shell_output;
3977 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003978
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003979 wl_list_for_each(shell_output, &shell->output_list, link) {
3980 if (!shell_output->fade.startup_timer)
3981 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003982
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003983 wl_event_source_remove(shell_output->fade.startup_timer);
3984 shell_output->fade.startup_timer = NULL;
3985 has_fade = true;
3986 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003987
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003988 if (has_fade) {
3989 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3990 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3991 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003992}
3993
3994static int
3995fade_startup_timeout(void *data)
3996{
3997 struct desktop_shell *shell = data;
3998
3999 shell_fade_startup(shell);
4000 return 0;
4001}
4002
4003static void
4004shell_fade_init(struct desktop_shell *shell)
4005{
4006 /* Make compositor output all black, and wait for the desktop-shell
4007 * client to signal it is ready, then fade in. The timer triggers a
4008 * fade-in, in case the desktop-shell client takes too long.
4009 */
4010
4011 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004012 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004013
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004014 if (shell->startup_animation_type == ANIMATION_NONE)
4015 return;
4016
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004017 wl_list_for_each(shell_output, &shell->output_list, link) {
4018 if (shell_output->fade.view != NULL) {
4019 weston_log("%s: warning: fade surface already exists\n",
4020 __func__);
4021 continue;
4022 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004023
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004024 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4025 if (!shell_output->fade.view)
4026 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004027
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004028 weston_view_update_transform(shell_output->fade.view);
4029 weston_surface_damage(shell_output->fade.view->surface);
4030
4031 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4032 shell_output->fade.startup_timer =
4033 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4034 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4035 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004036}
4037
4038static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004039idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004040{
4041 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004042 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004043
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004044 struct weston_seat *seat;
4045
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004046 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4047 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004048
4049 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004050 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004051}
4052
4053static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004054wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004055{
4056 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004057 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004058
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004059 unlock(shell);
4060}
4061
4062static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004063transform_handler(struct wl_listener *listener, void *data)
4064{
4065 struct weston_surface *surface = data;
4066 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004067 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004068 int x, y;
4069
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004070 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004071 return;
4072
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004073 api = shsurf->shell->xwayland_surface_api;
4074 if (!api) {
4075 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4076 shsurf->shell->xwayland_surface_api = api;
4077 }
4078
4079 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004080 return;
4081
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004082 if (!weston_view_is_mapped(shsurf->view))
4083 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004084
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004085 x = shsurf->view->geometry.x;
4086 y = shsurf->view->geometry.y;
4087
4088 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004089}
4090
4091static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004092center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004093{
Giulio Camuffob8366642013-04-25 13:57:46 +03004094 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004095 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004096
Jason Ekstranda7af7042013-10-12 22:38:11 -05004097 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004098
4099 x = output->x + (output->width - width) / 2 - surf_x / 2;
4100 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004101
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004102 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004103}
4104
4105static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004106weston_view_set_initial_position(struct weston_view *view,
4107 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004108{
4109 struct weston_compositor *compositor = shell->compositor;
4110 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004111 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004112 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004113 struct weston_output *output, *target_output = NULL;
4114 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004115 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004116
4117 /* As a heuristic place the new window on the same output as the
4118 * pointer. Falling back to the output containing 0, 0.
4119 *
4120 * TODO: Do something clever for touch too?
4121 */
4122 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004123 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4124
4125 if (pointer) {
4126 ix = wl_fixed_to_int(pointer->x);
4127 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004128 break;
4129 }
4130 }
4131
4132 wl_list_for_each(output, &compositor->output_list, link) {
4133 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4134 target_output = output;
4135 break;
4136 }
4137 }
4138
4139 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004140 weston_view_set_position(view, 10 + random() % 400,
4141 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004142 return;
4143 }
4144
4145 /* Valid range within output where the surface will still be onscreen.
4146 * If this is negative it means that the surface is bigger than
4147 * output.
4148 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004149 get_output_work_area(shell, target_output, &area);
4150
Derek Foremanf814c5d2015-04-15 12:23:54 -05004151 x = area.x;
4152 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004153 range_x = area.width - view->surface->width;
4154 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004155
Rob Bradford4cb88c72012-08-13 15:18:44 +01004156 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004157 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004158
4159 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004160 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004161
Jason Ekstranda7af7042013-10-12 22:38:11 -05004162 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004163}
4164
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004165static bool
4166check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4167{
4168 struct timespec now;
4169
4170 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4171 return false;
4172
4173 /*
4174 * If the shell helper client dies before the session has been
4175 * up for roughly 30 seconds, better just make Weston shut down,
4176 * because the user likely has no way to interact with the desktop
4177 * anyway.
4178 */
4179 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4180 weston_log("Error: %s apparently cannot run at all.\n",
4181 shell->client);
4182 weston_log_continue(STAMP_SPACE "Quitting...");
4183 wl_display_terminate(shell->compositor->wl_display);
4184
4185 return true;
4186 }
4187
4188 return false;
4189}
4190
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004191static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004192
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004193static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004194respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004195{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004196 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004197
4198 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4199 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004200 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004201 shell->child.deathstamp = time;
4202 shell->child.deathcount = 0;
4203 }
4204
4205 shell->child.deathcount++;
4206 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004207 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004208 return;
4209 }
4210
Pekka Paalanen826dc142014-08-27 12:11:53 +03004211 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004212 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004213}
4214
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004215static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004216desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4217{
4218 struct desktop_shell *shell;
4219
4220 shell = container_of(listener, struct desktop_shell,
4221 child.client_destroy_listener);
4222
Pekka Paalanen826dc142014-08-27 12:11:53 +03004223 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004224 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004225 /*
4226 * unbind_desktop_shell() will reset shell->child.desktop_shell
4227 * before the respawned process has a chance to create a new
4228 * desktop_shell object, because we are being called from the
4229 * wl_client destructor which destroys all wl_resources before
4230 * returning.
4231 */
4232
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004233 if (!check_desktop_shell_crash_too_early(shell))
4234 respawn_desktop_shell_process(shell);
4235
Pekka Paalanen826dc142014-08-27 12:11:53 +03004236 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004237}
4238
4239static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004240launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004241{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004242 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004243
Pekka Paalanen826dc142014-08-27 12:11:53 +03004244 shell->child.client = weston_client_start(shell->compositor,
4245 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004246
Arnaud Vrac42631192014-08-25 20:56:46 +02004247 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004248 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004249 return;
4250 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004251
4252 shell->child.client_destroy_listener.notify =
4253 desktop_shell_client_destroy;
4254 wl_client_add_destroy_listener(shell->child.client,
4255 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004256}
4257
4258static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004259unbind_desktop_shell(struct wl_resource *resource)
4260{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004261 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004262
4263 if (shell->locked)
4264 resume_desktop(shell);
4265
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004266 shell->child.desktop_shell = NULL;
4267 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004268}
4269
4270static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004271bind_desktop_shell(struct wl_client *client,
4272 void *data, uint32_t version, uint32_t id)
4273{
Tiago Vignattibe143262012-04-16 17:31:41 +03004274 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004275 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004276
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004277 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4278 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004279
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004280 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004281 wl_resource_set_implementation(resource,
4282 &desktop_shell_implementation,
4283 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004284 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004285 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004286 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004287
4288 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4289 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004290}
4291
Kristian Høgsberg07045392012-02-19 18:52:44 -05004292struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004293 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004294 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004295 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004296 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004297 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298};
4299
4300static void
4301switcher_next(struct switcher *switcher)
4302{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004303 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004304 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004305 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004306 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004307
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004308 /* temporary re-display minimized surfaces */
4309 struct weston_view *tmp;
4310 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004311 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4312 weston_layer_entry_remove(&view->layer_link);
4313 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004314 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4315 *minimized = view;
4316 }
4317
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004318 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004319 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004320 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004321 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004322 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004323 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004324 next = view;
4325 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 view->alpha = 0.25;
4327 weston_view_geometry_dirty(view);
4328 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004329 }
Alex Wu1659daa2012-04-01 20:13:09 +08004330
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004331 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004332 view->alpha = 0.25;
4333 weston_view_geometry_dirty(view);
4334 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004335 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004336 }
4337
4338 if (next == NULL)
4339 next = first;
4340
Alex Wu07b26062012-03-12 16:06:01 +08004341 if (next == NULL)
4342 return;
4343
Kristian Høgsberg07045392012-02-19 18:52:44 -05004344 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004345 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004346
4347 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004348 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004350
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004351 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004352 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004354}
4355
4356static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004357switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004358{
4359 struct switcher *switcher =
4360 container_of(listener, struct switcher, listener);
4361
4362 switcher_next(switcher);
4363}
4364
4365static void
Daniel Stone351eb612012-05-31 15:27:47 -04004366switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004367{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004369 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004370 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004371
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004372 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004373 if (is_focus_view(view))
4374 continue;
4375
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 view->alpha = 1.0;
4377 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004378 }
4379
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004380 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004381 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004382 keyboard->seat,
4383 WESTON_ACTIVATE_FLAG_CONFIGURE);
4384 }
4385
Kristian Høgsberg07045392012-02-19 18:52:44 -05004386 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004387 weston_keyboard_end_grab(keyboard);
4388 if (keyboard->input_method_resource)
4389 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004390
4391 /* re-hide surfaces that were temporary shown during the switch */
4392 struct weston_view **minimized;
4393 wl_array_for_each(minimized, &switcher->minimized_array) {
4394 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004395 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004396 weston_layer_entry_remove(&(*minimized)->layer_link);
4397 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004398 weston_view_damage_below(*minimized);
4399 }
4400 }
4401 wl_array_release(&switcher->minimized_array);
4402
Kristian Høgsberg07045392012-02-19 18:52:44 -05004403 free(switcher);
4404}
4405
4406static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004407switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004408 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004409{
4410 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004411 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004412
Daniel Stonec9785ea2012-05-30 16:31:52 +01004413 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004414 switcher_next(switcher);
4415}
4416
4417static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004418switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004419 uint32_t mods_depressed, uint32_t mods_latched,
4420 uint32_t mods_locked, uint32_t group)
4421{
4422 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004423 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004424
Daniel Stone351eb612012-05-31 15:27:47 -04004425 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4426 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004427}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004428
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004429static void
4430switcher_cancel(struct weston_keyboard_grab *grab)
4431{
4432 struct switcher *switcher = container_of(grab, struct switcher, grab);
4433
4434 switcher_destroy(switcher);
4435}
4436
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004437static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004438 switcher_key,
4439 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004440 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004441};
4442
4443static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004444switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4445 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004446{
Tiago Vignattibe143262012-04-16 17:31:41 +03004447 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004448 struct switcher *switcher;
4449
4450 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004451 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004452 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004453 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004454 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004455 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004456
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004457 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004458 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004459 weston_keyboard_start_grab(keyboard, &switcher->grab);
4460 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004461 switcher_next(switcher);
4462}
4463
Pekka Paalanen3c647232011-12-22 13:43:43 +02004464static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004465backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4466 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004467{
4468 struct weston_compositor *compositor = data;
4469 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004470 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004471
4472 /* TODO: we're limiting to simple use cases, where we assume just
4473 * control on the primary display. We'd have to extend later if we
4474 * ever get support for setting backlights on random desktop LCD
4475 * panels though */
4476 output = get_default_output(compositor);
4477 if (!output)
4478 return;
4479
4480 if (!output->set_backlight)
4481 return;
4482
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004483 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4484 backlight_new = output->backlight_current - 25;
4485 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4486 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004487
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004488 if (backlight_new < 5)
4489 backlight_new = 5;
4490 if (backlight_new > 255)
4491 backlight_new = 255;
4492
4493 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004494 output->set_backlight(output, output->backlight_current);
4495}
4496
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004497static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004498force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4499 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004500{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004501 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004502 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004503 struct desktop_shell *shell = data;
4504 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004505 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004506
Derek Foreman8ae2db52015-07-15 13:00:36 -05004507 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004508 if (!focus_surface)
4509 return;
4510
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004511 wl_signal_emit(&compositor->kill_signal, focus_surface);
4512
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004513 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004514 wl_client_get_credentials(client, &pid, NULL, NULL);
4515
4516 /* Skip clients that we launched ourselves (the credentials of
4517 * the socketpair is ours) */
4518 if (pid == getpid())
4519 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004520
Daniel Stone325fc2d2012-05-30 16:31:58 +01004521 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004522}
4523
4524static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004525workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004526 uint32_t key, void *data)
4527{
4528 struct desktop_shell *shell = data;
4529 unsigned int new_index = shell->workspaces.current;
4530
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004531 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004532 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004533 if (new_index != 0)
4534 new_index--;
4535
4536 change_workspace(shell, new_index);
4537}
4538
4539static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004540workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004541 uint32_t key, void *data)
4542{
4543 struct desktop_shell *shell = data;
4544 unsigned int new_index = shell->workspaces.current;
4545
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004546 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004547 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004548 if (new_index < shell->workspaces.num - 1)
4549 new_index++;
4550
4551 change_workspace(shell, new_index);
4552}
4553
4554static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004555workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004556 uint32_t key, void *data)
4557{
4558 struct desktop_shell *shell = data;
4559 unsigned int new_index;
4560
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004561 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004562 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004563 new_index = key - KEY_F1;
4564 if (new_index >= shell->workspaces.num)
4565 new_index = shell->workspaces.num - 1;
4566
4567 change_workspace(shell, new_index);
4568}
4569
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004570static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004571workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4572 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004573{
4574 struct desktop_shell *shell = data;
4575 unsigned int new_index = shell->workspaces.current;
4576
4577 if (shell->locked)
4578 return;
4579
4580 if (new_index != 0)
4581 new_index--;
4582
Derek Foreman8ae2db52015-07-15 13:00:36 -05004583 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004584}
4585
4586static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004587workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4588 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004589{
4590 struct desktop_shell *shell = data;
4591 unsigned int new_index = shell->workspaces.current;
4592
4593 if (shell->locked)
4594 return;
4595
4596 if (new_index < shell->workspaces.num - 1)
4597 new_index++;
4598
Derek Foreman8ae2db52015-07-15 13:00:36 -05004599 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004600}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004601
4602static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004603shell_reposition_view_on_output_destroy(struct weston_view *view)
4604{
4605 struct weston_output *output, *first_output;
4606 struct weston_compositor *ec = view->surface->compositor;
4607 struct shell_surface *shsurf;
4608 float x, y;
4609 int visible;
4610
4611 x = view->geometry.x;
4612 y = view->geometry.y;
4613
4614 /* At this point the destroyed output is not in the list anymore.
4615 * If the view is still visible somewhere, we leave where it is,
4616 * otherwise, move it to the first output. */
4617 visible = 0;
4618 wl_list_for_each(output, &ec->output_list, link) {
4619 if (pixman_region32_contains_point(&output->region,
4620 x, y, NULL)) {
4621 visible = 1;
4622 break;
4623 }
4624 }
4625
4626 if (!visible) {
4627 first_output = container_of(ec->output_list.next,
4628 struct weston_output, link);
4629
4630 x = first_output->x + first_output->width / 4;
4631 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004632
4633 weston_view_set_position(view, x, y);
4634 } else {
4635 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004636 }
4637
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004638
4639 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004640 if (!shsurf)
4641 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004642
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004643 shsurf->saved_position_valid = false;
4644 set_maximized(shsurf, false);
4645 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004646}
4647
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004648void
4649shell_for_each_layer(struct desktop_shell *shell,
4650 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004651{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004652 struct workspace **ws;
4653
4654 func(shell, &shell->fullscreen_layer, data);
4655 func(shell, &shell->panel_layer, data);
4656 func(shell, &shell->background_layer, data);
4657 func(shell, &shell->lock_layer, data);
4658 func(shell, &shell->input_panel_layer, data);
4659
4660 wl_array_for_each(ws, &shell->workspaces.array)
4661 func(shell, &(*ws)->layer, data);
4662}
4663
4664static void
4665shell_output_destroy_move_layer(struct desktop_shell *shell,
4666 struct weston_layer *layer,
4667 void *data)
4668{
4669 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004670 struct weston_view *view;
4671
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004672 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004673 if (view->output != output)
4674 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004675
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004676 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004677 }
4678}
4679
4680static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004681handle_output_destroy(struct wl_listener *listener, void *data)
4682{
4683 struct shell_output *output_listener =
4684 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004685 struct weston_output *output = output_listener->output;
4686 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004687
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004688 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004689
Quentin Glidic561201c2016-08-20 18:19:51 +02004690 wl_list_remove(&output_listener->panel_surface_listener.link);
4691 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004692 wl_list_remove(&output_listener->destroy_listener.link);
4693 wl_list_remove(&output_listener->link);
4694 free(output_listener);
4695}
4696
4697static void
David Fort50d962f2016-06-09 17:55:52 +02004698shell_resize_surface_to_output(struct desktop_shell *shell,
4699 struct weston_surface *surface,
4700 const struct weston_output *output)
4701{
4702 if (!surface)
4703 return;
4704
4705 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4706 surface->resource,
4707 output->width,
4708 output->height);
4709}
4710
4711
4712static void
4713handle_output_resized(struct wl_listener *listener, void *data)
4714{
4715 struct desktop_shell *shell =
4716 container_of(listener, struct desktop_shell, resized_listener);
4717 struct weston_output *output = (struct weston_output *)data;
4718 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4719
4720 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4721 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4722}
4723
4724static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004725create_shell_output(struct desktop_shell *shell,
4726 struct weston_output *output)
4727{
4728 struct shell_output *shell_output;
4729
4730 shell_output = zalloc(sizeof *shell_output);
4731 if (shell_output == NULL)
4732 return;
4733
4734 shell_output->output = output;
4735 shell_output->shell = shell;
4736 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004737 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004738 wl_signal_add(&output->destroy_signal,
4739 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004740 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004741}
4742
4743static void
4744handle_output_create(struct wl_listener *listener, void *data)
4745{
4746 struct desktop_shell *shell =
4747 container_of(listener, struct desktop_shell, output_create_listener);
4748 struct weston_output *output = (struct weston_output *)data;
4749
4750 create_shell_output(shell, output);
4751}
4752
4753static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004754handle_output_move_layer(struct desktop_shell *shell,
4755 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004756{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004757 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004758 struct weston_view *view;
4759 float x, y;
4760
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004761 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004762 if (view->output != output)
4763 continue;
4764
4765 x = view->geometry.x + output->move_x;
4766 y = view->geometry.y + output->move_y;
4767 weston_view_set_position(view, x, y);
4768 }
4769}
4770
4771static void
4772handle_output_move(struct wl_listener *listener, void *data)
4773{
4774 struct desktop_shell *shell;
4775
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004776 shell = container_of(listener, struct desktop_shell,
4777 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004778
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004779 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004780}
4781
4782static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004783setup_output_destroy_handler(struct weston_compositor *ec,
4784 struct desktop_shell *shell)
4785{
4786 struct weston_output *output;
4787
4788 wl_list_init(&shell->output_list);
4789 wl_list_for_each(output, &ec->output_list, link)
4790 create_shell_output(shell, output);
4791
4792 shell->output_create_listener.notify = handle_output_create;
4793 wl_signal_add(&ec->output_created_signal,
4794 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004795
4796 shell->output_move_listener.notify = handle_output_move;
4797 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004798}
4799
4800static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004801shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004802{
Tiago Vignattibe143262012-04-16 17:31:41 +03004803 struct desktop_shell *shell =
4804 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004805 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004806 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004807
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004808 /* Force state to unlocked so we don't try to fade */
4809 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004810
4811 if (shell->child.client) {
4812 /* disable respawn */
4813 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004814 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004815 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004816
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004817 wl_list_remove(&shell->idle_listener.link);
4818 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004819 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004820
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004821 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004822 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004823
Xiong Zhang6b481422013-10-23 13:58:32 +08004824 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4825 wl_list_remove(&shell_output->destroy_listener.link);
4826 wl_list_remove(&shell_output->link);
4827 free(shell_output);
4828 }
4829
4830 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004831 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004832 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004833
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004834 wl_array_for_each(ws, &shell->workspaces.array)
4835 workspace_destroy(*ws);
4836 wl_array_release(&shell->workspaces.array);
4837
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004838 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004839 free(shell);
4840}
4841
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004842static void
4843shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4844{
4845 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004846 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004847
Bob Ham744e6532016-01-12 10:21:48 +00004848 if (shell->allow_zap)
4849 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4850 MODIFIER_CTRL | MODIFIER_ALT,
4851 terminate_binding, ec);
4852
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004853 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004854 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4855 click_to_activate_binding,
4856 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004857 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4858 click_to_activate_binding,
4859 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004860 weston_compositor_add_touch_binding(ec, 0,
4861 touch_to_activate_binding,
4862 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004863 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4864 backlight_binding, ec);
4865 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4866 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004867
4868 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004869 if (shell->exposay_modifier)
4870 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4871 exposay_binding, shell);
4872
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004873 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004874 if (!mod)
4875 return;
4876
Ian Ray13404c42017-09-18 15:22:01 +03004877 /* This binding is not configurable, but is only enabled if there is a
4878 * valid binding modifier. */
4879 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4880 MODIFIER_SUPER | MODIFIER_ALT,
4881 surface_opacity_binding, NULL);
4882
Ian Rayab7c0b62017-09-18 15:22:00 +03004883 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4884 mod, zoom_axis_binding,
4885 NULL);
4886
Daniel Stone325fc2d2012-05-30 16:31:58 +01004887 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4888 zoom_key_binding, NULL);
4889 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4890 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004891 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4892 maximize_binding, NULL);
4893 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4894 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004895 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4896 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004897 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004898 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004899 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004900 weston_compositor_add_button_binding(ec, BTN_LEFT,
4901 mod | MODIFIER_SHIFT,
4902 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004903
4904 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004905 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004906 rotate_binding, NULL);
4907
Daniel Stone325fc2d2012-05-30 16:31:58 +01004908 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4909 shell);
4910 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4911 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004912 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4913 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004914 weston_compositor_add_key_binding(ec, KEY_K, mod,
4915 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004916 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4917 workspace_up_binding, shell);
4918 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4919 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004920 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4921 workspace_move_surface_up_binding,
4922 shell);
4923 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4924 workspace_move_surface_down_binding,
4925 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004926
4927 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4928 if (shell->workspaces.num > 1) {
4929 num_workspace_bindings = shell->workspaces.num;
4930 if (num_workspace_bindings > 6)
4931 num_workspace_bindings = 6;
4932 for (i = 0; i < num_workspace_bindings; i++)
4933 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4934 workspace_f_binding,
4935 shell);
4936 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004937
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004938 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004939}
4940
Jason Ekstrand024177c2014-04-21 19:42:58 -05004941static void
4942handle_seat_created(struct wl_listener *listener, void *data)
4943{
4944 struct weston_seat *seat = data;
4945
4946 create_shell_seat(seat);
4947}
4948
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004949WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004950wet_shell_init(struct weston_compositor *ec,
4951 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004952{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004953 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004954 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004955 struct workspace **pws;
4956 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004957 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004958
Peter Huttererf3d62272013-08-08 11:57:05 +10004959 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004960 if (shell == NULL)
4961 return -1;
4962
Kristian Høgsberg75840622011-09-06 13:48:16 -04004963 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004964
4965 shell->destroy_listener.notify = shell_destroy;
4966 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004967 shell->idle_listener.notify = idle_handler;
4968 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4969 shell->wake_listener.notify = wake_handler;
4970 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004971 shell->transform_listener.notify = transform_handler;
4972 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004973
Quentin Glidic82681572016-12-17 13:40:51 +01004974 weston_layer_init(&shell->fullscreen_layer, ec);
4975 weston_layer_init(&shell->panel_layer, ec);
4976 weston_layer_init(&shell->background_layer, ec);
4977 weston_layer_init(&shell->lock_layer, ec);
4978 weston_layer_init(&shell->input_panel_layer, ec);
4979
4980 weston_layer_set_position(&shell->fullscreen_layer,
4981 WESTON_LAYER_POSITION_FULLSCREEN);
4982 weston_layer_set_position(&shell->panel_layer,
4983 WESTON_LAYER_POSITION_UI);
4984 weston_layer_set_position(&shell->background_layer,
4985 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004986
4987 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004988 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004989
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004990 if (input_panel_setup(shell) < 0)
4991 return -1;
4992
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004993 shell->text_backend = text_backend_init(ec);
4994 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004995 return -1;
4996
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004997 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004998
Daniel Stonedf8133b2013-11-19 11:37:14 +01004999 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5000 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5001
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005002 for (i = 0; i < shell->workspaces.num; i++) {
5003 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5004 if (pws == NULL)
5005 return -1;
5006
Quentin Glidic82681572016-12-17 13:40:51 +01005007 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005008 if (*pws == NULL)
5009 return -1;
5010 }
5011 activate_workspace(shell, 0);
5012
Quentin Glidic82681572016-12-17 13:40:51 +01005013 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005014
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005015 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005016 wl_list_init(&shell->workspaces.animation.link);
5017 shell->workspaces.animation.frame = animate_workspace_change_frame;
5018
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005019 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5020 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005021 return -1;
5022
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005023 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005024 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005025 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005026 return -1;
5027
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005028 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005029
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005030 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005031
Xiong Zhang6b481422013-10-23 13:58:32 +08005032 setup_output_destroy_handler(ec, shell);
5033
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005034 loop = wl_display_get_event_loop(ec->wl_display);
5035 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005036
Jason Ekstrand024177c2014-04-21 19:42:58 -05005037 wl_list_for_each(seat, &ec->seat_list, link)
5038 handle_seat_created(NULL, seat);
5039 shell->seat_create_listener.notify = handle_seat_created;
5040 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005041
David Fort50d962f2016-06-09 17:55:52 +02005042 shell->resized_listener.notify = handle_output_resized;
5043 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5044
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005045 screenshooter_create(ec);
5046
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005047 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005048
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005049 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005050
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005051 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5052
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005053 return 0;
5054}