blob: ce4b870477c6572fbe90f070f25ccbc11aa990d0 [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
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500137 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500138
139 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200140};
141
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300142struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400143 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300144 struct shell_surface *shsurf;
145 struct wl_listener shsurf_destroy_listener;
146};
147
Rusty Lynch1084da52013-08-15 09:10:08 -0700148struct shell_touch_grab {
149 struct weston_touch_grab grab;
150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152 struct weston_touch *touch;
153};
154
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300155struct weston_move_grab {
156 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100157 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500158 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159};
160
Rusty Lynch1084da52013-08-15 09:10:08 -0700161struct weston_touch_move_grab {
162 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800163 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700164 wl_fixed_t dx, dy;
165};
166
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300168 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500169 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200170 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200171 float x;
172 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173 } center;
174};
175
Giulio Camuffo5085a752013-03-25 21:42:45 +0100176struct shell_seat {
177 struct weston_seat *seat;
178 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100180
Jason Ekstrand024177c2014-04-21 19:42:58 -0500181 struct wl_listener caps_changed_listener;
182 struct wl_listener pointer_focus_listener;
183 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184};
185
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800186
Alex Wubd3354b2012-04-17 17:20:49 +0800187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500190static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200191set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
192
193static void
Derek Foreman74de4692015-07-15 13:00:39 -0500194surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500195
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300196static void
197shell_fade_startup(struct desktop_shell *shell);
198
Philip Withnallbecb77e2013-11-25 18:01:30 +0000199static struct shell_seat *
200get_shell_seat(struct weston_seat *seat);
201
Jonny Lambd73c6942014-08-20 15:53:20 +0200202static void
203get_output_panel_size(struct desktop_shell *shell,
204 struct weston_output *output,
205 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700206
Philip Withnall648a4dd2013-11-25 18:01:44 +0000207static void
208shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
209
Pekka Paalanen8274d902014-08-06 19:36:51 +0300210static int
211shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
212{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300213 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200214 struct weston_desktop_surface *desktop_surface =
215 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200217 t = weston_desktop_surface_get_title(desktop_surface);
218 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219
220 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200221 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222 t ? " '" : "", t ?: "", t ? "'" : "",
223 c ? " of " : "", c ?: "");
224}
225
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500226static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400227destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228{
229 struct shell_grab *grab;
230
231 grab = container_of(listener, struct shell_grab,
232 shsurf_destroy_listener);
233
234 grab->shsurf = NULL;
235}
236
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800237struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500238get_default_view(struct weston_surface *surface)
239{
240 struct shell_surface *shsurf;
241 struct weston_view *view;
242
243 if (!surface || wl_list_empty(&surface->views))
244 return NULL;
245
246 shsurf = get_shell_surface(surface);
247 if (shsurf)
248 return shsurf->view;
249
250 wl_list_for_each(view, &surface->views, surface_link)
251 if (weston_view_is_mapped(view))
252 return view;
253
254 return container_of(surface->views.next, struct weston_view, surface_link);
255}
256
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300257static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300258shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300260 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800262 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264 struct desktop_shell *shell = shsurf->shell;
265
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200266 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400267
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 grab->grab.interface = interface;
269 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400270 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500271 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400272 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700274 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400275 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400276 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800277 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400278 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 weston_pointer_set_focus(pointer,
280 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400281 wl_fixed_from_int(0),
282 wl_fixed_from_int(0));
283 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284}
285
Jonny Lambd73c6942014-08-20 15:53:20 +0200286static void
Quentin Glidic581df062016-06-23 18:55:19 +0200287get_panel_size(struct desktop_shell *shell,
288 struct weston_view *view,
289 int *width,
290 int *height)
291{
292 float x1, y1;
293 float x2, y2;
294 weston_view_to_global_float(view, 0, 0, &x1, &y1);
295 weston_view_to_global_float(view,
296 view->surface->width,
297 view->surface->height,
298 &x2, &y2);
299 *width = (int)(x2 - x1);
300 *height = (int)(y2 - y1);
301}
302
303static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200304get_output_panel_size(struct desktop_shell *shell,
305 struct weston_output *output,
306 int *width,
307 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700308{
309 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200310
311 *width = 0;
312 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700313
314 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200315 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200318 if (view->surface->output == output) {
319 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200320 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700321 }
322 }
323
Jonny Lambd73c6942014-08-20 15:53:20 +0200324 /* the correct view wasn't found */
325}
326
327static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200328get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200329 struct weston_output *output,
330 pixman_rectangle32_t *area)
331{
332 int32_t panel_width = 0, panel_height = 0;
333
Derek Foremanf814c5d2015-04-15 12:23:54 -0500334 area->x = output->x;
335 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200336
337 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200338 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800339 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200340 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500341 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800342 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200343 area->width = output->width;
344 area->height = output->height - panel_height;
345 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800346 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500347 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800348 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 area->width = output->width - panel_width;
350 area->height = output->height;
351 break;
352 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700353}
354
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400355static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700358 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400359 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700360 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400361
362 if (grab->shsurf->resize_edges) {
363 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400364 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700365 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700367 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368}
369
370static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700371shell_touch_grab_start(struct shell_touch_grab *grab,
372 const struct weston_touch_grab_interface *interface,
373 struct shell_surface *shsurf,
374 struct weston_touch *touch)
375{
376 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800377
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200378 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700379
Rusty Lynch1084da52013-08-15 09:10:08 -0700380 grab->grab.interface = interface;
381 grab->shsurf = shsurf;
382 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
383 wl_signal_add(&shsurf->destroy_signal,
384 &grab->shsurf_destroy_listener);
385
386 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700388
389 weston_touch_start_grab(touch, &grab->grab);
390 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500391 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700393}
394
395static void
396shell_touch_grab_end(struct shell_touch_grab *grab)
397{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700399 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700400 grab->shsurf->grabbed = 0;
401 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700402
403 weston_touch_end_grab(grab->touch);
404}
405
406static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500407center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800408 struct weston_output *output);
409
Daniel Stone496ca172012-05-30 16:31:42 +0100410static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300411get_modifier(char *modifier)
412{
413 if (!modifier)
414 return MODIFIER_SUPER;
415
416 if (!strcmp("ctrl", modifier))
417 return MODIFIER_CTRL;
418 else if (!strcmp("alt", modifier))
419 return MODIFIER_ALT;
420 else if (!strcmp("super", modifier))
421 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000422 else if (!strcmp("none", modifier))
423 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300424 else
425 return MODIFIER_SUPER;
426}
427
Juan Zhaoe10d2792012-04-25 19:09:52 +0800428static enum animation_type
429get_animation_type(char *animation)
430{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800431 if (!animation)
432 return ANIMATION_NONE;
433
Juan Zhaoe10d2792012-04-25 19:09:52 +0800434 if (!strcmp("zoom", animation))
435 return ANIMATION_ZOOM;
436 else if (!strcmp("fade", animation))
437 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100438 else if (!strcmp("dim-layer", animation))
439 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800440 else
441 return ANIMATION_NONE;
442}
443
Alex Wu4539b082012-03-01 12:57:46 +0800444static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400445shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200446{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400447 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500448 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300449 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000450 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200451
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300452 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300454 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
455 WESTON_SHELL_CLIENT);
456 if (ret < 0)
457 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500459 "client", &s, client);
460 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100461 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000462
463 weston_config_section_get_bool(section,
464 "allow-zap", &allow_zap, true);
465 shell->allow_zap = allow_zap;
466
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100467 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400468 "binding-modifier", &s, "super");
469 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200470 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800471
472 weston_config_section_get_string(section,
473 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000474 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800475 free(s);
476
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400477 weston_config_section_get_string(section, "animation", &s, "none");
478 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200479 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200480 weston_config_section_get_string(section, "close-animation", &s, "fade");
481 shell->win_close_animation_type = get_animation_type(s);
482 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700483 weston_config_section_get_string(section,
484 "startup-animation", &s, "fade");
485 shell->startup_animation_type = get_animation_type(s);
486 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700487 if (shell->startup_animation_type == ANIMATION_ZOOM)
488 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489 weston_config_section_get_string(section, "focus-animation", &s, "none");
490 shell->focus_animation_type = get_animation_type(s);
491 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400492 weston_config_section_get_uint(section, "num-workspaces",
493 &shell->workspaces.num,
494 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200495}
496
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800497struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100498get_default_output(struct weston_compositor *compositor)
499{
Armin Krezović10b06182016-06-23 11:59:30 +0200500 if (wl_list_empty(&compositor->output_list))
501 return NULL;
502
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100503 return container_of(compositor->output_list.next,
504 struct weston_output, link);
505}
506
Pekka Paalanen8274d902014-08-06 19:36:51 +0300507static int
508focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
509{
510 return snprintf(buf, len, "focus highlight effect for output %s",
511 surface->output->name);
512}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100513
514/* no-op func for checking focus surface */
515static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200516focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100517{
518}
519
520static struct focus_surface *
521get_focus_surface(struct weston_surface *surface)
522{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200523 if (surface->committed == focus_surface_committed)
524 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525 else
526 return NULL;
527}
528
529static bool
530is_focus_surface (struct weston_surface *es)
531{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200532 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533}
534
535static bool
536is_focus_view (struct weston_view *view)
537{
538 return is_focus_surface (view->surface);
539}
540
541static struct focus_surface *
542create_focus_surface(struct weston_compositor *ec,
543 struct weston_output *output)
544{
545 struct focus_surface *fsurf = NULL;
546 struct weston_surface *surface = NULL;
547
548 fsurf = malloc(sizeof *fsurf);
549 if (!fsurf)
550 return NULL;
551
552 fsurf->surface = weston_surface_create(ec);
553 surface = fsurf->surface;
554 if (surface == NULL) {
555 free(fsurf);
556 return NULL;
557 }
558
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200559 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100560 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200561 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200562 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300563 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100564
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800565 fsurf->view = weston_view_create(surface);
566 if (fsurf->view == NULL) {
567 weston_surface_destroy(surface);
568 free(fsurf);
569 return NULL;
570 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100571 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100573
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600574 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600575 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
577 pixman_region32_fini(&surface->opaque);
578 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
579 output->width, output->height);
580 pixman_region32_fini(&surface->input);
581 pixman_region32_init(&surface->input);
582
583 wl_list_init(&fsurf->workspace_transform.link);
584
585 return fsurf;
586}
587
588static void
589focus_surface_destroy(struct focus_surface *fsurf)
590{
591 weston_surface_destroy(fsurf->surface);
592 free(fsurf);
593}
594
595static void
596focus_animation_done(struct weston_view_animation *animation, void *data)
597{
598 struct workspace *ws = data;
599
600 ws->focus_animation = NULL;
601}
602
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200603static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200604focus_state_destroy(struct focus_state *state)
605{
606 wl_list_remove(&state->seat_destroy_listener.link);
607 wl_list_remove(&state->surface_destroy_listener.link);
608 free(state);
609}
610
611static void
612focus_state_seat_destroy(struct wl_listener *listener, void *data)
613{
614 struct focus_state *state = container_of(listener,
615 struct focus_state,
616 seat_destroy_listener);
617
618 wl_list_remove(&state->link);
619 focus_state_destroy(state);
620}
621
622static void
623focus_state_surface_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400627 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200628 struct weston_surface *main_surface;
629 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200631
Pekka Paalanen01388e22013-04-25 13:57:44 +0300632 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
633
Kristian Høgsberge3778222012-07-31 17:29:30 -0400634 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300635 wl_list_for_each(view,
636 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400638 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100639 if (is_focus_view(view))
640 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200641 if (!get_shell_surface(view->surface))
642 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200644 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 break;
646 }
647
Pekka Paalanen01388e22013-04-25 13:57:44 +0300648 /* if the focus was a sub-surface, activate its main surface */
649 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200650 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300651
Kristian Høgsberge3778222012-07-31 17:29:30 -0400652 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100653 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200654 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200655 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200657 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100658 if (state->ws->focus_animation)
659 weston_view_animation_destroy(state->ws->focus_animation);
660
661 state->ws->focus_animation = weston_fade_run(
662 state->ws->fsurf_front->view,
663 state->ws->fsurf_front->view->alpha, 0.0, 300,
664 focus_animation_done, state->ws);
665 }
666
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 wl_list_remove(&state->link);
668 focus_state_destroy(state);
669 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200670}
671
672static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200673focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
674 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200675{
Jonas Ådahl04769742012-06-13 00:01:24 +0200676 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200677
678 state = malloc(sizeof *state);
679 if (state == NULL)
680 return NULL;
681
Quentin Glidicfff39812016-08-15 10:56:52 +0200682 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400684 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200685 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400686 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200687
688 state->seat_destroy_listener.notify = focus_state_seat_destroy;
689 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400690 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200691 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400692 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200693
694 return state;
695}
696
Jonas Ådahl8538b222012-08-29 22:13:03 +0200697static struct focus_state *
698ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
699{
700 struct workspace *ws = get_current_workspace(shell);
701 struct focus_state *state;
702
703 wl_list_for_each(state, &ws->focus_list, link)
704 if (state->seat == seat)
705 break;
706
707 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200708 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200709
710 return state;
711}
712
Jonas Ådahl04769742012-06-13 00:01:24 +0200713static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800714focus_state_set_focus(struct focus_state *state,
715 struct weston_surface *surface)
716{
717 if (state->keyboard_focus) {
718 wl_list_remove(&state->surface_destroy_listener.link);
719 wl_list_init(&state->surface_destroy_listener.link);
720 }
721
722 state->keyboard_focus = surface;
723 if (surface)
724 wl_signal_add(&surface->destroy_signal,
725 &state->surface_destroy_listener);
726}
727
728static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400729restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200730{
731 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400732 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100733 struct wl_list pending_seat_list;
734 struct weston_seat *seat, *next_seat;
735
736 /* Temporarily steal the list of seats so that we can keep
737 * track of the seats we've already processed */
738 wl_list_init(&pending_seat_list);
739 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
740 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200741
742 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500743 struct weston_keyboard *keyboard =
744 weston_seat_get_keyboard(state->seat);
745
Neil Roberts4237f502014-04-09 16:33:31 +0100746 wl_list_remove(&state->seat->link);
747 wl_list_insert(&shell->compositor->seat_list,
748 &state->seat->link);
749
Derek Foreman1281a362015-07-31 16:55:32 -0500750 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800751 continue;
752
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400753 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200754
Derek Foreman1281a362015-07-31 16:55:32 -0500755 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200756 }
Neil Roberts4237f502014-04-09 16:33:31 +0100757
758 /* For any remaining seats that we don't have a focus state
759 * for we'll reset the keyboard focus to NULL */
760 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500761 struct weston_keyboard *keyboard =
762 weston_seat_get_keyboard(seat);
763
Neil Roberts4237f502014-04-09 16:33:31 +0100764 wl_list_insert(&shell->compositor->seat_list, &seat->link);
765
Derek Foreman1281a362015-07-31 16:55:32 -0500766 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100767 continue;
768
Derek Foreman1281a362015-07-31 16:55:32 -0500769 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100770 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200771}
772
773static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200774replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
775 struct weston_seat *seat)
776{
Derek Foreman1281a362015-07-31 16:55:32 -0500777 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200779
780 wl_list_for_each(state, &ws->focus_list, link) {
781 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500782 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783 return;
784 }
785 }
786}
787
788static void
789drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
790 struct weston_surface *surface)
791{
792 struct focus_state *state;
793
794 wl_list_for_each(state, &ws->focus_list, link)
795 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800796 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200797}
798
799static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_view *from, struct weston_view *to)
802{
803 struct weston_output *output;
804 bool focus_surface_created = false;
805
806 /* FIXME: Only support dim animation using two layers */
807 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
808 return;
809
810 output = get_default_output(shell->compositor);
811 if (ws->fsurf_front == NULL && (from || to)) {
812 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800813 if (ws->fsurf_front == NULL)
814 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100815 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800816
817 ws->fsurf_back = create_focus_surface(shell->compositor, output);
818 if (ws->fsurf_back == NULL) {
819 focus_surface_destroy(ws->fsurf_front);
820 return;
821 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100822 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800823
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100824 focus_surface_created = true;
825 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300826 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
827 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 }
829
830 if (ws->focus_animation) {
831 weston_view_animation_destroy(ws->focus_animation);
832 ws->focus_animation = NULL;
833 }
834
835 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300836 weston_layer_entry_insert(&to->layer_link,
837 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100838 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300839 weston_layer_entry_insert(&ws->layer.view_list,
840 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841
842 if (focus_surface_created) {
843 ws->focus_animation = weston_fade_run(
844 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200845 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 focus_animation_done, ws);
847 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&from->layer_link,
849 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 ws->focus_animation = weston_stable_fade_run(
851 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200852 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 focus_animation_done, ws);
854 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300855 weston_layer_entry_insert(&ws->layer.view_list,
856 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 ws->focus_animation = weston_stable_fade_run(
858 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200859 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 focus_animation_done, ws);
861 }
862}
863
864static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200865workspace_destroy(struct workspace *ws)
866{
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 struct focus_state *state, *next;
868
869 wl_list_for_each_safe(state, next, &ws->focus_list, link)
870 focus_state_destroy(state);
871
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 if (ws->fsurf_front)
873 focus_surface_destroy(ws->fsurf_front);
874 if (ws->fsurf_back)
875 focus_surface_destroy(ws->fsurf_back);
876
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877 free(ws);
878}
879
Jonas Ådahl04769742012-06-13 00:01:24 +0200880static void
881seat_destroyed(struct wl_listener *listener, void *data)
882{
883 struct weston_seat *seat = data;
884 struct focus_state *state, *next;
885 struct workspace *ws = container_of(listener,
886 struct workspace,
887 seat_destroyed_listener);
888
889 wl_list_for_each_safe(state, next, &ws->focus_list, link)
890 if (state->seat == seat)
891 wl_list_remove(&state->link);
892}
893
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200894static struct workspace *
895workspace_create(void)
896{
897 struct workspace *ws = malloc(sizeof *ws);
898 if (ws == NULL)
899 return NULL;
900
901 weston_layer_init(&ws->layer, NULL);
902
Jonas Ådahl04769742012-06-13 00:01:24 +0200903 wl_list_init(&ws->focus_list);
904 wl_list_init(&ws->seat_destroyed_listener.link);
905 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100906 ws->fsurf_front = NULL;
907 ws->fsurf_back = NULL;
908 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200909
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910 return ws;
911}
912
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913static int
914workspace_is_empty(struct workspace *ws)
915{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300916 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917}
918
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919static struct workspace *
920get_workspace(struct desktop_shell *shell, unsigned int index)
921{
922 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200923 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924 pws += index;
925 return *pws;
926}
927
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800928struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929get_current_workspace(struct desktop_shell *shell)
930{
931 return get_workspace(shell, shell->workspaces.current);
932}
933
934static void
935activate_workspace(struct desktop_shell *shell, unsigned int index)
936{
937 struct workspace *ws;
938
939 ws = get_workspace(shell, index);
940 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
941
942 shell->workspaces.current = index;
943}
944
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945static unsigned int
946get_output_height(struct weston_output *output)
947{
948 return abs(output->region.extents.y1 - output->region.extents.y2);
949}
950
951static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954 struct weston_transform *transform;
955
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100956 if (is_focus_view(view)) {
957 struct focus_surface *fsurf = get_focus_surface(view->surface);
958 transform = &fsurf->workspace_transform;
959 } else {
960 struct shell_surface *shsurf = get_shell_surface(view->surface);
961 transform = &shsurf->workspace_transform;
962 }
963
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 weston_matrix_init(&transform->matrix);
969 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972}
973
974static void
975workspace_translate_out(struct workspace *ws, double fraction)
976{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 unsigned int height;
979 double d;
980
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300981 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983 d = height * fraction;
984
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 }
987}
988
989static void
990workspace_translate_in(struct workspace *ws, double fraction)
991{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993 unsigned int height;
994 double d;
995
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300996 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 if (fraction > 0)
1000 d = -(height - height * fraction);
1001 else
1002 d = height + height * fraction;
1003
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 }
1006}
1007
1008static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009reverse_workspace_change_animation(struct desktop_shell *shell,
1010 unsigned int index,
1011 struct workspace *from,
1012 struct workspace *to)
1013{
1014 shell->workspaces.current = index;
1015
1016 shell->workspaces.anim_to = to;
1017 shell->workspaces.anim_from = from;
1018 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1019 shell->workspaces.anim_timestamp = 0;
1020
Scott Moreau4272e632012-08-13 09:58:41 -06001021 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022}
1023
1024static void
1025workspace_deactivate_transforms(struct workspace *ws)
1026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001029
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001030 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 if (is_focus_view(view)) {
1032 struct focus_surface *fsurf = get_focus_surface(view->surface);
1033 transform = &fsurf->workspace_transform;
1034 } else {
1035 struct shell_surface *shsurf = get_shell_surface(view->surface);
1036 transform = &shsurf->workspace_transform;
1037 }
1038
1039 if (!wl_list_empty(&transform->link)) {
1040 wl_list_remove(&transform->link);
1041 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001042 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044 }
1045}
1046
1047static void
1048finish_workspace_change_animation(struct desktop_shell *shell,
1049 struct workspace *from,
1050 struct workspace *to)
1051{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001052 struct weston_view *view;
1053
Scott Moreau4272e632012-08-13 09:58:41 -06001054 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001056 /* Views that extend past the bottom of the output are still
1057 * visible after the workspace animation ends but before its layer
1058 * is hidden. In that case, we need to damage below those views so
1059 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001060 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001061 weston_view_damage_below(view);
1062
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063 wl_list_remove(&shell->workspaces.animation.link);
1064 workspace_deactivate_transforms(from);
1065 workspace_deactivate_transforms(to);
1066 shell->workspaces.anim_to = NULL;
1067
1068 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1069}
1070
1071static void
1072animate_workspace_change_frame(struct weston_animation *animation,
1073 struct weston_output *output, uint32_t msecs)
1074{
1075 struct desktop_shell *shell =
1076 container_of(animation, struct desktop_shell,
1077 workspaces.animation);
1078 struct workspace *from = shell->workspaces.anim_from;
1079 struct workspace *to = shell->workspaces.anim_to;
1080 uint32_t t;
1081 double x, y;
1082
1083 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1084 finish_workspace_change_animation(shell, from, to);
1085 return;
1086 }
1087
1088 if (shell->workspaces.anim_timestamp == 0) {
1089 if (shell->workspaces.anim_current == 0.0)
1090 shell->workspaces.anim_timestamp = msecs;
1091 else
1092 shell->workspaces.anim_timestamp =
1093 msecs -
1094 /* Invers of movement function 'y' below. */
1095 (asin(1.0 - shell->workspaces.anim_current) *
1096 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1097 M_2_PI);
1098 }
1099
1100 t = msecs - shell->workspaces.anim_timestamp;
1101
1102 /*
1103 * x = [0, π/2]
1104 * y(x) = sin(x)
1105 */
1106 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1107 y = sin(x);
1108
1109 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001110 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111
1112 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1113 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1114 shell->workspaces.anim_current = y;
1115
Scott Moreau4272e632012-08-13 09:58:41 -06001116 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001118 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120}
1121
1122static void
1123animate_workspace_change(struct desktop_shell *shell,
1124 unsigned int index,
1125 struct workspace *from,
1126 struct workspace *to)
1127{
1128 struct weston_output *output;
1129
1130 int dir;
1131
1132 if (index > shell->workspaces.current)
1133 dir = -1;
1134 else
1135 dir = 1;
1136
1137 shell->workspaces.current = index;
1138
1139 shell->workspaces.anim_dir = dir;
1140 shell->workspaces.anim_from = from;
1141 shell->workspaces.anim_to = to;
1142 shell->workspaces.anim_current = 0.0;
1143 shell->workspaces.anim_timestamp = 0;
1144
1145 output = container_of(shell->compositor->output_list.next,
1146 struct weston_output, link);
1147 wl_list_insert(&output->animation_list,
1148 &shell->workspaces.animation.link);
1149
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001150 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151
1152 workspace_translate_in(to, 0);
1153
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001154 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001155
Scott Moreau4272e632012-08-13 09:58:41 -06001156 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157}
1158
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001159static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160update_workspace(struct desktop_shell *shell, unsigned int index,
1161 struct workspace *from, struct workspace *to)
1162{
1163 shell->workspaces.current = index;
1164 wl_list_insert(&from->layer.link, &to->layer.link);
1165 wl_list_remove(&from->layer.link);
1166}
1167
1168static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169change_workspace(struct desktop_shell *shell, unsigned int index)
1170{
1171 struct workspace *from;
1172 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001173 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174
1175 if (index == shell->workspaces.current)
1176 return;
1177
1178 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001179 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180 return;
1181
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001182 from = get_current_workspace(shell);
1183 to = get_workspace(shell, index);
1184
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 if (shell->workspaces.anim_from == to &&
1186 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188 reverse_workspace_change_animation(shell, index, from, to);
1189 return;
1190 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 if (shell->workspaces.anim_to != NULL)
1193 finish_workspace_change_animation(shell,
1194 shell->workspaces.anim_from,
1195 shell->workspaces.anim_to);
1196
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001198
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001199 if (shell->focus_animation_type != ANIMATION_NONE) {
1200 wl_list_for_each(state, &from->focus_list, link)
1201 if (state->keyboard_focus)
1202 animate_focus_change(shell, from,
1203 get_default_view(state->keyboard_focus), NULL);
1204
1205 wl_list_for_each(state, &to->focus_list, link)
1206 if (state->keyboard_focus)
1207 animate_focus_change(shell, to,
1208 NULL, get_default_view(state->keyboard_focus));
1209 }
1210
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211 if (workspace_is_empty(to) && workspace_is_empty(from))
1212 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001213 else
1214 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001215}
1216
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217static bool
1218workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1219{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001220 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 struct wl_list *e;
1222
1223 if (wl_list_empty(list))
1224 return false;
1225
1226 e = list->next;
1227
1228 if (e->next != list)
1229 return false;
1230
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001231 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232}
1233
1234static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001235surface_keyboard_focus_lost(struct weston_surface *surface)
1236{
1237 struct weston_compositor *compositor = surface->compositor;
1238 struct weston_seat *seat;
1239 struct weston_surface *focus;
1240
1241 wl_list_for_each(seat, &compositor->seat_list, link) {
1242 struct weston_keyboard *keyboard =
1243 weston_seat_get_keyboard(seat);
1244
1245 if (!keyboard)
1246 continue;
1247
1248 focus = weston_surface_get_main_surface(keyboard->focus);
1249 if (focus == surface)
1250 weston_keyboard_set_focus(keyboard, NULL);
1251 }
1252}
1253
1254static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 unsigned int index)
1258{
Derek Foreman1281a362015-07-31 16:55:32 -05001259 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001260 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262 struct shell_surface *shsurf;
1263 struct workspace *from;
1264 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001265 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266
Derek Foreman1281a362015-07-31 16:55:32 -05001267 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view = get_default_view(surface);
1269 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001270 index == shell->workspaces.current ||
1271 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 return;
1273
1274 from = get_current_workspace(shell);
1275 to = get_workspace(shell, index);
1276
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001277 weston_layer_entry_remove(&view->layer_link);
1278 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279
Philip Withnall659163d2013-11-25 18:01:36 +00001280 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001281 if (shsurf != NULL)
1282 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001283
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001285 drop_focus_state(shell, from, surface);
1286
1287 if (shell->workspaces.anim_from == to &&
1288 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001289 wl_list_remove(&to->layer.link);
1290 wl_list_insert(from->layer.link.prev, &to->layer.link);
1291
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001292 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 return;
1295 }
1296
1297 if (shell->workspaces.anim_to != NULL)
1298 finish_workspace_change_animation(shell,
1299 shell->workspaces.anim_from,
1300 shell->workspaces.anim_to);
1301
1302 if (workspace_is_empty(from) &&
1303 workspace_has_only(to, surface))
1304 update_workspace(shell, index, from, to);
1305 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001306 if (shsurf != NULL &&
1307 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 wl_list_insert(&shell->workspaces.anim_sticky_list,
1309 &shsurf->workspace_transform.link);
1310
1311 animate_workspace_change(shell, index, from, to);
1312 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001313
Jonas Ådahl8538b222012-08-29 22:13:03 +02001314 state = ensure_focus_state(shell, seat);
1315 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001316 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317}
1318
1319static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001320touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001321 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001322{
1323}
1324
1325static void
1326touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1327{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001328 struct weston_touch_move_grab *move =
1329 (struct weston_touch_move_grab *) container_of(
1330 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001331
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001332 if (touch_id == 0)
1333 move->active = 0;
1334
Jonas Ådahl9484b692013-12-02 22:05:03 +01001335 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001336 shell_touch_grab_end(&move->base);
1337 free(move);
1338 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001339}
1340
1341static void
1342touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001343 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001344{
1345 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1346 struct shell_surface *shsurf = move->base.shsurf;
1347 struct weston_surface *es;
1348 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1349 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1350
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001351 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001352 return;
1353
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001354 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001355
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001356 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001357
1358 weston_compositor_schedule_repaint(es->compositor);
1359}
1360
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001361static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001362touch_move_grab_frame(struct weston_touch_grab *grab)
1363{
1364}
1365
1366static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001367touch_move_grab_cancel(struct weston_touch_grab *grab)
1368{
1369 struct weston_touch_move_grab *move =
1370 (struct weston_touch_move_grab *) container_of(
1371 grab, struct shell_touch_grab, grab);
1372
1373 shell_touch_grab_end(&move->base);
1374 free(move);
1375}
1376
Rusty Lynch1084da52013-08-15 09:10:08 -07001377static const struct weston_touch_grab_interface touch_move_grab_interface = {
1378 touch_move_grab_down,
1379 touch_move_grab_up,
1380 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001381 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001383};
1384
1385static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001386surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001387{
1388 struct weston_touch_move_grab *move;
1389
1390 if (!shsurf)
1391 return -1;
1392
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001393 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1394 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001395 return 0;
1396
1397 move = malloc(sizeof *move);
1398 if (!move)
1399 return -1;
1400
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001401 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001403 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001405 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001406
1407 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001408 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001409
1410 return 0;
1411}
1412
1413static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001414noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001415{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001416}
1417
1418static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001419noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001420 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001421{
1422}
1423
1424static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001425noop_grab_axis_source(struct weston_pointer_grab *grab,
1426 uint32_t source)
1427{
1428}
1429
1430static void
1431noop_grab_frame(struct weston_pointer_grab *grab)
1432{
1433}
1434
1435static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001436constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1437{
1438 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001439 struct weston_surface *surface =
1440 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001441 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001442 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001443 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001444 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001445 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001446
1447 x = wl_fixed_to_int(pointer->x + move->dx);
1448 y = wl_fixed_to_int(pointer->y + move->dy);
1449
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001450 if (shsurf->shell->panel_position ==
1451 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001452 get_output_work_area(shsurf->shell, surface->output, &area);
1453 geometry =
1454 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001456 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001457 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001458 y = area.y + safety - geometry.height
1459 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001460
1461 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001462 y + geometry.y < area.y)
1463 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001464 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
1466 *cx = x;
1467 *cy = y;
1468}
1469
1470static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001471move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001472 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001473{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001474 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001475 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001476 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001477 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001478 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479
Jonas Ådahld2510102014-10-05 21:39:14 +02001480 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001481 if (!shsurf)
1482 return;
1483
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1485
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001486 constrain_position(move, &cx, &cy);
1487
1488 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001489
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001491}
1492
1493static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001494move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001495 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001496{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001497 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1498 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001499 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001500 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 if (pointer->button_count == 0 &&
1503 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001504 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001505 free(grab);
1506 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507}
1508
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001509static void
1510move_grab_cancel(struct weston_pointer_grab *grab)
1511{
1512 struct shell_grab *shell_grab =
1513 container_of(grab, struct shell_grab, grab);
1514
1515 shell_grab_end(shell_grab);
1516 free(grab);
1517}
1518
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001520 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001521 move_grab_motion,
1522 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001523 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001524 noop_grab_axis_source,
1525 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001526 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527};
1528
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001529static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001530surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001531 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001532{
1533 struct weston_move_grab *move;
1534
1535 if (!shsurf)
1536 return -1;
1537
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001538 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001539 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1540 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541 return 0;
1542
1543 move = malloc(sizeof *move);
1544 if (!move)
1545 return -1;
1546
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001548 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001550 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001551 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552
1553 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001554 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001555
1556 return 0;
1557}
1558
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559struct weston_resize_grab {
1560 struct shell_grab base;
1561 uint32_t edges;
1562 int32_t width, height;
1563};
1564
1565static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001567 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568{
1569 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001570 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571 struct shell_surface *shsurf = resize->base.shsurf;
1572 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001573 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574 wl_fixed_t from_x, from_y;
1575 wl_fixed_t to_x, to_y;
1576
Jonas Ådahld2510102014-10-05 21:39:14 +02001577 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001578
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001579 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580 return;
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 weston_view_from_global_fixed(shsurf->view,
1583 pointer->grab_x, pointer->grab_y,
1584 &from_x, &from_y);
1585 weston_view_from_global_fixed(shsurf->view,
1586 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587
1588 width = resize->width;
1589 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1590 width += wl_fixed_to_int(from_x - to_x);
1591 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1592 width += wl_fixed_to_int(to_x - from_x);
1593 }
1594
1595 height = resize->height;
1596 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1597 height += wl_fixed_to_int(from_y - to_y);
1598 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1599 height += wl_fixed_to_int(to_y - from_y);
1600 }
1601
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001602 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1603 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1604
1605 min_size.width = MAX(1, min_size.width);
1606 min_size.height = MAX(1, min_size.height);
1607
1608 if (width < min_size.width)
1609 width = min_size.width;
1610 else if (max_size.width > 0 && width > max_size.width)
1611 width = max_size.width;
1612 if (height < min_size.height)
1613 height = min_size.height;
1614 else if (max_size.width > 0 && width > max_size.width)
1615 width = max_size.width;
1616 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617}
1618
1619static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 uint32_t time, uint32_t button, uint32_t state_w)
1622{
1623 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001624 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625 enum wl_pointer_button_state state = state_w;
1626
1627 if (pointer->button_count == 0 &&
1628 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1629 shell_grab_end(&resize->base);
1630 free(grab);
1631 }
1632}
1633
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001634static void
1635resize_grab_cancel(struct weston_pointer_grab *grab)
1636{
1637 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1638
1639 shell_grab_end(&resize->base);
1640 free(grab);
1641}
1642
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001643static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644 noop_grab_focus,
1645 resize_grab_motion,
1646 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001647 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001648 noop_grab_axis_source,
1649 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001650 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651};
1652
Giulio Camuffob8366642013-04-25 13:57:46 +03001653/*
1654 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001655 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001656static void
1657surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1658 int32_t *y, int32_t *w, int32_t *h) {
1659 pixman_region32_t region;
1660 pixman_box32_t *box;
1661 struct weston_subsurface *subsurface;
1662
1663 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 surface->width,
1665 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001666
1667 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1668 pixman_region32_union_rect(&region, &region,
1669 subsurface->position.x,
1670 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 subsurface->surface->width,
1672 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001673 }
1674
1675 box = pixman_region32_extents(&region);
1676 if (x)
1677 *x = box->x1;
1678 if (y)
1679 *y = box->y1;
1680 if (w)
1681 *w = box->x2 - box->x1;
1682 if (h)
1683 *h = box->y2 - box->y1;
1684
1685 pixman_region32_fini(&region);
1686}
1687
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688static int
1689surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001690 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691{
1692 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001693 const unsigned resize_topbottom =
1694 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1695 const unsigned resize_leftright =
1696 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1697 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001698 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001700 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001701 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1702 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 return 0;
1704
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001705 /* Check for invalid edge combinations. */
1706 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1707 (edges & resize_topbottom) == resize_topbottom ||
1708 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709 return 0;
1710
1711 resize = malloc(sizeof *resize);
1712 if (!resize)
1713 return -1;
1714
1715 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001717 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1718 resize->width = geometry.width;
1719 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001720
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001721 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001722 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001723 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724
1725 return 0;
1726}
1727
1728static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001729busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001730{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001731 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001732 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001733 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001735 wl_fixed_t sx, sy;
1736
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 view = weston_compositor_pick_view(pointer->seat->compositor,
1738 pointer->x, pointer->y,
1739 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001740 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001741
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001742 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001743 shell_grab_end(grab);
1744 free(grab);
1745 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001746}
1747
1748static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001749busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001750 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001751{
Jonas Ådahld2510102014-10-05 21:39:14 +02001752 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001753}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001756busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757 uint32_t time, uint32_t button, uint32_t state)
1758{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001759 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001760 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001761 struct weston_pointer *pointer = grab->grab.pointer;
1762 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001763
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001764 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001765 activate(shsurf->shell, shsurf->view, seat,
1766 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001767 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001768 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001769 activate(shsurf->shell, shsurf->view, seat,
1770 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001771 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001772 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001773}
1774
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001775static void
1776busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1777{
1778 struct shell_grab *grab = (struct shell_grab *) base;
1779
1780 shell_grab_end(grab);
1781 free(grab);
1782}
1783
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001784static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785 busy_cursor_grab_focus,
1786 busy_cursor_grab_motion,
1787 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001788 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001789 noop_grab_axis_source,
1790 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001791 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792};
1793
1794static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795handle_pointer_focus(struct wl_listener *listener, void *data)
1796{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001799 struct shell_surface *shsurf;
1800 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803 return;
1804
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001805 shsurf = get_shell_surface(view->surface);
1806 if (!shsurf)
1807 return;
1808
1809 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1810
1811 if (shsurf->unresponsive)
1812 set_busy_cursor(shsurf, pointer);
1813 else
1814 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815}
1816
1817static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001818shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1819{
1820 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001821 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001822}
1823
1824static void
1825shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1826{
1827 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001828 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001829}
1830
1831static void
1832handle_keyboard_focus(struct wl_listener *listener, void *data)
1833{
1834 struct weston_keyboard *keyboard = data;
1835 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1836
1837 if (seat->focused_surface) {
1838 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1839 if (shsurf)
1840 shell_surface_lose_keyboard_focus(shsurf);
1841 }
1842
1843 seat->focused_surface = keyboard->focus;
1844
1845 if (seat->focused_surface) {
1846 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1847 if (shsurf)
1848 shell_surface_gain_keyboard_focus(shsurf);
1849 }
1850}
1851
Philip Withnall07926d92013-11-25 18:01:40 +00001852/* The surface will be inserted into the list immediately after the link
1853 * returned by this function (i.e. will be stacked immediately above the
1854 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001855static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001856shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1857{
1858 struct workspace *ws;
1859
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001860 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1861 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001862 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001863 }
1864
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001865 /* Move the surface to a normal workspace layer so that surfaces
1866 * which were previously fullscreen or transient are no longer
1867 * rendered on top. */
1868 ws = get_current_workspace(shsurf->shell);
1869 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001870}
1871
Philip Withnall648a4dd2013-11-25 18:01:44 +00001872static void
1873shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1874{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001876}
1877
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001878/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001879 * geometry to ensure the changes are redrawn.
1880 *
1881 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1882 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001883static void
1884shell_surface_update_layer(struct shell_surface *shsurf)
1885{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001886 struct weston_surface *surface =
1887 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001888 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001889
1890 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1891
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001892 if (new_layer_link == NULL)
1893 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001894 if (new_layer_link == &shsurf->view->layer_link)
1895 return;
1896
1897 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001898 weston_layer_entry_remove(&shsurf->view->layer_link);
1899 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001900 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001901 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001902
1903 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001904}
1905
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001906static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001907shell_surface_set_output(struct shell_surface *shsurf,
1908 struct weston_output *output)
1909{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001910 struct weston_surface *es =
1911 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001912
1913 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001914 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001915 if (output)
1916 shsurf->output = output;
1917 else if (es->output)
1918 shsurf->output = es->output;
1919 else
1920 shsurf->output = get_default_output(es->compositor);
1921}
1922
1923static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001924weston_view_set_initial_position(struct weston_view *view,
1925 struct desktop_shell *shell);
1926
1927static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001928unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001929{
Philip Withnallf85fe842013-11-25 18:01:37 +00001930 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001931 wl_list_remove(&shsurf->fullscreen.transform.link);
1932 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001933
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934 if (shsurf->fullscreen.black_view)
1935 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1936 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001937
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001938 if (shsurf->saved_position_valid)
1939 weston_view_set_position(shsurf->view,
1940 shsurf->saved_x, shsurf->saved_y);
1941 else
1942 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001943 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001944
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001945 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001947 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001948 shsurf->saved_rotation_valid = false;
1949 }
Alex Wu4539b082012-03-01 12:57:46 +08001950}
1951
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001952static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001953unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001954{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001955 struct weston_surface *surface =
1956 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1957
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001958 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001959 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001960
1961 if (shsurf->saved_position_valid)
1962 weston_view_set_position(shsurf->view,
1963 shsurf->saved_x, shsurf->saved_y);
1964 else
1965 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001966 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001967
1968 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 wl_list_insert(&shsurf->view->geometry.transformation_list,
1970 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001971 shsurf->saved_rotation_valid = false;
1972 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001973}
1974
Philip Withnallbecb77e2013-11-25 18:01:30 +00001975static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001976set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001977{
1978 struct shell_surface *shsurf;
1979 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001980 struct weston_view *view;
1981
1982 view = get_default_view(surface);
1983 if (!view)
1984 return;
1985
1986 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1987
1988 shsurf = get_shell_surface(surface);
1989 current_ws = get_current_workspace(shsurf->shell);
1990
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001991 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001992 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001993
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001994 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02001995 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001996
1997 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001998 weston_view_damage_below(view);
1999}
2000
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002001
Tiago Vignattibe143262012-04-16 17:31:41 +03002002static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002003shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002004{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002005 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002006}
2007
Pekka Paalanen8274d902014-08-06 19:36:51 +03002008static int
2009black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2010{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002011 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002012 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002013 int n;
2014 int rem;
2015 int ret;
2016
2017 n = snprintf(buf, len, "black background surface for ");
2018 if (n < 0)
2019 return n;
2020
2021 rem = (int)len - n;
2022 if (rem < 0)
2023 rem = 0;
2024
2025 if (fs_surface->get_label)
2026 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2027 else
2028 ret = snprintf(buf + n, rem, "<unknown>");
2029
2030 if (ret < 0)
2031 return n;
2032
2033 return n + ret;
2034}
2035
Alex Wu21858432012-04-01 20:13:08 +08002036static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002037black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002038
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002040create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002041 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002042 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002043{
2044 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002046
2047 surface = weston_surface_create(ec);
2048 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002049 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002050 return NULL;
2051 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 view = weston_view_create(surface);
2053 if (surface == NULL) {
2054 weston_log("no memory\n");
2055 weston_surface_destroy(surface);
2056 return NULL;
2057 }
Alex Wu4539b082012-03-01 12:57:46 +08002058
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002059 surface->committed = black_surface_committed;
2060 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002061 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002062 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002063 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002064 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002065 pixman_region32_fini(&surface->input);
2066 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002067
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002068 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002069 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070
2071 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002072}
2073
Philip Withnalled8f1a92013-11-25 18:01:43 +00002074static void
2075shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2076{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002077 struct weston_surface *surface =
2078 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002079 struct weston_output *output = shsurf->fullscreen_output;
2080
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002081 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002082
2083 if (!shsurf->fullscreen.black_view)
2084 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002085 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002086 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002087 output->x, output->y,
2088 output->width,
2089 output->height);
2090
2091 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002092 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2093 weston_layer_entry_insert(&shsurf->view->layer_link,
2094 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002095 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002096 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002097
Armin Krezović4663aca2016-06-30 06:04:29 +02002098 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002099 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002100}
2101
Alex Wu4539b082012-03-01 12:57:46 +08002102/* Create black surface and append it to the associated fullscreen surface.
2103 * Handle size dismatch and positioning according to the method. */
2104static void
2105shell_configure_fullscreen(struct shell_surface *shsurf)
2106{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002107 struct weston_surface *surface =
2108 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002109 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002110
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002111 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002112 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002113 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2114 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002115
Philip Withnalled8f1a92013-11-25 18:01:43 +00002116 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002117
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002118 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002119 &surf_width, &surf_height);
2120
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002121 if (surface->buffer_ref.buffer)
2122 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002123}
2124
Alex Wu4539b082012-03-01 12:57:46 +08002125static void
2126shell_map_fullscreen(struct shell_surface *shsurf)
2127{
Alex Wubd3354b2012-04-17 17:20:49 +08002128 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002129}
2130
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002131static struct weston_output *
2132get_focused_output(struct weston_compositor *compositor)
2133{
2134 struct weston_seat *seat;
2135 struct weston_output *output = NULL;
2136
2137 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002138 struct weston_touch *touch = weston_seat_get_touch(seat);
2139 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2140 struct weston_keyboard *keyboard =
2141 weston_seat_get_keyboard(seat);
2142
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002143 /* Priority has touch focus, then pointer and
2144 * then keyboard focus. We should probably have
2145 * three for loops and check frist for touch,
2146 * then for pointer, etc. but unless somebody has some
2147 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002148 if (touch && touch->focus)
2149 output = touch->focus->output;
2150 else if (pointer && pointer->focus)
2151 output = pointer->focus->output;
2152 else if (keyboard && keyboard->focus)
2153 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002154
2155 if (output)
2156 break;
2157 }
2158
2159 return output;
2160}
2161
2162static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002163destroy_shell_seat(struct wl_listener *listener, void *data)
2164{
2165 struct shell_seat *shseat =
2166 container_of(listener,
2167 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002168
2169 wl_list_remove(&shseat->seat_destroy_listener.link);
2170 free(shseat);
2171}
2172
Jason Ekstrand024177c2014-04-21 19:42:58 -05002173static void
2174shell_seat_caps_changed(struct wl_listener *listener, void *data)
2175{
Derek Foreman1281a362015-07-31 16:55:32 -05002176 struct weston_keyboard *keyboard;
2177 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002178 struct shell_seat *seat;
2179
2180 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002181 keyboard = weston_seat_get_keyboard(seat->seat);
2182 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002183
Derek Foreman1281a362015-07-31 16:55:32 -05002184 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002185 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002186 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002187 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002188 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002189 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190 wl_list_init(&seat->keyboard_focus_listener.link);
2191 }
2192
Derek Foreman1281a362015-07-31 16:55:32 -05002193 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002194 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002195 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002196 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002197 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002198 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002199 wl_list_init(&seat->pointer_focus_listener.link);
2200 }
2201}
2202
Giulio Camuffo5085a752013-03-25 21:42:45 +01002203static struct shell_seat *
2204create_shell_seat(struct weston_seat *seat)
2205{
2206 struct shell_seat *shseat;
2207
2208 shseat = calloc(1, sizeof *shseat);
2209 if (!shseat) {
2210 weston_log("no memory to allocate shell seat\n");
2211 return NULL;
2212 }
2213
2214 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002215
2216 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2217 wl_signal_add(&seat->destroy_signal,
2218 &shseat->seat_destroy_listener);
2219
Jason Ekstrand024177c2014-04-21 19:42:58 -05002220 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2221 wl_list_init(&shseat->keyboard_focus_listener.link);
2222
2223 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2224 wl_list_init(&shseat->pointer_focus_listener.link);
2225
2226 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2227 wl_signal_add(&seat->updated_caps_signal,
2228 &shseat->caps_changed_listener);
2229 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2230
Giulio Camuffo5085a752013-03-25 21:42:45 +01002231 return shseat;
2232}
2233
2234static struct shell_seat *
2235get_shell_seat(struct weston_seat *seat)
2236{
2237 struct wl_listener *listener;
2238
2239 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002240 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002241
2242 return container_of(listener,
2243 struct shell_seat, seat_destroy_listener);
2244}
2245
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002246static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002247fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002248{
2249 struct shell_surface *shsurf = data;
2250
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002251 weston_surface_destroy(shsurf->view->surface);
2252 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002253}
2254
2255static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002256fade_out_done(struct weston_view_animation *animation, void *data)
2257{
2258 struct shell_surface *shsurf = data;
2259 struct wl_event_loop *loop;
2260
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002261 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002263 if (weston_view_is_mapped(shsurf->view)) {
2264 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002265 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002266 }
2267}
2268
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002269struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002270get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002271{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002272 if (weston_surface_is_desktop_surface(surface)) {
2273 struct weston_desktop_surface *desktop_surface =
2274 weston_surface_get_desktop_surface(surface);
2275 return weston_desktop_surface_get_user_data(desktop_surface);
2276 }
2277 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002278}
2279
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002280/*
2281 * libweston-desktop
2282 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002283
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002284static void
2285desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2286 void *shell)
2287{
2288 struct weston_desktop_client *client =
2289 weston_desktop_surface_get_client(desktop_surface);
2290 struct wl_client *wl_client =
2291 weston_desktop_client_get_client(client);
2292 struct weston_view *view;
2293 struct shell_surface *shsurf;
2294 struct weston_surface *surface =
2295 weston_desktop_surface_get_surface(desktop_surface);
2296
2297 view = weston_desktop_surface_create_view(desktop_surface);
2298 if (!view)
2299 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002300
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301 shsurf = calloc(1, sizeof *shsurf);
2302 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002303 if (wl_client)
2304 wl_client_post_no_memory(wl_client);
2305 else
2306 weston_log("no memory to allocate shell surface\n");
2307 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002308 }
2309
Pekka Paalanen8274d902014-08-06 19:36:51 +03002310 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002311
Tiago Vignattibc052c92012-04-19 16:18:18 +03002312 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002313 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002314 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002315 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002316 shsurf->desktop_surface = desktop_surface;
2317 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002319 wl_list_init(&shsurf->fullscreen.transform.link);
2320
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002321 shsurf->output = get_default_output(shsurf->shell->compositor);
2322
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002323 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002324
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325 /* empty when not in use */
2326 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002327 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002328
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002329 wl_list_init(&shsurf->workspace_transform.link);
2330
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002331 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2332 weston_desktop_surface_set_activated(desktop_surface,
2333 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002334}
2335
Tiago Vignattibc052c92012-04-19 16:18:18 +03002336static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002337desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2338 void *shell)
2339{
2340 struct shell_surface *shsurf =
2341 weston_desktop_surface_get_user_data(desktop_surface);
2342 struct weston_surface *surface =
2343 weston_desktop_surface_get_surface(desktop_surface);
2344
2345 if (!shsurf)
2346 return;
2347
2348 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2349
2350 if (shsurf->fullscreen.black_view)
2351 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2352
2353 weston_surface_set_label_func(surface, NULL);
2354 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2355 shsurf->desktop_surface = NULL;
2356
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002357 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002358 if (weston_surface_is_mapped(surface) &&
2359 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2360 pixman_region32_fini(&surface->pending.input);
2361 pixman_region32_init(&surface->pending.input);
2362 pixman_region32_fini(&surface->input);
2363 pixman_region32_init(&surface->input);
2364 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2365 fade_out_done, shsurf);
2366 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367 weston_view_destroy(shsurf->view);
2368 free(shsurf);
2369 }
2370}
2371
2372static void
2373set_maximized_position(struct desktop_shell *shell,
2374 struct shell_surface *shsurf)
2375{
2376 pixman_rectangle32_t area;
2377 struct weston_geometry geometry;
2378
2379 get_output_work_area(shell, shsurf->output, &area);
2380 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2381
2382 weston_view_set_position(shsurf->view,
2383 area.x - geometry.x,
2384 area.y - geometry.y);
2385}
2386
2387static void
2388map(struct desktop_shell *shell, struct shell_surface *shsurf,
2389 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002390{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002391 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002392 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2393 struct weston_compositor *compositor = shell->compositor;
2394 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002395
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002396 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002397 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002398 center_on_output(shsurf->view, shsurf->fullscreen_output);
2399 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002400 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002401 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002402 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002403 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002404 }
2405
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002406 /* Surface stacking order, see also activate(). */
2407 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002408
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002409 weston_view_update_transform(shsurf->view);
2410 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002411 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002412 surface->output = shsurf->output;
2413 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002414 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002415
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 if (!shell->locked) {
2417 wl_list_for_each(seat, &compositor->seat_list, link)
2418 activate(shell, shsurf->view, seat,
2419 WESTON_ACTIVATE_FLAG_CONFIGURE);
2420 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002421
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002422 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002423 switch (shell->win_animation_type) {
2424 case ANIMATION_FADE:
2425 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2426 break;
2427 case ANIMATION_ZOOM:
2428 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2429 break;
2430 case ANIMATION_NONE:
2431 default:
2432 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002433 }
2434 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002435}
2436
2437static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002438desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2439 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002440{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002441 struct shell_surface *shsurf =
2442 weston_desktop_surface_get_user_data(desktop_surface);
2443 struct weston_surface *surface =
2444 weston_desktop_surface_get_surface(desktop_surface);
2445 struct weston_view *view = shsurf->view;
2446 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002447 bool was_fullscreen;
2448 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449
2450 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002451 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002453 was_fullscreen = shsurf->state.fullscreen;
2454 was_maximized = shsurf->state.maximized;
2455
2456 shsurf->state.fullscreen =
2457 weston_desktop_surface_get_fullscreen(desktop_surface);
2458 shsurf->state.maximized =
2459 weston_desktop_surface_get_maximized(desktop_surface);
2460
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461 if (!weston_surface_is_mapped(surface)) {
2462 map(shell, shsurf, sx, sy);
2463 surface->is_mapped = true;
2464 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2465 ++surface->ref_count;
2466 return;
2467 }
2468
2469 if (sx == 0 && sy == 0 &&
2470 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002471 shsurf->last_height == surface->height &&
2472 was_fullscreen == shsurf->state.fullscreen &&
2473 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 return;
2475
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002476 if (was_fullscreen)
2477 unset_fullscreen(shsurf);
2478 if (was_maximized)
2479 unset_maximized(shsurf);
2480
Quentin Glidic920cf042016-08-16 14:26:20 +02002481 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2482 !shsurf->saved_position_valid) {
2483 shsurf->saved_x = shsurf->view->geometry.x;
2484 shsurf->saved_y = shsurf->view->geometry.y;
2485 shsurf->saved_position_valid = true;
2486
2487 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2488 wl_list_remove(&shsurf->rotation.transform.link);
2489 wl_list_init(&shsurf->rotation.transform.link);
2490 weston_view_geometry_dirty(shsurf->view);
2491 shsurf->saved_rotation_valid = true;
2492 }
2493 }
2494
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002495 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002496 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002497 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002498 set_maximized_position(shell, shsurf);
2499 surface->output = shsurf->output;
2500 } else {
2501 float from_x, from_y;
2502 float to_x, to_y;
2503 float x, y;
2504
2505 if (shsurf->resize_edges) {
2506 sx = 0;
2507 sy = 0;
2508 }
2509
2510 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2511 sx = shsurf->last_width - surface->width;
2512 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2513 sy = shsurf->last_height - surface->height;
2514
2515 shsurf->last_width = surface->width;
2516 shsurf->last_height = surface->height;
2517
2518 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2519 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2520 x = shsurf->view->geometry.x + to_x - from_x;
2521 y = shsurf->view->geometry.y + to_y - from_y;
2522
2523 weston_view_set_position(shsurf->view, x, y);
2524 }
2525
2526 /* XXX: would a fullscreen surface need the same handling? */
2527 if (surface->output) {
2528 wl_list_for_each(view, &surface->views, surface_link)
2529 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002530 }
2531}
2532
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002533static void
2534set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2535 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002536{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002537 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2538 struct weston_surface *surface =
2539 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2540 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002541
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002542 if (fullscreen) {
2543 /* handle clients launching in fullscreen */
2544 if (output == NULL && !weston_surface_is_mapped(surface)) {
2545 /* Set the output to the one that has focus currently. */
2546 output = get_focused_output(surface->compositor);
2547 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002548
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549 shell_surface_set_output(shsurf, output);
2550 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002551
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 width = shsurf->output->width;
2553 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002554 }
2555 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2556 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002557}
2558
2559static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002560desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2561 struct weston_seat *seat, uint32_t serial, void *shell)
2562{
2563 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2564 struct weston_touch *touch = weston_seat_get_touch(seat);
2565 struct shell_surface *shsurf =
2566 weston_desktop_surface_get_user_data(desktop_surface);
2567 struct weston_surface *surface =
2568 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2569 struct wl_resource *resource = surface->resource;
2570 struct weston_surface *focus;
2571
2572 if (pointer &&
2573 pointer->focus &&
2574 pointer->button_count > 0 &&
2575 pointer->grab_serial == serial) {
2576 focus = weston_surface_get_main_surface(pointer->focus->surface);
2577 if ((focus == surface) &&
2578 (surface_move(shsurf, pointer, true) < 0))
2579 wl_resource_post_no_memory(resource);
2580 } else if (touch &&
2581 touch->focus &&
2582 touch->grab_serial == serial) {
2583 focus = weston_surface_get_main_surface(touch->focus->surface);
2584 if ((focus == surface) &&
2585 (surface_touch_move(shsurf, touch) < 0))
2586 wl_resource_post_no_memory(resource);
2587 }
2588}
2589
2590static void
2591desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2592 struct weston_seat *seat, uint32_t serial,
2593 enum weston_desktop_surface_edge edges, void *shell)
2594{
2595 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2596 struct shell_surface *shsurf =
2597 weston_desktop_surface_get_user_data(desktop_surface);
2598 struct weston_surface *surface =
2599 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2600 struct wl_resource *resource = surface->resource;
2601 struct weston_surface *focus;
2602
2603 if (!pointer ||
2604 pointer->button_count == 0 ||
2605 pointer->grab_serial != serial ||
2606 pointer->focus == NULL)
2607 return;
2608
2609 focus = weston_surface_get_main_surface(pointer->focus->surface);
2610 if (focus != surface)
2611 return;
2612
2613 if (surface_resize(shsurf, pointer, edges) < 0)
2614 wl_resource_post_no_memory(resource);
2615}
2616
2617static void
2618desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2619 bool fullscreen,
2620 struct weston_output *output, void *shell)
2621{
2622 struct shell_surface *shsurf =
2623 weston_desktop_surface_get_user_data(desktop_surface);
2624
2625 set_fullscreen(shsurf, fullscreen, output);
2626}
2627
2628static void
2629set_maximized(struct shell_surface *shsurf, bool maximized)
2630{
2631 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2632 struct weston_surface *surface =
2633 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2634 int32_t width = 0, height = 0;
2635
2636 if (maximized) {
2637 struct weston_output *output;
2638 struct desktop_shell *shell;
2639 pixman_rectangle32_t area;
2640
2641 if (!weston_surface_is_mapped(surface))
2642 output = get_focused_output(surface->compositor);
2643 else
2644 output = surface->output;
2645
2646 shell_surface_set_output(shsurf, output);
2647
2648 shell = shell_surface_get_shell(shsurf);
2649 get_output_work_area(shell, shsurf->output, &area);
2650
2651 width = area.width;
2652 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002653 }
2654 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2655 weston_desktop_surface_set_size(desktop_surface, width, height);
2656}
2657
2658static void
2659desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2660 bool maximized, void *shell)
2661{
2662 struct shell_surface *shsurf =
2663 weston_desktop_surface_get_user_data(desktop_surface);
2664
2665 set_maximized(shsurf, maximized);
2666}
2667
2668static void
2669desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2670 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002671{
2672 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002673 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002674
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002675 /* apply compositor's own minimization logic (hide) */
2676 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002677}
2678
Rafael Antognollie2a34552013-12-03 15:35:45 -02002679static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002680set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002681{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002683
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002684 if (pointer->grab->interface == &busy_cursor_grab_interface)
2685 return;
2686
2687 grab = malloc(sizeof *grab);
2688 if (!grab)
2689 return;
2690
2691 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2692 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2693 /* Mark the shsurf as ungrabbed so that button binding is able
2694 * to move it. */
2695 shsurf->grabbed = 0;
2696}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002697
2698static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002699end_busy_cursor(struct weston_compositor *compositor,
2700 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002701{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002702 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002703 struct shell_grab *grab;
2704 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002705
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002706 wl_list_for_each(seat, &compositor->seat_list, link) {
2707 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2708 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002709
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002710 if (!pointer)
2711 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002712
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002713 if (pointer->grab->interface != &busy_cursor_grab_interface)
2714 continue;
2715
2716 grab = (struct shell_grab *) pointer->grab;
2717 shsurf = grab->shsurf;
2718 if (!shsurf)
2719 continue;
2720
2721 grab_client =
2722 weston_desktop_surface_get_client(shsurf->desktop_surface);
2723 if (grab_client == desktop_client) {
2724 shell_grab_end(grab);
2725 free(grab);
2726 }
2727 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002728}
2729
2730static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002731desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2732 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002733{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002734 struct shell_surface *shsurf =
2735 weston_desktop_surface_get_user_data(desktop_surface);
2736 bool *unresponsive = user_data;
2737
2738 shsurf->unresponsive = *unresponsive;
2739}
2740
2741static void
2742desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2743 void *shell_)
2744{
2745 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002746 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002747 struct weston_seat *seat;
2748 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002749
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002750 weston_desktop_client_for_each_surface(desktop_client,
2751 desktop_surface_set_unresponsive,
2752 &unresponsive);
2753
2754
2755 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2756 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2757 struct weston_desktop_client *grab_client;
2758
2759 if (!pointer || !pointer->focus)
2760 continue;
2761
2762 shsurf = get_shell_surface(pointer->focus->surface);
2763 if (!shsurf)
2764 continue;
2765
2766 grab_client =
2767 weston_desktop_surface_get_client(shsurf->desktop_surface);
2768 if (grab_client == desktop_client)
2769 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002770 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771}
2772
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002773static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002774desktop_surface_pong(struct weston_desktop_client *desktop_client,
2775 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002776{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777 struct desktop_shell *shell = shell_;
2778 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780 weston_desktop_client_for_each_surface(desktop_client,
2781 desktop_surface_set_unresponsive,
2782 &unresponsive);
2783 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002784}
2785
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002786static const struct weston_desktop_api shell_desktop_api = {
2787 .struct_size = sizeof(struct weston_desktop_api),
2788 .surface_added = desktop_surface_added,
2789 .surface_removed = desktop_surface_removed,
2790 .committed = desktop_surface_committed,
2791 .move = desktop_surface_move,
2792 .resize = desktop_surface_resize,
2793 .fullscreen_requested = desktop_surface_fullscreen_requested,
2794 .maximized_requested = desktop_surface_maximized_requested,
2795 .minimized_requested = desktop_surface_minimized_requested,
2796 .ping_timeout = desktop_surface_ping_timeout,
2797 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002798};
2799
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002800/* ************************ *
2801 * end of libweston-desktop *
2802 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002803static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002804shell_fade(struct desktop_shell *shell, enum fade_type type);
2805
2806static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002807configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002808{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002810
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002811 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 if (v->output == ev->output && v != ev) {
2813 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002814 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002815 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002816 }
2817 }
2818
Quentin Glidice8bf9592016-06-23 18:55:20 +02002819 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002820 ev->surface->is_mapped = true;
2821 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002822
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002823 if (wl_list_empty(&ev->layer_link.link)) {
2824 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002826 }
2827}
2828
David Fort50d962f2016-06-09 17:55:52 +02002829
2830static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831find_shell_output_from_weston_output(struct desktop_shell *shell,
2832 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002833{
2834 struct shell_output *shell_output;
2835
2836 wl_list_for_each(shell_output, &shell->output_list, link) {
2837 if (shell_output->output == output)
2838 return shell_output;
2839 }
2840
2841 return NULL;
2842}
2843
Pekka Paalanen8274d902014-08-06 19:36:51 +03002844static int
2845background_get_label(struct weston_surface *surface, char *buf, size_t len)
2846{
2847 return snprintf(buf, len, "background for output %s",
2848 surface->output->name);
2849}
2850
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002851static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002852background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002853{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002854 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002856
Jason Ekstranda7af7042013-10-12 22:38:11 -05002857 view = container_of(es->views.next, struct weston_view, surface_link);
2858
Quentin Glidice8bf9592016-06-23 18:55:20 +02002859 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002860}
2861
2862static void
David Fort50d962f2016-06-09 17:55:52 +02002863handle_background_surface_destroy(struct wl_listener *listener, void *data)
2864{
2865 struct shell_output *output =
2866 container_of(listener, struct shell_output, background_surface_listener);
2867
2868 weston_log("background surface gone\n");
2869 output->background_surface = NULL;
2870}
2871
2872static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002873desktop_shell_set_background(struct wl_client *client,
2874 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002875 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002876 struct wl_resource *surface_resource)
2877{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002878 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002879 struct weston_surface *surface =
2880 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002881 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002883
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002884 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002885 wl_resource_post_error(surface_resource,
2886 WL_DISPLAY_ERROR_INVALID_OBJECT,
2887 "surface role already assigned");
2888 return;
2889 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002890
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2892 weston_view_destroy(view);
2893 view = weston_view_create(surface);
2894
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002895 surface->committed = background_committed;
2896 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002897 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002898 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002900 weston_desktop_shell_send_configure(resource, 0,
2901 surface_resource,
2902 surface->output->width,
2903 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002904
2905 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2906 sh_output->background_surface = surface;
2907
2908 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2909 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002910}
2911
Pekka Paalanen8274d902014-08-06 19:36:51 +03002912static int
2913panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2914{
2915 return snprintf(buf, len, "panel for output %s",
2916 surface->output->name);
2917}
2918
Kristian Høgsberg75840622011-09-06 13:48:16 -04002919static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002920panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002921{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002922 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002924 int width, height;
2925 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002926
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 view = container_of(es->views.next, struct weston_view, surface_link);
2928
Quentin Glidice8bf9592016-06-23 18:55:20 +02002929 get_panel_size(shell, view, &width, &height);
2930 switch (shell->panel_position) {
2931 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2932 break;
2933 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2934 y = view->output->height - height;
2935 break;
2936 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2937 break;
2938 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2939 x = view->output->width - width;
2940 break;
2941 }
2942
2943 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002944}
2945
2946static void
David Fort50d962f2016-06-09 17:55:52 +02002947handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2948{
2949 struct shell_output *output =
2950 container_of(listener, struct shell_output, panel_surface_listener);
2951
2952 weston_log("panel surface gone\n");
2953 output->panel_surface = NULL;
2954}
2955
2956
2957static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002958desktop_shell_set_panel(struct wl_client *client,
2959 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002960 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002961 struct wl_resource *surface_resource)
2962{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002963 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002964 struct weston_surface *surface =
2965 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002966 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002967 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002968
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002969 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002970 wl_resource_post_error(surface_resource,
2971 WL_DISPLAY_ERROR_INVALID_OBJECT,
2972 "surface role already assigned");
2973 return;
2974 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002975
Jason Ekstranda7af7042013-10-12 22:38:11 -05002976 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2977 weston_view_destroy(view);
2978 view = weston_view_create(surface);
2979
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002980 surface->committed = panel_committed;
2981 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002982 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002985 weston_desktop_shell_send_configure(resource, 0,
2986 surface_resource,
2987 surface->output->width,
2988 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002989
2990 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2991 sh_output->panel_surface = surface;
2992
2993 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2994 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995}
2996
Pekka Paalanen8274d902014-08-06 19:36:51 +03002997static int
2998lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2999{
3000 return snprintf(buf, len, "lock window");
3001}
3002
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003003static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003004lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003005{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003006 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 struct weston_view *view;
3008
3009 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003010
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003011 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003012 return;
3013
Jason Ekstranda7af7042013-10-12 22:38:11 -05003014 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003015
3016 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003017 weston_layer_entry_insert(&shell->lock_layer.view_list,
3018 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003020 surface->is_mapped = true;
3021 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003022 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003023 }
3024}
3025
3026static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003027handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003028{
Tiago Vignattibe143262012-04-16 17:31:41 +03003029 struct desktop_shell *shell =
3030 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003031
Martin Minarik6d118362012-06-07 18:01:59 +02003032 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003033 shell->lock_surface = NULL;
3034}
3035
3036static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003037desktop_shell_set_lock_surface(struct wl_client *client,
3038 struct wl_resource *resource,
3039 struct wl_resource *surface_resource)
3040{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003041 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003042 struct weston_surface *surface =
3043 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003044
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003045 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003046
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003047 if (!shell->locked)
3048 return;
3049
Pekka Paalanen98262232011-12-01 10:42:22 +02003050 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003051
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003052 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003053 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003054 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003055
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003056 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003057 surface->committed = lock_surface_committed;
3058 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003059 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003060}
3061
3062static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003063resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003064{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003065 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003066
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003067 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003068 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003069 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003070 &shell->input_panel_layer.link);
3071 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003072 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003073 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003074 wl_list_insert(&shell->compositor->cursor_layer.link,
3075 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003076 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003077 wl_list_insert(&shell->fullscreen_layer.link,
3078 &shell->panel_layer.link);
3079 wl_list_insert(&shell->panel_layer.link,
3080 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003081
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003082 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003083
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003084 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003085 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003086 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003087}
3088
3089static void
3090desktop_shell_unlock(struct wl_client *client,
3091 struct wl_resource *resource)
3092{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003093 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003094
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003095 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003096
3097 if (shell->locked)
3098 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003099}
3100
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003101static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003102desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003103 struct wl_resource *resource,
3104 struct wl_resource *surface_resource)
3105{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003106 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003107
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003108 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003109 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003110}
3111
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003112static void
3113desktop_shell_desktop_ready(struct wl_client *client,
3114 struct wl_resource *resource)
3115{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003116 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003117
3118 shell_fade_startup(shell);
3119}
3120
Jonny Lamb765760d2014-08-20 15:53:19 +02003121static void
3122desktop_shell_set_panel_position(struct wl_client *client,
3123 struct wl_resource *resource,
3124 uint32_t position)
3125{
3126 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3127
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003128 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3129 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3130 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3131 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003132 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003133 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003134 "bad position argument");
3135 return;
3136 }
3137
3138 shell->panel_position = position;
3139}
3140
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003141static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003142 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003143 desktop_shell_set_panel,
3144 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003145 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003146 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003147 desktop_shell_desktop_ready,
3148 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003149};
3150
3151static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003152move_binding(struct weston_pointer *pointer, uint32_t time,
3153 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003154{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003155 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003156 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003157 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003158
Derek Foreman8ae2db52015-07-15 13:00:36 -05003159 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003160 return;
3161
Derek Foreman8ae2db52015-07-15 13:00:36 -05003162 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003163
Pekka Paalanen01388e22013-04-25 13:57:44 +03003164 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003165 if (surface == NULL)
3166 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003167
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003168 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003169 if (shsurf == NULL ||
3170 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3171 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003172 return;
3173
Derek Foreman794fa0e2015-07-15 13:00:38 -05003174 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003175}
3176
3177static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003178maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3179 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003180{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003181 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003182 struct weston_surface *surface;
3183 struct shell_surface *shsurf;
3184
3185 surface = weston_surface_get_main_surface(focus);
3186 if (surface == NULL)
3187 return;
3188
3189 shsurf = get_shell_surface(surface);
3190 if (shsurf == NULL)
3191 return;
3192
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003193 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003194}
3195
3196static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003197fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3198 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003199{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003200 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003201 struct weston_surface *surface;
3202 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003203 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003204
3205 surface = weston_surface_get_main_surface(focus);
3206 if (surface == NULL)
3207 return;
3208
3209 shsurf = get_shell_surface(surface);
3210 if (shsurf == NULL)
3211 return;
3212
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003213 fullscreen =
3214 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003215
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003216 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003217}
3218
3219static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003220touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003221{
Derek Foreman362656b2014-09-04 10:23:05 -05003222 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003223 struct weston_surface *surface;
3224 struct shell_surface *shsurf;
3225
Derek Foreman8ae2db52015-07-15 13:00:36 -05003226 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003227 return;
3228
Derek Foreman8ae2db52015-07-15 13:00:36 -05003229 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003230 surface = weston_surface_get_main_surface(focus);
3231 if (surface == NULL)
3232 return;
3233
3234 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003235 if (shsurf == NULL ||
3236 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3237 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003238 return;
3239
Derek Foremanb7674ae2015-07-15 13:00:37 -05003240 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003241}
3242
3243static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003244resize_binding(struct weston_pointer *pointer, uint32_t time,
3245 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003246{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003247 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003248 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003249 uint32_t edges = 0;
3250 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003251 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003252
Derek Foreman8ae2db52015-07-15 13:00:36 -05003253 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003254 return;
3255
Derek Foreman8ae2db52015-07-15 13:00:36 -05003256 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003257
Pekka Paalanen01388e22013-04-25 13:57:44 +03003258 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003259 if (surface == NULL)
3260 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003261
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003262 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003263 if (shsurf == NULL ||
3264 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3265 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003266 return;
3267
Jason Ekstranda7af7042013-10-12 22:38:11 -05003268 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003269 wl_fixed_to_int(pointer->grab_x),
3270 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003271 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003273 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003274 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003275 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003276 edges |= 0;
3277 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003278 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003279
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003280 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003281 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003282 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003283 edges |= 0;
3284 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003285 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003286
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003287 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003288}
3289
3290static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003291surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003292 struct weston_pointer_axis_event *event,
3293 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003294{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003295 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003296 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003297 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003298 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003299
Pekka Paalanen01388e22013-04-25 13:57:44 +03003300 /* XXX: broken for windows containing sub-surfaces */
3301 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003302 if (surface == NULL)
3303 return;
3304
3305 shsurf = get_shell_surface(surface);
3306 if (!shsurf)
3307 return;
3308
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003309 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003310
Jason Ekstranda7af7042013-10-12 22:38:11 -05003311 if (shsurf->view->alpha > 1.0)
3312 shsurf->view->alpha = 1.0;
3313 if (shsurf->view->alpha < step)
3314 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003315
Jason Ekstranda7af7042013-10-12 22:38:11 -05003316 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003317 weston_surface_damage(surface);
3318}
3319
3320static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003321do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003322 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003323{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003324 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003325 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003326 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003327 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003328
Derek Foreman1281a362015-07-31 16:55:32 -05003329 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003330 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3331 return;
3332 }
3333
Scott Moreauccbf29d2012-02-22 14:21:41 -07003334 wl_list_for_each(output, &compositor->output_list, link) {
3335 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003336 wl_fixed_to_double(pointer->x),
3337 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003338 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003339 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003340 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003341 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003342 increment = -output->zoom.increment;
3343 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003344 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003345 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003346 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003347 else
3348 increment = 0;
3349
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003350 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003351
Scott Moreaue6603982012-06-11 13:07:51 -06003352 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003353 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003354 else if (output->zoom.level > output->zoom.max_level)
3355 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003356
3357 if (!output->zoom.active) {
3358 if (output->zoom.level <= 0.0)
3359 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003360 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003361 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003362
Scott Moreaue6603982012-06-11 13:07:51 -06003363 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003364
Jason Ekstranda7af7042013-10-12 22:38:11 -05003365 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003366 }
3367 }
3368}
3369
Scott Moreauccbf29d2012-02-22 14:21:41 -07003370static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003371zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003372 struct weston_pointer_axis_event *event,
3373 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003374{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003375 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003376}
3377
3378static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003379zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3380 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003381{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003382 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003383}
3384
3385static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003386terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3387 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003388{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003389 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003390
Daniel Stone325fc2d2012-05-30 16:31:58 +01003391 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003392}
3393
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003394static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003395rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003396 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003397{
3398 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003399 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003400 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003401 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003402 struct weston_surface *surface =
3403 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003404 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003405
Jonas Ådahld2510102014-10-05 21:39:14 +02003406 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003407
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003408 if (!shsurf)
3409 return;
3410
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003411 cx = 0.5f * surface->width;
3412 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003413
Daniel Stone37816df2012-05-16 18:45:18 +01003414 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3415 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003416 r = sqrtf(dx * dx + dy * dy);
3417
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003418 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003419 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003420
3421 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003422 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003423 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003424
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003425 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003426 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003427
3428 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003429 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003430 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003431 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003432 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003433
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003434 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003435 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003436 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003437 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003438 wl_list_init(&shsurf->rotation.transform.link);
3439 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003440 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003441 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003442
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003443 /* We need to adjust the position of the surface
3444 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003445 cposx = shsurf->view->geometry.x + cx;
3446 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003447 dposx = rotate->center.x - cposx;
3448 dposy = rotate->center.y - cposy;
3449 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003450 weston_view_set_position(shsurf->view,
3451 shsurf->view->geometry.x + dposx,
3452 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003453 }
3454
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003455 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003456 * lazily applies the damage due to rotation update.
3457 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003458 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003459}
3460
3461static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003462rotate_grab_button(struct weston_pointer_grab *grab,
3463 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003464{
3465 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003466 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003467 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003468 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003469 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003470
Daniel Stone4dbadb12012-05-30 16:31:51 +01003471 if (pointer->button_count == 0 &&
3472 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003473 if (shsurf)
3474 weston_matrix_multiply(&shsurf->rotation.rotation,
3475 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003476 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003477 free(rotate);
3478 }
3479}
3480
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003481static void
3482rotate_grab_cancel(struct weston_pointer_grab *grab)
3483{
3484 struct rotate_grab *rotate =
3485 container_of(grab, struct rotate_grab, base.grab);
3486
3487 shell_grab_end(&rotate->base);
3488 free(rotate);
3489}
3490
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003491static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003492 noop_grab_focus,
3493 rotate_grab_motion,
3494 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003495 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003496 noop_grab_axis_source,
3497 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003498 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003499};
3500
3501static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003502surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003503{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003504 struct weston_surface *surface =
3505 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003507 float dx, dy;
3508 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003509
Pekka Paalanen460099f2012-01-20 16:48:25 +02003510 rotate = malloc(sizeof *rotate);
3511 if (!rotate)
3512 return;
3513
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003514 weston_view_to_global_float(shsurf->view,
3515 surface->width * 0.5f,
3516 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003517 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003518
Derek Foreman74de4692015-07-15 13:00:39 -05003519 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3520 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003521 r = sqrtf(dx * dx + dy * dy);
3522 if (r > 20.0f) {
3523 struct weston_matrix inverse;
3524
3525 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003526 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003527 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003528
3529 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003530 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003531 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003532 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003533 weston_matrix_init(&rotate->rotation);
3534 }
3535
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003536 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003537 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003538}
3539
3540static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003541rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003542 void *data)
3543{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003544 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003545 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003546 struct shell_surface *surface;
3547
Derek Foreman8ae2db52015-07-15 13:00:36 -05003548 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003549 return;
3550
Derek Foreman8ae2db52015-07-15 13:00:36 -05003551 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003552
Pekka Paalanen01388e22013-04-25 13:57:44 +03003553 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003554 if (base_surface == NULL)
3555 return;
3556
3557 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003558 if (surface == NULL ||
3559 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3560 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003561 return;
3562
Derek Foreman74de4692015-07-15 13:00:39 -05003563 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003564}
3565
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003566/* Move all fullscreen layers down to the current workspace and hide their
3567 * black views. The surfaces' state is set to both fullscreen and lowered,
3568 * and this is reversed when such a surface is re-configured, see
3569 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3570 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003571 * lowering_output = NULL - Lower on all outputs, else only lower on the
3572 * specified output.
3573 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003574 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003575 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003576void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003577lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003578 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003579{
3580 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003582
3583 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003585 &shell->fullscreen_layer.view_list.link,
3586 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003587 struct shell_surface *shsurf = get_shell_surface(view->surface);
3588
3589 if (!shsurf)
3590 continue;
3591
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003592 /* Only lower surfaces which have lowering_output as their fullscreen
3593 * output, unless a NULL output asks for lowering on all outputs.
3594 */
3595 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3596 continue;
3597
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003598 /* We can have a non-fullscreen popup for a fullscreen surface
3599 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003600 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003601 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003602 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3603 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003604 weston_view_damage_below(shsurf->fullscreen.black_view);
3605
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003606 }
3607
3608 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003609 weston_layer_entry_remove(&view->layer_link);
3610 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003611 weston_view_damage_below(view);
3612 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003613
3614 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003615 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003616}
3617
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003618void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003619activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003620 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003621{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003622 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003623 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003624 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003625 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003626 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003627 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003628
Pekka Paalanen01388e22013-04-25 13:57:44 +03003629 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003630 shsurf = get_shell_surface(main_surface);
3631 assert(shsurf);
3632
3633 /* Only demote fullscreen surfaces on the output of activated shsurf.
3634 * Leave fullscreen surfaces on unrelated outputs alone. */
3635 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003636
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003637 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003638
Jonas Ådahl8538b222012-08-29 22:13:03 +02003639 state = ensure_focus_state(shell, seat);
3640 if (state == NULL)
3641 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003642
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003643 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003644 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003645
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003646 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3647 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003648 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003649
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003650 /* Update the surface’s layer. This brings it to the top of the stacking
3651 * order as appropriate. */
3652 shell_surface_update_layer(shsurf);
3653
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003654 if (shell->focus_animation_type != ANIMATION_NONE) {
3655 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003656 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003657 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003658}
3659
Alex Wu21858432012-04-01 20:13:08 +08003660/* no-op func for checking black surface */
3661static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003662black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003663{
3664}
3665
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003666static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003667is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003668{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003669 struct weston_surface *surface = view->surface;
3670
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003671 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003672 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003673 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003674 return true;
3675 }
3676 return false;
3677}
3678
Kristian Høgsberg75840622011-09-06 13:48:16 -04003679static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003680activate_binding(struct weston_seat *seat,
3681 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003682 struct weston_view *focus_view,
3683 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003684{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003685 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003686 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003687
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003688 if (!focus_view)
3689 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003690
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003691 if (is_black_surface_view(focus_view, &main_view))
3692 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003693
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003694 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003695 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003696 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003697
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003698 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003699}
3700
3701static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003702click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3703 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003704{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003705 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003706 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003707 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003708 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003709
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003710 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003711 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003712 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003713}
3714
3715static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003716touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3717 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003718{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003719 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003720 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003721 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003722 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003723
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003724 activate_binding(touch->seat, data, touch->focus,
3725 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003726}
3727
3728static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003729unfocus_all_seats(struct desktop_shell *shell)
3730{
3731 struct weston_seat *seat, *next;
3732
3733 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003734 struct weston_keyboard *keyboard =
3735 weston_seat_get_keyboard(seat);
3736
3737 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003738 continue;
3739
Derek Foreman1281a362015-07-31 16:55:32 -05003740 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003741 }
3742}
3743
3744static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003745lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003746{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003747 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003748
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003749 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003750 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003751 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003752 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003753
3754 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003755
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003756 /* Hide all surfaces by removing the fullscreen, panel and
3757 * toplevel layers. This way nothing else can show or receive
3758 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003759
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003760 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003761 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003762 if (shell->showing_input_panels)
3763 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003764 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003765 wl_list_insert(&shell->compositor->cursor_layer.link,
3766 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003767
Derek Foreman004b4a12015-07-20 16:28:13 -05003768 weston_compositor_sleep(shell->compositor);
3769
Neil Robertsb4a91702014-04-09 16:33:32 +01003770 /* Remove the keyboard focus on all seats. This will be
3771 * restored to the workspace's saved state via
3772 * restore_focus_state when the compositor is unlocked */
3773 unfocus_all_seats(shell);
3774
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003775 /* TODO: disable bindings that should not work while locked. */
3776
3777 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003778}
3779
3780static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003781unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003782{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003783 struct wl_resource *shell_resource;
3784
Pekka Paalanend81c2162011-11-16 13:47:34 +02003785 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003786 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003787 return;
3788 }
3789
3790 /* If desktop-shell client has gone away, unlock immediately. */
3791 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003792 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003793 return;
3794 }
3795
3796 if (shell->prepare_event_sent)
3797 return;
3798
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003799 shell_resource = shell->child.desktop_shell;
3800 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003801 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003802}
3803
3804static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003805shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003806{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003807 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003808
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003809 shell->fade.animation = NULL;
3810
3811 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003812 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003813 weston_surface_destroy(shell->fade.view->surface);
3814 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003815 break;
3816 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003817 lock(shell);
3818 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003819 default:
3820 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003821 }
3822}
3823
Jason Ekstranda7af7042013-10-12 22:38:11 -05003824static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003825shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003826{
3827 struct weston_compositor *compositor = shell->compositor;
3828 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003829 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003830
3831 surface = weston_surface_create(compositor);
3832 if (!surface)
3833 return NULL;
3834
Jason Ekstranda7af7042013-10-12 22:38:11 -05003835 view = weston_view_create(surface);
3836 if (!view) {
3837 weston_surface_destroy(surface);
3838 return NULL;
3839 }
3840
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003841 weston_surface_set_size(surface, 8192, 8192);
3842 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003843 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003844 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3845 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003846 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003847 surface->is_mapped = true;
3848 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003849
Jason Ekstranda7af7042013-10-12 22:38:11 -05003850 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003851}
3852
3853static void
3854shell_fade(struct desktop_shell *shell, enum fade_type type)
3855{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003856 float tint;
3857
3858 switch (type) {
3859 case FADE_IN:
3860 tint = 0.0;
3861 break;
3862 case FADE_OUT:
3863 tint = 1.0;
3864 break;
3865 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003866 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003867 return;
3868 }
3869
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003870 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003871
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003872 if (shell->fade.view == NULL) {
3873 shell->fade.view = shell_fade_create_surface(shell);
3874 if (!shell->fade.view)
3875 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003876
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003877 shell->fade.view->alpha = 1.0 - tint;
3878 weston_view_update_transform(shell->fade.view);
3879 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003880
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003881 if (shell->fade.view->output == NULL) {
3882 /* If the black view gets a NULL output, we lost the
3883 * last output and we'll just cancel the fade. This
3884 * happens when you close the last window under the
3885 * X11 or Wayland backends. */
3886 shell->locked = false;
3887 weston_surface_destroy(shell->fade.view->surface);
3888 shell->fade.view = NULL;
3889 } else if (shell->fade.animation) {
3890 weston_fade_update(shell->fade.animation, tint);
3891 } else {
3892 shell->fade.animation =
3893 weston_fade_run(shell->fade.view,
3894 1.0 - tint, tint, 300.0,
3895 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003896 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003897}
3898
3899static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003900do_shell_fade_startup(void *data)
3901{
3902 struct desktop_shell *shell = data;
3903
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003904 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003905 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003906 } else {
3907 weston_log("desktop shell: "
3908 "unexpected fade-in animation type %d\n",
3909 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003910 weston_surface_destroy(shell->fade.view->surface);
3911 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003912 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003913}
3914
3915static void
3916shell_fade_startup(struct desktop_shell *shell)
3917{
3918 struct wl_event_loop *loop;
3919
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003920 if (!shell->fade.startup_timer)
3921 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003922
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003923 wl_event_source_remove(shell->fade.startup_timer);
3924 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003925
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003926 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3927 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003928}
3929
3930static int
3931fade_startup_timeout(void *data)
3932{
3933 struct desktop_shell *shell = data;
3934
3935 shell_fade_startup(shell);
3936 return 0;
3937}
3938
3939static void
3940shell_fade_init(struct desktop_shell *shell)
3941{
3942 /* Make compositor output all black, and wait for the desktop-shell
3943 * client to signal it is ready, then fade in. The timer triggers a
3944 * fade-in, in case the desktop-shell client takes too long.
3945 */
3946
3947 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003948
3949 if (shell->fade.view != NULL) {
3950 weston_log("%s: warning: fade surface already exists\n",
3951 __func__);
3952 return;
3953 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003954
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003955 if (shell->startup_animation_type == ANIMATION_NONE)
3956 return;
3957
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003958 shell->fade.view = shell_fade_create_surface(shell);
3959 if (!shell->fade.view)
3960 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003961
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003962 weston_view_update_transform(shell->fade.view);
3963 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003964
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003965 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3966 shell->fade.startup_timer =
3967 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3968 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003969}
3970
3971static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003972idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003973{
3974 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003975 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003976
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003977 struct weston_seat *seat;
3978
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003979 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3980 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003981
3982 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003983 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003984}
3985
3986static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003987wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003988{
3989 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003990 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003991
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003992 unlock(shell);
3993}
3994
3995static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003996transform_handler(struct wl_listener *listener, void *data)
3997{
3998 struct weston_surface *surface = data;
3999 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004000 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004001 int x, y;
4002
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004003 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004004 return;
4005
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004006 api = shsurf->shell->xwayland_surface_api;
4007 if (!api) {
4008 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4009 shsurf->shell->xwayland_surface_api = api;
4010 }
4011
4012 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004013 return;
4014
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004015 if (!weston_view_is_mapped(shsurf->view))
4016 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004017
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004018 x = shsurf->view->geometry.x;
4019 y = shsurf->view->geometry.y;
4020
4021 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004022}
4023
4024static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004025center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004026{
Giulio Camuffob8366642013-04-25 13:57:46 +03004027 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004028 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004029
Jason Ekstranda7af7042013-10-12 22:38:11 -05004030 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004031
4032 x = output->x + (output->width - width) / 2 - surf_x / 2;
4033 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004034
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004035 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004036}
4037
4038static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039weston_view_set_initial_position(struct weston_view *view,
4040 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004041{
4042 struct weston_compositor *compositor = shell->compositor;
4043 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004044 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004045 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004046 struct weston_output *output, *target_output = NULL;
4047 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004048 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004049
4050 /* As a heuristic place the new window on the same output as the
4051 * pointer. Falling back to the output containing 0, 0.
4052 *
4053 * TODO: Do something clever for touch too?
4054 */
4055 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004056 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4057
4058 if (pointer) {
4059 ix = wl_fixed_to_int(pointer->x);
4060 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004061 break;
4062 }
4063 }
4064
4065 wl_list_for_each(output, &compositor->output_list, link) {
4066 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4067 target_output = output;
4068 break;
4069 }
4070 }
4071
4072 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004073 weston_view_set_position(view, 10 + random() % 400,
4074 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004075 return;
4076 }
4077
4078 /* Valid range within output where the surface will still be onscreen.
4079 * If this is negative it means that the surface is bigger than
4080 * output.
4081 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004082 get_output_work_area(shell, target_output, &area);
4083
Derek Foremanf814c5d2015-04-15 12:23:54 -05004084 x = area.x;
4085 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004086 range_x = area.width - view->surface->width;
4087 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004088
Rob Bradford4cb88c72012-08-13 15:18:44 +01004089 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004090 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004091
4092 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004093 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004094
Jason Ekstranda7af7042013-10-12 22:38:11 -05004095 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004096}
4097
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004098static bool
4099check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4100{
4101 struct timespec now;
4102
4103 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4104 return false;
4105
4106 /*
4107 * If the shell helper client dies before the session has been
4108 * up for roughly 30 seconds, better just make Weston shut down,
4109 * because the user likely has no way to interact with the desktop
4110 * anyway.
4111 */
4112 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4113 weston_log("Error: %s apparently cannot run at all.\n",
4114 shell->client);
4115 weston_log_continue(STAMP_SPACE "Quitting...");
4116 wl_display_terminate(shell->compositor->wl_display);
4117
4118 return true;
4119 }
4120
4121 return false;
4122}
4123
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004124static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004125
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004126static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004127respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004128{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004129 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004130
4131 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4132 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004133 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004134 shell->child.deathstamp = time;
4135 shell->child.deathcount = 0;
4136 }
4137
4138 shell->child.deathcount++;
4139 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004140 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004141 return;
4142 }
4143
Pekka Paalanen826dc142014-08-27 12:11:53 +03004144 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004145 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004146}
4147
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004148static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004149desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4150{
4151 struct desktop_shell *shell;
4152
4153 shell = container_of(listener, struct desktop_shell,
4154 child.client_destroy_listener);
4155
Pekka Paalanen826dc142014-08-27 12:11:53 +03004156 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004157 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004158 /*
4159 * unbind_desktop_shell() will reset shell->child.desktop_shell
4160 * before the respawned process has a chance to create a new
4161 * desktop_shell object, because we are being called from the
4162 * wl_client destructor which destroys all wl_resources before
4163 * returning.
4164 */
4165
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004166 if (!check_desktop_shell_crash_too_early(shell))
4167 respawn_desktop_shell_process(shell);
4168
Pekka Paalanen826dc142014-08-27 12:11:53 +03004169 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004170}
4171
4172static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004173launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004174{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004175 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004176
Pekka Paalanen826dc142014-08-27 12:11:53 +03004177 shell->child.client = weston_client_start(shell->compositor,
4178 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004179
Arnaud Vrac42631192014-08-25 20:56:46 +02004180 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004181 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004182 return;
4183 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004184
4185 shell->child.client_destroy_listener.notify =
4186 desktop_shell_client_destroy;
4187 wl_client_add_destroy_listener(shell->child.client,
4188 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004189}
4190
4191static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004192unbind_desktop_shell(struct wl_resource *resource)
4193{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004194 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004195
4196 if (shell->locked)
4197 resume_desktop(shell);
4198
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004199 shell->child.desktop_shell = NULL;
4200 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004201}
4202
4203static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004204bind_desktop_shell(struct wl_client *client,
4205 void *data, uint32_t version, uint32_t id)
4206{
Tiago Vignattibe143262012-04-16 17:31:41 +03004207 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004208 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004209
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004210 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4211 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004212
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004213 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004214 wl_resource_set_implementation(resource,
4215 &desktop_shell_implementation,
4216 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004217 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004218 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004219 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004220
4221 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4222 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004223}
4224
Kristian Høgsberg07045392012-02-19 18:52:44 -05004225struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004226 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004227 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004228 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004229 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004230 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004231};
4232
4233static void
4234switcher_next(struct switcher *switcher)
4235{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004237 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004238 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004239 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004240
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004241 /* temporary re-display minimized surfaces */
4242 struct weston_view *tmp;
4243 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004244 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4245 weston_layer_entry_remove(&view->layer_link);
4246 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004247 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4248 *minimized = view;
4249 }
4250
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004251 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004252 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004253 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004254 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004255 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004256 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004257 next = view;
4258 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004259 view->alpha = 0.25;
4260 weston_view_geometry_dirty(view);
4261 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004262 }
Alex Wu1659daa2012-04-01 20:13:09 +08004263
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004264 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 view->alpha = 0.25;
4266 weston_view_geometry_dirty(view);
4267 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004268 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004269 }
4270
4271 if (next == NULL)
4272 next = first;
4273
Alex Wu07b26062012-03-12 16:06:01 +08004274 if (next == NULL)
4275 return;
4276
Kristian Høgsberg07045392012-02-19 18:52:44 -05004277 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004278 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004279
4280 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004281 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004283
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004284 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004285 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004286 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004287}
4288
4289static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004290switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004291{
4292 struct switcher *switcher =
4293 container_of(listener, struct switcher, listener);
4294
4295 switcher_next(switcher);
4296}
4297
4298static void
Daniel Stone351eb612012-05-31 15:27:47 -04004299switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004300{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004301 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004302 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004303 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004304
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004305 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004306 if (is_focus_view(view))
4307 continue;
4308
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 view->alpha = 1.0;
4310 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004311 }
4312
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004313 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004314 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004315 keyboard->seat,
4316 WESTON_ACTIVATE_FLAG_CONFIGURE);
4317 }
4318
Kristian Høgsberg07045392012-02-19 18:52:44 -05004319 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004320 weston_keyboard_end_grab(keyboard);
4321 if (keyboard->input_method_resource)
4322 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004323
4324 /* re-hide surfaces that were temporary shown during the switch */
4325 struct weston_view **minimized;
4326 wl_array_for_each(minimized, &switcher->minimized_array) {
4327 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004328 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004329 weston_layer_entry_remove(&(*minimized)->layer_link);
4330 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004331 weston_view_damage_below(*minimized);
4332 }
4333 }
4334 wl_array_release(&switcher->minimized_array);
4335
Kristian Høgsberg07045392012-02-19 18:52:44 -05004336 free(switcher);
4337}
4338
4339static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004340switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004341 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004342{
4343 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004344 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004345
Daniel Stonec9785ea2012-05-30 16:31:52 +01004346 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004347 switcher_next(switcher);
4348}
4349
4350static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004351switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004352 uint32_t mods_depressed, uint32_t mods_latched,
4353 uint32_t mods_locked, uint32_t group)
4354{
4355 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004356 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357
Daniel Stone351eb612012-05-31 15:27:47 -04004358 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4359 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004360}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004361
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004362static void
4363switcher_cancel(struct weston_keyboard_grab *grab)
4364{
4365 struct switcher *switcher = container_of(grab, struct switcher, grab);
4366
4367 switcher_destroy(switcher);
4368}
4369
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004370static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004371 switcher_key,
4372 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004373 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004374};
4375
4376static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004377switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4378 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379{
Tiago Vignattibe143262012-04-16 17:31:41 +03004380 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004381 struct switcher *switcher;
4382
4383 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004384 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004385 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004386 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004387 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004388 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004389
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004390 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004391 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004392 weston_keyboard_start_grab(keyboard, &switcher->grab);
4393 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004394 switcher_next(switcher);
4395}
4396
Pekka Paalanen3c647232011-12-22 13:43:43 +02004397static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004398backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4399 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004400{
4401 struct weston_compositor *compositor = data;
4402 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004403 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004404
4405 /* TODO: we're limiting to simple use cases, where we assume just
4406 * control on the primary display. We'd have to extend later if we
4407 * ever get support for setting backlights on random desktop LCD
4408 * panels though */
4409 output = get_default_output(compositor);
4410 if (!output)
4411 return;
4412
4413 if (!output->set_backlight)
4414 return;
4415
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004416 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4417 backlight_new = output->backlight_current - 25;
4418 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4419 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004420
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004421 if (backlight_new < 5)
4422 backlight_new = 5;
4423 if (backlight_new > 255)
4424 backlight_new = 255;
4425
4426 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004427 output->set_backlight(output, output->backlight_current);
4428}
4429
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004430static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004431force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4432 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004433{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004434 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004435 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004436 struct desktop_shell *shell = data;
4437 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004438 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004439
Derek Foreman8ae2db52015-07-15 13:00:36 -05004440 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004441 if (!focus_surface)
4442 return;
4443
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004444 wl_signal_emit(&compositor->kill_signal, focus_surface);
4445
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004446 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004447 wl_client_get_credentials(client, &pid, NULL, NULL);
4448
4449 /* Skip clients that we launched ourselves (the credentials of
4450 * the socketpair is ours) */
4451 if (pid == getpid())
4452 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004453
Daniel Stone325fc2d2012-05-30 16:31:58 +01004454 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004455}
4456
4457static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004458workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004459 uint32_t key, void *data)
4460{
4461 struct desktop_shell *shell = data;
4462 unsigned int new_index = shell->workspaces.current;
4463
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004464 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004465 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004466 if (new_index != 0)
4467 new_index--;
4468
4469 change_workspace(shell, new_index);
4470}
4471
4472static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004473workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004474 uint32_t key, void *data)
4475{
4476 struct desktop_shell *shell = data;
4477 unsigned int new_index = shell->workspaces.current;
4478
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004479 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004480 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004481 if (new_index < shell->workspaces.num - 1)
4482 new_index++;
4483
4484 change_workspace(shell, new_index);
4485}
4486
4487static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004488workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004489 uint32_t key, void *data)
4490{
4491 struct desktop_shell *shell = data;
4492 unsigned int new_index;
4493
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004494 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004495 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004496 new_index = key - KEY_F1;
4497 if (new_index >= shell->workspaces.num)
4498 new_index = shell->workspaces.num - 1;
4499
4500 change_workspace(shell, new_index);
4501}
4502
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004503static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004504workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4505 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004506{
4507 struct desktop_shell *shell = data;
4508 unsigned int new_index = shell->workspaces.current;
4509
4510 if (shell->locked)
4511 return;
4512
4513 if (new_index != 0)
4514 new_index--;
4515
Derek Foreman8ae2db52015-07-15 13:00:36 -05004516 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004517}
4518
4519static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004520workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4521 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004522{
4523 struct desktop_shell *shell = data;
4524 unsigned int new_index = shell->workspaces.current;
4525
4526 if (shell->locked)
4527 return;
4528
4529 if (new_index < shell->workspaces.num - 1)
4530 new_index++;
4531
Derek Foreman8ae2db52015-07-15 13:00:36 -05004532 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004533}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004534
4535static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004536shell_reposition_view_on_output_destroy(struct weston_view *view)
4537{
4538 struct weston_output *output, *first_output;
4539 struct weston_compositor *ec = view->surface->compositor;
4540 struct shell_surface *shsurf;
4541 float x, y;
4542 int visible;
4543
4544 x = view->geometry.x;
4545 y = view->geometry.y;
4546
4547 /* At this point the destroyed output is not in the list anymore.
4548 * If the view is still visible somewhere, we leave where it is,
4549 * otherwise, move it to the first output. */
4550 visible = 0;
4551 wl_list_for_each(output, &ec->output_list, link) {
4552 if (pixman_region32_contains_point(&output->region,
4553 x, y, NULL)) {
4554 visible = 1;
4555 break;
4556 }
4557 }
4558
4559 if (!visible) {
4560 first_output = container_of(ec->output_list.next,
4561 struct weston_output, link);
4562
4563 x = first_output->x + first_output->width / 4;
4564 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004565
4566 weston_view_set_position(view, x, y);
4567 } else {
4568 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004569 }
4570
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004571
4572 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004573 if (!shsurf)
4574 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004575
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004576 shsurf->saved_position_valid = false;
4577 set_maximized(shsurf, false);
4578 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004579}
4580
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004581void
4582shell_for_each_layer(struct desktop_shell *shell,
4583 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004584{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004585 struct workspace **ws;
4586
4587 func(shell, &shell->fullscreen_layer, data);
4588 func(shell, &shell->panel_layer, data);
4589 func(shell, &shell->background_layer, data);
4590 func(shell, &shell->lock_layer, data);
4591 func(shell, &shell->input_panel_layer, data);
4592
4593 wl_array_for_each(ws, &shell->workspaces.array)
4594 func(shell, &(*ws)->layer, data);
4595}
4596
4597static void
4598shell_output_destroy_move_layer(struct desktop_shell *shell,
4599 struct weston_layer *layer,
4600 void *data)
4601{
4602 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004603 struct weston_view *view;
4604
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004605 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004606 if (view->output != output)
4607 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004608
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004609 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004610 }
4611}
4612
4613static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004614handle_output_destroy(struct wl_listener *listener, void *data)
4615{
4616 struct shell_output *output_listener =
4617 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004618 struct weston_output *output = output_listener->output;
4619 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004620
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004621 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004622
Quentin Glidic561201c2016-08-20 18:19:51 +02004623 wl_list_remove(&output_listener->panel_surface_listener.link);
4624 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004625 wl_list_remove(&output_listener->destroy_listener.link);
4626 wl_list_remove(&output_listener->link);
4627 free(output_listener);
4628}
4629
4630static void
David Fort50d962f2016-06-09 17:55:52 +02004631shell_resize_surface_to_output(struct desktop_shell *shell,
4632 struct weston_surface *surface,
4633 const struct weston_output *output)
4634{
4635 if (!surface)
4636 return;
4637
4638 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4639 surface->resource,
4640 output->width,
4641 output->height);
4642}
4643
4644
4645static void
4646handle_output_resized(struct wl_listener *listener, void *data)
4647{
4648 struct desktop_shell *shell =
4649 container_of(listener, struct desktop_shell, resized_listener);
4650 struct weston_output *output = (struct weston_output *)data;
4651 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4652
4653 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4654 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4655}
4656
4657static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004658create_shell_output(struct desktop_shell *shell,
4659 struct weston_output *output)
4660{
4661 struct shell_output *shell_output;
4662
4663 shell_output = zalloc(sizeof *shell_output);
4664 if (shell_output == NULL)
4665 return;
4666
4667 shell_output->output = output;
4668 shell_output->shell = shell;
4669 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004670 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004671 wl_signal_add(&output->destroy_signal,
4672 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004673 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004674}
4675
4676static void
4677handle_output_create(struct wl_listener *listener, void *data)
4678{
4679 struct desktop_shell *shell =
4680 container_of(listener, struct desktop_shell, output_create_listener);
4681 struct weston_output *output = (struct weston_output *)data;
4682
4683 create_shell_output(shell, output);
4684}
4685
4686static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004687handle_output_move_layer(struct desktop_shell *shell,
4688 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004689{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004690 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004691 struct weston_view *view;
4692 float x, y;
4693
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004694 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004695 if (view->output != output)
4696 continue;
4697
4698 x = view->geometry.x + output->move_x;
4699 y = view->geometry.y + output->move_y;
4700 weston_view_set_position(view, x, y);
4701 }
4702}
4703
4704static void
4705handle_output_move(struct wl_listener *listener, void *data)
4706{
4707 struct desktop_shell *shell;
4708
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004709 shell = container_of(listener, struct desktop_shell,
4710 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004711
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004712 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004713}
4714
4715static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004716setup_output_destroy_handler(struct weston_compositor *ec,
4717 struct desktop_shell *shell)
4718{
4719 struct weston_output *output;
4720
4721 wl_list_init(&shell->output_list);
4722 wl_list_for_each(output, &ec->output_list, link)
4723 create_shell_output(shell, output);
4724
4725 shell->output_create_listener.notify = handle_output_create;
4726 wl_signal_add(&ec->output_created_signal,
4727 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004728
4729 shell->output_move_listener.notify = handle_output_move;
4730 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004731}
4732
4733static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004734shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004735{
Tiago Vignattibe143262012-04-16 17:31:41 +03004736 struct desktop_shell *shell =
4737 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004738 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004739 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004740
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004741 /* Force state to unlocked so we don't try to fade */
4742 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004743
4744 if (shell->child.client) {
4745 /* disable respawn */
4746 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004747 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004748 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004749
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004750 wl_list_remove(&shell->idle_listener.link);
4751 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004752 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004753
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004754 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004755 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004756
Xiong Zhang6b481422013-10-23 13:58:32 +08004757 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4758 wl_list_remove(&shell_output->destroy_listener.link);
4759 wl_list_remove(&shell_output->link);
4760 free(shell_output);
4761 }
4762
4763 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004764 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004765 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004766
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004767 wl_array_for_each(ws, &shell->workspaces.array)
4768 workspace_destroy(*ws);
4769 wl_array_release(&shell->workspaces.array);
4770
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004771 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004772 free(shell);
4773}
4774
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004775static void
4776shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4777{
4778 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004779 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004780
Bob Ham744e6532016-01-12 10:21:48 +00004781 if (shell->allow_zap)
4782 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4783 MODIFIER_CTRL | MODIFIER_ALT,
4784 terminate_binding, ec);
4785
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004786 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004787 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4788 click_to_activate_binding,
4789 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004790 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4791 click_to_activate_binding,
4792 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004793 weston_compositor_add_touch_binding(ec, 0,
4794 touch_to_activate_binding,
4795 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004796 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4797 MODIFIER_SUPER | MODIFIER_ALT,
4798 surface_opacity_binding, NULL);
4799 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4800 MODIFIER_SUPER, zoom_axis_binding,
4801 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004802 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4803 backlight_binding, ec);
4804 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4805 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004806
4807 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004808 if (shell->exposay_modifier)
4809 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4810 exposay_binding, shell);
4811
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004812 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004813 if (!mod)
4814 return;
4815
Daniel Stone325fc2d2012-05-30 16:31:58 +01004816 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4817 zoom_key_binding, NULL);
4818 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4819 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004820 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4821 maximize_binding, NULL);
4822 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4823 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4825 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004826 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004827 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004828 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004829 weston_compositor_add_button_binding(ec, BTN_LEFT,
4830 mod | MODIFIER_SHIFT,
4831 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004832
4833 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004834 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004835 rotate_binding, NULL);
4836
Daniel Stone325fc2d2012-05-30 16:31:58 +01004837 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4838 shell);
4839 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4840 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004841 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4842 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004843 weston_compositor_add_key_binding(ec, KEY_K, mod,
4844 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004845 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4846 workspace_up_binding, shell);
4847 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4848 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004849 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4850 workspace_move_surface_up_binding,
4851 shell);
4852 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4853 workspace_move_surface_down_binding,
4854 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004855
4856 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4857 if (shell->workspaces.num > 1) {
4858 num_workspace_bindings = shell->workspaces.num;
4859 if (num_workspace_bindings > 6)
4860 num_workspace_bindings = 6;
4861 for (i = 0; i < num_workspace_bindings; i++)
4862 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4863 workspace_f_binding,
4864 shell);
4865 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004866
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004867 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004868}
4869
Jason Ekstrand024177c2014-04-21 19:42:58 -05004870static void
4871handle_seat_created(struct wl_listener *listener, void *data)
4872{
4873 struct weston_seat *seat = data;
4874
4875 create_shell_seat(seat);
4876}
4877
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004878WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004879module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004880 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004881{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004882 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004883 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004884 struct workspace **pws;
4885 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004886 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004887
Peter Huttererf3d62272013-08-08 11:57:05 +10004888 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004889 if (shell == NULL)
4890 return -1;
4891
Kristian Høgsberg75840622011-09-06 13:48:16 -04004892 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004893
4894 shell->destroy_listener.notify = shell_destroy;
4895 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004896 shell->idle_listener.notify = idle_handler;
4897 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4898 shell->wake_listener.notify = wake_handler;
4899 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004900 shell->transform_listener.notify = transform_handler;
4901 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004902
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004903 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4904 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004905 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4906 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004907 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004908
4909 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004910 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004911
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004912 if (input_panel_setup(shell) < 0)
4913 return -1;
4914
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004915 shell->text_backend = text_backend_init(ec);
4916 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004917 return -1;
4918
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004919 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004920
Daniel Stonedf8133b2013-11-19 11:37:14 +01004921 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4922 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4923
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004924 for (i = 0; i < shell->workspaces.num; i++) {
4925 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4926 if (pws == NULL)
4927 return -1;
4928
4929 *pws = workspace_create();
4930 if (*pws == NULL)
4931 return -1;
4932 }
4933 activate_workspace(shell, 0);
4934
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004935 weston_layer_init(&shell->minimized_layer, NULL);
4936
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004937 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004938 wl_list_init(&shell->workspaces.animation.link);
4939 shell->workspaces.animation.frame = animate_workspace_change_frame;
4940
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004941 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4942 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004943 return -1;
4944
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004945 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004946 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004947 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004948 return -1;
4949
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004950 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004951
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004952 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004953
Xiong Zhang6b481422013-10-23 13:58:32 +08004954 setup_output_destroy_handler(ec, shell);
4955
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004956 loop = wl_display_get_event_loop(ec->wl_display);
4957 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004958
Jason Ekstrand024177c2014-04-21 19:42:58 -05004959 wl_list_for_each(seat, &ec->seat_list, link)
4960 handle_seat_created(NULL, seat);
4961 shell->seat_create_listener.notify = handle_seat_created;
4962 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004963
David Fort50d962f2016-06-09 17:55:52 +02004964 shell->resized_listener.notify = handle_output_resized;
4965 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4966
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004967 screenshooter_create(ec);
4968
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004969 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004970
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004971 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004972
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004973 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4974
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004975 return 0;
4976}