blob: 1b182110a8d84d022a358b775fb384dfb4781485 [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 *
Quentin Glidic82681572016-12-17 13:40:51 +0100895workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896{
897 struct workspace *ws = malloc(sizeof *ws);
898 if (ws == NULL)
899 return NULL;
900
Quentin Glidic82681572016-12-17 13:40:51 +0100901 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200902
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);
Quentin Glidic82681572016-12-17 13:40:51 +0100940 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941
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
Quentin Glidic82681572016-12-17 13:40:51 +01001021 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1022 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1023
Scott Moreau4272e632012-08-13 09:58:41 -06001024 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001025}
1026
1027static void
1028workspace_deactivate_transforms(struct workspace *ws)
1029{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001033 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 if (is_focus_view(view)) {
1035 struct focus_surface *fsurf = get_focus_surface(view->surface);
1036 transform = &fsurf->workspace_transform;
1037 } else {
1038 struct shell_surface *shsurf = get_shell_surface(view->surface);
1039 transform = &shsurf->workspace_transform;
1040 }
1041
1042 if (!wl_list_empty(&transform->link)) {
1043 wl_list_remove(&transform->link);
1044 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001045 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047 }
1048}
1049
1050static void
1051finish_workspace_change_animation(struct desktop_shell *shell,
1052 struct workspace *from,
1053 struct workspace *to)
1054{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001055 struct weston_view *view;
1056
Scott Moreau4272e632012-08-13 09:58:41 -06001057 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001059 /* Views that extend past the bottom of the output are still
1060 * visible after the workspace animation ends but before its layer
1061 * is hidden. In that case, we need to damage below those views so
1062 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001063 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001064 weston_view_damage_below(view);
1065
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066 wl_list_remove(&shell->workspaces.animation.link);
1067 workspace_deactivate_transforms(from);
1068 workspace_deactivate_transforms(to);
1069 shell->workspaces.anim_to = NULL;
1070
Quentin Glidic82681572016-12-17 13:40:51 +01001071 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072}
1073
1074static void
1075animate_workspace_change_frame(struct weston_animation *animation,
1076 struct weston_output *output, uint32_t msecs)
1077{
1078 struct desktop_shell *shell =
1079 container_of(animation, struct desktop_shell,
1080 workspaces.animation);
1081 struct workspace *from = shell->workspaces.anim_from;
1082 struct workspace *to = shell->workspaces.anim_to;
1083 uint32_t t;
1084 double x, y;
1085
1086 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1087 finish_workspace_change_animation(shell, from, to);
1088 return;
1089 }
1090
1091 if (shell->workspaces.anim_timestamp == 0) {
1092 if (shell->workspaces.anim_current == 0.0)
1093 shell->workspaces.anim_timestamp = msecs;
1094 else
1095 shell->workspaces.anim_timestamp =
1096 msecs -
1097 /* Invers of movement function 'y' below. */
1098 (asin(1.0 - shell->workspaces.anim_current) *
1099 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1100 M_2_PI);
1101 }
1102
1103 t = msecs - shell->workspaces.anim_timestamp;
1104
1105 /*
1106 * x = [0, π/2]
1107 * y(x) = sin(x)
1108 */
1109 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1110 y = sin(x);
1111
1112 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001113 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114
1115 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1116 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1117 shell->workspaces.anim_current = y;
1118
Scott Moreau4272e632012-08-13 09:58:41 -06001119 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001121 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123}
1124
1125static void
1126animate_workspace_change(struct desktop_shell *shell,
1127 unsigned int index,
1128 struct workspace *from,
1129 struct workspace *to)
1130{
1131 struct weston_output *output;
1132
1133 int dir;
1134
1135 if (index > shell->workspaces.current)
1136 dir = -1;
1137 else
1138 dir = 1;
1139
1140 shell->workspaces.current = index;
1141
1142 shell->workspaces.anim_dir = dir;
1143 shell->workspaces.anim_from = from;
1144 shell->workspaces.anim_to = to;
1145 shell->workspaces.anim_current = 0.0;
1146 shell->workspaces.anim_timestamp = 0;
1147
1148 output = container_of(shell->compositor->output_list.next,
1149 struct weston_output, link);
1150 wl_list_insert(&output->animation_list,
1151 &shell->workspaces.animation.link);
1152
Quentin Glidic82681572016-12-17 13:40:51 +01001153 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1154 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155
1156 workspace_translate_in(to, 0);
1157
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001158 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001159
Scott Moreau4272e632012-08-13 09:58:41 -06001160 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161}
1162
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001163static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001164update_workspace(struct desktop_shell *shell, unsigned int index,
1165 struct workspace *from, struct workspace *to)
1166{
1167 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001168 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1169 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001170}
1171
1172static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001173change_workspace(struct desktop_shell *shell, unsigned int index)
1174{
1175 struct workspace *from;
1176 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001177 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001178
1179 if (index == shell->workspaces.current)
1180 return;
1181
1182 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001183 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184 return;
1185
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186 from = get_current_workspace(shell);
1187 to = get_workspace(shell, index);
1188
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001189 if (shell->workspaces.anim_from == to &&
1190 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 reverse_workspace_change_animation(shell, index, from, to);
1193 return;
1194 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001195
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001196 if (shell->workspaces.anim_to != NULL)
1197 finish_workspace_change_animation(shell,
1198 shell->workspaces.anim_from,
1199 shell->workspaces.anim_to);
1200
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001201 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001202
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001203 if (shell->focus_animation_type != ANIMATION_NONE) {
1204 wl_list_for_each(state, &from->focus_list, link)
1205 if (state->keyboard_focus)
1206 animate_focus_change(shell, from,
1207 get_default_view(state->keyboard_focus), NULL);
1208
1209 wl_list_for_each(state, &to->focus_list, link)
1210 if (state->keyboard_focus)
1211 animate_focus_change(shell, to,
1212 NULL, get_default_view(state->keyboard_focus));
1213 }
1214
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001215 if (workspace_is_empty(to) && workspace_is_empty(from))
1216 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217 else
1218 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001219}
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221static bool
1222workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1223{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001224 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001225 struct wl_list *e;
1226
1227 if (wl_list_empty(list))
1228 return false;
1229
1230 e = list->next;
1231
1232 if (e->next != list)
1233 return false;
1234
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001235 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001236}
1237
1238static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001239surface_keyboard_focus_lost(struct weston_surface *surface)
1240{
1241 struct weston_compositor *compositor = surface->compositor;
1242 struct weston_seat *seat;
1243 struct weston_surface *focus;
1244
1245 wl_list_for_each(seat, &compositor->seat_list, link) {
1246 struct weston_keyboard *keyboard =
1247 weston_seat_get_keyboard(seat);
1248
1249 if (!keyboard)
1250 continue;
1251
1252 focus = weston_surface_get_main_surface(keyboard->focus);
1253 if (focus == surface)
1254 weston_keyboard_set_focus(keyboard, NULL);
1255 }
1256}
1257
1258static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001259take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001260 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261 unsigned int index)
1262{
Derek Foreman1281a362015-07-31 16:55:32 -05001263 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001264 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 struct shell_surface *shsurf;
1267 struct workspace *from;
1268 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001269 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270
Derek Foreman1281a362015-07-31 16:55:32 -05001271 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 view = get_default_view(surface);
1273 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001274 index == shell->workspaces.current ||
1275 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276 return;
1277
1278 from = get_current_workspace(shell);
1279 to = get_workspace(shell, index);
1280
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001281 weston_layer_entry_remove(&view->layer_link);
1282 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283
Philip Withnall659163d2013-11-25 18:01:36 +00001284 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001285 if (shsurf != NULL)
1286 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001287
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 drop_focus_state(shell, from, surface);
1290
1291 if (shell->workspaces.anim_from == to &&
1292 shell->workspaces.anim_to == from) {
1293 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001294
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 return;
1296 }
1297
1298 if (shell->workspaces.anim_to != NULL)
1299 finish_workspace_change_animation(shell,
1300 shell->workspaces.anim_from,
1301 shell->workspaces.anim_to);
1302
1303 if (workspace_is_empty(from) &&
1304 workspace_has_only(to, surface))
1305 update_workspace(shell, index, from, to);
1306 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001307 if (shsurf != NULL &&
1308 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001309 wl_list_insert(&shell->workspaces.anim_sticky_list,
1310 &shsurf->workspace_transform.link);
1311
1312 animate_workspace_change(shell, index, from, to);
1313 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001314
Jonas Ådahl8538b222012-08-29 22:13:03 +02001315 state = ensure_focus_state(shell, seat);
1316 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001317 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318}
1319
1320static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001321touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001322 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001323{
1324}
1325
1326static void
1327touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1328{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001329 struct weston_touch_move_grab *move =
1330 (struct weston_touch_move_grab *) container_of(
1331 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001332
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001333 if (touch_id == 0)
1334 move->active = 0;
1335
Jonas Ådahl9484b692013-12-02 22:05:03 +01001336 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001337 shell_touch_grab_end(&move->base);
1338 free(move);
1339 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001340}
1341
1342static void
1343touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001344 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001345{
1346 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1347 struct shell_surface *shsurf = move->base.shsurf;
1348 struct weston_surface *es;
1349 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1350 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1351
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001352 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353 return;
1354
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001355 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001356
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001357 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001358
1359 weston_compositor_schedule_repaint(es->compositor);
1360}
1361
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001362static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001363touch_move_grab_frame(struct weston_touch_grab *grab)
1364{
1365}
1366
1367static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001368touch_move_grab_cancel(struct weston_touch_grab *grab)
1369{
1370 struct weston_touch_move_grab *move =
1371 (struct weston_touch_move_grab *) container_of(
1372 grab, struct shell_touch_grab, grab);
1373
1374 shell_touch_grab_end(&move->base);
1375 free(move);
1376}
1377
Rusty Lynch1084da52013-08-15 09:10:08 -07001378static const struct weston_touch_grab_interface touch_move_grab_interface = {
1379 touch_move_grab_down,
1380 touch_move_grab_up,
1381 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001382 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001383 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001384};
1385
1386static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001387surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001388{
1389 struct weston_touch_move_grab *move;
1390
1391 if (!shsurf)
1392 return -1;
1393
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001394 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1395 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001396 return 0;
1397
1398 move = malloc(sizeof *move);
1399 if (!move)
1400 return -1;
1401
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001402 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001404 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001406 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001407
1408 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001409 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001410
1411 return 0;
1412}
1413
1414static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001415noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001416{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001417}
1418
1419static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001420noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001421 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001422{
1423}
1424
1425static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001426noop_grab_axis_source(struct weston_pointer_grab *grab,
1427 uint32_t source)
1428{
1429}
1430
1431static void
1432noop_grab_frame(struct weston_pointer_grab *grab)
1433{
1434}
1435
1436static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001437constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1438{
1439 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001440 struct weston_surface *surface =
1441 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001442 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001443 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001444 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001445 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001446 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001447
1448 x = wl_fixed_to_int(pointer->x + move->dx);
1449 y = wl_fixed_to_int(pointer->y + move->dy);
1450
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001451 if (shsurf->shell->panel_position ==
1452 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001453 get_output_work_area(shsurf->shell, surface->output, &area);
1454 geometry =
1455 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001456
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001457 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001458 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001459 y = area.y + safety - geometry.height
1460 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001461
1462 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001463 y + geometry.y < area.y)
1464 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001465 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001466
1467 *cx = x;
1468 *cy = y;
1469}
1470
1471static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001472move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001473 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001474{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001476 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001477 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001478 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001479 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001480
Jonas Ådahld2510102014-10-05 21:39:14 +02001481 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001482 if (!shsurf)
1483 return;
1484
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001485 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1486
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001487 constrain_position(move, &cx, &cy);
1488
1489 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001490
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001491 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001492}
1493
1494static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001495move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001496 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001497{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001498 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1499 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001500 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001501 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502
Daniel Stone4dbadb12012-05-30 16:31:51 +01001503 if (pointer->button_count == 0 &&
1504 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001505 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001506 free(grab);
1507 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508}
1509
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001510static void
1511move_grab_cancel(struct weston_pointer_grab *grab)
1512{
1513 struct shell_grab *shell_grab =
1514 container_of(grab, struct shell_grab, grab);
1515
1516 shell_grab_end(shell_grab);
1517 free(grab);
1518}
1519
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001520static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001521 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522 move_grab_motion,
1523 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001524 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001525 noop_grab_axis_source,
1526 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001527 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001528};
1529
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001530static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001531surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001532 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001533{
1534 struct weston_move_grab *move;
1535
1536 if (!shsurf)
1537 return -1;
1538
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001539 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001540 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1541 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542 return 0;
1543
1544 move = malloc(sizeof *move);
1545 if (!move)
1546 return -1;
1547
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001549 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001551 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001552 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553
1554 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001555 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556
1557 return 0;
1558}
1559
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001560struct weston_resize_grab {
1561 struct shell_grab base;
1562 uint32_t edges;
1563 int32_t width, height;
1564};
1565
1566static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001567resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001568 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569{
1570 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001571 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572 struct shell_surface *shsurf = resize->base.shsurf;
1573 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001574 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575 wl_fixed_t from_x, from_y;
1576 wl_fixed_t to_x, to_y;
1577
Jonas Ådahld2510102014-10-05 21:39:14 +02001578 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001579
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001580 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 return;
1582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 weston_view_from_global_fixed(shsurf->view,
1584 pointer->grab_x, pointer->grab_y,
1585 &from_x, &from_y);
1586 weston_view_from_global_fixed(shsurf->view,
1587 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588
1589 width = resize->width;
1590 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1591 width += wl_fixed_to_int(from_x - to_x);
1592 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1593 width += wl_fixed_to_int(to_x - from_x);
1594 }
1595
1596 height = resize->height;
1597 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1598 height += wl_fixed_to_int(from_y - to_y);
1599 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1600 height += wl_fixed_to_int(to_y - from_y);
1601 }
1602
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001603 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1604 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1605
1606 min_size.width = MAX(1, min_size.width);
1607 min_size.height = MAX(1, min_size.height);
1608
1609 if (width < min_size.width)
1610 width = min_size.width;
1611 else if (max_size.width > 0 && width > max_size.width)
1612 width = max_size.width;
1613 if (height < min_size.height)
1614 height = min_size.height;
1615 else if (max_size.width > 0 && width > max_size.width)
1616 width = max_size.width;
1617 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001618}
1619
1620static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001621resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622 uint32_t time, uint32_t button, uint32_t state_w)
1623{
1624 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001625 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626 enum wl_pointer_button_state state = state_w;
1627
1628 if (pointer->button_count == 0 &&
1629 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1630 shell_grab_end(&resize->base);
1631 free(grab);
1632 }
1633}
1634
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001635static void
1636resize_grab_cancel(struct weston_pointer_grab *grab)
1637{
1638 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1639
1640 shell_grab_end(&resize->base);
1641 free(grab);
1642}
1643
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001644static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645 noop_grab_focus,
1646 resize_grab_motion,
1647 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001648 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001649 noop_grab_axis_source,
1650 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001651 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001652};
1653
Giulio Camuffob8366642013-04-25 13:57:46 +03001654/*
1655 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001656 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001657static void
1658surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1659 int32_t *y, int32_t *w, int32_t *h) {
1660 pixman_region32_t region;
1661 pixman_box32_t *box;
1662 struct weston_subsurface *subsurface;
1663
1664 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 surface->width,
1666 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001667
1668 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1669 pixman_region32_union_rect(&region, &region,
1670 subsurface->position.x,
1671 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 subsurface->surface->width,
1673 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001674 }
1675
1676 box = pixman_region32_extents(&region);
1677 if (x)
1678 *x = box->x1;
1679 if (y)
1680 *y = box->y1;
1681 if (w)
1682 *w = box->x2 - box->x1;
1683 if (h)
1684 *h = box->y2 - box->y1;
1685
1686 pixman_region32_fini(&region);
1687}
1688
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001689static int
1690surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001691 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692{
1693 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001694 const unsigned resize_topbottom =
1695 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1696 const unsigned resize_leftright =
1697 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1698 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001699 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001701 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001702 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1703 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704 return 0;
1705
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001706 /* Check for invalid edge combinations. */
1707 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1708 (edges & resize_topbottom) == resize_topbottom ||
1709 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710 return 0;
1711
1712 resize = malloc(sizeof *resize);
1713 if (!resize)
1714 return -1;
1715
1716 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001718 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1719 resize->width = geometry.width;
1720 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001721
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001722 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001724 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725
1726 return 0;
1727}
1728
1729static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001730busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001731{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001732 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001733 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001734 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001736 wl_fixed_t sx, sy;
1737
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 view = weston_compositor_pick_view(pointer->seat->compositor,
1739 pointer->x, pointer->y,
1740 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001741 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001742
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001743 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001744 shell_grab_end(grab);
1745 free(grab);
1746 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001747}
1748
1749static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001750busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001751 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001752{
Jonas Ådahld2510102014-10-05 21:39:14 +02001753 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001757busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001758 uint32_t time, uint32_t button, uint32_t state)
1759{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001760 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001761 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001762 struct weston_pointer *pointer = grab->grab.pointer;
1763 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001764
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001765 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001766 activate(shsurf->shell, shsurf->view, seat,
1767 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001768 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001769 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001770 activate(shsurf->shell, shsurf->view, seat,
1771 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001772 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001773 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774}
1775
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001776static void
1777busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1778{
1779 struct shell_grab *grab = (struct shell_grab *) base;
1780
1781 shell_grab_end(grab);
1782 free(grab);
1783}
1784
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001785static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786 busy_cursor_grab_focus,
1787 busy_cursor_grab_motion,
1788 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001789 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001790 noop_grab_axis_source,
1791 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001792 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793};
1794
1795static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796handle_pointer_focus(struct wl_listener *listener, void *data)
1797{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001798 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001800 struct shell_surface *shsurf;
1801 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 return;
1805
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001806 shsurf = get_shell_surface(view->surface);
1807 if (!shsurf)
1808 return;
1809
1810 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1811
1812 if (shsurf->unresponsive)
1813 set_busy_cursor(shsurf, pointer);
1814 else
1815 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816}
1817
1818static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001819shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1820{
1821 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001822 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001823}
1824
1825static void
1826shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1827{
1828 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001829 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001830}
1831
1832static void
1833handle_keyboard_focus(struct wl_listener *listener, void *data)
1834{
1835 struct weston_keyboard *keyboard = data;
1836 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1837
1838 if (seat->focused_surface) {
1839 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1840 if (shsurf)
1841 shell_surface_lose_keyboard_focus(shsurf);
1842 }
1843
1844 seat->focused_surface = keyboard->focus;
1845
1846 if (seat->focused_surface) {
1847 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1848 if (shsurf)
1849 shell_surface_gain_keyboard_focus(shsurf);
1850 }
1851}
1852
Philip Withnall07926d92013-11-25 18:01:40 +00001853/* The surface will be inserted into the list immediately after the link
1854 * returned by this function (i.e. will be stacked immediately above the
1855 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001856static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001857shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1858{
1859 struct workspace *ws;
1860
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001861 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1862 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001863 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001864 }
1865
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001866 /* Move the surface to a normal workspace layer so that surfaces
1867 * which were previously fullscreen or transient are no longer
1868 * rendered on top. */
1869 ws = get_current_workspace(shsurf->shell);
1870 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001871}
1872
Philip Withnall648a4dd2013-11-25 18:01:44 +00001873static void
1874shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1875{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001876 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001877}
1878
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001879/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001880 * geometry to ensure the changes are redrawn.
1881 *
1882 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1883 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001884static void
1885shell_surface_update_layer(struct shell_surface *shsurf)
1886{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001887 struct weston_surface *surface =
1888 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001889 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001890
1891 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1892
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001893 if (new_layer_link == NULL)
1894 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001895 if (new_layer_link == &shsurf->view->layer_link)
1896 return;
1897
1898 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001899 weston_layer_entry_remove(&shsurf->view->layer_link);
1900 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001901 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001902 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001903
1904 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001905}
1906
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001907static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001908shell_surface_set_output(struct shell_surface *shsurf,
1909 struct weston_output *output)
1910{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001911 struct weston_surface *es =
1912 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001913
1914 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001915 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001916 if (output)
1917 shsurf->output = output;
1918 else if (es->output)
1919 shsurf->output = es->output;
1920 else
1921 shsurf->output = get_default_output(es->compositor);
1922}
1923
1924static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001925weston_view_set_initial_position(struct weston_view *view,
1926 struct desktop_shell *shell);
1927
1928static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001929unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001930{
Philip Withnallf85fe842013-11-25 18:01:37 +00001931 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001932 wl_list_remove(&shsurf->fullscreen.transform.link);
1933 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001934
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935 if (shsurf->fullscreen.black_view)
1936 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1937 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001938
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001939 if (shsurf->saved_position_valid)
1940 weston_view_set_position(shsurf->view,
1941 shsurf->saved_x, shsurf->saved_y);
1942 else
1943 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001944 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001945
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001946 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001948 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001949 shsurf->saved_rotation_valid = false;
1950 }
Alex Wu4539b082012-03-01 12:57:46 +08001951}
1952
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001953static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001954unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001955{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001956 struct weston_surface *surface =
1957 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1958
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001959 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001960 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001961
1962 if (shsurf->saved_position_valid)
1963 weston_view_set_position(shsurf->view,
1964 shsurf->saved_x, shsurf->saved_y);
1965 else
1966 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001967 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001968
1969 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 wl_list_insert(&shsurf->view->geometry.transformation_list,
1971 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001972 shsurf->saved_rotation_valid = false;
1973 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001974}
1975
Philip Withnallbecb77e2013-11-25 18:01:30 +00001976static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001977set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001978{
1979 struct shell_surface *shsurf;
1980 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001981 struct weston_view *view;
1982
1983 view = get_default_view(surface);
1984 if (!view)
1985 return;
1986
1987 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1988
1989 shsurf = get_shell_surface(surface);
1990 current_ws = get_current_workspace(shsurf->shell);
1991
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001992 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001993 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001994
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001995 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02001996 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001997
1998 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001999 weston_view_damage_below(view);
2000}
2001
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002002
Tiago Vignattibe143262012-04-16 17:31:41 +03002003static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002004shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002005{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002006 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002007}
2008
Pekka Paalanen8274d902014-08-06 19:36:51 +03002009static int
2010black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2011{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002012 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002013 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002014 int n;
2015 int rem;
2016 int ret;
2017
2018 n = snprintf(buf, len, "black background surface for ");
2019 if (n < 0)
2020 return n;
2021
2022 rem = (int)len - n;
2023 if (rem < 0)
2024 rem = 0;
2025
2026 if (fs_surface->get_label)
2027 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2028 else
2029 ret = snprintf(buf + n, rem, "<unknown>");
2030
2031 if (ret < 0)
2032 return n;
2033
2034 return n + ret;
2035}
2036
Alex Wu21858432012-04-01 20:13:08 +08002037static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002038black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002039
Jason Ekstranda7af7042013-10-12 22:38:11 -05002040static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002041create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002042 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002043 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002044{
2045 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002047
2048 surface = weston_surface_create(ec);
2049 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002050 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002051 return NULL;
2052 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 view = weston_view_create(surface);
2054 if (surface == NULL) {
2055 weston_log("no memory\n");
2056 weston_surface_destroy(surface);
2057 return NULL;
2058 }
Alex Wu4539b082012-03-01 12:57:46 +08002059
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002060 surface->committed = black_surface_committed;
2061 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002062 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002063 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002064 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002065 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002066 pixman_region32_fini(&surface->input);
2067 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002068
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002069 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002070 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071
2072 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002073}
2074
Philip Withnalled8f1a92013-11-25 18:01:43 +00002075static void
2076shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2077{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002078 struct weston_surface *surface =
2079 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002080 struct weston_output *output = shsurf->fullscreen_output;
2081
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002082 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002083
2084 if (!shsurf->fullscreen.black_view)
2085 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002086 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002087 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002088 output->x, output->y,
2089 output->width,
2090 output->height);
2091
2092 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002093 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2094 weston_layer_entry_insert(&shsurf->view->layer_link,
2095 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002096 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002097 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002098
Armin Krezović4663aca2016-06-30 06:04:29 +02002099 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002100 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002101}
2102
Alex Wu4539b082012-03-01 12:57:46 +08002103/* Create black surface and append it to the associated fullscreen surface.
2104 * Handle size dismatch and positioning according to the method. */
2105static void
2106shell_configure_fullscreen(struct shell_surface *shsurf)
2107{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002108 struct weston_surface *surface =
2109 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002110 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002111
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002112 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002113 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002114 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2115 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002116
Philip Withnalled8f1a92013-11-25 18:01:43 +00002117 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002118
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002119 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002120 &surf_width, &surf_height);
2121
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002122 if (surface->buffer_ref.buffer)
2123 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002124}
2125
Alex Wu4539b082012-03-01 12:57:46 +08002126static void
2127shell_map_fullscreen(struct shell_surface *shsurf)
2128{
Alex Wubd3354b2012-04-17 17:20:49 +08002129 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002130}
2131
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002132static struct weston_output *
2133get_focused_output(struct weston_compositor *compositor)
2134{
2135 struct weston_seat *seat;
2136 struct weston_output *output = NULL;
2137
2138 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002139 struct weston_touch *touch = weston_seat_get_touch(seat);
2140 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2141 struct weston_keyboard *keyboard =
2142 weston_seat_get_keyboard(seat);
2143
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002144 /* Priority has touch focus, then pointer and
2145 * then keyboard focus. We should probably have
2146 * three for loops and check frist for touch,
2147 * then for pointer, etc. but unless somebody has some
2148 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002149 if (touch && touch->focus)
2150 output = touch->focus->output;
2151 else if (pointer && pointer->focus)
2152 output = pointer->focus->output;
2153 else if (keyboard && keyboard->focus)
2154 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002155
2156 if (output)
2157 break;
2158 }
2159
2160 return output;
2161}
2162
2163static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002164destroy_shell_seat(struct wl_listener *listener, void *data)
2165{
2166 struct shell_seat *shseat =
2167 container_of(listener,
2168 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002169
2170 wl_list_remove(&shseat->seat_destroy_listener.link);
2171 free(shseat);
2172}
2173
Jason Ekstrand024177c2014-04-21 19:42:58 -05002174static void
2175shell_seat_caps_changed(struct wl_listener *listener, void *data)
2176{
Derek Foreman1281a362015-07-31 16:55:32 -05002177 struct weston_keyboard *keyboard;
2178 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002179 struct shell_seat *seat;
2180
2181 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002182 keyboard = weston_seat_get_keyboard(seat->seat);
2183 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002184
Derek Foreman1281a362015-07-31 16:55:32 -05002185 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002186 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002187 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002188 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002189 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002190 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002191 wl_list_init(&seat->keyboard_focus_listener.link);
2192 }
2193
Derek Foreman1281a362015-07-31 16:55:32 -05002194 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002196 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002198 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002199 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002200 wl_list_init(&seat->pointer_focus_listener.link);
2201 }
2202}
2203
Giulio Camuffo5085a752013-03-25 21:42:45 +01002204static struct shell_seat *
2205create_shell_seat(struct weston_seat *seat)
2206{
2207 struct shell_seat *shseat;
2208
2209 shseat = calloc(1, sizeof *shseat);
2210 if (!shseat) {
2211 weston_log("no memory to allocate shell seat\n");
2212 return NULL;
2213 }
2214
2215 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002216
2217 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2218 wl_signal_add(&seat->destroy_signal,
2219 &shseat->seat_destroy_listener);
2220
Jason Ekstrand024177c2014-04-21 19:42:58 -05002221 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2222 wl_list_init(&shseat->keyboard_focus_listener.link);
2223
2224 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2225 wl_list_init(&shseat->pointer_focus_listener.link);
2226
2227 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2228 wl_signal_add(&seat->updated_caps_signal,
2229 &shseat->caps_changed_listener);
2230 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2231
Giulio Camuffo5085a752013-03-25 21:42:45 +01002232 return shseat;
2233}
2234
2235static struct shell_seat *
2236get_shell_seat(struct weston_seat *seat)
2237{
2238 struct wl_listener *listener;
2239
2240 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002241 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002242
2243 return container_of(listener,
2244 struct shell_seat, seat_destroy_listener);
2245}
2246
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002247static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002248fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002249{
2250 struct shell_surface *shsurf = data;
2251
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002252 weston_surface_destroy(shsurf->view->surface);
2253 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002254}
2255
2256static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002257fade_out_done(struct weston_view_animation *animation, void *data)
2258{
2259 struct shell_surface *shsurf = data;
2260 struct wl_event_loop *loop;
2261
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002262 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002263
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002264 if (weston_view_is_mapped(shsurf->view)) {
2265 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002266 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002267 }
2268}
2269
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002270struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002271get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002272{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273 if (weston_surface_is_desktop_surface(surface)) {
2274 struct weston_desktop_surface *desktop_surface =
2275 weston_surface_get_desktop_surface(surface);
2276 return weston_desktop_surface_get_user_data(desktop_surface);
2277 }
2278 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002279}
2280
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002281/*
2282 * libweston-desktop
2283 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002284
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002285static void
2286desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2287 void *shell)
2288{
2289 struct weston_desktop_client *client =
2290 weston_desktop_surface_get_client(desktop_surface);
2291 struct wl_client *wl_client =
2292 weston_desktop_client_get_client(client);
2293 struct weston_view *view;
2294 struct shell_surface *shsurf;
2295 struct weston_surface *surface =
2296 weston_desktop_surface_get_surface(desktop_surface);
2297
2298 view = weston_desktop_surface_create_view(desktop_surface);
2299 if (!view)
2300 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002301
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002302 shsurf = calloc(1, sizeof *shsurf);
2303 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002304 if (wl_client)
2305 wl_client_post_no_memory(wl_client);
2306 else
2307 weston_log("no memory to allocate shell surface\n");
2308 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002309 }
2310
Pekka Paalanen8274d902014-08-06 19:36:51 +03002311 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002312
Tiago Vignattibc052c92012-04-19 16:18:18 +03002313 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002314 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002315 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002316 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002317 shsurf->desktop_surface = desktop_surface;
2318 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002320 wl_list_init(&shsurf->fullscreen.transform.link);
2321
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002322 shsurf->output = get_default_output(shsurf->shell->compositor);
2323
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002324 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002325
Pekka Paalanen460099f2012-01-20 16:48:25 +02002326 /* empty when not in use */
2327 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002328 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002329
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002330 wl_list_init(&shsurf->workspace_transform.link);
2331
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002332 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2333 weston_desktop_surface_set_activated(desktop_surface,
2334 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002335}
2336
Tiago Vignattibc052c92012-04-19 16:18:18 +03002337static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002338desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2339 void *shell)
2340{
2341 struct shell_surface *shsurf =
2342 weston_desktop_surface_get_user_data(desktop_surface);
2343 struct weston_surface *surface =
2344 weston_desktop_surface_get_surface(desktop_surface);
2345
2346 if (!shsurf)
2347 return;
2348
2349 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2350
2351 if (shsurf->fullscreen.black_view)
2352 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2353
2354 weston_surface_set_label_func(surface, NULL);
2355 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2356 shsurf->desktop_surface = NULL;
2357
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002358 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002359 if (weston_surface_is_mapped(surface) &&
2360 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2361 pixman_region32_fini(&surface->pending.input);
2362 pixman_region32_init(&surface->pending.input);
2363 pixman_region32_fini(&surface->input);
2364 pixman_region32_init(&surface->input);
2365 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2366 fade_out_done, shsurf);
2367 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002368 weston_view_destroy(shsurf->view);
2369 free(shsurf);
2370 }
2371}
2372
2373static void
2374set_maximized_position(struct desktop_shell *shell,
2375 struct shell_surface *shsurf)
2376{
2377 pixman_rectangle32_t area;
2378 struct weston_geometry geometry;
2379
2380 get_output_work_area(shell, shsurf->output, &area);
2381 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2382
2383 weston_view_set_position(shsurf->view,
2384 area.x - geometry.x,
2385 area.y - geometry.y);
2386}
2387
2388static void
2389map(struct desktop_shell *shell, struct shell_surface *shsurf,
2390 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002391{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002392 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002393 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2394 struct weston_compositor *compositor = shell->compositor;
2395 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002396
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002397 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002398 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002399 center_on_output(shsurf->view, shsurf->fullscreen_output);
2400 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002401 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002402 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002403 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002404 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002405 }
2406
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002407 /* Surface stacking order, see also activate(). */
2408 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002409
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002410 weston_view_update_transform(shsurf->view);
2411 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002412 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002413 surface->output = shsurf->output;
2414 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002415 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002416
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002417 if (!shell->locked) {
2418 wl_list_for_each(seat, &compositor->seat_list, link)
2419 activate(shell, shsurf->view, seat,
2420 WESTON_ACTIVATE_FLAG_CONFIGURE);
2421 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002422
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002423 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002424 switch (shell->win_animation_type) {
2425 case ANIMATION_FADE:
2426 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2427 break;
2428 case ANIMATION_ZOOM:
2429 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2430 break;
2431 case ANIMATION_NONE:
2432 default:
2433 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002434 }
2435 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002436}
2437
2438static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002439desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2440 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002441{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 struct shell_surface *shsurf =
2443 weston_desktop_surface_get_user_data(desktop_surface);
2444 struct weston_surface *surface =
2445 weston_desktop_surface_get_surface(desktop_surface);
2446 struct weston_view *view = shsurf->view;
2447 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002448 bool was_fullscreen;
2449 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002450
2451 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002452 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002453
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002454 was_fullscreen = shsurf->state.fullscreen;
2455 was_maximized = shsurf->state.maximized;
2456
2457 shsurf->state.fullscreen =
2458 weston_desktop_surface_get_fullscreen(desktop_surface);
2459 shsurf->state.maximized =
2460 weston_desktop_surface_get_maximized(desktop_surface);
2461
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002462 if (!weston_surface_is_mapped(surface)) {
2463 map(shell, shsurf, sx, sy);
2464 surface->is_mapped = true;
2465 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2466 ++surface->ref_count;
2467 return;
2468 }
2469
2470 if (sx == 0 && sy == 0 &&
2471 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002472 shsurf->last_height == surface->height &&
2473 was_fullscreen == shsurf->state.fullscreen &&
2474 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002475 return;
2476
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002477 if (was_fullscreen)
2478 unset_fullscreen(shsurf);
2479 if (was_maximized)
2480 unset_maximized(shsurf);
2481
Quentin Glidic920cf042016-08-16 14:26:20 +02002482 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2483 !shsurf->saved_position_valid) {
2484 shsurf->saved_x = shsurf->view->geometry.x;
2485 shsurf->saved_y = shsurf->view->geometry.y;
2486 shsurf->saved_position_valid = true;
2487
2488 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2489 wl_list_remove(&shsurf->rotation.transform.link);
2490 wl_list_init(&shsurf->rotation.transform.link);
2491 weston_view_geometry_dirty(shsurf->view);
2492 shsurf->saved_rotation_valid = true;
2493 }
2494 }
2495
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002496 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002497 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002498 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002499 set_maximized_position(shell, shsurf);
2500 surface->output = shsurf->output;
2501 } else {
2502 float from_x, from_y;
2503 float to_x, to_y;
2504 float x, y;
2505
2506 if (shsurf->resize_edges) {
2507 sx = 0;
2508 sy = 0;
2509 }
2510
2511 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2512 sx = shsurf->last_width - surface->width;
2513 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2514 sy = shsurf->last_height - surface->height;
2515
2516 shsurf->last_width = surface->width;
2517 shsurf->last_height = surface->height;
2518
2519 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2520 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2521 x = shsurf->view->geometry.x + to_x - from_x;
2522 y = shsurf->view->geometry.y + to_y - from_y;
2523
2524 weston_view_set_position(shsurf->view, x, y);
2525 }
2526
2527 /* XXX: would a fullscreen surface need the same handling? */
2528 if (surface->output) {
2529 wl_list_for_each(view, &surface->views, surface_link)
2530 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002531 }
2532}
2533
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002534static void
2535set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2536 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002537{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002538 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2539 struct weston_surface *surface =
2540 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2541 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002542
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002543 if (fullscreen) {
2544 /* handle clients launching in fullscreen */
2545 if (output == NULL && !weston_surface_is_mapped(surface)) {
2546 /* Set the output to the one that has focus currently. */
2547 output = get_focused_output(surface->compositor);
2548 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002549
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002550 shell_surface_set_output(shsurf, output);
2551 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002552
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002553 width = shsurf->output->width;
2554 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002555 }
2556 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2557 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002558}
2559
2560static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2562 struct weston_seat *seat, uint32_t serial, void *shell)
2563{
2564 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2565 struct weston_touch *touch = weston_seat_get_touch(seat);
2566 struct shell_surface *shsurf =
2567 weston_desktop_surface_get_user_data(desktop_surface);
2568 struct weston_surface *surface =
2569 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2570 struct wl_resource *resource = surface->resource;
2571 struct weston_surface *focus;
2572
2573 if (pointer &&
2574 pointer->focus &&
2575 pointer->button_count > 0 &&
2576 pointer->grab_serial == serial) {
2577 focus = weston_surface_get_main_surface(pointer->focus->surface);
2578 if ((focus == surface) &&
2579 (surface_move(shsurf, pointer, true) < 0))
2580 wl_resource_post_no_memory(resource);
2581 } else if (touch &&
2582 touch->focus &&
2583 touch->grab_serial == serial) {
2584 focus = weston_surface_get_main_surface(touch->focus->surface);
2585 if ((focus == surface) &&
2586 (surface_touch_move(shsurf, touch) < 0))
2587 wl_resource_post_no_memory(resource);
2588 }
2589}
2590
2591static void
2592desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2593 struct weston_seat *seat, uint32_t serial,
2594 enum weston_desktop_surface_edge edges, void *shell)
2595{
2596 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2597 struct shell_surface *shsurf =
2598 weston_desktop_surface_get_user_data(desktop_surface);
2599 struct weston_surface *surface =
2600 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2601 struct wl_resource *resource = surface->resource;
2602 struct weston_surface *focus;
2603
2604 if (!pointer ||
2605 pointer->button_count == 0 ||
2606 pointer->grab_serial != serial ||
2607 pointer->focus == NULL)
2608 return;
2609
2610 focus = weston_surface_get_main_surface(pointer->focus->surface);
2611 if (focus != surface)
2612 return;
2613
2614 if (surface_resize(shsurf, pointer, edges) < 0)
2615 wl_resource_post_no_memory(resource);
2616}
2617
2618static void
2619desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2620 bool fullscreen,
2621 struct weston_output *output, void *shell)
2622{
2623 struct shell_surface *shsurf =
2624 weston_desktop_surface_get_user_data(desktop_surface);
2625
2626 set_fullscreen(shsurf, fullscreen, output);
2627}
2628
2629static void
2630set_maximized(struct shell_surface *shsurf, bool maximized)
2631{
2632 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2633 struct weston_surface *surface =
2634 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2635 int32_t width = 0, height = 0;
2636
2637 if (maximized) {
2638 struct weston_output *output;
2639 struct desktop_shell *shell;
2640 pixman_rectangle32_t area;
2641
2642 if (!weston_surface_is_mapped(surface))
2643 output = get_focused_output(surface->compositor);
2644 else
2645 output = surface->output;
2646
2647 shell_surface_set_output(shsurf, output);
2648
2649 shell = shell_surface_get_shell(shsurf);
2650 get_output_work_area(shell, shsurf->output, &area);
2651
2652 width = area.width;
2653 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002654 }
2655 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2656 weston_desktop_surface_set_size(desktop_surface, width, height);
2657}
2658
2659static void
2660desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2661 bool maximized, void *shell)
2662{
2663 struct shell_surface *shsurf =
2664 weston_desktop_surface_get_user_data(desktop_surface);
2665
2666 set_maximized(shsurf, maximized);
2667}
2668
2669static void
2670desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2671 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002672{
2673 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002674 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002675
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002676 /* apply compositor's own minimization logic (hide) */
2677 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002678}
2679
Rafael Antognollie2a34552013-12-03 15:35:45 -02002680static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002681set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002682{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002683 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002684
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002685 if (pointer->grab->interface == &busy_cursor_grab_interface)
2686 return;
2687
2688 grab = malloc(sizeof *grab);
2689 if (!grab)
2690 return;
2691
2692 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2693 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2694 /* Mark the shsurf as ungrabbed so that button binding is able
2695 * to move it. */
2696 shsurf->grabbed = 0;
2697}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002698
2699static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002700end_busy_cursor(struct weston_compositor *compositor,
2701 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002702{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002703 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002704 struct shell_grab *grab;
2705 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002706
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002707 wl_list_for_each(seat, &compositor->seat_list, link) {
2708 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2709 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002710
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711 if (!pointer)
2712 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002713
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002714 if (pointer->grab->interface != &busy_cursor_grab_interface)
2715 continue;
2716
2717 grab = (struct shell_grab *) pointer->grab;
2718 shsurf = grab->shsurf;
2719 if (!shsurf)
2720 continue;
2721
2722 grab_client =
2723 weston_desktop_surface_get_client(shsurf->desktop_surface);
2724 if (grab_client == desktop_client) {
2725 shell_grab_end(grab);
2726 free(grab);
2727 }
2728 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002729}
2730
2731static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002732desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2733 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002734{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002735 struct shell_surface *shsurf =
2736 weston_desktop_surface_get_user_data(desktop_surface);
2737 bool *unresponsive = user_data;
2738
2739 shsurf->unresponsive = *unresponsive;
2740}
2741
2742static void
2743desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2744 void *shell_)
2745{
2746 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002747 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002748 struct weston_seat *seat;
2749 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002750
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002751 weston_desktop_client_for_each_surface(desktop_client,
2752 desktop_surface_set_unresponsive,
2753 &unresponsive);
2754
2755
2756 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2757 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2758 struct weston_desktop_client *grab_client;
2759
2760 if (!pointer || !pointer->focus)
2761 continue;
2762
2763 shsurf = get_shell_surface(pointer->focus->surface);
2764 if (!shsurf)
2765 continue;
2766
2767 grab_client =
2768 weston_desktop_surface_get_client(shsurf->desktop_surface);
2769 if (grab_client == desktop_client)
2770 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002771 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002772}
2773
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002774static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002775desktop_surface_pong(struct weston_desktop_client *desktop_client,
2776 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002777{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002778 struct desktop_shell *shell = shell_;
2779 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002780
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002781 weston_desktop_client_for_each_surface(desktop_client,
2782 desktop_surface_set_unresponsive,
2783 &unresponsive);
2784 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002785}
2786
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002787static const struct weston_desktop_api shell_desktop_api = {
2788 .struct_size = sizeof(struct weston_desktop_api),
2789 .surface_added = desktop_surface_added,
2790 .surface_removed = desktop_surface_removed,
2791 .committed = desktop_surface_committed,
2792 .move = desktop_surface_move,
2793 .resize = desktop_surface_resize,
2794 .fullscreen_requested = desktop_surface_fullscreen_requested,
2795 .maximized_requested = desktop_surface_maximized_requested,
2796 .minimized_requested = desktop_surface_minimized_requested,
2797 .ping_timeout = desktop_surface_ping_timeout,
2798 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002799};
2800
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002801/* ************************ *
2802 * end of libweston-desktop *
2803 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002804static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002805shell_fade(struct desktop_shell *shell, enum fade_type type);
2806
2807static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002808configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002809{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002811
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002812 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 if (v->output == ev->output && v != ev) {
2814 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002815 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002816 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002817 }
2818 }
2819
Quentin Glidice8bf9592016-06-23 18:55:20 +02002820 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002821 ev->surface->is_mapped = true;
2822 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002823
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002824 if (wl_list_empty(&ev->layer_link.link)) {
2825 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002827 }
2828}
2829
David Fort50d962f2016-06-09 17:55:52 +02002830
2831static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002832find_shell_output_from_weston_output(struct desktop_shell *shell,
2833 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002834{
2835 struct shell_output *shell_output;
2836
2837 wl_list_for_each(shell_output, &shell->output_list, link) {
2838 if (shell_output->output == output)
2839 return shell_output;
2840 }
2841
2842 return NULL;
2843}
2844
Pekka Paalanen8274d902014-08-06 19:36:51 +03002845static int
2846background_get_label(struct weston_surface *surface, char *buf, size_t len)
2847{
2848 return snprintf(buf, len, "background for output %s",
2849 surface->output->name);
2850}
2851
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002852static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002853background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002854{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002855 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002857
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 view = container_of(es->views.next, struct weston_view, surface_link);
2859
Quentin Glidice8bf9592016-06-23 18:55:20 +02002860 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002861}
2862
2863static void
David Fort50d962f2016-06-09 17:55:52 +02002864handle_background_surface_destroy(struct wl_listener *listener, void *data)
2865{
2866 struct shell_output *output =
2867 container_of(listener, struct shell_output, background_surface_listener);
2868
2869 weston_log("background surface gone\n");
2870 output->background_surface = NULL;
2871}
2872
2873static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002874desktop_shell_set_background(struct wl_client *client,
2875 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002876 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002877 struct wl_resource *surface_resource)
2878{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002879 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002880 struct weston_surface *surface =
2881 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002882 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002884
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002885 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002886 wl_resource_post_error(surface_resource,
2887 WL_DISPLAY_ERROR_INVALID_OBJECT,
2888 "surface role already assigned");
2889 return;
2890 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002891
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2893 weston_view_destroy(view);
2894 view = weston_view_create(surface);
2895
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002896 surface->committed = background_committed;
2897 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002898 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002899 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002901 weston_desktop_shell_send_configure(resource, 0,
2902 surface_resource,
2903 surface->output->width,
2904 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002905
2906 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2907 sh_output->background_surface = surface;
2908
2909 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2910 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002911}
2912
Pekka Paalanen8274d902014-08-06 19:36:51 +03002913static int
2914panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2915{
2916 return snprintf(buf, len, "panel for output %s",
2917 surface->output->name);
2918}
2919
Kristian Høgsberg75840622011-09-06 13:48:16 -04002920static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002921panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002922{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002923 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002924 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002925 int width, height;
2926 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002927
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 view = container_of(es->views.next, struct weston_view, surface_link);
2929
Quentin Glidice8bf9592016-06-23 18:55:20 +02002930 get_panel_size(shell, view, &width, &height);
2931 switch (shell->panel_position) {
2932 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2933 break;
2934 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2935 y = view->output->height - height;
2936 break;
2937 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2938 break;
2939 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2940 x = view->output->width - width;
2941 break;
2942 }
2943
2944 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002945}
2946
2947static void
David Fort50d962f2016-06-09 17:55:52 +02002948handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2949{
2950 struct shell_output *output =
2951 container_of(listener, struct shell_output, panel_surface_listener);
2952
2953 weston_log("panel surface gone\n");
2954 output->panel_surface = NULL;
2955}
2956
2957
2958static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002959desktop_shell_set_panel(struct wl_client *client,
2960 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002961 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002962 struct wl_resource *surface_resource)
2963{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002964 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002965 struct weston_surface *surface =
2966 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002967 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002968 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002969
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002970 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002971 wl_resource_post_error(surface_resource,
2972 WL_DISPLAY_ERROR_INVALID_OBJECT,
2973 "surface role already assigned");
2974 return;
2975 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002976
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2978 weston_view_destroy(view);
2979 view = weston_view_create(surface);
2980
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002981 surface->committed = panel_committed;
2982 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002983 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002984 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002986 weston_desktop_shell_send_configure(resource, 0,
2987 surface_resource,
2988 surface->output->width,
2989 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002990
2991 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2992 sh_output->panel_surface = surface;
2993
2994 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2995 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002996}
2997
Pekka Paalanen8274d902014-08-06 19:36:51 +03002998static int
2999lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3000{
3001 return snprintf(buf, len, "lock window");
3002}
3003
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003004static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003005lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003006{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003007 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 struct weston_view *view;
3009
3010 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003011
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003012 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003013 return;
3014
Jason Ekstranda7af7042013-10-12 22:38:11 -05003015 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003016
3017 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003018 weston_layer_entry_insert(&shell->lock_layer.view_list,
3019 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003021 surface->is_mapped = true;
3022 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003023 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003024 }
3025}
3026
3027static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003028handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003029{
Tiago Vignattibe143262012-04-16 17:31:41 +03003030 struct desktop_shell *shell =
3031 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003032
Martin Minarik6d118362012-06-07 18:01:59 +02003033 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003034 shell->lock_surface = NULL;
3035}
3036
3037static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003038desktop_shell_set_lock_surface(struct wl_client *client,
3039 struct wl_resource *resource,
3040 struct wl_resource *surface_resource)
3041{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003042 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003043 struct weston_surface *surface =
3044 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003045
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003046 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003047
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003048 if (!shell->locked)
3049 return;
3050
Pekka Paalanen98262232011-12-01 10:42:22 +02003051 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003052
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003053 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003054 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003055 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003056
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003057 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003058 surface->committed = lock_surface_committed;
3059 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003060 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003061}
3062
3063static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003064resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003065{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003066 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003067
Quentin Glidic82681572016-12-17 13:40:51 +01003068 weston_layer_unset_position(&shell->lock_layer);
3069
3070 if (shell->showing_input_panels)
3071 weston_layer_set_position(&shell->input_panel_layer,
3072 WESTON_LAYER_POSITION_TOP_UI);
3073 weston_layer_set_position(&shell->fullscreen_layer,
3074 WESTON_LAYER_POSITION_FULLSCREEN);
3075 weston_layer_set_position(&shell->panel_layer,
3076 WESTON_LAYER_POSITION_UI);
3077 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003078
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003079 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003080
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003081 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003082 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003083 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003084}
3085
3086static void
3087desktop_shell_unlock(struct wl_client *client,
3088 struct wl_resource *resource)
3089{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003090 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003091
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003092 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003093
3094 if (shell->locked)
3095 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003096}
3097
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003098static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003099desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003100 struct wl_resource *resource,
3101 struct wl_resource *surface_resource)
3102{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003103 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003104
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003105 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003106 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003107}
3108
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003109static void
3110desktop_shell_desktop_ready(struct wl_client *client,
3111 struct wl_resource *resource)
3112{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003113 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003114
3115 shell_fade_startup(shell);
3116}
3117
Jonny Lamb765760d2014-08-20 15:53:19 +02003118static void
3119desktop_shell_set_panel_position(struct wl_client *client,
3120 struct wl_resource *resource,
3121 uint32_t position)
3122{
3123 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3124
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003125 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3126 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3127 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3128 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003129 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003130 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003131 "bad position argument");
3132 return;
3133 }
3134
3135 shell->panel_position = position;
3136}
3137
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003138static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003139 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003140 desktop_shell_set_panel,
3141 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003142 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003143 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003144 desktop_shell_desktop_ready,
3145 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003146};
3147
3148static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003149move_binding(struct weston_pointer *pointer, uint32_t time,
3150 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003151{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003152 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003153 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003154 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003155
Derek Foreman8ae2db52015-07-15 13:00:36 -05003156 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003157 return;
3158
Derek Foreman8ae2db52015-07-15 13:00:36 -05003159 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003160
Pekka Paalanen01388e22013-04-25 13:57:44 +03003161 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003162 if (surface == NULL)
3163 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003164
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003165 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003166 if (shsurf == NULL ||
3167 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3168 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003169 return;
3170
Derek Foreman794fa0e2015-07-15 13:00:38 -05003171 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003172}
3173
3174static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003175maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3176 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003177{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003178 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003179 struct weston_surface *surface;
3180 struct shell_surface *shsurf;
3181
3182 surface = weston_surface_get_main_surface(focus);
3183 if (surface == NULL)
3184 return;
3185
3186 shsurf = get_shell_surface(surface);
3187 if (shsurf == NULL)
3188 return;
3189
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003190 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003191}
3192
3193static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003194fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3195 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003196{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003197 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003198 struct weston_surface *surface;
3199 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003200 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003201
3202 surface = weston_surface_get_main_surface(focus);
3203 if (surface == NULL)
3204 return;
3205
3206 shsurf = get_shell_surface(surface);
3207 if (shsurf == NULL)
3208 return;
3209
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003210 fullscreen =
3211 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003212
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003213 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003214}
3215
3216static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003217touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003218{
Derek Foreman362656b2014-09-04 10:23:05 -05003219 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003220 struct weston_surface *surface;
3221 struct shell_surface *shsurf;
3222
Derek Foreman8ae2db52015-07-15 13:00:36 -05003223 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003224 return;
3225
Derek Foreman8ae2db52015-07-15 13:00:36 -05003226 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003227 surface = weston_surface_get_main_surface(focus);
3228 if (surface == NULL)
3229 return;
3230
3231 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003232 if (shsurf == NULL ||
3233 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3234 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003235 return;
3236
Derek Foremanb7674ae2015-07-15 13:00:37 -05003237 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003238}
3239
3240static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003241resize_binding(struct weston_pointer *pointer, uint32_t time,
3242 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003243{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003244 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003245 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003246 uint32_t edges = 0;
3247 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003248 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003249
Derek Foreman8ae2db52015-07-15 13:00:36 -05003250 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003251 return;
3252
Derek Foreman8ae2db52015-07-15 13:00:36 -05003253 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003254
Pekka Paalanen01388e22013-04-25 13:57:44 +03003255 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003256 if (surface == NULL)
3257 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003258
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003259 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003260 if (shsurf == NULL ||
3261 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3262 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003263 return;
3264
Jason Ekstranda7af7042013-10-12 22:38:11 -05003265 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003266 wl_fixed_to_int(pointer->grab_x),
3267 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003268 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003270 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003271 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003272 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003273 edges |= 0;
3274 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003275 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003276
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003277 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003278 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003279 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003280 edges |= 0;
3281 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003282 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003283
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003284 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003285}
3286
3287static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003288surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003289 struct weston_pointer_axis_event *event,
3290 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003291{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003292 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003293 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003294 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003295 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003296
Pekka Paalanen01388e22013-04-25 13:57:44 +03003297 /* XXX: broken for windows containing sub-surfaces */
3298 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003299 if (surface == NULL)
3300 return;
3301
3302 shsurf = get_shell_surface(surface);
3303 if (!shsurf)
3304 return;
3305
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003306 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003307
Jason Ekstranda7af7042013-10-12 22:38:11 -05003308 if (shsurf->view->alpha > 1.0)
3309 shsurf->view->alpha = 1.0;
3310 if (shsurf->view->alpha < step)
3311 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003312
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003314 weston_surface_damage(surface);
3315}
3316
3317static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003318do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003319 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003321 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003322 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003323 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003324 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003325
Derek Foreman1281a362015-07-31 16:55:32 -05003326 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003327 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3328 return;
3329 }
3330
Scott Moreauccbf29d2012-02-22 14:21:41 -07003331 wl_list_for_each(output, &compositor->output_list, link) {
3332 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003333 wl_fixed_to_double(pointer->x),
3334 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003335 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003336 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003337 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003338 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003339 increment = -output->zoom.increment;
3340 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003341 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003342 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003343 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003344 else
3345 increment = 0;
3346
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003347 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003348
Scott Moreaue6603982012-06-11 13:07:51 -06003349 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003350 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003351 else if (output->zoom.level > output->zoom.max_level)
3352 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003353
3354 if (!output->zoom.active) {
3355 if (output->zoom.level <= 0.0)
3356 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003357 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003358 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003359
Scott Moreaue6603982012-06-11 13:07:51 -06003360 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003361
Jason Ekstranda7af7042013-10-12 22:38:11 -05003362 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003363 }
3364 }
3365}
3366
Scott Moreauccbf29d2012-02-22 14:21:41 -07003367static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003368zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003369 struct weston_pointer_axis_event *event,
3370 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003371{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003372 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003373}
3374
3375static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003376zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3377 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003378{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003379 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003380}
3381
3382static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003383terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3384 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003385{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003386 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003387
Daniel Stone325fc2d2012-05-30 16:31:58 +01003388 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003389}
3390
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003391static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003392rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003393 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003394{
3395 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003396 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003397 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003398 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003399 struct weston_surface *surface =
3400 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003401 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003402
Jonas Ådahld2510102014-10-05 21:39:14 +02003403 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003404
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003405 if (!shsurf)
3406 return;
3407
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003408 cx = 0.5f * surface->width;
3409 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003410
Daniel Stone37816df2012-05-16 18:45:18 +01003411 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3412 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003413 r = sqrtf(dx * dx + dy * dy);
3414
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003415 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003416 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003417
3418 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003419 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003420 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003421
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003422 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003423 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003424
3425 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003426 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003427 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003428 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003429 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003430
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003431 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003432 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003433 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003434 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003435 wl_list_init(&shsurf->rotation.transform.link);
3436 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003437 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003438 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003439
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003440 /* We need to adjust the position of the surface
3441 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003442 cposx = shsurf->view->geometry.x + cx;
3443 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003444 dposx = rotate->center.x - cposx;
3445 dposy = rotate->center.y - cposy;
3446 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003447 weston_view_set_position(shsurf->view,
3448 shsurf->view->geometry.x + dposx,
3449 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003450 }
3451
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003452 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003453 * lazily applies the damage due to rotation update.
3454 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003455 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003456}
3457
3458static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003459rotate_grab_button(struct weston_pointer_grab *grab,
3460 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003461{
3462 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003463 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003464 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003465 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003466 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003467
Daniel Stone4dbadb12012-05-30 16:31:51 +01003468 if (pointer->button_count == 0 &&
3469 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003470 if (shsurf)
3471 weston_matrix_multiply(&shsurf->rotation.rotation,
3472 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003473 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003474 free(rotate);
3475 }
3476}
3477
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003478static void
3479rotate_grab_cancel(struct weston_pointer_grab *grab)
3480{
3481 struct rotate_grab *rotate =
3482 container_of(grab, struct rotate_grab, base.grab);
3483
3484 shell_grab_end(&rotate->base);
3485 free(rotate);
3486}
3487
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003488static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003489 noop_grab_focus,
3490 rotate_grab_motion,
3491 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003492 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003493 noop_grab_axis_source,
3494 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003495 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003496};
3497
3498static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003499surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003500{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003501 struct weston_surface *surface =
3502 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003503 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003504 float dx, dy;
3505 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506
Pekka Paalanen460099f2012-01-20 16:48:25 +02003507 rotate = malloc(sizeof *rotate);
3508 if (!rotate)
3509 return;
3510
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003511 weston_view_to_global_float(shsurf->view,
3512 surface->width * 0.5f,
3513 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003514 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003515
Derek Foreman74de4692015-07-15 13:00:39 -05003516 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3517 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003518 r = sqrtf(dx * dx + dy * dy);
3519 if (r > 20.0f) {
3520 struct weston_matrix inverse;
3521
3522 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003523 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003524 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003525
3526 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003527 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003528 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003529 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003530 weston_matrix_init(&rotate->rotation);
3531 }
3532
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003533 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003534 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003535}
3536
3537static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003538rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003539 void *data)
3540{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003541 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003542 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003543 struct shell_surface *surface;
3544
Derek Foreman8ae2db52015-07-15 13:00:36 -05003545 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003546 return;
3547
Derek Foreman8ae2db52015-07-15 13:00:36 -05003548 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003549
Pekka Paalanen01388e22013-04-25 13:57:44 +03003550 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003551 if (base_surface == NULL)
3552 return;
3553
3554 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003555 if (surface == NULL ||
3556 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3557 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003558 return;
3559
Derek Foreman74de4692015-07-15 13:00:39 -05003560 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003561}
3562
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003563/* Move all fullscreen layers down to the current workspace and hide their
3564 * black views. The surfaces' state is set to both fullscreen and lowered,
3565 * and this is reversed when such a surface is re-configured, see
3566 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3567 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003568 * lowering_output = NULL - Lower on all outputs, else only lower on the
3569 * specified output.
3570 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003571 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003572 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003573void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003574lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003575 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003576{
3577 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003579
3580 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003582 &shell->fullscreen_layer.view_list.link,
3583 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003584 struct shell_surface *shsurf = get_shell_surface(view->surface);
3585
3586 if (!shsurf)
3587 continue;
3588
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003589 /* Only lower surfaces which have lowering_output as their fullscreen
3590 * output, unless a NULL output asks for lowering on all outputs.
3591 */
3592 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3593 continue;
3594
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003595 /* We can have a non-fullscreen popup for a fullscreen surface
3596 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003597 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003598 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003599 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3600 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003601 weston_view_damage_below(shsurf->fullscreen.black_view);
3602
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003603 }
3604
3605 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003606 weston_layer_entry_remove(&view->layer_link);
3607 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003608 weston_view_damage_below(view);
3609 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003610
3611 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003612 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003613}
3614
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003615void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003616activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003617 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003618{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003619 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003620 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003621 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003622 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003623 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003624 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003625
Pekka Paalanen01388e22013-04-25 13:57:44 +03003626 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003627 shsurf = get_shell_surface(main_surface);
3628 assert(shsurf);
3629
3630 /* Only demote fullscreen surfaces on the output of activated shsurf.
3631 * Leave fullscreen surfaces on unrelated outputs alone. */
3632 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003633
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003634 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003635
Jonas Ådahl8538b222012-08-29 22:13:03 +02003636 state = ensure_focus_state(shell, seat);
3637 if (state == NULL)
3638 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003639
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003640 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003641 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003642
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003643 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3644 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003645 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003646
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003647 /* Update the surface’s layer. This brings it to the top of the stacking
3648 * order as appropriate. */
3649 shell_surface_update_layer(shsurf);
3650
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003651 if (shell->focus_animation_type != ANIMATION_NONE) {
3652 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003653 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003654 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003655}
3656
Alex Wu21858432012-04-01 20:13:08 +08003657/* no-op func for checking black surface */
3658static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003659black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003660{
3661}
3662
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003663static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003664is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003665{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003666 struct weston_surface *surface = view->surface;
3667
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003668 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003669 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003670 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003671 return true;
3672 }
3673 return false;
3674}
3675
Kristian Høgsberg75840622011-09-06 13:48:16 -04003676static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003677activate_binding(struct weston_seat *seat,
3678 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003679 struct weston_view *focus_view,
3680 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003681{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003682 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003683 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003684
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003685 if (!focus_view)
3686 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003687
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003688 if (is_black_surface_view(focus_view, &main_view))
3689 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003690
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003691 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003692 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003693 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003694
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003695 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003696}
3697
3698static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003699click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3700 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003701{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003702 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003703 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003704 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003705 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003706
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003707 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003708 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003709 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003710}
3711
3712static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003713touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3714 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003715{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003716 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003717 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003718 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003719 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003720
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003721 activate_binding(touch->seat, data, touch->focus,
3722 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003723}
3724
3725static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003726unfocus_all_seats(struct desktop_shell *shell)
3727{
3728 struct weston_seat *seat, *next;
3729
3730 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003731 struct weston_keyboard *keyboard =
3732 weston_seat_get_keyboard(seat);
3733
3734 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003735 continue;
3736
Derek Foreman1281a362015-07-31 16:55:32 -05003737 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003738 }
3739}
3740
3741static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003742lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003743{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003744 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003745
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003746 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003747 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003748 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003749 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003750
3751 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003752
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003753 /* Hide all surfaces by removing the fullscreen, panel and
3754 * toplevel layers. This way nothing else can show or receive
3755 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003756
Quentin Glidic82681572016-12-17 13:40:51 +01003757 weston_layer_unset_position(&shell->panel_layer);
3758 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003759 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003760 weston_layer_unset_position(&shell->input_panel_layer);
3761 weston_layer_unset_position(&ws->layer);
3762
3763 weston_layer_set_position(&shell->lock_layer,
3764 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003765
Derek Foreman004b4a12015-07-20 16:28:13 -05003766 weston_compositor_sleep(shell->compositor);
3767
Neil Robertsb4a91702014-04-09 16:33:32 +01003768 /* Remove the keyboard focus on all seats. This will be
3769 * restored to the workspace's saved state via
3770 * restore_focus_state when the compositor is unlocked */
3771 unfocus_all_seats(shell);
3772
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003773 /* TODO: disable bindings that should not work while locked. */
3774
3775 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003776}
3777
3778static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003779unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003780{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003781 struct wl_resource *shell_resource;
3782
Pekka Paalanend81c2162011-11-16 13:47:34 +02003783 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003784 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003785 return;
3786 }
3787
3788 /* If desktop-shell client has gone away, unlock immediately. */
3789 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003790 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003791 return;
3792 }
3793
3794 if (shell->prepare_event_sent)
3795 return;
3796
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003797 shell_resource = shell->child.desktop_shell;
3798 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003799 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003800}
3801
3802static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003803shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003804{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003805 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003806
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003807 shell->fade.animation = NULL;
3808
3809 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003810 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003811 weston_surface_destroy(shell->fade.view->surface);
3812 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003813 break;
3814 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003815 lock(shell);
3816 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003817 default:
3818 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003819 }
3820}
3821
Jason Ekstranda7af7042013-10-12 22:38:11 -05003822static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003823shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003824{
3825 struct weston_compositor *compositor = shell->compositor;
3826 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003827 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003828
3829 surface = weston_surface_create(compositor);
3830 if (!surface)
3831 return NULL;
3832
Jason Ekstranda7af7042013-10-12 22:38:11 -05003833 view = weston_view_create(surface);
3834 if (!view) {
3835 weston_surface_destroy(surface);
3836 return NULL;
3837 }
3838
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003839 weston_surface_set_size(surface, 8192, 8192);
3840 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003841 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003842 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3843 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003844 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003845 surface->is_mapped = true;
3846 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003847
Jason Ekstranda7af7042013-10-12 22:38:11 -05003848 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003849}
3850
3851static void
3852shell_fade(struct desktop_shell *shell, enum fade_type type)
3853{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003854 float tint;
3855
3856 switch (type) {
3857 case FADE_IN:
3858 tint = 0.0;
3859 break;
3860 case FADE_OUT:
3861 tint = 1.0;
3862 break;
3863 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003864 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003865 return;
3866 }
3867
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003868 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003869
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003870 if (shell->fade.view == NULL) {
3871 shell->fade.view = shell_fade_create_surface(shell);
3872 if (!shell->fade.view)
3873 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003874
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003875 shell->fade.view->alpha = 1.0 - tint;
3876 weston_view_update_transform(shell->fade.view);
3877 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003878
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003879 if (shell->fade.view->output == NULL) {
3880 /* If the black view gets a NULL output, we lost the
3881 * last output and we'll just cancel the fade. This
3882 * happens when you close the last window under the
3883 * X11 or Wayland backends. */
3884 shell->locked = false;
3885 weston_surface_destroy(shell->fade.view->surface);
3886 shell->fade.view = NULL;
3887 } else if (shell->fade.animation) {
3888 weston_fade_update(shell->fade.animation, tint);
3889 } else {
3890 shell->fade.animation =
3891 weston_fade_run(shell->fade.view,
3892 1.0 - tint, tint, 300.0,
3893 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003894 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003895}
3896
3897static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003898do_shell_fade_startup(void *data)
3899{
3900 struct desktop_shell *shell = data;
3901
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003902 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003903 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003904 } else {
3905 weston_log("desktop shell: "
3906 "unexpected fade-in animation type %d\n",
3907 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003908 weston_surface_destroy(shell->fade.view->surface);
3909 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003910 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003911}
3912
3913static void
3914shell_fade_startup(struct desktop_shell *shell)
3915{
3916 struct wl_event_loop *loop;
3917
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003918 if (!shell->fade.startup_timer)
3919 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003920
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003921 wl_event_source_remove(shell->fade.startup_timer);
3922 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003923
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003924 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3925 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003926}
3927
3928static int
3929fade_startup_timeout(void *data)
3930{
3931 struct desktop_shell *shell = data;
3932
3933 shell_fade_startup(shell);
3934 return 0;
3935}
3936
3937static void
3938shell_fade_init(struct desktop_shell *shell)
3939{
3940 /* Make compositor output all black, and wait for the desktop-shell
3941 * client to signal it is ready, then fade in. The timer triggers a
3942 * fade-in, in case the desktop-shell client takes too long.
3943 */
3944
3945 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003946
3947 if (shell->fade.view != NULL) {
3948 weston_log("%s: warning: fade surface already exists\n",
3949 __func__);
3950 return;
3951 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003952
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003953 if (shell->startup_animation_type == ANIMATION_NONE)
3954 return;
3955
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003956 shell->fade.view = shell_fade_create_surface(shell);
3957 if (!shell->fade.view)
3958 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003959
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003960 weston_view_update_transform(shell->fade.view);
3961 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003962
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003963 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3964 shell->fade.startup_timer =
3965 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3966 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003967}
3968
3969static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003970idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971{
3972 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003973 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003974
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003975 struct weston_seat *seat;
3976
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003977 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3978 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003979
3980 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003981 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982}
3983
3984static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003985wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003986{
3987 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003988 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003989
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003990 unlock(shell);
3991}
3992
3993static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003994transform_handler(struct wl_listener *listener, void *data)
3995{
3996 struct weston_surface *surface = data;
3997 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003998 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003999 int x, y;
4000
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004001 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004002 return;
4003
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004004 api = shsurf->shell->xwayland_surface_api;
4005 if (!api) {
4006 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4007 shsurf->shell->xwayland_surface_api = api;
4008 }
4009
4010 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004011 return;
4012
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004013 if (!weston_view_is_mapped(shsurf->view))
4014 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004015
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004016 x = shsurf->view->geometry.x;
4017 y = shsurf->view->geometry.y;
4018
4019 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004020}
4021
4022static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004024{
Giulio Camuffob8366642013-04-25 13:57:46 +03004025 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004026 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004027
Jason Ekstranda7af7042013-10-12 22:38:11 -05004028 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004029
4030 x = output->x + (output->width - width) / 2 - surf_x / 2;
4031 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004032
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004033 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004034}
4035
4036static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004037weston_view_set_initial_position(struct weston_view *view,
4038 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004039{
4040 struct weston_compositor *compositor = shell->compositor;
4041 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004042 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004043 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004044 struct weston_output *output, *target_output = NULL;
4045 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004046 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004047
4048 /* As a heuristic place the new window on the same output as the
4049 * pointer. Falling back to the output containing 0, 0.
4050 *
4051 * TODO: Do something clever for touch too?
4052 */
4053 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004054 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4055
4056 if (pointer) {
4057 ix = wl_fixed_to_int(pointer->x);
4058 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004059 break;
4060 }
4061 }
4062
4063 wl_list_for_each(output, &compositor->output_list, link) {
4064 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4065 target_output = output;
4066 break;
4067 }
4068 }
4069
4070 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004071 weston_view_set_position(view, 10 + random() % 400,
4072 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004073 return;
4074 }
4075
4076 /* Valid range within output where the surface will still be onscreen.
4077 * If this is negative it means that the surface is bigger than
4078 * output.
4079 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004080 get_output_work_area(shell, target_output, &area);
4081
Derek Foremanf814c5d2015-04-15 12:23:54 -05004082 x = area.x;
4083 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004084 range_x = area.width - view->surface->width;
4085 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004086
Rob Bradford4cb88c72012-08-13 15:18:44 +01004087 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004088 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004089
4090 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004091 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004092
Jason Ekstranda7af7042013-10-12 22:38:11 -05004093 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004094}
4095
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004096static bool
4097check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4098{
4099 struct timespec now;
4100
4101 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4102 return false;
4103
4104 /*
4105 * If the shell helper client dies before the session has been
4106 * up for roughly 30 seconds, better just make Weston shut down,
4107 * because the user likely has no way to interact with the desktop
4108 * anyway.
4109 */
4110 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4111 weston_log("Error: %s apparently cannot run at all.\n",
4112 shell->client);
4113 weston_log_continue(STAMP_SPACE "Quitting...");
4114 wl_display_terminate(shell->compositor->wl_display);
4115
4116 return true;
4117 }
4118
4119 return false;
4120}
4121
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004122static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004123
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004124static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004125respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004126{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004127 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004128
4129 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4130 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004131 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004132 shell->child.deathstamp = time;
4133 shell->child.deathcount = 0;
4134 }
4135
4136 shell->child.deathcount++;
4137 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004138 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004139 return;
4140 }
4141
Pekka Paalanen826dc142014-08-27 12:11:53 +03004142 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004143 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004144}
4145
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004146static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004147desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4148{
4149 struct desktop_shell *shell;
4150
4151 shell = container_of(listener, struct desktop_shell,
4152 child.client_destroy_listener);
4153
Pekka Paalanen826dc142014-08-27 12:11:53 +03004154 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004155 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004156 /*
4157 * unbind_desktop_shell() will reset shell->child.desktop_shell
4158 * before the respawned process has a chance to create a new
4159 * desktop_shell object, because we are being called from the
4160 * wl_client destructor which destroys all wl_resources before
4161 * returning.
4162 */
4163
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004164 if (!check_desktop_shell_crash_too_early(shell))
4165 respawn_desktop_shell_process(shell);
4166
Pekka Paalanen826dc142014-08-27 12:11:53 +03004167 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004168}
4169
4170static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004171launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004172{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004173 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004174
Pekka Paalanen826dc142014-08-27 12:11:53 +03004175 shell->child.client = weston_client_start(shell->compositor,
4176 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004177
Arnaud Vrac42631192014-08-25 20:56:46 +02004178 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004179 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004180 return;
4181 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004182
4183 shell->child.client_destroy_listener.notify =
4184 desktop_shell_client_destroy;
4185 wl_client_add_destroy_listener(shell->child.client,
4186 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004187}
4188
4189static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004190unbind_desktop_shell(struct wl_resource *resource)
4191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004192 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004193
4194 if (shell->locked)
4195 resume_desktop(shell);
4196
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004197 shell->child.desktop_shell = NULL;
4198 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004199}
4200
4201static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004202bind_desktop_shell(struct wl_client *client,
4203 void *data, uint32_t version, uint32_t id)
4204{
Tiago Vignattibe143262012-04-16 17:31:41 +03004205 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004206 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004207
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004208 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4209 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004210
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004211 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004212 wl_resource_set_implementation(resource,
4213 &desktop_shell_implementation,
4214 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004215 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004216 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004217 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004218
4219 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4220 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004221}
4222
Kristian Høgsberg07045392012-02-19 18:52:44 -05004223struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004224 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004225 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004226 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004227 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004228 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004229};
4230
4231static void
4232switcher_next(struct switcher *switcher)
4233{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004234 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004235 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004236 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004237 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004239 /* temporary re-display minimized surfaces */
4240 struct weston_view *tmp;
4241 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004242 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4243 weston_layer_entry_remove(&view->layer_link);
4244 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004245 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4246 *minimized = view;
4247 }
4248
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004249 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004250 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004251 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004252 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004253 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004254 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004255 next = view;
4256 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 view->alpha = 0.25;
4258 weston_view_geometry_dirty(view);
4259 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004260 }
Alex Wu1659daa2012-04-01 20:13:09 +08004261
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004262 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004263 view->alpha = 0.25;
4264 weston_view_geometry_dirty(view);
4265 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004266 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004267 }
4268
4269 if (next == NULL)
4270 next = first;
4271
Alex Wu07b26062012-03-12 16:06:01 +08004272 if (next == NULL)
4273 return;
4274
Kristian Høgsberg07045392012-02-19 18:52:44 -05004275 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004276 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004277
4278 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004279 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004281
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004282 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004283 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004284 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004285}
4286
4287static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004288switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004289{
4290 struct switcher *switcher =
4291 container_of(listener, struct switcher, listener);
4292
4293 switcher_next(switcher);
4294}
4295
4296static void
Daniel Stone351eb612012-05-31 15:27:47 -04004297switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004299 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004300 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004301 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004302
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004303 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004304 if (is_focus_view(view))
4305 continue;
4306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 view->alpha = 1.0;
4308 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004309 }
4310
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004311 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004312 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004313 keyboard->seat,
4314 WESTON_ACTIVATE_FLAG_CONFIGURE);
4315 }
4316
Kristian Høgsberg07045392012-02-19 18:52:44 -05004317 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004318 weston_keyboard_end_grab(keyboard);
4319 if (keyboard->input_method_resource)
4320 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004321
4322 /* re-hide surfaces that were temporary shown during the switch */
4323 struct weston_view **minimized;
4324 wl_array_for_each(minimized, &switcher->minimized_array) {
4325 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004326 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004327 weston_layer_entry_remove(&(*minimized)->layer_link);
4328 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004329 weston_view_damage_below(*minimized);
4330 }
4331 }
4332 wl_array_release(&switcher->minimized_array);
4333
Kristian Høgsberg07045392012-02-19 18:52:44 -05004334 free(switcher);
4335}
4336
4337static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004338switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004339 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004340{
4341 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004342 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004343
Daniel Stonec9785ea2012-05-30 16:31:52 +01004344 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004345 switcher_next(switcher);
4346}
4347
4348static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004349switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004350 uint32_t mods_depressed, uint32_t mods_latched,
4351 uint32_t mods_locked, uint32_t group)
4352{
4353 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004354 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004355
Daniel Stone351eb612012-05-31 15:27:47 -04004356 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4357 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004358}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004359
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004360static void
4361switcher_cancel(struct weston_keyboard_grab *grab)
4362{
4363 struct switcher *switcher = container_of(grab, struct switcher, grab);
4364
4365 switcher_destroy(switcher);
4366}
4367
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004368static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004369 switcher_key,
4370 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004371 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004372};
4373
4374static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004375switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4376 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004377{
Tiago Vignattibe143262012-04-16 17:31:41 +03004378 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379 struct switcher *switcher;
4380
4381 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004382 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004384 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004385 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004386 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004387
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004388 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004389 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004390 weston_keyboard_start_grab(keyboard, &switcher->grab);
4391 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004392 switcher_next(switcher);
4393}
4394
Pekka Paalanen3c647232011-12-22 13:43:43 +02004395static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004396backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4397 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004398{
4399 struct weston_compositor *compositor = data;
4400 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004401 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004402
4403 /* TODO: we're limiting to simple use cases, where we assume just
4404 * control on the primary display. We'd have to extend later if we
4405 * ever get support for setting backlights on random desktop LCD
4406 * panels though */
4407 output = get_default_output(compositor);
4408 if (!output)
4409 return;
4410
4411 if (!output->set_backlight)
4412 return;
4413
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004414 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4415 backlight_new = output->backlight_current - 25;
4416 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4417 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004418
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004419 if (backlight_new < 5)
4420 backlight_new = 5;
4421 if (backlight_new > 255)
4422 backlight_new = 255;
4423
4424 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004425 output->set_backlight(output, output->backlight_current);
4426}
4427
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004428static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004429force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4430 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004431{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004432 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004433 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004434 struct desktop_shell *shell = data;
4435 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004436 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004437
Derek Foreman8ae2db52015-07-15 13:00:36 -05004438 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004439 if (!focus_surface)
4440 return;
4441
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004442 wl_signal_emit(&compositor->kill_signal, focus_surface);
4443
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004444 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004445 wl_client_get_credentials(client, &pid, NULL, NULL);
4446
4447 /* Skip clients that we launched ourselves (the credentials of
4448 * the socketpair is ours) */
4449 if (pid == getpid())
4450 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004451
Daniel Stone325fc2d2012-05-30 16:31:58 +01004452 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004453}
4454
4455static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004456workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004457 uint32_t key, void *data)
4458{
4459 struct desktop_shell *shell = data;
4460 unsigned int new_index = shell->workspaces.current;
4461
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004462 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004463 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004464 if (new_index != 0)
4465 new_index--;
4466
4467 change_workspace(shell, new_index);
4468}
4469
4470static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004471workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004472 uint32_t key, void *data)
4473{
4474 struct desktop_shell *shell = data;
4475 unsigned int new_index = shell->workspaces.current;
4476
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004477 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004478 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004479 if (new_index < shell->workspaces.num - 1)
4480 new_index++;
4481
4482 change_workspace(shell, new_index);
4483}
4484
4485static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004486workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004487 uint32_t key, void *data)
4488{
4489 struct desktop_shell *shell = data;
4490 unsigned int new_index;
4491
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004492 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004493 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004494 new_index = key - KEY_F1;
4495 if (new_index >= shell->workspaces.num)
4496 new_index = shell->workspaces.num - 1;
4497
4498 change_workspace(shell, new_index);
4499}
4500
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004501static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004502workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4503 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004504{
4505 struct desktop_shell *shell = data;
4506 unsigned int new_index = shell->workspaces.current;
4507
4508 if (shell->locked)
4509 return;
4510
4511 if (new_index != 0)
4512 new_index--;
4513
Derek Foreman8ae2db52015-07-15 13:00:36 -05004514 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004515}
4516
4517static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004518workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4519 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004520{
4521 struct desktop_shell *shell = data;
4522 unsigned int new_index = shell->workspaces.current;
4523
4524 if (shell->locked)
4525 return;
4526
4527 if (new_index < shell->workspaces.num - 1)
4528 new_index++;
4529
Derek Foreman8ae2db52015-07-15 13:00:36 -05004530 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004531}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004532
4533static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004534shell_reposition_view_on_output_destroy(struct weston_view *view)
4535{
4536 struct weston_output *output, *first_output;
4537 struct weston_compositor *ec = view->surface->compositor;
4538 struct shell_surface *shsurf;
4539 float x, y;
4540 int visible;
4541
4542 x = view->geometry.x;
4543 y = view->geometry.y;
4544
4545 /* At this point the destroyed output is not in the list anymore.
4546 * If the view is still visible somewhere, we leave where it is,
4547 * otherwise, move it to the first output. */
4548 visible = 0;
4549 wl_list_for_each(output, &ec->output_list, link) {
4550 if (pixman_region32_contains_point(&output->region,
4551 x, y, NULL)) {
4552 visible = 1;
4553 break;
4554 }
4555 }
4556
4557 if (!visible) {
4558 first_output = container_of(ec->output_list.next,
4559 struct weston_output, link);
4560
4561 x = first_output->x + first_output->width / 4;
4562 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004563
4564 weston_view_set_position(view, x, y);
4565 } else {
4566 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004567 }
4568
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004569
4570 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004571 if (!shsurf)
4572 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004573
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004574 shsurf->saved_position_valid = false;
4575 set_maximized(shsurf, false);
4576 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004577}
4578
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004579void
4580shell_for_each_layer(struct desktop_shell *shell,
4581 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004582{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004583 struct workspace **ws;
4584
4585 func(shell, &shell->fullscreen_layer, data);
4586 func(shell, &shell->panel_layer, data);
4587 func(shell, &shell->background_layer, data);
4588 func(shell, &shell->lock_layer, data);
4589 func(shell, &shell->input_panel_layer, data);
4590
4591 wl_array_for_each(ws, &shell->workspaces.array)
4592 func(shell, &(*ws)->layer, data);
4593}
4594
4595static void
4596shell_output_destroy_move_layer(struct desktop_shell *shell,
4597 struct weston_layer *layer,
4598 void *data)
4599{
4600 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004601 struct weston_view *view;
4602
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004603 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004604 if (view->output != output)
4605 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004606
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004607 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004608 }
4609}
4610
4611static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004612handle_output_destroy(struct wl_listener *listener, void *data)
4613{
4614 struct shell_output *output_listener =
4615 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004616 struct weston_output *output = output_listener->output;
4617 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004618
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004619 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004620
Quentin Glidic561201c2016-08-20 18:19:51 +02004621 wl_list_remove(&output_listener->panel_surface_listener.link);
4622 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004623 wl_list_remove(&output_listener->destroy_listener.link);
4624 wl_list_remove(&output_listener->link);
4625 free(output_listener);
4626}
4627
4628static void
David Fort50d962f2016-06-09 17:55:52 +02004629shell_resize_surface_to_output(struct desktop_shell *shell,
4630 struct weston_surface *surface,
4631 const struct weston_output *output)
4632{
4633 if (!surface)
4634 return;
4635
4636 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4637 surface->resource,
4638 output->width,
4639 output->height);
4640}
4641
4642
4643static void
4644handle_output_resized(struct wl_listener *listener, void *data)
4645{
4646 struct desktop_shell *shell =
4647 container_of(listener, struct desktop_shell, resized_listener);
4648 struct weston_output *output = (struct weston_output *)data;
4649 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4650
4651 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4652 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4653}
4654
4655static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004656create_shell_output(struct desktop_shell *shell,
4657 struct weston_output *output)
4658{
4659 struct shell_output *shell_output;
4660
4661 shell_output = zalloc(sizeof *shell_output);
4662 if (shell_output == NULL)
4663 return;
4664
4665 shell_output->output = output;
4666 shell_output->shell = shell;
4667 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004668 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004669 wl_signal_add(&output->destroy_signal,
4670 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004671 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004672}
4673
4674static void
4675handle_output_create(struct wl_listener *listener, void *data)
4676{
4677 struct desktop_shell *shell =
4678 container_of(listener, struct desktop_shell, output_create_listener);
4679 struct weston_output *output = (struct weston_output *)data;
4680
4681 create_shell_output(shell, output);
4682}
4683
4684static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004685handle_output_move_layer(struct desktop_shell *shell,
4686 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004687{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004688 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004689 struct weston_view *view;
4690 float x, y;
4691
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004692 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004693 if (view->output != output)
4694 continue;
4695
4696 x = view->geometry.x + output->move_x;
4697 y = view->geometry.y + output->move_y;
4698 weston_view_set_position(view, x, y);
4699 }
4700}
4701
4702static void
4703handle_output_move(struct wl_listener *listener, void *data)
4704{
4705 struct desktop_shell *shell;
4706
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004707 shell = container_of(listener, struct desktop_shell,
4708 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004709
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004710 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004711}
4712
4713static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004714setup_output_destroy_handler(struct weston_compositor *ec,
4715 struct desktop_shell *shell)
4716{
4717 struct weston_output *output;
4718
4719 wl_list_init(&shell->output_list);
4720 wl_list_for_each(output, &ec->output_list, link)
4721 create_shell_output(shell, output);
4722
4723 shell->output_create_listener.notify = handle_output_create;
4724 wl_signal_add(&ec->output_created_signal,
4725 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004726
4727 shell->output_move_listener.notify = handle_output_move;
4728 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004729}
4730
4731static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004732shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004733{
Tiago Vignattibe143262012-04-16 17:31:41 +03004734 struct desktop_shell *shell =
4735 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004736 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004737 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004738
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004739 /* Force state to unlocked so we don't try to fade */
4740 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004741
4742 if (shell->child.client) {
4743 /* disable respawn */
4744 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004745 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004746 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004747
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004748 wl_list_remove(&shell->idle_listener.link);
4749 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004750 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004751
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004752 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004753 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004754
Xiong Zhang6b481422013-10-23 13:58:32 +08004755 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4756 wl_list_remove(&shell_output->destroy_listener.link);
4757 wl_list_remove(&shell_output->link);
4758 free(shell_output);
4759 }
4760
4761 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004762 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004763 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004764
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004765 wl_array_for_each(ws, &shell->workspaces.array)
4766 workspace_destroy(*ws);
4767 wl_array_release(&shell->workspaces.array);
4768
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004769 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004770 free(shell);
4771}
4772
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004773static void
4774shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4775{
4776 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004777 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004778
Bob Ham744e6532016-01-12 10:21:48 +00004779 if (shell->allow_zap)
4780 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4781 MODIFIER_CTRL | MODIFIER_ALT,
4782 terminate_binding, ec);
4783
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004784 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004785 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4786 click_to_activate_binding,
4787 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004788 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4789 click_to_activate_binding,
4790 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004791 weston_compositor_add_touch_binding(ec, 0,
4792 touch_to_activate_binding,
4793 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004794 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4795 MODIFIER_SUPER | MODIFIER_ALT,
4796 surface_opacity_binding, NULL);
4797 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4798 MODIFIER_SUPER, zoom_axis_binding,
4799 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004800 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4801 backlight_binding, ec);
4802 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4803 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004804
4805 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004806 if (shell->exposay_modifier)
4807 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4808 exposay_binding, shell);
4809
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004810 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004811 if (!mod)
4812 return;
4813
Daniel Stone325fc2d2012-05-30 16:31:58 +01004814 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4815 zoom_key_binding, NULL);
4816 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4817 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004818 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4819 maximize_binding, NULL);
4820 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4821 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004822 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4823 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004824 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004825 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004827 weston_compositor_add_button_binding(ec, BTN_LEFT,
4828 mod | MODIFIER_SHIFT,
4829 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004830
4831 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004832 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004833 rotate_binding, NULL);
4834
Daniel Stone325fc2d2012-05-30 16:31:58 +01004835 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4836 shell);
4837 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4838 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004839 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4840 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004841 weston_compositor_add_key_binding(ec, KEY_K, mod,
4842 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004843 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4844 workspace_up_binding, shell);
4845 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4846 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004847 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4848 workspace_move_surface_up_binding,
4849 shell);
4850 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4851 workspace_move_surface_down_binding,
4852 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004853
4854 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4855 if (shell->workspaces.num > 1) {
4856 num_workspace_bindings = shell->workspaces.num;
4857 if (num_workspace_bindings > 6)
4858 num_workspace_bindings = 6;
4859 for (i = 0; i < num_workspace_bindings; i++)
4860 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4861 workspace_f_binding,
4862 shell);
4863 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004864
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004865 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004866}
4867
Jason Ekstrand024177c2014-04-21 19:42:58 -05004868static void
4869handle_seat_created(struct wl_listener *listener, void *data)
4870{
4871 struct weston_seat *seat = data;
4872
4873 create_shell_seat(seat);
4874}
4875
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004876WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004877module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004878 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004879{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004880 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004881 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004882 struct workspace **pws;
4883 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004884 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004885
Peter Huttererf3d62272013-08-08 11:57:05 +10004886 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004887 if (shell == NULL)
4888 return -1;
4889
Kristian Høgsberg75840622011-09-06 13:48:16 -04004890 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004891
4892 shell->destroy_listener.notify = shell_destroy;
4893 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004894 shell->idle_listener.notify = idle_handler;
4895 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4896 shell->wake_listener.notify = wake_handler;
4897 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004898 shell->transform_listener.notify = transform_handler;
4899 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004900
Quentin Glidic82681572016-12-17 13:40:51 +01004901 weston_layer_init(&shell->fullscreen_layer, ec);
4902 weston_layer_init(&shell->panel_layer, ec);
4903 weston_layer_init(&shell->background_layer, ec);
4904 weston_layer_init(&shell->lock_layer, ec);
4905 weston_layer_init(&shell->input_panel_layer, ec);
4906
4907 weston_layer_set_position(&shell->fullscreen_layer,
4908 WESTON_LAYER_POSITION_FULLSCREEN);
4909 weston_layer_set_position(&shell->panel_layer,
4910 WESTON_LAYER_POSITION_UI);
4911 weston_layer_set_position(&shell->background_layer,
4912 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004913
4914 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004915 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004916
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004917 if (input_panel_setup(shell) < 0)
4918 return -1;
4919
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004920 shell->text_backend = text_backend_init(ec);
4921 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004922 return -1;
4923
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004924 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004925
Daniel Stonedf8133b2013-11-19 11:37:14 +01004926 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4927 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4928
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004929 for (i = 0; i < shell->workspaces.num; i++) {
4930 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4931 if (pws == NULL)
4932 return -1;
4933
Quentin Glidic82681572016-12-17 13:40:51 +01004934 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004935 if (*pws == NULL)
4936 return -1;
4937 }
4938 activate_workspace(shell, 0);
4939
Quentin Glidic82681572016-12-17 13:40:51 +01004940 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004941
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004942 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004943 wl_list_init(&shell->workspaces.animation.link);
4944 shell->workspaces.animation.frame = animate_workspace_change_frame;
4945
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004946 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4947 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004948 return -1;
4949
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004950 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004951 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004952 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004953 return -1;
4954
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004955 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004956
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004957 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004958
Xiong Zhang6b481422013-10-23 13:58:32 +08004959 setup_output_destroy_handler(ec, shell);
4960
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004961 loop = wl_display_get_event_loop(ec->wl_display);
4962 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004963
Jason Ekstrand024177c2014-04-21 19:42:58 -05004964 wl_list_for_each(seat, &ec->seat_list, link)
4965 handle_seat_created(NULL, seat);
4966 shell->seat_create_listener.notify = handle_seat_created;
4967 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004968
David Fort50d962f2016-06-09 17:55:52 +02004969 shell->resized_listener.notify = handle_output_resized;
4970 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4971
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004972 screenshooter_create(ec);
4973
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004974 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004975
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004976 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004977
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004978 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4979
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004980 return 0;
4981}