blob: a43c2e2affec20b9304f0b8e9a864e1311c5588f [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
287get_output_panel_size(struct desktop_shell *shell,
288 struct weston_output *output,
289 int *width,
290 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700291{
292 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200293
294 *width = 0;
295 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700296
297 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200298 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700299
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300300 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200301 float x, y;
302
303 if (view->surface->output != output)
304 continue;
305
306 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800307 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
308 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200309 weston_view_to_global_float(view,
310 view->surface->width, 0,
311 &x, &y);
312
Derek Foreman612341f2015-04-15 12:23:55 -0500313 *width = (int)x - output->x;
314 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200315 return;
316
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800317 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
318 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200319 weston_view_to_global_float(view,
320 0, view->surface->height,
321 &x, &y);
322
Derek Foreman612341f2015-04-15 12:23:55 -0500323 *width = view->surface->width + (int)x - output->x;
324 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200325 return;
326
327 default:
328 /* we've already set width and height to
329 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700330 break;
331 }
332 }
333
Jonny Lambd73c6942014-08-20 15:53:20 +0200334 /* the correct view wasn't found */
335}
336
337static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200338get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200339 struct weston_output *output,
340 pixman_rectangle32_t *area)
341{
342 int32_t panel_width = 0, panel_height = 0;
343
Derek Foremanf814c5d2015-04-15 12:23:54 -0500344 area->x = output->x;
345 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200346
347 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200348 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800349 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200350 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500351 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800352 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200353 area->width = output->width;
354 area->height = output->height - panel_height;
355 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800358 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200359 area->width = output->width - panel_width;
360 area->height = output->height;
361 break;
362 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700363}
364
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400365static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400369 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400371
372 if (grab->shsurf->resize_edges) {
373 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400374 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700375 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700377 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378}
379
380static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700381shell_touch_grab_start(struct shell_touch_grab *grab,
382 const struct weston_touch_grab_interface *interface,
383 struct shell_surface *shsurf,
384 struct weston_touch *touch)
385{
386 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800387
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200388 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700389
Rusty Lynch1084da52013-08-15 09:10:08 -0700390 grab->grab.interface = interface;
391 grab->shsurf = shsurf;
392 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
393 wl_signal_add(&shsurf->destroy_signal,
394 &grab->shsurf_destroy_listener);
395
396 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700397 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700398
399 weston_touch_start_grab(touch, &grab->grab);
400 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500401 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700403}
404
405static void
406shell_touch_grab_end(struct shell_touch_grab *grab)
407{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700409 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700410 grab->shsurf->grabbed = 0;
411 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700412
413 weston_touch_end_grab(grab->touch);
414}
415
416static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800418 struct weston_output *output);
419
Daniel Stone496ca172012-05-30 16:31:42 +0100420static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300421get_modifier(char *modifier)
422{
423 if (!modifier)
424 return MODIFIER_SUPER;
425
426 if (!strcmp("ctrl", modifier))
427 return MODIFIER_CTRL;
428 else if (!strcmp("alt", modifier))
429 return MODIFIER_ALT;
430 else if (!strcmp("super", modifier))
431 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000432 else if (!strcmp("none", modifier))
433 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300434 else
435 return MODIFIER_SUPER;
436}
437
Juan Zhaoe10d2792012-04-25 19:09:52 +0800438static enum animation_type
439get_animation_type(char *animation)
440{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800441 if (!animation)
442 return ANIMATION_NONE;
443
Juan Zhaoe10d2792012-04-25 19:09:52 +0800444 if (!strcmp("zoom", animation))
445 return ANIMATION_ZOOM;
446 else if (!strcmp("fade", animation))
447 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100448 else if (!strcmp("dim-layer", animation))
449 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800450 else
451 return ANIMATION_NONE;
452}
453
Alex Wu4539b082012-03-01 12:57:46 +0800454static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400455shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200456{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400457 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500458 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300459 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000460 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200461
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300462 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400463 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300464 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
465 WESTON_SHELL_CLIENT);
466 if (ret < 0)
467 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400468 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500469 "client", &s, client);
470 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100471 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000472
473 weston_config_section_get_bool(section,
474 "allow-zap", &allow_zap, true);
475 shell->allow_zap = allow_zap;
476
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100477 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400478 "binding-modifier", &s, "super");
479 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200480 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800481
482 weston_config_section_get_string(section,
483 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000484 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800485 free(s);
486
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400487 weston_config_section_get_string(section, "animation", &s, "none");
488 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200489 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200490 weston_config_section_get_string(section, "close-animation", &s, "fade");
491 shell->win_close_animation_type = get_animation_type(s);
492 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700493 weston_config_section_get_string(section,
494 "startup-animation", &s, "fade");
495 shell->startup_animation_type = get_animation_type(s);
496 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700497 if (shell->startup_animation_type == ANIMATION_ZOOM)
498 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100499 weston_config_section_get_string(section, "focus-animation", &s, "none");
500 shell->focus_animation_type = get_animation_type(s);
501 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400502 weston_config_section_get_uint(section, "num-workspaces",
503 &shell->workspaces.num,
504 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200505}
506
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800507struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100508get_default_output(struct weston_compositor *compositor)
509{
Armin Krezović10b06182016-06-23 11:59:30 +0200510 if (wl_list_empty(&compositor->output_list))
511 return NULL;
512
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100513 return container_of(compositor->output_list.next,
514 struct weston_output, link);
515}
516
Pekka Paalanen8274d902014-08-06 19:36:51 +0300517static int
518focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
519{
520 return snprintf(buf, len, "focus highlight effect for output %s",
521 surface->output->name);
522}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100523
524/* no-op func for checking focus surface */
525static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200526focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100527{
528}
529
530static struct focus_surface *
531get_focus_surface(struct weston_surface *surface)
532{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200533 if (surface->committed == focus_surface_committed)
534 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535 else
536 return NULL;
537}
538
539static bool
540is_focus_surface (struct weston_surface *es)
541{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200542 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100543}
544
545static bool
546is_focus_view (struct weston_view *view)
547{
548 return is_focus_surface (view->surface);
549}
550
551static struct focus_surface *
552create_focus_surface(struct weston_compositor *ec,
553 struct weston_output *output)
554{
555 struct focus_surface *fsurf = NULL;
556 struct weston_surface *surface = NULL;
557
558 fsurf = malloc(sizeof *fsurf);
559 if (!fsurf)
560 return NULL;
561
562 fsurf->surface = weston_surface_create(ec);
563 surface = fsurf->surface;
564 if (surface == NULL) {
565 free(fsurf);
566 return NULL;
567 }
568
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200569 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100570 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200571 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200572 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300573 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100574
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800575 fsurf->view = weston_view_create(surface);
576 if (fsurf->view == NULL) {
577 weston_surface_destroy(surface);
578 free(fsurf);
579 return NULL;
580 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100581 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200582 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100583
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600584 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600585 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
587 pixman_region32_fini(&surface->opaque);
588 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
589 output->width, output->height);
590 pixman_region32_fini(&surface->input);
591 pixman_region32_init(&surface->input);
592
593 wl_list_init(&fsurf->workspace_transform.link);
594
595 return fsurf;
596}
597
598static void
599focus_surface_destroy(struct focus_surface *fsurf)
600{
601 weston_surface_destroy(fsurf->surface);
602 free(fsurf);
603}
604
605static void
606focus_animation_done(struct weston_view_animation *animation, void *data)
607{
608 struct workspace *ws = data;
609
610 ws->focus_animation = NULL;
611}
612
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200613static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200614focus_state_destroy(struct focus_state *state)
615{
616 wl_list_remove(&state->seat_destroy_listener.link);
617 wl_list_remove(&state->surface_destroy_listener.link);
618 free(state);
619}
620
621static void
622focus_state_seat_destroy(struct wl_listener *listener, void *data)
623{
624 struct focus_state *state = container_of(listener,
625 struct focus_state,
626 seat_destroy_listener);
627
628 wl_list_remove(&state->link);
629 focus_state_destroy(state);
630}
631
632static void
633focus_state_surface_destroy(struct wl_listener *listener, void *data)
634{
635 struct focus_state *state = container_of(listener,
636 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400637 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200638 struct weston_surface *main_surface;
639 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200641
Pekka Paalanen01388e22013-04-25 13:57:44 +0300642 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
643
Kristian Høgsberge3778222012-07-31 17:29:30 -0400644 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300645 wl_list_for_each(view,
646 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400648 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100649 if (is_focus_view(view))
650 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200651 if (!get_shell_surface(view->surface))
652 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400653
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200654 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400655 break;
656 }
657
Pekka Paalanen01388e22013-04-25 13:57:44 +0300658 /* if the focus was a sub-surface, activate its main surface */
659 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200660 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300661
Kristian Høgsberge3778222012-07-31 17:29:30 -0400662 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100663 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200664 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200665 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400666 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200667 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100668 if (state->ws->focus_animation)
669 weston_view_animation_destroy(state->ws->focus_animation);
670
671 state->ws->focus_animation = weston_fade_run(
672 state->ws->fsurf_front->view,
673 state->ws->fsurf_front->view->alpha, 0.0, 300,
674 focus_animation_done, state->ws);
675 }
676
Kristian Høgsberge3778222012-07-31 17:29:30 -0400677 wl_list_remove(&state->link);
678 focus_state_destroy(state);
679 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200680}
681
682static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200683focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
684 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200685{
Jonas Ådahl04769742012-06-13 00:01:24 +0200686 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200687
688 state = malloc(sizeof *state);
689 if (state == NULL)
690 return NULL;
691
Quentin Glidicfff39812016-08-15 10:56:52 +0200692 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400694 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200695 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400696 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 state->seat_destroy_listener.notify = focus_state_seat_destroy;
699 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400700 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200701 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400702 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200703
704 return state;
705}
706
Jonas Ådahl8538b222012-08-29 22:13:03 +0200707static struct focus_state *
708ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
709{
710 struct workspace *ws = get_current_workspace(shell);
711 struct focus_state *state;
712
713 wl_list_for_each(state, &ws->focus_list, link)
714 if (state->seat == seat)
715 break;
716
717 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200718 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200719
720 return state;
721}
722
Jonas Ådahl04769742012-06-13 00:01:24 +0200723static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800724focus_state_set_focus(struct focus_state *state,
725 struct weston_surface *surface)
726{
727 if (state->keyboard_focus) {
728 wl_list_remove(&state->surface_destroy_listener.link);
729 wl_list_init(&state->surface_destroy_listener.link);
730 }
731
732 state->keyboard_focus = surface;
733 if (surface)
734 wl_signal_add(&surface->destroy_signal,
735 &state->surface_destroy_listener);
736}
737
738static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400739restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200740{
741 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400742 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100743 struct wl_list pending_seat_list;
744 struct weston_seat *seat, *next_seat;
745
746 /* Temporarily steal the list of seats so that we can keep
747 * track of the seats we've already processed */
748 wl_list_init(&pending_seat_list);
749 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
750 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200751
752 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500753 struct weston_keyboard *keyboard =
754 weston_seat_get_keyboard(state->seat);
755
Neil Roberts4237f502014-04-09 16:33:31 +0100756 wl_list_remove(&state->seat->link);
757 wl_list_insert(&shell->compositor->seat_list,
758 &state->seat->link);
759
Derek Foreman1281a362015-07-31 16:55:32 -0500760 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800761 continue;
762
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400763 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200764
Derek Foreman1281a362015-07-31 16:55:32 -0500765 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200766 }
Neil Roberts4237f502014-04-09 16:33:31 +0100767
768 /* For any remaining seats that we don't have a focus state
769 * for we'll reset the keyboard focus to NULL */
770 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500771 struct weston_keyboard *keyboard =
772 weston_seat_get_keyboard(seat);
773
Neil Roberts4237f502014-04-09 16:33:31 +0100774 wl_list_insert(&shell->compositor->seat_list, &seat->link);
775
Derek Foreman1281a362015-07-31 16:55:32 -0500776 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100777 continue;
778
Derek Foreman1281a362015-07-31 16:55:32 -0500779 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100780 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200781}
782
783static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200784replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
785 struct weston_seat *seat)
786{
Derek Foreman1281a362015-07-31 16:55:32 -0500787 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200788 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789
790 wl_list_for_each(state, &ws->focus_list, link) {
791 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500792 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200793 return;
794 }
795 }
796}
797
798static void
799drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
800 struct weston_surface *surface)
801{
802 struct focus_state *state;
803
804 wl_list_for_each(state, &ws->focus_list, link)
805 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800806 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200807}
808
809static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100810animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
811 struct weston_view *from, struct weston_view *to)
812{
813 struct weston_output *output;
814 bool focus_surface_created = false;
815
816 /* FIXME: Only support dim animation using two layers */
817 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
818 return;
819
820 output = get_default_output(shell->compositor);
821 if (ws->fsurf_front == NULL && (from || to)) {
822 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800823 if (ws->fsurf_front == NULL)
824 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100825 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800826
827 ws->fsurf_back = create_focus_surface(shell->compositor, output);
828 if (ws->fsurf_back == NULL) {
829 focus_surface_destroy(ws->fsurf_front);
830 return;
831 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800833
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 focus_surface_created = true;
835 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300836 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
837 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100838 }
839
840 if (ws->focus_animation) {
841 weston_view_animation_destroy(ws->focus_animation);
842 ws->focus_animation = NULL;
843 }
844
845 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300846 weston_layer_entry_insert(&to->layer_link,
847 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100848 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300849 weston_layer_entry_insert(&ws->layer.view_list,
850 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851
852 if (focus_surface_created) {
853 ws->focus_animation = weston_fade_run(
854 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200855 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100856 focus_animation_done, ws);
857 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300858 weston_layer_entry_insert(&from->layer_link,
859 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 ws->focus_animation = weston_stable_fade_run(
861 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200862 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 focus_animation_done, ws);
864 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300865 weston_layer_entry_insert(&ws->layer.view_list,
866 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 ws->focus_animation = weston_stable_fade_run(
868 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200869 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 focus_animation_done, ws);
871 }
872}
873
874static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200875workspace_destroy(struct workspace *ws)
876{
Jonas Ådahl04769742012-06-13 00:01:24 +0200877 struct focus_state *state, *next;
878
879 wl_list_for_each_safe(state, next, &ws->focus_list, link)
880 focus_state_destroy(state);
881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 if (ws->fsurf_front)
883 focus_surface_destroy(ws->fsurf_front);
884 if (ws->fsurf_back)
885 focus_surface_destroy(ws->fsurf_back);
886
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887 free(ws);
888}
889
Jonas Ådahl04769742012-06-13 00:01:24 +0200890static void
891seat_destroyed(struct wl_listener *listener, void *data)
892{
893 struct weston_seat *seat = data;
894 struct focus_state *state, *next;
895 struct workspace *ws = container_of(listener,
896 struct workspace,
897 seat_destroyed_listener);
898
899 wl_list_for_each_safe(state, next, &ws->focus_list, link)
900 if (state->seat == seat)
901 wl_list_remove(&state->link);
902}
903
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200904static struct workspace *
905workspace_create(void)
906{
907 struct workspace *ws = malloc(sizeof *ws);
908 if (ws == NULL)
909 return NULL;
910
911 weston_layer_init(&ws->layer, NULL);
912
Jonas Ådahl04769742012-06-13 00:01:24 +0200913 wl_list_init(&ws->focus_list);
914 wl_list_init(&ws->seat_destroyed_listener.link);
915 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100916 ws->fsurf_front = NULL;
917 ws->fsurf_back = NULL;
918 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200919
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200920 return ws;
921}
922
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200923static int
924workspace_is_empty(struct workspace *ws)
925{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300926 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927}
928
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929static struct workspace *
930get_workspace(struct desktop_shell *shell, unsigned int index)
931{
932 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200933 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200934 pws += index;
935 return *pws;
936}
937
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800938struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200939get_current_workspace(struct desktop_shell *shell)
940{
941 return get_workspace(shell, shell->workspaces.current);
942}
943
944static void
945activate_workspace(struct desktop_shell *shell, unsigned int index)
946{
947 struct workspace *ws;
948
949 ws = get_workspace(shell, index);
950 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
951
952 shell->workspaces.current = index;
953}
954
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955static unsigned int
956get_output_height(struct weston_output *output)
957{
958 return abs(output->region.extents.y1 - output->region.extents.y2);
959}
960
961static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100962view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200963{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 struct weston_transform *transform;
965
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 if (is_focus_view(view)) {
967 struct focus_surface *fsurf = get_focus_surface(view->surface);
968 transform = &fsurf->workspace_transform;
969 } else {
970 struct shell_surface *shsurf = get_shell_surface(view->surface);
971 transform = &shsurf->workspace_transform;
972 }
973
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 weston_matrix_init(&transform->matrix);
979 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982}
983
984static void
985workspace_translate_out(struct workspace *ws, double fraction)
986{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988 unsigned int height;
989 double d;
990
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300991 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993 d = height * fraction;
994
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 }
997}
998
999static void
1000workspace_translate_in(struct workspace *ws, double fraction)
1001{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003 unsigned int height;
1004 double d;
1005
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001006 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008
1009 if (fraction > 0)
1010 d = -(height - height * fraction);
1011 else
1012 d = height + height * fraction;
1013
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 }
1016}
1017
1018static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019reverse_workspace_change_animation(struct desktop_shell *shell,
1020 unsigned int index,
1021 struct workspace *from,
1022 struct workspace *to)
1023{
1024 shell->workspaces.current = index;
1025
1026 shell->workspaces.anim_to = to;
1027 shell->workspaces.anim_from = from;
1028 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1029 shell->workspaces.anim_timestamp = 0;
1030
Scott Moreau4272e632012-08-13 09:58:41 -06001031 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032}
1033
1034static void
1035workspace_deactivate_transforms(struct workspace *ws)
1036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001038 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001039
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001040 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001041 if (is_focus_view(view)) {
1042 struct focus_surface *fsurf = get_focus_surface(view->surface);
1043 transform = &fsurf->workspace_transform;
1044 } else {
1045 struct shell_surface *shsurf = get_shell_surface(view->surface);
1046 transform = &shsurf->workspace_transform;
1047 }
1048
1049 if (!wl_list_empty(&transform->link)) {
1050 wl_list_remove(&transform->link);
1051 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001052 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001054 }
1055}
1056
1057static void
1058finish_workspace_change_animation(struct desktop_shell *shell,
1059 struct workspace *from,
1060 struct workspace *to)
1061{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001062 struct weston_view *view;
1063
Scott Moreau4272e632012-08-13 09:58:41 -06001064 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001066 /* Views that extend past the bottom of the output are still
1067 * visible after the workspace animation ends but before its layer
1068 * is hidden. In that case, we need to damage below those views so
1069 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001070 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001071 weston_view_damage_below(view);
1072
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073 wl_list_remove(&shell->workspaces.animation.link);
1074 workspace_deactivate_transforms(from);
1075 workspace_deactivate_transforms(to);
1076 shell->workspaces.anim_to = NULL;
1077
1078 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1079}
1080
1081static void
1082animate_workspace_change_frame(struct weston_animation *animation,
1083 struct weston_output *output, uint32_t msecs)
1084{
1085 struct desktop_shell *shell =
1086 container_of(animation, struct desktop_shell,
1087 workspaces.animation);
1088 struct workspace *from = shell->workspaces.anim_from;
1089 struct workspace *to = shell->workspaces.anim_to;
1090 uint32_t t;
1091 double x, y;
1092
1093 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1094 finish_workspace_change_animation(shell, from, to);
1095 return;
1096 }
1097
1098 if (shell->workspaces.anim_timestamp == 0) {
1099 if (shell->workspaces.anim_current == 0.0)
1100 shell->workspaces.anim_timestamp = msecs;
1101 else
1102 shell->workspaces.anim_timestamp =
1103 msecs -
1104 /* Invers of movement function 'y' below. */
1105 (asin(1.0 - shell->workspaces.anim_current) *
1106 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1107 M_2_PI);
1108 }
1109
1110 t = msecs - shell->workspaces.anim_timestamp;
1111
1112 /*
1113 * x = [0, π/2]
1114 * y(x) = sin(x)
1115 */
1116 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1117 y = sin(x);
1118
1119 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001120 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121
1122 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1123 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1124 shell->workspaces.anim_current = y;
1125
Scott Moreau4272e632012-08-13 09:58:41 -06001126 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001128 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130}
1131
1132static void
1133animate_workspace_change(struct desktop_shell *shell,
1134 unsigned int index,
1135 struct workspace *from,
1136 struct workspace *to)
1137{
1138 struct weston_output *output;
1139
1140 int dir;
1141
1142 if (index > shell->workspaces.current)
1143 dir = -1;
1144 else
1145 dir = 1;
1146
1147 shell->workspaces.current = index;
1148
1149 shell->workspaces.anim_dir = dir;
1150 shell->workspaces.anim_from = from;
1151 shell->workspaces.anim_to = to;
1152 shell->workspaces.anim_current = 0.0;
1153 shell->workspaces.anim_timestamp = 0;
1154
1155 output = container_of(shell->compositor->output_list.next,
1156 struct weston_output, link);
1157 wl_list_insert(&output->animation_list,
1158 &shell->workspaces.animation.link);
1159
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161
1162 workspace_translate_in(to, 0);
1163
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001164 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001165
Scott Moreau4272e632012-08-13 09:58:41 -06001166 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167}
1168
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001170update_workspace(struct desktop_shell *shell, unsigned int index,
1171 struct workspace *from, struct workspace *to)
1172{
1173 shell->workspaces.current = index;
1174 wl_list_insert(&from->layer.link, &to->layer.link);
1175 wl_list_remove(&from->layer.link);
1176}
1177
1178static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179change_workspace(struct desktop_shell *shell, unsigned int index)
1180{
1181 struct workspace *from;
1182 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184
1185 if (index == shell->workspaces.current)
1186 return;
1187
1188 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001189 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001190 return;
1191
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192 from = get_current_workspace(shell);
1193 to = get_workspace(shell, index);
1194
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195 if (shell->workspaces.anim_from == to &&
1196 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198 reverse_workspace_change_animation(shell, index, from, to);
1199 return;
1200 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 if (shell->workspaces.anim_to != NULL)
1203 finish_workspace_change_animation(shell,
1204 shell->workspaces.anim_from,
1205 shell->workspaces.anim_to);
1206
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001207 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001208
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001209 if (shell->focus_animation_type != ANIMATION_NONE) {
1210 wl_list_for_each(state, &from->focus_list, link)
1211 if (state->keyboard_focus)
1212 animate_focus_change(shell, from,
1213 get_default_view(state->keyboard_focus), NULL);
1214
1215 wl_list_for_each(state, &to->focus_list, link)
1216 if (state->keyboard_focus)
1217 animate_focus_change(shell, to,
1218 NULL, get_default_view(state->keyboard_focus));
1219 }
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 if (workspace_is_empty(to) && workspace_is_empty(from))
1222 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001223 else
1224 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001225}
1226
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227static bool
1228workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1229{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001230 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 struct wl_list *e;
1232
1233 if (wl_list_empty(list))
1234 return false;
1235
1236 e = list->next;
1237
1238 if (e->next != list)
1239 return false;
1240
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001241 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242}
1243
1244static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001245surface_keyboard_focus_lost(struct weston_surface *surface)
1246{
1247 struct weston_compositor *compositor = surface->compositor;
1248 struct weston_seat *seat;
1249 struct weston_surface *focus;
1250
1251 wl_list_for_each(seat, &compositor->seat_list, link) {
1252 struct weston_keyboard *keyboard =
1253 weston_seat_get_keyboard(seat);
1254
1255 if (!keyboard)
1256 continue;
1257
1258 focus = weston_surface_get_main_surface(keyboard->focus);
1259 if (focus == surface)
1260 weston_keyboard_set_focus(keyboard, NULL);
1261 }
1262}
1263
1264static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001266 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 unsigned int index)
1268{
Derek Foreman1281a362015-07-31 16:55:32 -05001269 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001270 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 struct shell_surface *shsurf;
1273 struct workspace *from;
1274 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001275 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276
Derek Foreman1281a362015-07-31 16:55:32 -05001277 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 view = get_default_view(surface);
1279 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001280 index == shell->workspaces.current ||
1281 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282 return;
1283
1284 from = get_current_workspace(shell);
1285 to = get_workspace(shell, index);
1286
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001287 weston_layer_entry_remove(&view->layer_link);
1288 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289
Philip Withnall659163d2013-11-25 18:01:36 +00001290 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001291 if (shsurf != NULL)
1292 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001293
Jonas Ådahle9d22502012-08-29 22:13:01 +02001294 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 drop_focus_state(shell, from, surface);
1296
1297 if (shell->workspaces.anim_from == to &&
1298 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 wl_list_remove(&to->layer.link);
1300 wl_list_insert(from->layer.link.prev, &to->layer.link);
1301
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 return;
1305 }
1306
1307 if (shell->workspaces.anim_to != NULL)
1308 finish_workspace_change_animation(shell,
1309 shell->workspaces.anim_from,
1310 shell->workspaces.anim_to);
1311
1312 if (workspace_is_empty(from) &&
1313 workspace_has_only(to, surface))
1314 update_workspace(shell, index, from, to);
1315 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001316 if (shsurf != NULL &&
1317 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 wl_list_insert(&shell->workspaces.anim_sticky_list,
1319 &shsurf->workspace_transform.link);
1320
1321 animate_workspace_change(shell, index, from, to);
1322 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
Jonas Ådahl8538b222012-08-29 22:13:03 +02001324 state = ensure_focus_state(shell, seat);
1325 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001326 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327}
1328
1329static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001330touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001331 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001332{
1333}
1334
1335static void
1336touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1337{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001338 struct weston_touch_move_grab *move =
1339 (struct weston_touch_move_grab *) container_of(
1340 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001341
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001342 if (touch_id == 0)
1343 move->active = 0;
1344
Jonas Ådahl9484b692013-12-02 22:05:03 +01001345 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001346 shell_touch_grab_end(&move->base);
1347 free(move);
1348 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001349}
1350
1351static void
1352touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001353 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001354{
1355 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1356 struct shell_surface *shsurf = move->base.shsurf;
1357 struct weston_surface *es;
1358 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1359 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1360
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001361 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001362 return;
1363
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001364 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001365
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001366 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001367
1368 weston_compositor_schedule_repaint(es->compositor);
1369}
1370
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001371static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001372touch_move_grab_frame(struct weston_touch_grab *grab)
1373{
1374}
1375
1376static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001377touch_move_grab_cancel(struct weston_touch_grab *grab)
1378{
1379 struct weston_touch_move_grab *move =
1380 (struct weston_touch_move_grab *) container_of(
1381 grab, struct shell_touch_grab, grab);
1382
1383 shell_touch_grab_end(&move->base);
1384 free(move);
1385}
1386
Rusty Lynch1084da52013-08-15 09:10:08 -07001387static const struct weston_touch_grab_interface touch_move_grab_interface = {
1388 touch_move_grab_down,
1389 touch_move_grab_up,
1390 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001391 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001392 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001393};
1394
1395static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001396surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001397{
1398 struct weston_touch_move_grab *move;
1399
1400 if (!shsurf)
1401 return -1;
1402
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001403 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1404 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001405 return 0;
1406
1407 move = malloc(sizeof *move);
1408 if (!move)
1409 return -1;
1410
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001411 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001413 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001415 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001416
1417 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001418 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001419
1420 return 0;
1421}
1422
1423static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001424noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001426}
1427
1428static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001429noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001430 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001431{
1432}
1433
1434static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001435noop_grab_axis_source(struct weston_pointer_grab *grab,
1436 uint32_t source)
1437{
1438}
1439
1440static void
1441noop_grab_frame(struct weston_pointer_grab *grab)
1442{
1443}
1444
1445static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001446constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1447{
1448 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001449 struct weston_surface *surface =
1450 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001451 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001452 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001454 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001455 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001456
1457 x = wl_fixed_to_int(pointer->x + move->dx);
1458 y = wl_fixed_to_int(pointer->y + move->dy);
1459
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001460 if (shsurf->shell->panel_position ==
1461 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001462 get_output_work_area(shsurf->shell, surface->output, &area);
1463 geometry =
1464 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001466 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001467 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001468 y = area.y + safety - geometry.height
1469 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001470
1471 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001472 y + geometry.y < area.y)
1473 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001474 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475
1476 *cx = x;
1477 *cy = y;
1478}
1479
1480static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001481move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001482 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001485 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001486 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001487 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001488 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001489
Jonas Ådahld2510102014-10-05 21:39:14 +02001490 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001491 if (!shsurf)
1492 return;
1493
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001494 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1495
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001496 constrain_position(move, &cx, &cy);
1497
1498 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001500 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501}
1502
1503static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001504move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001505 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001506{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001507 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1508 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001509 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001510 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511
Daniel Stone4dbadb12012-05-30 16:31:51 +01001512 if (pointer->button_count == 0 &&
1513 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001514 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001515 free(grab);
1516 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001517}
1518
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001519static void
1520move_grab_cancel(struct weston_pointer_grab *grab)
1521{
1522 struct shell_grab *shell_grab =
1523 container_of(grab, struct shell_grab, grab);
1524
1525 shell_grab_end(shell_grab);
1526 free(grab);
1527}
1528
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001529static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001530 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531 move_grab_motion,
1532 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001533 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001534 noop_grab_axis_source,
1535 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001536 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001537};
1538
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001540surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001541 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542{
1543 struct weston_move_grab *move;
1544
1545 if (!shsurf)
1546 return -1;
1547
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001548 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001549 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1550 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551 return 0;
1552
1553 move = malloc(sizeof *move);
1554 if (!move)
1555 return -1;
1556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001558 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001560 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001561 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562
1563 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001564 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565
1566 return 0;
1567}
1568
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569struct weston_resize_grab {
1570 struct shell_grab base;
1571 uint32_t edges;
1572 int32_t width, height;
1573};
1574
1575static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001576resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001577 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578{
1579 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001580 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 struct shell_surface *shsurf = resize->base.shsurf;
1582 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001583 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584 wl_fixed_t from_x, from_y;
1585 wl_fixed_t to_x, to_y;
1586
Jonas Ådahld2510102014-10-05 21:39:14 +02001587 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001588
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001589 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590 return;
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 weston_view_from_global_fixed(shsurf->view,
1593 pointer->grab_x, pointer->grab_y,
1594 &from_x, &from_y);
1595 weston_view_from_global_fixed(shsurf->view,
1596 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597
1598 width = resize->width;
1599 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1600 width += wl_fixed_to_int(from_x - to_x);
1601 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1602 width += wl_fixed_to_int(to_x - from_x);
1603 }
1604
1605 height = resize->height;
1606 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1607 height += wl_fixed_to_int(from_y - to_y);
1608 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1609 height += wl_fixed_to_int(to_y - from_y);
1610 }
1611
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001612 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1613 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1614
1615 min_size.width = MAX(1, min_size.width);
1616 min_size.height = MAX(1, min_size.height);
1617
1618 if (width < min_size.width)
1619 width = min_size.width;
1620 else if (max_size.width > 0 && width > max_size.width)
1621 width = max_size.width;
1622 if (height < min_size.height)
1623 height = min_size.height;
1624 else if (max_size.width > 0 && width > max_size.width)
1625 width = max_size.width;
1626 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001627}
1628
1629static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001630resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001631 uint32_t time, uint32_t button, uint32_t state_w)
1632{
1633 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001634 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001635 enum wl_pointer_button_state state = state_w;
1636
1637 if (pointer->button_count == 0 &&
1638 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1639 shell_grab_end(&resize->base);
1640 free(grab);
1641 }
1642}
1643
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001644static void
1645resize_grab_cancel(struct weston_pointer_grab *grab)
1646{
1647 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1648
1649 shell_grab_end(&resize->base);
1650 free(grab);
1651}
1652
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001653static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654 noop_grab_focus,
1655 resize_grab_motion,
1656 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001657 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001658 noop_grab_axis_source,
1659 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001660 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001661};
1662
Giulio Camuffob8366642013-04-25 13:57:46 +03001663/*
1664 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001665 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001666static void
1667surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1668 int32_t *y, int32_t *w, int32_t *h) {
1669 pixman_region32_t region;
1670 pixman_box32_t *box;
1671 struct weston_subsurface *subsurface;
1672
1673 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 surface->width,
1675 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001676
1677 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1678 pixman_region32_union_rect(&region, &region,
1679 subsurface->position.x,
1680 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 subsurface->surface->width,
1682 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001683 }
1684
1685 box = pixman_region32_extents(&region);
1686 if (x)
1687 *x = box->x1;
1688 if (y)
1689 *y = box->y1;
1690 if (w)
1691 *w = box->x2 - box->x1;
1692 if (h)
1693 *h = box->y2 - box->y1;
1694
1695 pixman_region32_fini(&region);
1696}
1697
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001698static int
1699surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001700 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701{
1702 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001703 const unsigned resize_topbottom =
1704 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1705 const unsigned resize_leftright =
1706 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1707 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001708 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001710 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001711 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1712 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713 return 0;
1714
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001715 /* Check for invalid edge combinations. */
1716 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1717 (edges & resize_topbottom) == resize_topbottom ||
1718 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 return 0;
1720
1721 resize = malloc(sizeof *resize);
1722 if (!resize)
1723 return -1;
1724
1725 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001727 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1728 resize->width = geometry.width;
1729 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001730
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001731 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001733 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734
1735 return 0;
1736}
1737
1738static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001743 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001745 wl_fixed_t sx, sy;
1746
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 view = weston_compositor_pick_view(pointer->seat->compositor,
1748 pointer->x, pointer->y,
1749 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001750 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001751
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001752 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001753 shell_grab_end(grab);
1754 free(grab);
1755 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001756}
1757
1758static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001759busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001760 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761{
Jonas Ådahld2510102014-10-05 21:39:14 +02001762 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001766busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001767 uint32_t time, uint32_t button, uint32_t state)
1768{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001769 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001770 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001771 struct weston_pointer *pointer = grab->grab.pointer;
1772 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001773
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001774 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001775 activate(shsurf->shell, shsurf->view, seat,
1776 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001777 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001778 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001779 activate(shsurf->shell, shsurf->view, seat,
1780 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001781 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001782 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783}
1784
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001785static void
1786busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1787{
1788 struct shell_grab *grab = (struct shell_grab *) base;
1789
1790 shell_grab_end(grab);
1791 free(grab);
1792}
1793
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001794static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795 busy_cursor_grab_focus,
1796 busy_cursor_grab_motion,
1797 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001798 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001799 noop_grab_axis_source,
1800 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001801 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802};
1803
1804static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805handle_pointer_focus(struct wl_listener *listener, void *data)
1806{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001807 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001809 struct shell_surface *shsurf;
1810 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813 return;
1814
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001815 shsurf = get_shell_surface(view->surface);
1816 if (!shsurf)
1817 return;
1818
1819 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1820
1821 if (shsurf->unresponsive)
1822 set_busy_cursor(shsurf, pointer);
1823 else
1824 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825}
1826
1827static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001828shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1829{
1830 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001831 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001832}
1833
1834static void
1835shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1836{
1837 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001838 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001839}
1840
1841static void
1842handle_keyboard_focus(struct wl_listener *listener, void *data)
1843{
1844 struct weston_keyboard *keyboard = data;
1845 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1846
1847 if (seat->focused_surface) {
1848 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1849 if (shsurf)
1850 shell_surface_lose_keyboard_focus(shsurf);
1851 }
1852
1853 seat->focused_surface = keyboard->focus;
1854
1855 if (seat->focused_surface) {
1856 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1857 if (shsurf)
1858 shell_surface_gain_keyboard_focus(shsurf);
1859 }
1860}
1861
Philip Withnall07926d92013-11-25 18:01:40 +00001862/* The surface will be inserted into the list immediately after the link
1863 * returned by this function (i.e. will be stacked immediately above the
1864 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001865static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001866shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1867{
1868 struct workspace *ws;
1869
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001870 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1871 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001872 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001873 }
1874
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 /* Move the surface to a normal workspace layer so that surfaces
1876 * which were previously fullscreen or transient are no longer
1877 * rendered on top. */
1878 ws = get_current_workspace(shsurf->shell);
1879 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001880}
1881
Philip Withnall648a4dd2013-11-25 18:01:44 +00001882static void
1883shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1884{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001885 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001886}
1887
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001888/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001889 * geometry to ensure the changes are redrawn.
1890 *
1891 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1892 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001893static void
1894shell_surface_update_layer(struct shell_surface *shsurf)
1895{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001896 struct weston_surface *surface =
1897 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001898 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001899
1900 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1901
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001902 if (new_layer_link == NULL)
1903 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001904 if (new_layer_link == &shsurf->view->layer_link)
1905 return;
1906
1907 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001908 weston_layer_entry_remove(&shsurf->view->layer_link);
1909 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001910 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001911 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001912
1913 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001914}
1915
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001916static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001917shell_surface_set_output(struct shell_surface *shsurf,
1918 struct weston_output *output)
1919{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001920 struct weston_surface *es =
1921 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001922
1923 /* get the default output, if the client set it as NULL
1924 check whether the ouput is available */
1925 if (output)
1926 shsurf->output = output;
1927 else if (es->output)
1928 shsurf->output = es->output;
1929 else
1930 shsurf->output = get_default_output(es->compositor);
1931}
1932
1933static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001934weston_view_set_initial_position(struct weston_view *view,
1935 struct desktop_shell *shell);
1936
1937static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001938unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001939{
Philip Withnallf85fe842013-11-25 18:01:37 +00001940 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001941 wl_list_remove(&shsurf->fullscreen.transform.link);
1942 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 if (shsurf->fullscreen.black_view)
1945 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1946 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001947
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001948 if (shsurf->saved_position_valid)
1949 weston_view_set_position(shsurf->view,
1950 shsurf->saved_x, shsurf->saved_y);
1951 else
1952 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001953 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001954
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001955 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001957 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001958 shsurf->saved_rotation_valid = false;
1959 }
Alex Wu4539b082012-03-01 12:57:46 +08001960}
1961
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001962static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001963unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001964{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001965 struct weston_surface *surface =
1966 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1967
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001968 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001969 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001970
1971 if (shsurf->saved_position_valid)
1972 weston_view_set_position(shsurf->view,
1973 shsurf->saved_x, shsurf->saved_y);
1974 else
1975 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001976 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001977
1978 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 wl_list_insert(&shsurf->view->geometry.transformation_list,
1980 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001981 shsurf->saved_rotation_valid = false;
1982 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001983}
1984
Philip Withnallbecb77e2013-11-25 18:01:30 +00001985static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001986set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001987{
1988 struct shell_surface *shsurf;
1989 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001990 struct weston_view *view;
1991
1992 view = get_default_view(surface);
1993 if (!view)
1994 return;
1995
1996 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1997
1998 shsurf = get_shell_surface(surface);
1999 current_ws = get_current_workspace(shsurf->shell);
2000
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002001 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002002 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002003
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002004 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002005 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002006
2007 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002008 weston_view_damage_below(view);
2009}
2010
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002011
Tiago Vignattibe143262012-04-16 17:31:41 +03002012static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002013shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002014{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002015 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002016}
2017
Pekka Paalanen8274d902014-08-06 19:36:51 +03002018static int
2019black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2020{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002021 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002022 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002023 int n;
2024 int rem;
2025 int ret;
2026
2027 n = snprintf(buf, len, "black background surface for ");
2028 if (n < 0)
2029 return n;
2030
2031 rem = (int)len - n;
2032 if (rem < 0)
2033 rem = 0;
2034
2035 if (fs_surface->get_label)
2036 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2037 else
2038 ret = snprintf(buf + n, rem, "<unknown>");
2039
2040 if (ret < 0)
2041 return n;
2042
2043 return n + ret;
2044}
2045
Alex Wu21858432012-04-01 20:13:08 +08002046static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002047black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002050create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002051 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002052 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002053{
2054 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002055 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002056
2057 surface = weston_surface_create(ec);
2058 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002059 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002060 return NULL;
2061 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 view = weston_view_create(surface);
2063 if (surface == NULL) {
2064 weston_log("no memory\n");
2065 weston_surface_destroy(surface);
2066 return NULL;
2067 }
Alex Wu4539b082012-03-01 12:57:46 +08002068
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002069 surface->committed = black_surface_committed;
2070 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002071 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002072 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002073 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002074 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002075 pixman_region32_fini(&surface->input);
2076 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002077
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002078 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002079 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080
2081 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002082}
2083
Philip Withnalled8f1a92013-11-25 18:01:43 +00002084static void
2085shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2086{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002087 struct weston_surface *surface =
2088 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002089 struct weston_output *output = shsurf->fullscreen_output;
2090
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002091 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002092
2093 if (!shsurf->fullscreen.black_view)
2094 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002095 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002096 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002097 output->x, output->y,
2098 output->width,
2099 output->height);
2100
2101 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002102 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2103 weston_layer_entry_insert(&shsurf->view->layer_link,
2104 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002105 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002106 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002107
Armin Krezović4663aca2016-06-30 06:04:29 +02002108 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002109 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002110}
2111
Alex Wu4539b082012-03-01 12:57:46 +08002112/* Create black surface and append it to the associated fullscreen surface.
2113 * Handle size dismatch and positioning according to the method. */
2114static void
2115shell_configure_fullscreen(struct shell_surface *shsurf)
2116{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002117 struct weston_surface *surface =
2118 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002119 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002120
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002121 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002122 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002123 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2124 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002125
Philip Withnalled8f1a92013-11-25 18:01:43 +00002126 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002127
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002128 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002129 &surf_width, &surf_height);
2130
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002131 if (surface->buffer_ref.buffer)
2132 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002133}
2134
Alex Wu4539b082012-03-01 12:57:46 +08002135static void
2136shell_map_fullscreen(struct shell_surface *shsurf)
2137{
Alex Wubd3354b2012-04-17 17:20:49 +08002138 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002139}
2140
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002141static struct weston_output *
2142get_focused_output(struct weston_compositor *compositor)
2143{
2144 struct weston_seat *seat;
2145 struct weston_output *output = NULL;
2146
2147 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002148 struct weston_touch *touch = weston_seat_get_touch(seat);
2149 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2150 struct weston_keyboard *keyboard =
2151 weston_seat_get_keyboard(seat);
2152
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002153 /* Priority has touch focus, then pointer and
2154 * then keyboard focus. We should probably have
2155 * three for loops and check frist for touch,
2156 * then for pointer, etc. but unless somebody has some
2157 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002158 if (touch && touch->focus)
2159 output = touch->focus->output;
2160 else if (pointer && pointer->focus)
2161 output = pointer->focus->output;
2162 else if (keyboard && keyboard->focus)
2163 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002164
2165 if (output)
2166 break;
2167 }
2168
2169 return output;
2170}
2171
2172static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002173destroy_shell_seat(struct wl_listener *listener, void *data)
2174{
2175 struct shell_seat *shseat =
2176 container_of(listener,
2177 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002178
2179 wl_list_remove(&shseat->seat_destroy_listener.link);
2180 free(shseat);
2181}
2182
Jason Ekstrand024177c2014-04-21 19:42:58 -05002183static void
2184shell_seat_caps_changed(struct wl_listener *listener, void *data)
2185{
Derek Foreman1281a362015-07-31 16:55:32 -05002186 struct weston_keyboard *keyboard;
2187 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002188 struct shell_seat *seat;
2189
2190 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002191 keyboard = weston_seat_get_keyboard(seat->seat);
2192 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002193
Derek Foreman1281a362015-07-31 16:55:32 -05002194 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002196 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002198 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002199 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002200 wl_list_init(&seat->keyboard_focus_listener.link);
2201 }
2202
Derek Foreman1281a362015-07-31 16:55:32 -05002203 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002204 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002205 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002207 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002208 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002209 wl_list_init(&seat->pointer_focus_listener.link);
2210 }
2211}
2212
Giulio Camuffo5085a752013-03-25 21:42:45 +01002213static struct shell_seat *
2214create_shell_seat(struct weston_seat *seat)
2215{
2216 struct shell_seat *shseat;
2217
2218 shseat = calloc(1, sizeof *shseat);
2219 if (!shseat) {
2220 weston_log("no memory to allocate shell seat\n");
2221 return NULL;
2222 }
2223
2224 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002225
2226 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2227 wl_signal_add(&seat->destroy_signal,
2228 &shseat->seat_destroy_listener);
2229
Jason Ekstrand024177c2014-04-21 19:42:58 -05002230 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2231 wl_list_init(&shseat->keyboard_focus_listener.link);
2232
2233 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2234 wl_list_init(&shseat->pointer_focus_listener.link);
2235
2236 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2237 wl_signal_add(&seat->updated_caps_signal,
2238 &shseat->caps_changed_listener);
2239 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2240
Giulio Camuffo5085a752013-03-25 21:42:45 +01002241 return shseat;
2242}
2243
2244static struct shell_seat *
2245get_shell_seat(struct weston_seat *seat)
2246{
2247 struct wl_listener *listener;
2248
2249 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002251
2252 return container_of(listener,
2253 struct shell_seat, seat_destroy_listener);
2254}
2255
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002256static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002257fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002258{
2259 struct shell_surface *shsurf = data;
2260
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002261 weston_surface_destroy(shsurf->view->surface);
2262 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002263}
2264
2265static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002266fade_out_done(struct weston_view_animation *animation, void *data)
2267{
2268 struct shell_surface *shsurf = data;
2269 struct wl_event_loop *loop;
2270
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002271 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273 if (weston_view_is_mapped(shsurf->view)) {
2274 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002275 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002276 }
2277}
2278
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002279struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002280get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002281{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002282 if (weston_surface_is_desktop_surface(surface)) {
2283 struct weston_desktop_surface *desktop_surface =
2284 weston_surface_get_desktop_surface(surface);
2285 return weston_desktop_surface_get_user_data(desktop_surface);
2286 }
2287 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002288}
2289
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002290/*
2291 * libweston-desktop
2292 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002293
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002294static void
2295desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2296 void *shell)
2297{
2298 struct weston_desktop_client *client =
2299 weston_desktop_surface_get_client(desktop_surface);
2300 struct wl_client *wl_client =
2301 weston_desktop_client_get_client(client);
2302 struct weston_view *view;
2303 struct shell_surface *shsurf;
2304 struct weston_surface *surface =
2305 weston_desktop_surface_get_surface(desktop_surface);
2306
2307 view = weston_desktop_surface_create_view(desktop_surface);
2308 if (!view)
2309 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002310
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002311 shsurf = calloc(1, sizeof *shsurf);
2312 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002313 if (wl_client)
2314 wl_client_post_no_memory(wl_client);
2315 else
2316 weston_log("no memory to allocate shell surface\n");
2317 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002318 }
2319
Pekka Paalanen8274d902014-08-06 19:36:51 +03002320 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002321
Tiago Vignattibc052c92012-04-19 16:18:18 +03002322 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002323 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002324 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002325 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002326 shsurf->desktop_surface = desktop_surface;
2327 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002329 wl_list_init(&shsurf->fullscreen.transform.link);
2330
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002331 shsurf->output = get_default_output(shsurf->shell->compositor);
2332
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002333 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002334
Pekka Paalanen460099f2012-01-20 16:48:25 +02002335 /* empty when not in use */
2336 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002337 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002338
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002339 wl_list_init(&shsurf->workspace_transform.link);
2340
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002341 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2342 weston_desktop_surface_set_activated(desktop_surface,
2343 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002344}
2345
Tiago Vignattibc052c92012-04-19 16:18:18 +03002346static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002347desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2348 void *shell)
2349{
2350 struct shell_surface *shsurf =
2351 weston_desktop_surface_get_user_data(desktop_surface);
2352 struct weston_surface *surface =
2353 weston_desktop_surface_get_surface(desktop_surface);
2354
2355 if (!shsurf)
2356 return;
2357
2358 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2359
2360 if (shsurf->fullscreen.black_view)
2361 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2362
2363 weston_surface_set_label_func(surface, NULL);
2364 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2365 shsurf->desktop_surface = NULL;
2366
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002367 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002368 if (weston_surface_is_mapped(surface) &&
2369 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2370 pixman_region32_fini(&surface->pending.input);
2371 pixman_region32_init(&surface->pending.input);
2372 pixman_region32_fini(&surface->input);
2373 pixman_region32_init(&surface->input);
2374 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2375 fade_out_done, shsurf);
2376 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002377 weston_view_destroy(shsurf->view);
2378 free(shsurf);
2379 }
2380}
2381
2382static void
2383set_maximized_position(struct desktop_shell *shell,
2384 struct shell_surface *shsurf)
2385{
2386 pixman_rectangle32_t area;
2387 struct weston_geometry geometry;
2388
2389 get_output_work_area(shell, shsurf->output, &area);
2390 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2391
2392 weston_view_set_position(shsurf->view,
2393 area.x - geometry.x,
2394 area.y - geometry.y);
2395}
2396
2397static void
2398map(struct desktop_shell *shell, struct shell_surface *shsurf,
2399 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002400{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002401 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002402 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2403 struct weston_compositor *compositor = shell->compositor;
2404 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002405
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002406 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002407 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002408 center_on_output(shsurf->view, shsurf->fullscreen_output);
2409 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002410 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002411 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002412 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002413 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002414 }
2415
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 /* Surface stacking order, see also activate(). */
2417 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002418
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419 weston_view_update_transform(shsurf->view);
2420 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002421 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002422 surface->output = shsurf->output;
2423 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002424 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002425
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002426 if (!shell->locked) {
2427 wl_list_for_each(seat, &compositor->seat_list, link)
2428 activate(shell, shsurf->view, seat,
2429 WESTON_ACTIVATE_FLAG_CONFIGURE);
2430 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002431
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002432 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002433 switch (shell->win_animation_type) {
2434 case ANIMATION_FADE:
2435 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2436 break;
2437 case ANIMATION_ZOOM:
2438 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2439 break;
2440 case ANIMATION_NONE:
2441 default:
2442 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002443 }
2444 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002445}
2446
2447static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002448desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2449 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002450{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 struct shell_surface *shsurf =
2452 weston_desktop_surface_get_user_data(desktop_surface);
2453 struct weston_surface *surface =
2454 weston_desktop_surface_get_surface(desktop_surface);
2455 struct weston_view *view = shsurf->view;
2456 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002457 bool was_fullscreen;
2458 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002459
2460 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002461 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002462
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002463 was_fullscreen = shsurf->state.fullscreen;
2464 was_maximized = shsurf->state.maximized;
2465
2466 shsurf->state.fullscreen =
2467 weston_desktop_surface_get_fullscreen(desktop_surface);
2468 shsurf->state.maximized =
2469 weston_desktop_surface_get_maximized(desktop_surface);
2470
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002471 if (!weston_surface_is_mapped(surface)) {
2472 map(shell, shsurf, sx, sy);
2473 surface->is_mapped = true;
2474 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2475 ++surface->ref_count;
2476 return;
2477 }
2478
2479 if (sx == 0 && sy == 0 &&
2480 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002481 shsurf->last_height == surface->height &&
2482 was_fullscreen == shsurf->state.fullscreen &&
2483 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002484 return;
2485
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002486 if (was_fullscreen)
2487 unset_fullscreen(shsurf);
2488 if (was_maximized)
2489 unset_maximized(shsurf);
2490
Quentin Glidic920cf042016-08-16 14:26:20 +02002491 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2492 !shsurf->saved_position_valid) {
2493 shsurf->saved_x = shsurf->view->geometry.x;
2494 shsurf->saved_y = shsurf->view->geometry.y;
2495 shsurf->saved_position_valid = true;
2496
2497 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2498 wl_list_remove(&shsurf->rotation.transform.link);
2499 wl_list_init(&shsurf->rotation.transform.link);
2500 weston_view_geometry_dirty(shsurf->view);
2501 shsurf->saved_rotation_valid = true;
2502 }
2503 }
2504
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002505 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002506 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002507 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002508 set_maximized_position(shell, shsurf);
2509 surface->output = shsurf->output;
2510 } else {
2511 float from_x, from_y;
2512 float to_x, to_y;
2513 float x, y;
2514
2515 if (shsurf->resize_edges) {
2516 sx = 0;
2517 sy = 0;
2518 }
2519
2520 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2521 sx = shsurf->last_width - surface->width;
2522 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2523 sy = shsurf->last_height - surface->height;
2524
2525 shsurf->last_width = surface->width;
2526 shsurf->last_height = surface->height;
2527
2528 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2529 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2530 x = shsurf->view->geometry.x + to_x - from_x;
2531 y = shsurf->view->geometry.y + to_y - from_y;
2532
2533 weston_view_set_position(shsurf->view, x, y);
2534 }
2535
2536 /* XXX: would a fullscreen surface need the same handling? */
2537 if (surface->output) {
2538 wl_list_for_each(view, &surface->views, surface_link)
2539 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002540 }
2541}
2542
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002543static void
2544set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2545 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002546{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002547 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2548 struct weston_surface *surface =
2549 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2550 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002551
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 if (fullscreen) {
2553 /* handle clients launching in fullscreen */
2554 if (output == NULL && !weston_surface_is_mapped(surface)) {
2555 /* Set the output to the one that has focus currently. */
2556 output = get_focused_output(surface->compositor);
2557 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002558
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002559 shell_surface_set_output(shsurf, output);
2560 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002561
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002562 width = shsurf->output->width;
2563 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002564 }
2565 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2566 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002567}
2568
2569static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002570desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2571 struct weston_seat *seat, uint32_t serial, void *shell)
2572{
2573 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2574 struct weston_touch *touch = weston_seat_get_touch(seat);
2575 struct shell_surface *shsurf =
2576 weston_desktop_surface_get_user_data(desktop_surface);
2577 struct weston_surface *surface =
2578 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2579 struct wl_resource *resource = surface->resource;
2580 struct weston_surface *focus;
2581
2582 if (pointer &&
2583 pointer->focus &&
2584 pointer->button_count > 0 &&
2585 pointer->grab_serial == serial) {
2586 focus = weston_surface_get_main_surface(pointer->focus->surface);
2587 if ((focus == surface) &&
2588 (surface_move(shsurf, pointer, true) < 0))
2589 wl_resource_post_no_memory(resource);
2590 } else if (touch &&
2591 touch->focus &&
2592 touch->grab_serial == serial) {
2593 focus = weston_surface_get_main_surface(touch->focus->surface);
2594 if ((focus == surface) &&
2595 (surface_touch_move(shsurf, touch) < 0))
2596 wl_resource_post_no_memory(resource);
2597 }
2598}
2599
2600static void
2601desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2602 struct weston_seat *seat, uint32_t serial,
2603 enum weston_desktop_surface_edge edges, void *shell)
2604{
2605 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2606 struct shell_surface *shsurf =
2607 weston_desktop_surface_get_user_data(desktop_surface);
2608 struct weston_surface *surface =
2609 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2610 struct wl_resource *resource = surface->resource;
2611 struct weston_surface *focus;
2612
2613 if (!pointer ||
2614 pointer->button_count == 0 ||
2615 pointer->grab_serial != serial ||
2616 pointer->focus == NULL)
2617 return;
2618
2619 focus = weston_surface_get_main_surface(pointer->focus->surface);
2620 if (focus != surface)
2621 return;
2622
2623 if (surface_resize(shsurf, pointer, edges) < 0)
2624 wl_resource_post_no_memory(resource);
2625}
2626
2627static void
2628desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2629 bool fullscreen,
2630 struct weston_output *output, void *shell)
2631{
2632 struct shell_surface *shsurf =
2633 weston_desktop_surface_get_user_data(desktop_surface);
2634
2635 set_fullscreen(shsurf, fullscreen, output);
2636}
2637
2638static void
2639set_maximized(struct shell_surface *shsurf, bool maximized)
2640{
2641 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2642 struct weston_surface *surface =
2643 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2644 int32_t width = 0, height = 0;
2645
2646 if (maximized) {
2647 struct weston_output *output;
2648 struct desktop_shell *shell;
2649 pixman_rectangle32_t area;
2650
2651 if (!weston_surface_is_mapped(surface))
2652 output = get_focused_output(surface->compositor);
2653 else
2654 output = surface->output;
2655
2656 shell_surface_set_output(shsurf, output);
2657
2658 shell = shell_surface_get_shell(shsurf);
2659 get_output_work_area(shell, shsurf->output, &area);
2660
2661 width = area.width;
2662 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002663 }
2664 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2665 weston_desktop_surface_set_size(desktop_surface, width, height);
2666}
2667
2668static void
2669desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2670 bool maximized, void *shell)
2671{
2672 struct shell_surface *shsurf =
2673 weston_desktop_surface_get_user_data(desktop_surface);
2674
2675 set_maximized(shsurf, maximized);
2676}
2677
2678static void
2679desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2680 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002681{
2682 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002683 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002684
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002685 /* apply compositor's own minimization logic (hide) */
2686 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002687}
2688
Rafael Antognollie2a34552013-12-03 15:35:45 -02002689static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002691{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002692 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002693
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002694 if (pointer->grab->interface == &busy_cursor_grab_interface)
2695 return;
2696
2697 grab = malloc(sizeof *grab);
2698 if (!grab)
2699 return;
2700
2701 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2702 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2703 /* Mark the shsurf as ungrabbed so that button binding is able
2704 * to move it. */
2705 shsurf->grabbed = 0;
2706}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002707
2708static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002709end_busy_cursor(struct weston_compositor *compositor,
2710 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002711{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002712 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002713 struct shell_grab *grab;
2714 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002715
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002716 wl_list_for_each(seat, &compositor->seat_list, link) {
2717 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2718 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002719
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002720 if (!pointer)
2721 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002722
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002723 if (pointer->grab->interface != &busy_cursor_grab_interface)
2724 continue;
2725
2726 grab = (struct shell_grab *) pointer->grab;
2727 shsurf = grab->shsurf;
2728 if (!shsurf)
2729 continue;
2730
2731 grab_client =
2732 weston_desktop_surface_get_client(shsurf->desktop_surface);
2733 if (grab_client == desktop_client) {
2734 shell_grab_end(grab);
2735 free(grab);
2736 }
2737 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002738}
2739
2740static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002741desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2742 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002743{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002744 struct shell_surface *shsurf =
2745 weston_desktop_surface_get_user_data(desktop_surface);
2746 bool *unresponsive = user_data;
2747
2748 shsurf->unresponsive = *unresponsive;
2749}
2750
2751static void
2752desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2753 void *shell_)
2754{
2755 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002756 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002757 struct weston_seat *seat;
2758 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002759
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002760 weston_desktop_client_for_each_surface(desktop_client,
2761 desktop_surface_set_unresponsive,
2762 &unresponsive);
2763
2764
2765 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2766 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2767 struct weston_desktop_client *grab_client;
2768
2769 if (!pointer || !pointer->focus)
2770 continue;
2771
2772 shsurf = get_shell_surface(pointer->focus->surface);
2773 if (!shsurf)
2774 continue;
2775
2776 grab_client =
2777 weston_desktop_surface_get_client(shsurf->desktop_surface);
2778 if (grab_client == desktop_client)
2779 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002780 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002781}
2782
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002783static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784desktop_surface_pong(struct weston_desktop_client *desktop_client,
2785 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002786{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002787 struct desktop_shell *shell = shell_;
2788 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002789
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002790 weston_desktop_client_for_each_surface(desktop_client,
2791 desktop_surface_set_unresponsive,
2792 &unresponsive);
2793 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002794}
2795
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002796static const struct weston_desktop_api shell_desktop_api = {
2797 .struct_size = sizeof(struct weston_desktop_api),
2798 .surface_added = desktop_surface_added,
2799 .surface_removed = desktop_surface_removed,
2800 .committed = desktop_surface_committed,
2801 .move = desktop_surface_move,
2802 .resize = desktop_surface_resize,
2803 .fullscreen_requested = desktop_surface_fullscreen_requested,
2804 .maximized_requested = desktop_surface_maximized_requested,
2805 .minimized_requested = desktop_surface_minimized_requested,
2806 .ping_timeout = desktop_surface_ping_timeout,
2807 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002808};
2809
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810/* ************************ *
2811 * end of libweston-desktop *
2812 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002813static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002814shell_fade(struct desktop_shell *shell, enum fade_type type);
2815
2816static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002817configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002818{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002820
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002821 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 if (v->output == ev->output && v != ev) {
2823 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002824 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002825 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002826 }
2827 }
2828
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002829 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002830 ev->surface->is_mapped = true;
2831 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002832
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002833 if (wl_list_empty(&ev->layer_link.link)) {
2834 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002836 }
2837}
2838
David Fort50d962f2016-06-09 17:55:52 +02002839
2840static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002841find_shell_output_from_weston_output(struct desktop_shell *shell,
2842 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002843{
2844 struct shell_output *shell_output;
2845
2846 wl_list_for_each(shell_output, &shell->output_list, link) {
2847 if (shell_output->output == output)
2848 return shell_output;
2849 }
2850
2851 return NULL;
2852}
2853
Pekka Paalanen8274d902014-08-06 19:36:51 +03002854static int
2855background_get_label(struct weston_surface *surface, char *buf, size_t len)
2856{
2857 return snprintf(buf, len, "background for output %s",
2858 surface->output->name);
2859}
2860
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002861static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002862background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002863{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002864 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002865 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002866
Jason Ekstranda7af7042013-10-12 22:38:11 -05002867 view = container_of(es->views.next, struct weston_view, surface_link);
2868
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002869 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002870}
2871
2872static void
David Fort50d962f2016-06-09 17:55:52 +02002873handle_background_surface_destroy(struct wl_listener *listener, void *data)
2874{
2875 struct shell_output *output =
2876 container_of(listener, struct shell_output, background_surface_listener);
2877
2878 weston_log("background surface gone\n");
2879 output->background_surface = NULL;
2880}
2881
2882static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002883desktop_shell_set_background(struct wl_client *client,
2884 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002885 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002886 struct wl_resource *surface_resource)
2887{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002888 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002889 struct weston_surface *surface =
2890 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002891 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002893
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002894 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002895 wl_resource_post_error(surface_resource,
2896 WL_DISPLAY_ERROR_INVALID_OBJECT,
2897 "surface role already assigned");
2898 return;
2899 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002900
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2902 weston_view_destroy(view);
2903 view = weston_view_create(surface);
2904
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002905 surface->committed = background_committed;
2906 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002907 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002908 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002909 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002910 weston_desktop_shell_send_configure(resource, 0,
2911 surface_resource,
2912 surface->output->width,
2913 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002914
2915 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2916 sh_output->background_surface = surface;
2917
2918 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2919 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002920}
2921
Pekka Paalanen8274d902014-08-06 19:36:51 +03002922static int
2923panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2924{
2925 return snprintf(buf, len, "panel for output %s",
2926 surface->output->name);
2927}
2928
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002930panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002931{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002932 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002934
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 view = container_of(es->views.next, struct weston_view, surface_link);
2936
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002937 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002938}
2939
2940static void
David Fort50d962f2016-06-09 17:55:52 +02002941handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2942{
2943 struct shell_output *output =
2944 container_of(listener, struct shell_output, panel_surface_listener);
2945
2946 weston_log("panel surface gone\n");
2947 output->panel_surface = NULL;
2948}
2949
2950
2951static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002952desktop_shell_set_panel(struct wl_client *client,
2953 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002954 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002955 struct wl_resource *surface_resource)
2956{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002957 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002958 struct weston_surface *surface =
2959 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002961 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002962
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002963 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002964 wl_resource_post_error(surface_resource,
2965 WL_DISPLAY_ERROR_INVALID_OBJECT,
2966 "surface role already assigned");
2967 return;
2968 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002969
Jason Ekstranda7af7042013-10-12 22:38:11 -05002970 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2971 weston_view_destroy(view);
2972 view = weston_view_create(surface);
2973
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002974 surface->committed = panel_committed;
2975 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002976 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002977 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002979 weston_desktop_shell_send_configure(resource, 0,
2980 surface_resource,
2981 surface->output->width,
2982 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002983
2984 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2985 sh_output->panel_surface = surface;
2986
2987 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2988 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002989}
2990
Pekka Paalanen8274d902014-08-06 19:36:51 +03002991static int
2992lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2993{
2994 return snprintf(buf, len, "lock window");
2995}
2996
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002997static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002998lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002999{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003000 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 struct weston_view *view;
3002
3003 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003004
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003005 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003006 return;
3007
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003009
3010 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003011 weston_layer_entry_insert(&shell->lock_layer.view_list,
3012 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003013 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003014 surface->is_mapped = true;
3015 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003016 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003017 }
3018}
3019
3020static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003021handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003022{
Tiago Vignattibe143262012-04-16 17:31:41 +03003023 struct desktop_shell *shell =
3024 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003025
Martin Minarik6d118362012-06-07 18:01:59 +02003026 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003027 shell->lock_surface = NULL;
3028}
3029
3030static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003031desktop_shell_set_lock_surface(struct wl_client *client,
3032 struct wl_resource *resource,
3033 struct wl_resource *surface_resource)
3034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003035 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003036 struct weston_surface *surface =
3037 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003038
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003039 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003040
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003041 if (!shell->locked)
3042 return;
3043
Pekka Paalanen98262232011-12-01 10:42:22 +02003044 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003045
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003046 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003047 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003048 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003049
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003050 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003051 surface->committed = lock_surface_committed;
3052 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003053 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003054}
3055
3056static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003057resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003058{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003059 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003060
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003061 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003062 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003063 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003064 &shell->input_panel_layer.link);
3065 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003066 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003067 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003068 wl_list_insert(&shell->compositor->cursor_layer.link,
3069 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003070 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003071 wl_list_insert(&shell->fullscreen_layer.link,
3072 &shell->panel_layer.link);
3073 wl_list_insert(&shell->panel_layer.link,
3074 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003075
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003076 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003077
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003078 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003079 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003080 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003081}
3082
3083static void
3084desktop_shell_unlock(struct wl_client *client,
3085 struct wl_resource *resource)
3086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003088
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003089 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003090
3091 if (shell->locked)
3092 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003093}
3094
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003095static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003096desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003097 struct wl_resource *resource,
3098 struct wl_resource *surface_resource)
3099{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003100 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003101
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003102 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003103 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003104}
3105
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003106static void
3107desktop_shell_desktop_ready(struct wl_client *client,
3108 struct wl_resource *resource)
3109{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003110 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003111
3112 shell_fade_startup(shell);
3113}
3114
Jonny Lamb765760d2014-08-20 15:53:19 +02003115static void
3116desktop_shell_set_panel_position(struct wl_client *client,
3117 struct wl_resource *resource,
3118 uint32_t position)
3119{
3120 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3121
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003122 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3123 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3124 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3125 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003126 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003127 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003128 "bad position argument");
3129 return;
3130 }
3131
3132 shell->panel_position = position;
3133}
3134
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003135static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003136 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003137 desktop_shell_set_panel,
3138 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003139 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003140 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003141 desktop_shell_desktop_ready,
3142 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003143};
3144
3145static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003146move_binding(struct weston_pointer *pointer, uint32_t time,
3147 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003148{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003149 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003150 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003151 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003152
Derek Foreman8ae2db52015-07-15 13:00:36 -05003153 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003154 return;
3155
Derek Foreman8ae2db52015-07-15 13:00:36 -05003156 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003157
Pekka Paalanen01388e22013-04-25 13:57:44 +03003158 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003159 if (surface == NULL)
3160 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003161
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003162 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003163 if (shsurf == NULL ||
3164 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3165 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003166 return;
3167
Derek Foreman794fa0e2015-07-15 13:00:38 -05003168 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003169}
3170
3171static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003172maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3173 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003174{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003175 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003176 struct weston_surface *surface;
3177 struct shell_surface *shsurf;
3178
3179 surface = weston_surface_get_main_surface(focus);
3180 if (surface == NULL)
3181 return;
3182
3183 shsurf = get_shell_surface(surface);
3184 if (shsurf == NULL)
3185 return;
3186
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003187 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003188}
3189
3190static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003191fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3192 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003193{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003194 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003195 struct weston_surface *surface;
3196 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003197 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003198
3199 surface = weston_surface_get_main_surface(focus);
3200 if (surface == NULL)
3201 return;
3202
3203 shsurf = get_shell_surface(surface);
3204 if (shsurf == NULL)
3205 return;
3206
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003207 fullscreen =
3208 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003209
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003210 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003211}
3212
3213static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003214touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003215{
Derek Foreman362656b2014-09-04 10:23:05 -05003216 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003217 struct weston_surface *surface;
3218 struct shell_surface *shsurf;
3219
Derek Foreman8ae2db52015-07-15 13:00:36 -05003220 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003221 return;
3222
Derek Foreman8ae2db52015-07-15 13:00:36 -05003223 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003224 surface = weston_surface_get_main_surface(focus);
3225 if (surface == NULL)
3226 return;
3227
3228 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003229 if (shsurf == NULL ||
3230 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3231 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003232 return;
3233
Derek Foremanb7674ae2015-07-15 13:00:37 -05003234 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003235}
3236
3237static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003238resize_binding(struct weston_pointer *pointer, uint32_t time,
3239 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003240{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003241 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003242 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003243 uint32_t edges = 0;
3244 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003245 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003246
Derek Foreman8ae2db52015-07-15 13:00:36 -05003247 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003248 return;
3249
Derek Foreman8ae2db52015-07-15 13:00:36 -05003250 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003251
Pekka Paalanen01388e22013-04-25 13:57:44 +03003252 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003253 if (surface == NULL)
3254 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003255
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003257 if (shsurf == NULL ||
3258 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3259 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003260 return;
3261
Jason Ekstranda7af7042013-10-12 22:38:11 -05003262 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003263 wl_fixed_to_int(pointer->grab_x),
3264 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003265 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003266
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003267 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003268 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003269 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003270 edges |= 0;
3271 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003272 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003273
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003274 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003275 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003276 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003277 edges |= 0;
3278 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003279 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003280
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003281 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003282}
3283
3284static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003285surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003286 struct weston_pointer_axis_event *event,
3287 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003288{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003289 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003290 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003291 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003292 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003293
Pekka Paalanen01388e22013-04-25 13:57:44 +03003294 /* XXX: broken for windows containing sub-surfaces */
3295 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003296 if (surface == NULL)
3297 return;
3298
3299 shsurf = get_shell_surface(surface);
3300 if (!shsurf)
3301 return;
3302
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003303 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003304
Jason Ekstranda7af7042013-10-12 22:38:11 -05003305 if (shsurf->view->alpha > 1.0)
3306 shsurf->view->alpha = 1.0;
3307 if (shsurf->view->alpha < step)
3308 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003311 weston_surface_damage(surface);
3312}
3313
3314static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003315do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003316 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003317{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003318 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003319 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003321 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003322
Derek Foreman1281a362015-07-31 16:55:32 -05003323 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003324 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3325 return;
3326 }
3327
Scott Moreauccbf29d2012-02-22 14:21:41 -07003328 wl_list_for_each(output, &compositor->output_list, link) {
3329 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003330 wl_fixed_to_double(pointer->x),
3331 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003332 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003333 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003334 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003335 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003336 increment = -output->zoom.increment;
3337 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003338 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003339 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003340 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003341 else
3342 increment = 0;
3343
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003344 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003345
Scott Moreaue6603982012-06-11 13:07:51 -06003346 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003347 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003348 else if (output->zoom.level > output->zoom.max_level)
3349 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003350
3351 if (!output->zoom.active) {
3352 if (output->zoom.level <= 0.0)
3353 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003354 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003355 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003356
Scott Moreaue6603982012-06-11 13:07:51 -06003357 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003358
Jason Ekstranda7af7042013-10-12 22:38:11 -05003359 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003360 }
3361 }
3362}
3363
Scott Moreauccbf29d2012-02-22 14:21:41 -07003364static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003365zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003366 struct weston_pointer_axis_event *event,
3367 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003368{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003369 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003370}
3371
3372static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003373zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3374 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003375{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003376 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003377}
3378
3379static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003380terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3381 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003382{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003383 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003384
Daniel Stone325fc2d2012-05-30 16:31:58 +01003385 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003386}
3387
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003388static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003389rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003390 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003391{
3392 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003393 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003394 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003395 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003396 struct weston_surface *surface =
3397 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003398 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003399
Jonas Ådahld2510102014-10-05 21:39:14 +02003400 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003401
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003402 if (!shsurf)
3403 return;
3404
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003405 cx = 0.5f * surface->width;
3406 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003407
Daniel Stone37816df2012-05-16 18:45:18 +01003408 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3409 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003410 r = sqrtf(dx * dx + dy * dy);
3411
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003412 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003413 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003414
3415 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003416 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003417 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003418
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003419 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003420 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003421
3422 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003423 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003424 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003425 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003426 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003427
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003428 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003429 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003430 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003431 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003432 wl_list_init(&shsurf->rotation.transform.link);
3433 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003434 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003435 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003436
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003437 /* We need to adjust the position of the surface
3438 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003439 cposx = shsurf->view->geometry.x + cx;
3440 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003441 dposx = rotate->center.x - cposx;
3442 dposy = rotate->center.y - cposy;
3443 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003444 weston_view_set_position(shsurf->view,
3445 shsurf->view->geometry.x + dposx,
3446 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003447 }
3448
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003449 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003450 * lazily applies the damage due to rotation update.
3451 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003452 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003453}
3454
3455static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003456rotate_grab_button(struct weston_pointer_grab *grab,
3457 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003458{
3459 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003460 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003461 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003462 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003463 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003464
Daniel Stone4dbadb12012-05-30 16:31:51 +01003465 if (pointer->button_count == 0 &&
3466 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003467 if (shsurf)
3468 weston_matrix_multiply(&shsurf->rotation.rotation,
3469 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003470 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003471 free(rotate);
3472 }
3473}
3474
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003475static void
3476rotate_grab_cancel(struct weston_pointer_grab *grab)
3477{
3478 struct rotate_grab *rotate =
3479 container_of(grab, struct rotate_grab, base.grab);
3480
3481 shell_grab_end(&rotate->base);
3482 free(rotate);
3483}
3484
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003485static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003486 noop_grab_focus,
3487 rotate_grab_motion,
3488 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003489 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003490 noop_grab_axis_source,
3491 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003492 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003493};
3494
3495static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003496surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003497{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003498 struct weston_surface *surface =
3499 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003500 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003501 float dx, dy;
3502 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003503
Pekka Paalanen460099f2012-01-20 16:48:25 +02003504 rotate = malloc(sizeof *rotate);
3505 if (!rotate)
3506 return;
3507
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003508 weston_view_to_global_float(shsurf->view,
3509 surface->width * 0.5f,
3510 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003511 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003512
Derek Foreman74de4692015-07-15 13:00:39 -05003513 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3514 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003515 r = sqrtf(dx * dx + dy * dy);
3516 if (r > 20.0f) {
3517 struct weston_matrix inverse;
3518
3519 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003520 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003521 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003522
3523 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003524 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003525 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003526 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003527 weston_matrix_init(&rotate->rotation);
3528 }
3529
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003530 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003531 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003532}
3533
3534static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003535rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003536 void *data)
3537{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003538 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003539 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003540 struct shell_surface *surface;
3541
Derek Foreman8ae2db52015-07-15 13:00:36 -05003542 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003543 return;
3544
Derek Foreman8ae2db52015-07-15 13:00:36 -05003545 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003546
Pekka Paalanen01388e22013-04-25 13:57:44 +03003547 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003548 if (base_surface == NULL)
3549 return;
3550
3551 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003552 if (surface == NULL ||
3553 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3554 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003555 return;
3556
Derek Foreman74de4692015-07-15 13:00:39 -05003557 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003558}
3559
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003560/* Move all fullscreen layers down to the current workspace and hide their
3561 * black views. The surfaces' state is set to both fullscreen and lowered,
3562 * and this is reversed when such a surface is re-configured, see
3563 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3564 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003565 * lowering_output = NULL - Lower on all outputs, else only lower on the
3566 * specified output.
3567 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003568 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003569 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003570void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003571lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003572 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003573{
3574 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003575 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003576
3577 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003579 &shell->fullscreen_layer.view_list.link,
3580 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003581 struct shell_surface *shsurf = get_shell_surface(view->surface);
3582
3583 if (!shsurf)
3584 continue;
3585
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003586 /* Only lower surfaces which have lowering_output as their fullscreen
3587 * output, unless a NULL output asks for lowering on all outputs.
3588 */
3589 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3590 continue;
3591
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003592 /* We can have a non-fullscreen popup for a fullscreen surface
3593 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003594 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003595 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003596 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3597 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003598 weston_view_damage_below(shsurf->fullscreen.black_view);
3599
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003600 }
3601
3602 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003603 weston_layer_entry_remove(&view->layer_link);
3604 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003605 weston_view_damage_below(view);
3606 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003607
3608 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003609 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003610}
3611
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003612void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003613activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003614 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003615{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003616 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003617 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003618 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003619 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003620 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003621 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003622
Pekka Paalanen01388e22013-04-25 13:57:44 +03003623 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003624 shsurf = get_shell_surface(main_surface);
3625 assert(shsurf);
3626
3627 /* Only demote fullscreen surfaces on the output of activated shsurf.
3628 * Leave fullscreen surfaces on unrelated outputs alone. */
3629 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003630
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003631 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003632
Jonas Ådahl8538b222012-08-29 22:13:03 +02003633 state = ensure_focus_state(shell, seat);
3634 if (state == NULL)
3635 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003636
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003637 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003638 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003639
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003640 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3641 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003642 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003643
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003644 /* Update the surface’s layer. This brings it to the top of the stacking
3645 * order as appropriate. */
3646 shell_surface_update_layer(shsurf);
3647
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003648 if (shell->focus_animation_type != ANIMATION_NONE) {
3649 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003650 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003651 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003652}
3653
Alex Wu21858432012-04-01 20:13:08 +08003654/* no-op func for checking black surface */
3655static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003656black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003657{
3658}
3659
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003660static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003661is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003662{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003663 struct weston_surface *surface = view->surface;
3664
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003665 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003666 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003667 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003668 return true;
3669 }
3670 return false;
3671}
3672
Kristian Høgsberg75840622011-09-06 13:48:16 -04003673static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003674activate_binding(struct weston_seat *seat,
3675 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003676 struct weston_view *focus_view,
3677 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003678{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003679 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003680 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003681
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003682 if (!focus_view)
3683 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003684
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003685 if (is_black_surface_view(focus_view, &main_view))
3686 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003687
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003688 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003689 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003690 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003691
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003692 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003693}
3694
3695static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003696click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3697 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003698{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003699 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003700 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003701 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003702 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003703
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003704 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003705 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003706 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003707}
3708
3709static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003710touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3711 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003712{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003713 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003714 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003715 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003716 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003717
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003718 activate_binding(touch->seat, data, touch->focus,
3719 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003720}
3721
3722static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003723unfocus_all_seats(struct desktop_shell *shell)
3724{
3725 struct weston_seat *seat, *next;
3726
3727 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003728 struct weston_keyboard *keyboard =
3729 weston_seat_get_keyboard(seat);
3730
3731 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003732 continue;
3733
Derek Foreman1281a362015-07-31 16:55:32 -05003734 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003735 }
3736}
3737
3738static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003739lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003740{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003741 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003742
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003743 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003744 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003745 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003746 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003747
3748 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003749
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003750 /* Hide all surfaces by removing the fullscreen, panel and
3751 * toplevel layers. This way nothing else can show or receive
3752 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003753
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003754 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003755 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003756 if (shell->showing_input_panels)
3757 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003758 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003759 wl_list_insert(&shell->compositor->cursor_layer.link,
3760 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003761
Derek Foreman004b4a12015-07-20 16:28:13 -05003762 weston_compositor_sleep(shell->compositor);
3763
Neil Robertsb4a91702014-04-09 16:33:32 +01003764 /* Remove the keyboard focus on all seats. This will be
3765 * restored to the workspace's saved state via
3766 * restore_focus_state when the compositor is unlocked */
3767 unfocus_all_seats(shell);
3768
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003769 /* TODO: disable bindings that should not work while locked. */
3770
3771 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003772}
3773
3774static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003775unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003776{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003777 struct wl_resource *shell_resource;
3778
Pekka Paalanend81c2162011-11-16 13:47:34 +02003779 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003780 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003781 return;
3782 }
3783
3784 /* If desktop-shell client has gone away, unlock immediately. */
3785 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003786 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003787 return;
3788 }
3789
3790 if (shell->prepare_event_sent)
3791 return;
3792
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003793 shell_resource = shell->child.desktop_shell;
3794 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003795 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003796}
3797
3798static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003799shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003800{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003801 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003802
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003803 shell->fade.animation = NULL;
3804
3805 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003806 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003807 weston_surface_destroy(shell->fade.view->surface);
3808 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003809 break;
3810 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003811 lock(shell);
3812 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003813 default:
3814 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003815 }
3816}
3817
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003819shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003820{
3821 struct weston_compositor *compositor = shell->compositor;
3822 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003823 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003824
3825 surface = weston_surface_create(compositor);
3826 if (!surface)
3827 return NULL;
3828
Jason Ekstranda7af7042013-10-12 22:38:11 -05003829 view = weston_view_create(surface);
3830 if (!view) {
3831 weston_surface_destroy(surface);
3832 return NULL;
3833 }
3834
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003835 weston_surface_set_size(surface, 8192, 8192);
3836 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003837 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003838 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3839 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003840 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003841 surface->is_mapped = true;
3842 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003843
Jason Ekstranda7af7042013-10-12 22:38:11 -05003844 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003845}
3846
3847static void
3848shell_fade(struct desktop_shell *shell, enum fade_type type)
3849{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003850 float tint;
3851
3852 switch (type) {
3853 case FADE_IN:
3854 tint = 0.0;
3855 break;
3856 case FADE_OUT:
3857 tint = 1.0;
3858 break;
3859 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003860 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003861 return;
3862 }
3863
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003864 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003865
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003866 if (shell->fade.view == NULL) {
3867 shell->fade.view = shell_fade_create_surface(shell);
3868 if (!shell->fade.view)
3869 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003870
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003871 shell->fade.view->alpha = 1.0 - tint;
3872 weston_view_update_transform(shell->fade.view);
3873 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003874
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003875 if (shell->fade.view->output == NULL) {
3876 /* If the black view gets a NULL output, we lost the
3877 * last output and we'll just cancel the fade. This
3878 * happens when you close the last window under the
3879 * X11 or Wayland backends. */
3880 shell->locked = false;
3881 weston_surface_destroy(shell->fade.view->surface);
3882 shell->fade.view = NULL;
3883 } else if (shell->fade.animation) {
3884 weston_fade_update(shell->fade.animation, tint);
3885 } else {
3886 shell->fade.animation =
3887 weston_fade_run(shell->fade.view,
3888 1.0 - tint, tint, 300.0,
3889 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003890 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003891}
3892
3893static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003894do_shell_fade_startup(void *data)
3895{
3896 struct desktop_shell *shell = data;
3897
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003898 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003899 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003900 } else {
3901 weston_log("desktop shell: "
3902 "unexpected fade-in animation type %d\n",
3903 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003904 weston_surface_destroy(shell->fade.view->surface);
3905 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003906 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003907}
3908
3909static void
3910shell_fade_startup(struct desktop_shell *shell)
3911{
3912 struct wl_event_loop *loop;
3913
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003914 if (!shell->fade.startup_timer)
3915 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003916
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003917 wl_event_source_remove(shell->fade.startup_timer);
3918 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003919
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003920 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3921 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003922}
3923
3924static int
3925fade_startup_timeout(void *data)
3926{
3927 struct desktop_shell *shell = data;
3928
3929 shell_fade_startup(shell);
3930 return 0;
3931}
3932
3933static void
3934shell_fade_init(struct desktop_shell *shell)
3935{
3936 /* Make compositor output all black, and wait for the desktop-shell
3937 * client to signal it is ready, then fade in. The timer triggers a
3938 * fade-in, in case the desktop-shell client takes too long.
3939 */
3940
3941 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003942
3943 if (shell->fade.view != NULL) {
3944 weston_log("%s: warning: fade surface already exists\n",
3945 __func__);
3946 return;
3947 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003948
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003949 if (shell->startup_animation_type == ANIMATION_NONE)
3950 return;
3951
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003952 shell->fade.view = shell_fade_create_surface(shell);
3953 if (!shell->fade.view)
3954 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003955
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003956 weston_view_update_transform(shell->fade.view);
3957 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003958
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003959 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3960 shell->fade.startup_timer =
3961 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3962 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003963}
3964
3965static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003966idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003967{
3968 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003969 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003970
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003971 struct weston_seat *seat;
3972
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003973 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3974 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003975
3976 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003977 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003978}
3979
3980static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003981wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982{
3983 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003984 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003985
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003986 unlock(shell);
3987}
3988
3989static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003990transform_handler(struct wl_listener *listener, void *data)
3991{
3992 struct weston_surface *surface = data;
3993 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003994 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003995 int x, y;
3996
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003997 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003998 return;
3999
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004000 api = shsurf->shell->xwayland_surface_api;
4001 if (!api) {
4002 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4003 shsurf->shell->xwayland_surface_api = api;
4004 }
4005
4006 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004007 return;
4008
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004009 if (!weston_view_is_mapped(shsurf->view))
4010 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004011
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004012 x = shsurf->view->geometry.x;
4013 y = shsurf->view->geometry.y;
4014
4015 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004016}
4017
4018static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004019center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004020{
Giulio Camuffob8366642013-04-25 13:57:46 +03004021 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004022 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004023
Jason Ekstranda7af7042013-10-12 22:38:11 -05004024 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004025
4026 x = output->x + (output->width - width) / 2 - surf_x / 2;
4027 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004028
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004029 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004030}
4031
4032static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033weston_view_set_initial_position(struct weston_view *view,
4034 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004035{
4036 struct weston_compositor *compositor = shell->compositor;
4037 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004038 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004039 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004040 struct weston_output *output, *target_output = NULL;
4041 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004042 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004043
4044 /* As a heuristic place the new window on the same output as the
4045 * pointer. Falling back to the output containing 0, 0.
4046 *
4047 * TODO: Do something clever for touch too?
4048 */
4049 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004050 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4051
4052 if (pointer) {
4053 ix = wl_fixed_to_int(pointer->x);
4054 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004055 break;
4056 }
4057 }
4058
4059 wl_list_for_each(output, &compositor->output_list, link) {
4060 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4061 target_output = output;
4062 break;
4063 }
4064 }
4065
4066 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004067 weston_view_set_position(view, 10 + random() % 400,
4068 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004069 return;
4070 }
4071
4072 /* Valid range within output where the surface will still be onscreen.
4073 * If this is negative it means that the surface is bigger than
4074 * output.
4075 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004076 get_output_work_area(shell, target_output, &area);
4077
Derek Foremanf814c5d2015-04-15 12:23:54 -05004078 x = area.x;
4079 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004080 range_x = area.width - view->surface->width;
4081 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004082
Rob Bradford4cb88c72012-08-13 15:18:44 +01004083 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004084 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004085
4086 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004087 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004088
Jason Ekstranda7af7042013-10-12 22:38:11 -05004089 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004090}
4091
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004092static bool
4093check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4094{
4095 struct timespec now;
4096
4097 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4098 return false;
4099
4100 /*
4101 * If the shell helper client dies before the session has been
4102 * up for roughly 30 seconds, better just make Weston shut down,
4103 * because the user likely has no way to interact with the desktop
4104 * anyway.
4105 */
4106 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4107 weston_log("Error: %s apparently cannot run at all.\n",
4108 shell->client);
4109 weston_log_continue(STAMP_SPACE "Quitting...");
4110 wl_display_terminate(shell->compositor->wl_display);
4111
4112 return true;
4113 }
4114
4115 return false;
4116}
4117
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004118static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004119
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004120static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004121respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004122{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004123 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004124
4125 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4126 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004127 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004128 shell->child.deathstamp = time;
4129 shell->child.deathcount = 0;
4130 }
4131
4132 shell->child.deathcount++;
4133 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004134 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004135 return;
4136 }
4137
Pekka Paalanen826dc142014-08-27 12:11:53 +03004138 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004139 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004140}
4141
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004142static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004143desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4144{
4145 struct desktop_shell *shell;
4146
4147 shell = container_of(listener, struct desktop_shell,
4148 child.client_destroy_listener);
4149
Pekka Paalanen826dc142014-08-27 12:11:53 +03004150 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004151 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004152 /*
4153 * unbind_desktop_shell() will reset shell->child.desktop_shell
4154 * before the respawned process has a chance to create a new
4155 * desktop_shell object, because we are being called from the
4156 * wl_client destructor which destroys all wl_resources before
4157 * returning.
4158 */
4159
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004160 if (!check_desktop_shell_crash_too_early(shell))
4161 respawn_desktop_shell_process(shell);
4162
Pekka Paalanen826dc142014-08-27 12:11:53 +03004163 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004164}
4165
4166static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004167launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004168{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004169 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004170
Pekka Paalanen826dc142014-08-27 12:11:53 +03004171 shell->child.client = weston_client_start(shell->compositor,
4172 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004173
Arnaud Vrac42631192014-08-25 20:56:46 +02004174 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004175 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004176 return;
4177 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004178
4179 shell->child.client_destroy_listener.notify =
4180 desktop_shell_client_destroy;
4181 wl_client_add_destroy_listener(shell->child.client,
4182 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004183}
4184
4185static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004186unbind_desktop_shell(struct wl_resource *resource)
4187{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004188 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004189
4190 if (shell->locked)
4191 resume_desktop(shell);
4192
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004193 shell->child.desktop_shell = NULL;
4194 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004195}
4196
4197static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004198bind_desktop_shell(struct wl_client *client,
4199 void *data, uint32_t version, uint32_t id)
4200{
Tiago Vignattibe143262012-04-16 17:31:41 +03004201 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004202 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004203
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004204 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4205 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004206
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004207 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004208 wl_resource_set_implementation(resource,
4209 &desktop_shell_implementation,
4210 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004211 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004212 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004213 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004214
4215 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4216 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004217}
4218
Kristian Høgsberg07045392012-02-19 18:52:44 -05004219struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004220 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004221 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004222 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004223 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004224 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004225};
4226
4227static void
4228switcher_next(struct switcher *switcher)
4229{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004231 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004232 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004233 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004234
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004235 /* temporary re-display minimized surfaces */
4236 struct weston_view *tmp;
4237 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004238 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4239 weston_layer_entry_remove(&view->layer_link);
4240 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004241 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4242 *minimized = view;
4243 }
4244
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004245 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004246 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004247 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004248 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004249 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004250 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004251 next = view;
4252 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004253 view->alpha = 0.25;
4254 weston_view_geometry_dirty(view);
4255 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004256 }
Alex Wu1659daa2012-04-01 20:13:09 +08004257
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004258 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004259 view->alpha = 0.25;
4260 weston_view_geometry_dirty(view);
4261 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004262 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004263 }
4264
4265 if (next == NULL)
4266 next = first;
4267
Alex Wu07b26062012-03-12 16:06:01 +08004268 if (next == NULL)
4269 return;
4270
Kristian Høgsberg07045392012-02-19 18:52:44 -05004271 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004272 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004273
4274 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004275 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004276 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004277
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004278 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004279 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004281}
4282
4283static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004284switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004285{
4286 struct switcher *switcher =
4287 container_of(listener, struct switcher, listener);
4288
4289 switcher_next(switcher);
4290}
4291
4292static void
Daniel Stone351eb612012-05-31 15:27:47 -04004293switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004296 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004297 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004298
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004299 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004300 if (is_focus_view(view))
4301 continue;
4302
Jason Ekstranda7af7042013-10-12 22:38:11 -05004303 view->alpha = 1.0;
4304 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004305 }
4306
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004307 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004308 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004309 keyboard->seat,
4310 WESTON_ACTIVATE_FLAG_CONFIGURE);
4311 }
4312
Kristian Høgsberg07045392012-02-19 18:52:44 -05004313 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004314 weston_keyboard_end_grab(keyboard);
4315 if (keyboard->input_method_resource)
4316 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004317
4318 /* re-hide surfaces that were temporary shown during the switch */
4319 struct weston_view **minimized;
4320 wl_array_for_each(minimized, &switcher->minimized_array) {
4321 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004322 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004323 weston_layer_entry_remove(&(*minimized)->layer_link);
4324 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004325 weston_view_damage_below(*minimized);
4326 }
4327 }
4328 wl_array_release(&switcher->minimized_array);
4329
Kristian Høgsberg07045392012-02-19 18:52:44 -05004330 free(switcher);
4331}
4332
4333static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004334switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004335 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004336{
4337 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004338 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004339
Daniel Stonec9785ea2012-05-30 16:31:52 +01004340 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004341 switcher_next(switcher);
4342}
4343
4344static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004345switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004346 uint32_t mods_depressed, uint32_t mods_latched,
4347 uint32_t mods_locked, uint32_t group)
4348{
4349 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004350 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004351
Daniel Stone351eb612012-05-31 15:27:47 -04004352 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4353 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004354}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004355
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004356static void
4357switcher_cancel(struct weston_keyboard_grab *grab)
4358{
4359 struct switcher *switcher = container_of(grab, struct switcher, grab);
4360
4361 switcher_destroy(switcher);
4362}
4363
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004364static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004365 switcher_key,
4366 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004367 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004368};
4369
4370static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004371switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4372 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373{
Tiago Vignattibe143262012-04-16 17:31:41 +03004374 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004375 struct switcher *switcher;
4376
4377 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004378 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004380 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004381 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004382 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004384 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004385 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004386 weston_keyboard_start_grab(keyboard, &switcher->grab);
4387 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004388 switcher_next(switcher);
4389}
4390
Pekka Paalanen3c647232011-12-22 13:43:43 +02004391static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004392backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4393 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004394{
4395 struct weston_compositor *compositor = data;
4396 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004397 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004398
4399 /* TODO: we're limiting to simple use cases, where we assume just
4400 * control on the primary display. We'd have to extend later if we
4401 * ever get support for setting backlights on random desktop LCD
4402 * panels though */
4403 output = get_default_output(compositor);
4404 if (!output)
4405 return;
4406
4407 if (!output->set_backlight)
4408 return;
4409
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004410 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4411 backlight_new = output->backlight_current - 25;
4412 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4413 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004414
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004415 if (backlight_new < 5)
4416 backlight_new = 5;
4417 if (backlight_new > 255)
4418 backlight_new = 255;
4419
4420 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004421 output->set_backlight(output, output->backlight_current);
4422}
4423
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004424static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004425force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4426 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004427{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004428 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004429 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004430 struct desktop_shell *shell = data;
4431 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004432 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004433
Derek Foreman8ae2db52015-07-15 13:00:36 -05004434 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004435 if (!focus_surface)
4436 return;
4437
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004438 wl_signal_emit(&compositor->kill_signal, focus_surface);
4439
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004440 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004441 wl_client_get_credentials(client, &pid, NULL, NULL);
4442
4443 /* Skip clients that we launched ourselves (the credentials of
4444 * the socketpair is ours) */
4445 if (pid == getpid())
4446 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004447
Daniel Stone325fc2d2012-05-30 16:31:58 +01004448 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004449}
4450
4451static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004452workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004453 uint32_t key, void *data)
4454{
4455 struct desktop_shell *shell = data;
4456 unsigned int new_index = shell->workspaces.current;
4457
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004458 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004459 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004460 if (new_index != 0)
4461 new_index--;
4462
4463 change_workspace(shell, new_index);
4464}
4465
4466static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004467workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004468 uint32_t key, void *data)
4469{
4470 struct desktop_shell *shell = data;
4471 unsigned int new_index = shell->workspaces.current;
4472
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004473 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004474 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004475 if (new_index < shell->workspaces.num - 1)
4476 new_index++;
4477
4478 change_workspace(shell, new_index);
4479}
4480
4481static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004482workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004483 uint32_t key, void *data)
4484{
4485 struct desktop_shell *shell = data;
4486 unsigned int new_index;
4487
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004488 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004489 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004490 new_index = key - KEY_F1;
4491 if (new_index >= shell->workspaces.num)
4492 new_index = shell->workspaces.num - 1;
4493
4494 change_workspace(shell, new_index);
4495}
4496
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004497static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004498workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4499 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004500{
4501 struct desktop_shell *shell = data;
4502 unsigned int new_index = shell->workspaces.current;
4503
4504 if (shell->locked)
4505 return;
4506
4507 if (new_index != 0)
4508 new_index--;
4509
Derek Foreman8ae2db52015-07-15 13:00:36 -05004510 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004511}
4512
4513static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004514workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4515 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004516{
4517 struct desktop_shell *shell = data;
4518 unsigned int new_index = shell->workspaces.current;
4519
4520 if (shell->locked)
4521 return;
4522
4523 if (new_index < shell->workspaces.num - 1)
4524 new_index++;
4525
Derek Foreman8ae2db52015-07-15 13:00:36 -05004526 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004527}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004528
4529static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004530shell_reposition_view_on_output_destroy(struct weston_view *view)
4531{
4532 struct weston_output *output, *first_output;
4533 struct weston_compositor *ec = view->surface->compositor;
4534 struct shell_surface *shsurf;
4535 float x, y;
4536 int visible;
4537
4538 x = view->geometry.x;
4539 y = view->geometry.y;
4540
4541 /* At this point the destroyed output is not in the list anymore.
4542 * If the view is still visible somewhere, we leave where it is,
4543 * otherwise, move it to the first output. */
4544 visible = 0;
4545 wl_list_for_each(output, &ec->output_list, link) {
4546 if (pixman_region32_contains_point(&output->region,
4547 x, y, NULL)) {
4548 visible = 1;
4549 break;
4550 }
4551 }
4552
4553 if (!visible) {
4554 first_output = container_of(ec->output_list.next,
4555 struct weston_output, link);
4556
4557 x = first_output->x + first_output->width / 4;
4558 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004559
4560 weston_view_set_position(view, x, y);
4561 } else {
4562 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004563 }
4564
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004565
4566 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004567 if (!shsurf)
4568 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004569
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004570 shsurf->saved_position_valid = false;
4571 set_maximized(shsurf, false);
4572 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004573}
4574
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004575void
4576shell_for_each_layer(struct desktop_shell *shell,
4577 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004578{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004579 struct workspace **ws;
4580
4581 func(shell, &shell->fullscreen_layer, data);
4582 func(shell, &shell->panel_layer, data);
4583 func(shell, &shell->background_layer, data);
4584 func(shell, &shell->lock_layer, data);
4585 func(shell, &shell->input_panel_layer, data);
4586
4587 wl_array_for_each(ws, &shell->workspaces.array)
4588 func(shell, &(*ws)->layer, data);
4589}
4590
4591static void
4592shell_output_destroy_move_layer(struct desktop_shell *shell,
4593 struct weston_layer *layer,
4594 void *data)
4595{
4596 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004597 struct weston_view *view;
4598
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004599 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004600 if (view->output != output)
4601 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004602
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004603 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004604 }
4605}
4606
4607static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004608handle_output_destroy(struct wl_listener *listener, void *data)
4609{
4610 struct shell_output *output_listener =
4611 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004612 struct weston_output *output = output_listener->output;
4613 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004614
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004615 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004616
Quentin Glidic561201c2016-08-20 18:19:51 +02004617 wl_list_remove(&output_listener->panel_surface_listener.link);
4618 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004619 wl_list_remove(&output_listener->destroy_listener.link);
4620 wl_list_remove(&output_listener->link);
4621 free(output_listener);
4622}
4623
4624static void
David Fort50d962f2016-06-09 17:55:52 +02004625shell_resize_surface_to_output(struct desktop_shell *shell,
4626 struct weston_surface *surface,
4627 const struct weston_output *output)
4628{
4629 if (!surface)
4630 return;
4631
4632 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4633 surface->resource,
4634 output->width,
4635 output->height);
4636}
4637
4638
4639static void
4640handle_output_resized(struct wl_listener *listener, void *data)
4641{
4642 struct desktop_shell *shell =
4643 container_of(listener, struct desktop_shell, resized_listener);
4644 struct weston_output *output = (struct weston_output *)data;
4645 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4646
4647 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4648 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4649}
4650
4651static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004652create_shell_output(struct desktop_shell *shell,
4653 struct weston_output *output)
4654{
4655 struct shell_output *shell_output;
4656
4657 shell_output = zalloc(sizeof *shell_output);
4658 if (shell_output == NULL)
4659 return;
4660
4661 shell_output->output = output;
4662 shell_output->shell = shell;
4663 shell_output->destroy_listener.notify = handle_output_destroy;
4664 wl_signal_add(&output->destroy_signal,
4665 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004666 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004667}
4668
4669static void
4670handle_output_create(struct wl_listener *listener, void *data)
4671{
4672 struct desktop_shell *shell =
4673 container_of(listener, struct desktop_shell, output_create_listener);
4674 struct weston_output *output = (struct weston_output *)data;
4675
4676 create_shell_output(shell, output);
4677}
4678
4679static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004680handle_output_move_layer(struct desktop_shell *shell,
4681 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004682{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004683 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004684 struct weston_view *view;
4685 float x, y;
4686
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004687 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004688 if (view->output != output)
4689 continue;
4690
4691 x = view->geometry.x + output->move_x;
4692 y = view->geometry.y + output->move_y;
4693 weston_view_set_position(view, x, y);
4694 }
4695}
4696
4697static void
4698handle_output_move(struct wl_listener *listener, void *data)
4699{
4700 struct desktop_shell *shell;
4701
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004702 shell = container_of(listener, struct desktop_shell,
4703 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004704
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004705 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004706}
4707
4708static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004709setup_output_destroy_handler(struct weston_compositor *ec,
4710 struct desktop_shell *shell)
4711{
4712 struct weston_output *output;
4713
4714 wl_list_init(&shell->output_list);
4715 wl_list_for_each(output, &ec->output_list, link)
4716 create_shell_output(shell, output);
4717
4718 shell->output_create_listener.notify = handle_output_create;
4719 wl_signal_add(&ec->output_created_signal,
4720 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004721
4722 shell->output_move_listener.notify = handle_output_move;
4723 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004724}
4725
4726static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004727shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004728{
Tiago Vignattibe143262012-04-16 17:31:41 +03004729 struct desktop_shell *shell =
4730 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004731 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004732 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004733
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004734 /* Force state to unlocked so we don't try to fade */
4735 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004736
4737 if (shell->child.client) {
4738 /* disable respawn */
4739 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004740 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004741 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004742
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004743 wl_list_remove(&shell->idle_listener.link);
4744 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004745 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004746
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004747 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004748 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004749
Xiong Zhang6b481422013-10-23 13:58:32 +08004750 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4751 wl_list_remove(&shell_output->destroy_listener.link);
4752 wl_list_remove(&shell_output->link);
4753 free(shell_output);
4754 }
4755
4756 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004757 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004758 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004759
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004760 wl_array_for_each(ws, &shell->workspaces.array)
4761 workspace_destroy(*ws);
4762 wl_array_release(&shell->workspaces.array);
4763
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004764 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004765 free(shell);
4766}
4767
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004768static void
4769shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4770{
4771 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004772 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004773
Bob Ham744e6532016-01-12 10:21:48 +00004774 if (shell->allow_zap)
4775 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4776 MODIFIER_CTRL | MODIFIER_ALT,
4777 terminate_binding, ec);
4778
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004779 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004780 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4781 click_to_activate_binding,
4782 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004783 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4784 click_to_activate_binding,
4785 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004786 weston_compositor_add_touch_binding(ec, 0,
4787 touch_to_activate_binding,
4788 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004789 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4790 MODIFIER_SUPER | MODIFIER_ALT,
4791 surface_opacity_binding, NULL);
4792 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4793 MODIFIER_SUPER, zoom_axis_binding,
4794 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004795 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4796 backlight_binding, ec);
4797 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4798 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004799
4800 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004801 if (shell->exposay_modifier)
4802 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4803 exposay_binding, shell);
4804
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004805 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004806 if (!mod)
4807 return;
4808
Daniel Stone325fc2d2012-05-30 16:31:58 +01004809 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4810 zoom_key_binding, NULL);
4811 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4812 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004813 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4814 maximize_binding, NULL);
4815 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4816 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004817 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4818 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004819 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004820 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004821 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004822 weston_compositor_add_button_binding(ec, BTN_LEFT,
4823 mod | MODIFIER_SHIFT,
4824 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004825
4826 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004827 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004828 rotate_binding, NULL);
4829
Daniel Stone325fc2d2012-05-30 16:31:58 +01004830 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4831 shell);
4832 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4833 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004834 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4835 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004836 weston_compositor_add_key_binding(ec, KEY_K, mod,
4837 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004838 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4839 workspace_up_binding, shell);
4840 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4841 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004842 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4843 workspace_move_surface_up_binding,
4844 shell);
4845 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4846 workspace_move_surface_down_binding,
4847 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004848
4849 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4850 if (shell->workspaces.num > 1) {
4851 num_workspace_bindings = shell->workspaces.num;
4852 if (num_workspace_bindings > 6)
4853 num_workspace_bindings = 6;
4854 for (i = 0; i < num_workspace_bindings; i++)
4855 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4856 workspace_f_binding,
4857 shell);
4858 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004859
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004860 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004861}
4862
Jason Ekstrand024177c2014-04-21 19:42:58 -05004863static void
4864handle_seat_created(struct wl_listener *listener, void *data)
4865{
4866 struct weston_seat *seat = data;
4867
4868 create_shell_seat(seat);
4869}
4870
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004871WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004872module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004873 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004874{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004875 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004876 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004877 struct workspace **pws;
4878 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004879 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004880
Peter Huttererf3d62272013-08-08 11:57:05 +10004881 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004882 if (shell == NULL)
4883 return -1;
4884
Kristian Høgsberg75840622011-09-06 13:48:16 -04004885 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004886
4887 shell->destroy_listener.notify = shell_destroy;
4888 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004889 shell->idle_listener.notify = idle_handler;
4890 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4891 shell->wake_listener.notify = wake_handler;
4892 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004893 shell->transform_listener.notify = transform_handler;
4894 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004895
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004896 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4897 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004898 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4899 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004900 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004901
4902 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004903 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004904
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004905 if (input_panel_setup(shell) < 0)
4906 return -1;
4907
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004908 shell->text_backend = text_backend_init(ec);
4909 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004910 return -1;
4911
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004912 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004913
Daniel Stonedf8133b2013-11-19 11:37:14 +01004914 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4915 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4916
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004917 for (i = 0; i < shell->workspaces.num; i++) {
4918 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4919 if (pws == NULL)
4920 return -1;
4921
4922 *pws = workspace_create();
4923 if (*pws == NULL)
4924 return -1;
4925 }
4926 activate_workspace(shell, 0);
4927
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004928 weston_layer_init(&shell->minimized_layer, NULL);
4929
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004930 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004931 wl_list_init(&shell->workspaces.animation.link);
4932 shell->workspaces.animation.frame = animate_workspace_change_frame;
4933
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004934 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4935 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004936 return -1;
4937
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004938 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004939 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004940 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004941 return -1;
4942
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004943 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004944
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004945 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004946
Xiong Zhang6b481422013-10-23 13:58:32 +08004947 setup_output_destroy_handler(ec, shell);
4948
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004949 loop = wl_display_get_event_loop(ec->wl_display);
4950 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004951
Jason Ekstrand024177c2014-04-21 19:42:58 -05004952 wl_list_for_each(seat, &ec->seat_list, link)
4953 handle_seat_created(NULL, seat);
4954 shell->seat_create_listener.notify = handle_seat_created;
4955 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004956
David Fort50d962f2016-06-09 17:55:52 +02004957 shell->resized_listener.notify = handle_output_resized;
4958 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4959
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004960 screenshooter_create(ec);
4961
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004962 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004963
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004964 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004965
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004966 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4967
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004968 return 0;
4969}