blob: 832a7b7486935e907879b52a298c6e689fe4559b [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020044#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020054 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040056 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020057 struct weston_surface *keyboard_focus;
58 struct wl_list link;
59 struct wl_listener seat_destroy_listener;
60 struct wl_listener surface_destroy_listener;
61};
62
Philip Withnall648a4dd2013-11-25 18:01:44 +000063/*
64 * Surface stacking and ordering.
65 *
66 * This is handled using several linked lists of surfaces, organised into
67 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
68 * above all of the surfaces in the layer below. The set of layers is static and
69 * in the following order (top-most first):
70 * • Lock layer (only ever displayed on its own)
71 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010072 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Fullscreen layer
74 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000075 * • Workspace layers
76 * • Background layer
77 *
78 * The list of layers may be manipulated to remove whole layers of surfaces from
79 * display. For example, when locking the screen, all layers except the lock
80 * layer are removed.
81 *
82 * A surface’s layer is modified on configuring the surface, in
83 * set_surface_type() (which is only called when the surface’s type change is
84 * _committed_). If a surface’s type changes (e.g. when making a window
85 * fullscreen) its layer changes too.
86 *
87 * In order to allow popup and transient surfaces to be correctly stacked above
88 * their parent surfaces, each surface tracks both its parent surface, and a
89 * linked list of its children. When a surface’s layer is updated, so are the
90 * layers of its children. Note that child surfaces are *not* the same as
91 * subsurfaces — child/parent surfaces are purely for maintaining stacking
92 * order.
93 *
94 * The children_link list of siblings of a surface (i.e. those surfaces which
95 * have the same parent) only contains weston_surfaces which have a
96 * shell_surface. Stacking is not implemented for non-shell_surface
97 * weston_surfaces. This means that the following implication does *not* hold:
98 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
99 */
100
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200101struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500102 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200104 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600106 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700107
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200130
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700131 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200132 bool fullscreen;
133 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100134 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200135 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700136
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200137 struct {
138 bool is_set;
139 int32_t x;
140 int32_t y;
141 } xwayland;
142
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500143 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500144
145 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200146};
147
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300148struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400149 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152};
153
Rusty Lynch1084da52013-08-15 09:10:08 -0700154struct shell_touch_grab {
155 struct weston_touch_grab grab;
156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
158 struct weston_touch *touch;
159};
160
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300161struct weston_move_grab {
162 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100163 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500164 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165};
166
Rusty Lynch1084da52013-08-15 09:10:08 -0700167struct weston_touch_move_grab {
168 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800169 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700170 wl_fixed_t dx, dy;
171};
172
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300174 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500175 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200177 float x;
178 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200179 } center;
180};
181
Giulio Camuffo5085a752013-03-25 21:42:45 +0100182struct shell_seat {
183 struct weston_seat *seat;
184 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500185 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100186
Jason Ekstrand024177c2014-04-21 19:42:58 -0500187 struct wl_listener caps_changed_listener;
188 struct wl_listener pointer_focus_listener;
189 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100190};
191
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800192
Alex Wubd3354b2012-04-17 17:20:49 +0800193static struct desktop_shell *
194shell_surface_get_shell(struct shell_surface *shsurf);
195
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500196static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200197set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
198
199static void
Derek Foreman74de4692015-07-15 13:00:39 -0500200surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500201
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300202static void
203shell_fade_startup(struct desktop_shell *shell);
204
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700205static void
206shell_fade(struct desktop_shell *shell, enum fade_type type);
207
Philip Withnallbecb77e2013-11-25 18:01:30 +0000208static struct shell_seat *
209get_shell_seat(struct weston_seat *seat);
210
Jonny Lambd73c6942014-08-20 15:53:20 +0200211static void
212get_output_panel_size(struct desktop_shell *shell,
213 struct weston_output *output,
214 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700215
Philip Withnall648a4dd2013-11-25 18:01:44 +0000216static void
217shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
218
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219static int
220shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
221{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200223 struct weston_desktop_surface *desktop_surface =
224 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300225
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200226 t = weston_desktop_surface_get_title(desktop_surface);
227 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300228
229 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200230 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300231 t ? " '" : "", t ?: "", t ? "'" : "",
232 c ? " of " : "", c ?: "");
233}
234
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500235static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400236destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237{
238 struct shell_grab *grab;
239
240 grab = container_of(listener, struct shell_grab,
241 shsurf_destroy_listener);
242
243 grab->shsurf = NULL;
244}
245
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800246struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500247get_default_view(struct weston_surface *surface)
248{
249 struct shell_surface *shsurf;
250 struct weston_view *view;
251
252 if (!surface || wl_list_empty(&surface->views))
253 return NULL;
254
255 shsurf = get_shell_surface(surface);
256 if (shsurf)
257 return shsurf->view;
258
259 wl_list_for_each(view, &surface->views, surface_link)
260 if (weston_view_is_mapped(view))
261 return view;
262
263 return container_of(surface->views.next, struct weston_view, surface_link);
264}
265
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400268 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300269 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400270 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800271 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300273 struct desktop_shell *shell = shsurf->shell;
274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200275 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400276
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277 grab->grab.interface = interface;
278 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500280 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400281 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700283 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400284 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400285 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800286 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400287 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 weston_pointer_set_focus(pointer,
289 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400290 wl_fixed_from_int(0),
291 wl_fixed_from_int(0));
292 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293}
294
Jonny Lambd73c6942014-08-20 15:53:20 +0200295static void
Quentin Glidic581df062016-06-23 18:55:19 +0200296get_panel_size(struct desktop_shell *shell,
297 struct weston_view *view,
298 int *width,
299 int *height)
300{
301 float x1, y1;
302 float x2, y2;
303 weston_view_to_global_float(view, 0, 0, &x1, &y1);
304 weston_view_to_global_float(view,
305 view->surface->width,
306 view->surface->height,
307 &x2, &y2);
308 *width = (int)(x2 - x1);
309 *height = (int)(y2 - y1);
310}
311
312static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200313get_output_panel_size(struct desktop_shell *shell,
314 struct weston_output *output,
315 int *width,
316 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700317{
318 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200319
320 *width = 0;
321 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700322
323 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200324 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700325
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300326 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200327 if (view->surface->output == output) {
328 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200329 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700330 }
331 }
332
Jonny Lambd73c6942014-08-20 15:53:20 +0200333 /* the correct view wasn't found */
334}
335
336static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200337get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200338 struct weston_output *output,
339 pixman_rectangle32_t *area)
340{
341 int32_t panel_width = 0, panel_height = 0;
342
Derek Foremanf814c5d2015-04-15 12:23:54 -0500343 area->x = output->x;
344 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200345
346 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200347 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800348 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500350 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000351 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800352 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200353 area->width = output->width;
354 area->height = output->height - panel_height;
355 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000358 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 area->width = output->width - panel_width;
361 area->height = output->height;
362 break;
363 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700364}
365
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400366static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300367shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700369 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400370 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372
373 if (grab->shsurf->resize_edges) {
374 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400375 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300377
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700378 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379}
380
381static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700382shell_touch_grab_start(struct shell_touch_grab *grab,
383 const struct weston_touch_grab_interface *interface,
384 struct shell_surface *shsurf,
385 struct weston_touch *touch)
386{
387 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800388
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200389 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700390
Rusty Lynch1084da52013-08-15 09:10:08 -0700391 grab->grab.interface = interface;
392 grab->shsurf = shsurf;
393 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
394 wl_signal_add(&shsurf->destroy_signal,
395 &grab->shsurf_destroy_listener);
396
397 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_start_grab(touch, &grab->grab);
401 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500402 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700404}
405
406static void
407shell_touch_grab_end(struct shell_touch_grab *grab)
408{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700409 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700410 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 grab->shsurf->grabbed = 0;
412 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700413
414 weston_touch_end_grab(grab->touch);
415}
416
417static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800419 struct weston_output *output);
420
Daniel Stone496ca172012-05-30 16:31:42 +0100421static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300422get_modifier(char *modifier)
423{
424 if (!modifier)
425 return MODIFIER_SUPER;
426
427 if (!strcmp("ctrl", modifier))
428 return MODIFIER_CTRL;
429 else if (!strcmp("alt", modifier))
430 return MODIFIER_ALT;
431 else if (!strcmp("super", modifier))
432 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000433 else if (!strcmp("none", modifier))
434 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300435 else
436 return MODIFIER_SUPER;
437}
438
Juan Zhaoe10d2792012-04-25 19:09:52 +0800439static enum animation_type
440get_animation_type(char *animation)
441{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800442 if (!animation)
443 return ANIMATION_NONE;
444
Juan Zhaoe10d2792012-04-25 19:09:52 +0800445 if (!strcmp("zoom", animation))
446 return ANIMATION_ZOOM;
447 else if (!strcmp("fade", animation))
448 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100449 else if (!strcmp("dim-layer", animation))
450 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800451 else
452 return ANIMATION_NONE;
453}
454
Alex Wu4539b082012-03-01 12:57:46 +0800455static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400456shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200457{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500459 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300460 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000461 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200462
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300463 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400464 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300465 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
466 WESTON_SHELL_CLIENT);
467 if (ret < 0)
468 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500470 "client", &s, client);
471 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100472 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000473
474 weston_config_section_get_bool(section,
475 "allow-zap", &allow_zap, true);
476 shell->allow_zap = allow_zap;
477
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100478 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479 "binding-modifier", &s, "super");
480 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200481 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800482
483 weston_config_section_get_string(section,
484 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000485 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800486 free(s);
487
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400488 weston_config_section_get_string(section, "animation", &s, "none");
489 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200490 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200491 weston_config_section_get_string(section, "close-animation", &s, "fade");
492 shell->win_close_animation_type = get_animation_type(s);
493 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700494 weston_config_section_get_string(section,
495 "startup-animation", &s, "fade");
496 shell->startup_animation_type = get_animation_type(s);
497 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700498 if (shell->startup_animation_type == ANIMATION_ZOOM)
499 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100500 weston_config_section_get_string(section, "focus-animation", &s, "none");
501 shell->focus_animation_type = get_animation_type(s);
502 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400503 weston_config_section_get_uint(section, "num-workspaces",
504 &shell->workspaces.num,
505 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200506}
507
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800508struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100509get_default_output(struct weston_compositor *compositor)
510{
Armin Krezović10b06182016-06-23 11:59:30 +0200511 if (wl_list_empty(&compositor->output_list))
512 return NULL;
513
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514 return container_of(compositor->output_list.next,
515 struct weston_output, link);
516}
517
Pekka Paalanen8274d902014-08-06 19:36:51 +0300518static int
519focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
520{
521 return snprintf(buf, len, "focus highlight effect for output %s",
522 surface->output->name);
523}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100524
525/* no-op func for checking focus surface */
526static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200527focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100528{
529}
530
531static struct focus_surface *
532get_focus_surface(struct weston_surface *surface)
533{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200534 if (surface->committed == focus_surface_committed)
535 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100536 else
537 return NULL;
538}
539
540static bool
541is_focus_surface (struct weston_surface *es)
542{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200543 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544}
545
546static bool
547is_focus_view (struct weston_view *view)
548{
549 return is_focus_surface (view->surface);
550}
551
552static struct focus_surface *
553create_focus_surface(struct weston_compositor *ec,
554 struct weston_output *output)
555{
556 struct focus_surface *fsurf = NULL;
557 struct weston_surface *surface = NULL;
558
559 fsurf = malloc(sizeof *fsurf);
560 if (!fsurf)
561 return NULL;
562
563 fsurf->surface = weston_surface_create(ec);
564 surface = fsurf->surface;
565 if (surface == NULL) {
566 free(fsurf);
567 return NULL;
568 }
569
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100571 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200573 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300574 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100575
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800576 fsurf->view = weston_view_create(surface);
577 if (fsurf->view == NULL) {
578 weston_surface_destroy(surface);
579 free(fsurf);
580 return NULL;
581 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100582 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200583 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600585 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600586 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100587 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
588 pixman_region32_fini(&surface->opaque);
589 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
590 output->width, output->height);
591 pixman_region32_fini(&surface->input);
592 pixman_region32_init(&surface->input);
593
594 wl_list_init(&fsurf->workspace_transform.link);
595
596 return fsurf;
597}
598
599static void
600focus_surface_destroy(struct focus_surface *fsurf)
601{
602 weston_surface_destroy(fsurf->surface);
603 free(fsurf);
604}
605
606static void
607focus_animation_done(struct weston_view_animation *animation, void *data)
608{
609 struct workspace *ws = data;
610
611 ws->focus_animation = NULL;
612}
613
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200615focus_state_destroy(struct focus_state *state)
616{
617 wl_list_remove(&state->seat_destroy_listener.link);
618 wl_list_remove(&state->surface_destroy_listener.link);
619 free(state);
620}
621
622static void
623focus_state_seat_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
627 seat_destroy_listener);
628
629 wl_list_remove(&state->link);
630 focus_state_destroy(state);
631}
632
633static void
634focus_state_surface_destroy(struct wl_listener *listener, void *data)
635{
636 struct focus_state *state = container_of(listener,
637 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400638 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200639 struct weston_surface *main_surface;
640 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
Pekka Paalanen01388e22013-04-25 13:57:44 +0300643 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
644
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300646 wl_list_for_each(view,
647 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100650 if (is_focus_view(view))
651 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200652 if (!get_shell_surface(view->surface))
653 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200655 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 break;
657 }
658
Pekka Paalanen01388e22013-04-25 13:57:44 +0300659 /* if the focus was a sub-surface, activate its main surface */
660 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200661 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300662
Kristian Høgsberge3778222012-07-31 17:29:30 -0400663 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100664 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200665 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200666 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200668 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669 if (state->ws->focus_animation)
670 weston_view_animation_destroy(state->ws->focus_animation);
671
672 state->ws->focus_animation = weston_fade_run(
673 state->ws->fsurf_front->view,
674 state->ws->fsurf_front->view->alpha, 0.0, 300,
675 focus_animation_done, state->ws);
676 }
677
Kristian Høgsberge3778222012-07-31 17:29:30 -0400678 wl_list_remove(&state->link);
679 focus_state_destroy(state);
680 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200681}
682
683static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200684focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
685 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200686{
Jonas Ådahl04769742012-06-13 00:01:24 +0200687 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200688
689 state = malloc(sizeof *state);
690 if (state == NULL)
691 return NULL;
692
Quentin Glidicfff39812016-08-15 10:56:52 +0200693 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400695 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200696 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200698
699 state->seat_destroy_listener.notify = focus_state_seat_destroy;
700 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400701 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200702 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200704
705 return state;
706}
707
Jonas Ådahl8538b222012-08-29 22:13:03 +0200708static struct focus_state *
709ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
710{
711 struct workspace *ws = get_current_workspace(shell);
712 struct focus_state *state;
713
714 wl_list_for_each(state, &ws->focus_list, link)
715 if (state->seat == seat)
716 break;
717
718 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200719 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200720
721 return state;
722}
723
Jonas Ådahl04769742012-06-13 00:01:24 +0200724static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800725focus_state_set_focus(struct focus_state *state,
726 struct weston_surface *surface)
727{
728 if (state->keyboard_focus) {
729 wl_list_remove(&state->surface_destroy_listener.link);
730 wl_list_init(&state->surface_destroy_listener.link);
731 }
732
733 state->keyboard_focus = surface;
734 if (surface)
735 wl_signal_add(&surface->destroy_signal,
736 &state->surface_destroy_listener);
737}
738
739static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400740restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200741{
742 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400743 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100744 struct wl_list pending_seat_list;
745 struct weston_seat *seat, *next_seat;
746
747 /* Temporarily steal the list of seats so that we can keep
748 * track of the seats we've already processed */
749 wl_list_init(&pending_seat_list);
750 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
751 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200752
753 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500754 struct weston_keyboard *keyboard =
755 weston_seat_get_keyboard(state->seat);
756
Neil Roberts4237f502014-04-09 16:33:31 +0100757 wl_list_remove(&state->seat->link);
758 wl_list_insert(&shell->compositor->seat_list,
759 &state->seat->link);
760
Derek Foreman1281a362015-07-31 16:55:32 -0500761 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800762 continue;
763
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400764 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200765
Derek Foreman1281a362015-07-31 16:55:32 -0500766 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200767 }
Neil Roberts4237f502014-04-09 16:33:31 +0100768
769 /* For any remaining seats that we don't have a focus state
770 * for we'll reset the keyboard focus to NULL */
771 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500772 struct weston_keyboard *keyboard =
773 weston_seat_get_keyboard(seat);
774
Neil Roberts4237f502014-04-09 16:33:31 +0100775 wl_list_insert(&shell->compositor->seat_list, &seat->link);
776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100778 continue;
779
Derek Foreman1281a362015-07-31 16:55:32 -0500780 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100781 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200782}
783
784static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200785replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
786 struct weston_seat *seat)
787{
Derek Foreman1281a362015-07-31 16:55:32 -0500788 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790
791 wl_list_for_each(state, &ws->focus_list, link) {
792 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500793 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794 return;
795 }
796 }
797}
798
799static void
800drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_surface *surface)
802{
803 struct focus_state *state;
804
805 wl_list_for_each(state, &ws->focus_list, link)
806 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800807 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200808}
809
810static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100811animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
812 struct weston_view *from, struct weston_view *to)
813{
814 struct weston_output *output;
815 bool focus_surface_created = false;
816
817 /* FIXME: Only support dim animation using two layers */
818 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
819 return;
820
821 output = get_default_output(shell->compositor);
822 if (ws->fsurf_front == NULL && (from || to)) {
823 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800824 if (ws->fsurf_front == NULL)
825 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100826 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800827
828 ws->fsurf_back = create_focus_surface(shell->compositor, output);
829 if (ws->fsurf_back == NULL) {
830 focus_surface_destroy(ws->fsurf_front);
831 return;
832 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 focus_surface_created = true;
836 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300837 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
838 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 }
840
841 if (ws->focus_animation) {
842 weston_view_animation_destroy(ws->focus_animation);
843 ws->focus_animation = NULL;
844 }
845
846 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300847 weston_layer_entry_insert(&to->layer_link,
848 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300850 weston_layer_entry_insert(&ws->layer.view_list,
851 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100852
853 if (focus_surface_created) {
854 ws->focus_animation = weston_fade_run(
855 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200856 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 focus_animation_done, ws);
858 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300859 weston_layer_entry_insert(&from->layer_link,
860 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 ws->focus_animation = weston_stable_fade_run(
862 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200863 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 focus_animation_done, ws);
865 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300866 weston_layer_entry_insert(&ws->layer.view_list,
867 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 ws->focus_animation = weston_stable_fade_run(
869 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200870 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100871 focus_animation_done, ws);
872 }
873}
874
875static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876workspace_destroy(struct workspace *ws)
877{
Jonas Ådahl04769742012-06-13 00:01:24 +0200878 struct focus_state *state, *next;
879
880 wl_list_for_each_safe(state, next, &ws->focus_list, link)
881 focus_state_destroy(state);
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 if (ws->fsurf_front)
884 focus_surface_destroy(ws->fsurf_front);
885 if (ws->fsurf_back)
886 focus_surface_destroy(ws->fsurf_back);
887
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200888 free(ws);
889}
890
Jonas Ådahl04769742012-06-13 00:01:24 +0200891static void
892seat_destroyed(struct wl_listener *listener, void *data)
893{
894 struct weston_seat *seat = data;
895 struct focus_state *state, *next;
896 struct workspace *ws = container_of(listener,
897 struct workspace,
898 seat_destroyed_listener);
899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link)
901 if (state->seat == seat)
902 wl_list_remove(&state->link);
903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100906workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907{
908 struct workspace *ws = malloc(sizeof *ws);
909 if (ws == NULL)
910 return NULL;
911
Quentin Glidic82681572016-12-17 13:40:51 +0100912 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913
Jonas Ådahl04769742012-06-13 00:01:24 +0200914 wl_list_init(&ws->focus_list);
915 wl_list_init(&ws->seat_destroyed_listener.link);
916 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100917 ws->fsurf_front = NULL;
918 ws->fsurf_back = NULL;
919 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200920
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921 return ws;
922}
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924static int
925workspace_is_empty(struct workspace *ws)
926{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300927 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928}
929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930static struct workspace *
931get_workspace(struct desktop_shell *shell, unsigned int index)
932{
933 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200934 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935 pws += index;
936 return *pws;
937}
938
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800939struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940get_current_workspace(struct desktop_shell *shell)
941{
942 return get_workspace(shell, shell->workspaces.current);
943}
944
945static void
946activate_workspace(struct desktop_shell *shell, unsigned int index)
947{
948 struct workspace *ws;
949
950 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100951 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200952
953 shell->workspaces.current = index;
954}
955
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956static unsigned int
957get_output_height(struct weston_output *output)
958{
959 return abs(output->region.extents.y1 - output->region.extents.y2);
960}
961
962static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 struct weston_transform *transform;
966
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 if (is_focus_view(view)) {
968 struct focus_surface *fsurf = get_focus_surface(view->surface);
969 transform = &fsurf->workspace_transform;
970 } else {
971 struct shell_surface *shsurf = get_shell_surface(view->surface);
972 transform = &shsurf->workspace_transform;
973 }
974
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 weston_matrix_init(&transform->matrix);
980 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983}
984
985static void
986workspace_translate_out(struct workspace *ws, double fraction)
987{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 unsigned int height;
990 double d;
991
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 d = height * fraction;
995
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 }
998}
999
1000static void
1001workspace_translate_in(struct workspace *ws, double fraction)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 unsigned int height;
1005 double d;
1006
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009
1010 if (fraction > 0)
1011 d = -(height - height * fraction);
1012 else
1013 d = height + height * fraction;
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
1017}
1018
1019static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020reverse_workspace_change_animation(struct desktop_shell *shell,
1021 unsigned int index,
1022 struct workspace *from,
1023 struct workspace *to)
1024{
1025 shell->workspaces.current = index;
1026
1027 shell->workspaces.anim_to = to;
1028 shell->workspaces.anim_from = from;
1029 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1030 shell->workspaces.anim_timestamp = 0;
1031
Quentin Glidic82681572016-12-17 13:40:51 +01001032 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1033 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1034
Scott Moreau4272e632012-08-13 09:58:41 -06001035 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036}
1037
1038static void
1039workspace_deactivate_transforms(struct workspace *ws)
1040{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001043
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001044 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045 if (is_focus_view(view)) {
1046 struct focus_surface *fsurf = get_focus_surface(view->surface);
1047 transform = &fsurf->workspace_transform;
1048 } else {
1049 struct shell_surface *shsurf = get_shell_surface(view->surface);
1050 transform = &shsurf->workspace_transform;
1051 }
1052
1053 if (!wl_list_empty(&transform->link)) {
1054 wl_list_remove(&transform->link);
1055 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001056 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058 }
1059}
1060
1061static void
1062finish_workspace_change_animation(struct desktop_shell *shell,
1063 struct workspace *from,
1064 struct workspace *to)
1065{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001066 struct weston_view *view;
1067
Scott Moreau4272e632012-08-13 09:58:41 -06001068 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001070 /* Views that extend past the bottom of the output are still
1071 * visible after the workspace animation ends but before its layer
1072 * is hidden. In that case, we need to damage below those views so
1073 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001074 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001075 weston_view_damage_below(view);
1076
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077 wl_list_remove(&shell->workspaces.animation.link);
1078 workspace_deactivate_transforms(from);
1079 workspace_deactivate_transforms(to);
1080 shell->workspaces.anim_to = NULL;
1081
Quentin Glidic82681572016-12-17 13:40:51 +01001082 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083}
1084
1085static void
1086animate_workspace_change_frame(struct weston_animation *animation,
1087 struct weston_output *output, uint32_t msecs)
1088{
1089 struct desktop_shell *shell =
1090 container_of(animation, struct desktop_shell,
1091 workspaces.animation);
1092 struct workspace *from = shell->workspaces.anim_from;
1093 struct workspace *to = shell->workspaces.anim_to;
1094 uint32_t t;
1095 double x, y;
1096
1097 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1098 finish_workspace_change_animation(shell, from, to);
1099 return;
1100 }
1101
1102 if (shell->workspaces.anim_timestamp == 0) {
1103 if (shell->workspaces.anim_current == 0.0)
1104 shell->workspaces.anim_timestamp = msecs;
1105 else
1106 shell->workspaces.anim_timestamp =
1107 msecs -
1108 /* Invers of movement function 'y' below. */
1109 (asin(1.0 - shell->workspaces.anim_current) *
1110 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1111 M_2_PI);
1112 }
1113
1114 t = msecs - shell->workspaces.anim_timestamp;
1115
1116 /*
1117 * x = [0, π/2]
1118 * y(x) = sin(x)
1119 */
1120 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1121 y = sin(x);
1122
1123 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001124 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125
1126 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1127 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1128 shell->workspaces.anim_current = y;
1129
Scott Moreau4272e632012-08-13 09:58:41 -06001130 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001132 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134}
1135
1136static void
1137animate_workspace_change(struct desktop_shell *shell,
1138 unsigned int index,
1139 struct workspace *from,
1140 struct workspace *to)
1141{
1142 struct weston_output *output;
1143
1144 int dir;
1145
1146 if (index > shell->workspaces.current)
1147 dir = -1;
1148 else
1149 dir = 1;
1150
1151 shell->workspaces.current = index;
1152
1153 shell->workspaces.anim_dir = dir;
1154 shell->workspaces.anim_from = from;
1155 shell->workspaces.anim_to = to;
1156 shell->workspaces.anim_current = 0.0;
1157 shell->workspaces.anim_timestamp = 0;
1158
1159 output = container_of(shell->compositor->output_list.next,
1160 struct weston_output, link);
1161 wl_list_insert(&output->animation_list,
1162 &shell->workspaces.animation.link);
1163
Quentin Glidic82681572016-12-17 13:40:51 +01001164 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1165 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166
1167 workspace_translate_in(to, 0);
1168
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001169 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001170
Scott Moreau4272e632012-08-13 09:58:41 -06001171 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001172}
1173
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175update_workspace(struct desktop_shell *shell, unsigned int index,
1176 struct workspace *from, struct workspace *to)
1177{
1178 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001179 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1180 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181}
1182
1183static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184change_workspace(struct desktop_shell *shell, unsigned int index)
1185{
1186 struct workspace *from;
1187 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001188 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001189
1190 if (index == shell->workspaces.current)
1191 return;
1192
1193 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001194 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001195 return;
1196
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001197 from = get_current_workspace(shell);
1198 to = get_workspace(shell, index);
1199
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200 if (shell->workspaces.anim_from == to &&
1201 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203 reverse_workspace_change_animation(shell, index, from, to);
1204 return;
1205 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001206
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001207 if (shell->workspaces.anim_to != NULL)
1208 finish_workspace_change_animation(shell,
1209 shell->workspaces.anim_from,
1210 shell->workspaces.anim_to);
1211
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001213
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001214 if (shell->focus_animation_type != ANIMATION_NONE) {
1215 wl_list_for_each(state, &from->focus_list, link)
1216 if (state->keyboard_focus)
1217 animate_focus_change(shell, from,
1218 get_default_view(state->keyboard_focus), NULL);
1219
1220 wl_list_for_each(state, &to->focus_list, link)
1221 if (state->keyboard_focus)
1222 animate_focus_change(shell, to,
1223 NULL, get_default_view(state->keyboard_focus));
1224 }
1225
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001226 if (workspace_is_empty(to) && workspace_is_empty(from))
1227 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 else
1229 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001230}
1231
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232static bool
1233workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1234{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001235 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001236 struct wl_list *e;
1237
1238 if (wl_list_empty(list))
1239 return false;
1240
1241 e = list->next;
1242
1243 if (e->next != list)
1244 return false;
1245
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001246 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247}
1248
1249static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001250surface_keyboard_focus_lost(struct weston_surface *surface)
1251{
1252 struct weston_compositor *compositor = surface->compositor;
1253 struct weston_seat *seat;
1254 struct weston_surface *focus;
1255
1256 wl_list_for_each(seat, &compositor->seat_list, link) {
1257 struct weston_keyboard *keyboard =
1258 weston_seat_get_keyboard(seat);
1259
1260 if (!keyboard)
1261 continue;
1262
1263 focus = weston_surface_get_main_surface(keyboard->focus);
1264 if (focus == surface)
1265 weston_keyboard_set_focus(keyboard, NULL);
1266 }
1267}
1268
1269static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001271 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 unsigned int index)
1273{
Derek Foreman1281a362015-07-31 16:55:32 -05001274 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001275 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 struct shell_surface *shsurf;
1278 struct workspace *from;
1279 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001280 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281
Derek Foreman1281a362015-07-31 16:55:32 -05001282 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 view = get_default_view(surface);
1284 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001285 index == shell->workspaces.current ||
1286 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287 return;
1288
1289 from = get_current_workspace(shell);
1290 to = get_workspace(shell, index);
1291
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001292 weston_layer_entry_remove(&view->layer_link);
1293 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294
Philip Withnall659163d2013-11-25 18:01:36 +00001295 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001296 if (shsurf != NULL)
1297 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001298
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 drop_focus_state(shell, from, surface);
1301
1302 if (shell->workspaces.anim_from == to &&
1303 shell->workspaces.anim_to == from) {
1304 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306 return;
1307 }
1308
1309 if (shell->workspaces.anim_to != NULL)
1310 finish_workspace_change_animation(shell,
1311 shell->workspaces.anim_from,
1312 shell->workspaces.anim_to);
1313
1314 if (workspace_is_empty(from) &&
1315 workspace_has_only(to, surface))
1316 update_workspace(shell, index, from, to);
1317 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001318 if (shsurf != NULL &&
1319 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001320 wl_list_insert(&shell->workspaces.anim_sticky_list,
1321 &shsurf->workspace_transform.link);
1322
1323 animate_workspace_change(shell, index, from, to);
1324 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325
Jonas Ådahl8538b222012-08-29 22:13:03 +02001326 state = ensure_focus_state(shell, seat);
1327 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001328 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001329}
1330
1331static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001332touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001333 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001334{
1335}
1336
1337static void
1338touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1339{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001340 struct weston_touch_move_grab *move =
1341 (struct weston_touch_move_grab *) container_of(
1342 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001343
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001344 if (touch_id == 0)
1345 move->active = 0;
1346
Jonas Ådahl9484b692013-12-02 22:05:03 +01001347 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001348 shell_touch_grab_end(&move->base);
1349 free(move);
1350 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001351}
1352
1353static void
1354touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001355 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001356{
1357 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1358 struct shell_surface *shsurf = move->base.shsurf;
1359 struct weston_surface *es;
1360 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1361 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1362
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001363 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001364 return;
1365
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001366 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001367
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001368 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001369
1370 weston_compositor_schedule_repaint(es->compositor);
1371}
1372
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001373static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001374touch_move_grab_frame(struct weston_touch_grab *grab)
1375{
1376}
1377
1378static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001379touch_move_grab_cancel(struct weston_touch_grab *grab)
1380{
1381 struct weston_touch_move_grab *move =
1382 (struct weston_touch_move_grab *) container_of(
1383 grab, struct shell_touch_grab, grab);
1384
1385 shell_touch_grab_end(&move->base);
1386 free(move);
1387}
1388
Rusty Lynch1084da52013-08-15 09:10:08 -07001389static const struct weston_touch_grab_interface touch_move_grab_interface = {
1390 touch_move_grab_down,
1391 touch_move_grab_up,
1392 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001393 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001395};
1396
1397static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001398surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001399{
1400 struct weston_touch_move_grab *move;
1401
1402 if (!shsurf)
1403 return -1;
1404
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001405 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1406 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001407 return 0;
1408
1409 move = malloc(sizeof *move);
1410 if (!move)
1411 return -1;
1412
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001413 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001415 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001417 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001418
1419 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001420 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001421
1422 return 0;
1423}
1424
1425static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001426noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001427{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001428}
1429
1430static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001431noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001432 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001433{
1434}
1435
1436static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001437noop_grab_axis_source(struct weston_pointer_grab *grab,
1438 uint32_t source)
1439{
1440}
1441
1442static void
1443noop_grab_frame(struct weston_pointer_grab *grab)
1444{
1445}
1446
1447static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001448constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1449{
1450 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001451 struct weston_surface *surface =
1452 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001454 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001456 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001457 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001458
1459 x = wl_fixed_to_int(pointer->x + move->dx);
1460 y = wl_fixed_to_int(pointer->y + move->dy);
1461
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001462 if (shsurf->shell->panel_position ==
1463 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001464 get_output_work_area(shsurf->shell, surface->output, &area);
1465 geometry =
1466 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001467
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001468 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001469 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001470 y = area.y + safety - geometry.height
1471 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001472
1473 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001474 y + geometry.y < area.y)
1475 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001476 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001477
1478 *cx = x;
1479 *cy = y;
1480}
1481
1482static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001483move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001484 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001487 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001488 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001489 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001490 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001491
Jonas Ådahld2510102014-10-05 21:39:14 +02001492 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001493 if (!shsurf)
1494 return;
1495
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001496 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1497
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001498 constrain_position(move, &cx, &cy);
1499
1500 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001502 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503}
1504
1505static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001506move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001507 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001509 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1510 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001512 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513
Daniel Stone4dbadb12012-05-30 16:31:51 +01001514 if (pointer->button_count == 0 &&
1515 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001516 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001517 free(grab);
1518 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001519}
1520
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001521static void
1522move_grab_cancel(struct weston_pointer_grab *grab)
1523{
1524 struct shell_grab *shell_grab =
1525 container_of(grab, struct shell_grab, grab);
1526
1527 shell_grab_end(shell_grab);
1528 free(grab);
1529}
1530
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001531static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001532 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001533 move_grab_motion,
1534 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001535 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001536 noop_grab_axis_source,
1537 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001539};
1540
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001542surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001543 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544{
1545 struct weston_move_grab *move;
1546
1547 if (!shsurf)
1548 return -1;
1549
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001550 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001551 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1552 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553 return 0;
1554
1555 move = malloc(sizeof *move);
1556 if (!move)
1557 return -1;
1558
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001560 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001562 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001563 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564
1565 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001566 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567
1568 return 0;
1569}
1570
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571struct weston_resize_grab {
1572 struct shell_grab base;
1573 uint32_t edges;
1574 int32_t width, height;
1575};
1576
1577static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001578resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001579 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580{
1581 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001582 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001583 struct shell_surface *shsurf = resize->base.shsurf;
1584 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001585 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586 wl_fixed_t from_x, from_y;
1587 wl_fixed_t to_x, to_y;
1588
Jonas Ådahld2510102014-10-05 21:39:14 +02001589 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001590
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001591 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 return;
1593
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 weston_view_from_global_fixed(shsurf->view,
1595 pointer->grab_x, pointer->grab_y,
1596 &from_x, &from_y);
1597 weston_view_from_global_fixed(shsurf->view,
1598 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599
1600 width = resize->width;
1601 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1602 width += wl_fixed_to_int(from_x - to_x);
1603 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1604 width += wl_fixed_to_int(to_x - from_x);
1605 }
1606
1607 height = resize->height;
1608 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1609 height += wl_fixed_to_int(from_y - to_y);
1610 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1611 height += wl_fixed_to_int(to_y - from_y);
1612 }
1613
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001614 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1615 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1616
1617 min_size.width = MAX(1, min_size.width);
1618 min_size.height = MAX(1, min_size.height);
1619
1620 if (width < min_size.width)
1621 width = min_size.width;
1622 else if (max_size.width > 0 && width > max_size.width)
1623 width = max_size.width;
1624 if (height < min_size.height)
1625 height = min_size.height;
1626 else if (max_size.width > 0 && width > max_size.width)
1627 width = max_size.width;
1628 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629}
1630
1631static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 uint32_t time, uint32_t button, uint32_t state_w)
1634{
1635 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001636 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637 enum wl_pointer_button_state state = state_w;
1638
1639 if (pointer->button_count == 0 &&
1640 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1641 shell_grab_end(&resize->base);
1642 free(grab);
1643 }
1644}
1645
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001646static void
1647resize_grab_cancel(struct weston_pointer_grab *grab)
1648{
1649 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1650
1651 shell_grab_end(&resize->base);
1652 free(grab);
1653}
1654
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001655static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 noop_grab_focus,
1657 resize_grab_motion,
1658 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001659 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001660 noop_grab_axis_source,
1661 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001662 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663};
1664
Giulio Camuffob8366642013-04-25 13:57:46 +03001665/*
1666 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001667 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001668static void
1669surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1670 int32_t *y, int32_t *w, int32_t *h) {
1671 pixman_region32_t region;
1672 pixman_box32_t *box;
1673 struct weston_subsurface *subsurface;
1674
1675 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 surface->width,
1677 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001678
1679 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1680 pixman_region32_union_rect(&region, &region,
1681 subsurface->position.x,
1682 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 subsurface->surface->width,
1684 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001685 }
1686
1687 box = pixman_region32_extents(&region);
1688 if (x)
1689 *x = box->x1;
1690 if (y)
1691 *y = box->y1;
1692 if (w)
1693 *w = box->x2 - box->x1;
1694 if (h)
1695 *h = box->y2 - box->y1;
1696
1697 pixman_region32_fini(&region);
1698}
1699
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700static int
1701surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001702 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703{
1704 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001705 const unsigned resize_topbottom =
1706 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1707 const unsigned resize_leftright =
1708 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1709 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001710 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001712 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001713 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1714 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return 0;
1716
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001717 /* Check for invalid edge combinations. */
1718 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1719 (edges & resize_topbottom) == resize_topbottom ||
1720 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721 return 0;
1722
1723 resize = malloc(sizeof *resize);
1724 if (!resize)
1725 return -1;
1726
1727 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001729 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1730 resize->width = geometry.width;
1731 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001732
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001733 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001735 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
1737 return 0;
1738}
1739
1740static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001741busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001742{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001745 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001747 wl_fixed_t sx, sy;
1748
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749 view = weston_compositor_pick_view(pointer->seat->compositor,
1750 pointer->x, pointer->y,
1751 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001752 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001754 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001755 shell_grab_end(grab);
1756 free(grab);
1757 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758}
1759
1760static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001761busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001762 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763{
Jonas Ådahld2510102014-10-05 21:39:14 +02001764 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001766
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001767static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001768busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769 uint32_t time, uint32_t button, uint32_t state)
1770{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001772 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001773 struct weston_pointer *pointer = grab->grab.pointer;
1774 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001775
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001776 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001777 activate(shsurf->shell, shsurf->view, seat,
1778 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001779 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001780 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001781 activate(shsurf->shell, shsurf->view, seat,
1782 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001783 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001784 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785}
1786
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001787static void
1788busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1789{
1790 struct shell_grab *grab = (struct shell_grab *) base;
1791
1792 shell_grab_end(grab);
1793 free(grab);
1794}
1795
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001796static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797 busy_cursor_grab_focus,
1798 busy_cursor_grab_motion,
1799 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001800 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001801 noop_grab_axis_source,
1802 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001803 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804};
1805
1806static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001807handle_pointer_focus(struct wl_listener *listener, void *data)
1808{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001809 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001811 struct shell_surface *shsurf;
1812 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815 return;
1816
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001817 shsurf = get_shell_surface(view->surface);
1818 if (!shsurf)
1819 return;
1820
1821 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1822
1823 if (shsurf->unresponsive)
1824 set_busy_cursor(shsurf, pointer);
1825 else
1826 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827}
1828
1829static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001830shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1831{
1832 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001833 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001834}
1835
1836static void
1837shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1838{
1839 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001840 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001841}
1842
1843static void
1844handle_keyboard_focus(struct wl_listener *listener, void *data)
1845{
1846 struct weston_keyboard *keyboard = data;
1847 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1848
1849 if (seat->focused_surface) {
1850 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1851 if (shsurf)
1852 shell_surface_lose_keyboard_focus(shsurf);
1853 }
1854
1855 seat->focused_surface = keyboard->focus;
1856
1857 if (seat->focused_surface) {
1858 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1859 if (shsurf)
1860 shell_surface_gain_keyboard_focus(shsurf);
1861 }
1862}
1863
Philip Withnall07926d92013-11-25 18:01:40 +00001864/* The surface will be inserted into the list immediately after the link
1865 * returned by this function (i.e. will be stacked immediately above the
1866 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001867static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001868shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1869{
1870 struct workspace *ws;
1871
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001872 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1873 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001874 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001875 }
1876
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001877 /* Move the surface to a normal workspace layer so that surfaces
1878 * which were previously fullscreen or transient are no longer
1879 * rendered on top. */
1880 ws = get_current_workspace(shsurf->shell);
1881 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001882}
1883
Philip Withnall648a4dd2013-11-25 18:01:44 +00001884static void
1885shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1886{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001887 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001888}
1889
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001890/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001891 * geometry to ensure the changes are redrawn.
1892 *
1893 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1894 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001895static void
1896shell_surface_update_layer(struct shell_surface *shsurf)
1897{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001898 struct weston_surface *surface =
1899 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001900 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001901
1902 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1903
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001904 if (new_layer_link == NULL)
1905 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001906 if (new_layer_link == &shsurf->view->layer_link)
1907 return;
1908
1909 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001910 weston_layer_entry_remove(&shsurf->view->layer_link);
1911 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001912 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001913 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001914
1915 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001916}
1917
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001918static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001919shell_surface_set_output(struct shell_surface *shsurf,
1920 struct weston_output *output)
1921{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001922 struct weston_surface *es =
1923 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001924
1925 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001926 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001927 if (output)
1928 shsurf->output = output;
1929 else if (es->output)
1930 shsurf->output = es->output;
1931 else
1932 shsurf->output = get_default_output(es->compositor);
1933}
1934
1935static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001936weston_view_set_initial_position(struct weston_view *view,
1937 struct desktop_shell *shell);
1938
1939static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001940unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001941{
Philip Withnallf85fe842013-11-25 18:01:37 +00001942 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001943 wl_list_remove(&shsurf->fullscreen.transform.link);
1944 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001945
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946 if (shsurf->fullscreen.black_view)
1947 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1948 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001949
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001950 if (shsurf->saved_position_valid)
1951 weston_view_set_position(shsurf->view,
1952 shsurf->saved_x, shsurf->saved_y);
1953 else
1954 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001955 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001956
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001957 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001959 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001960 shsurf->saved_rotation_valid = false;
1961 }
Alex Wu4539b082012-03-01 12:57:46 +08001962}
1963
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001964static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001965unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001966{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001967 struct weston_surface *surface =
1968 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1969
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001970 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001971 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001972
1973 if (shsurf->saved_position_valid)
1974 weston_view_set_position(shsurf->view,
1975 shsurf->saved_x, shsurf->saved_y);
1976 else
1977 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001978 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001979
1980 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 wl_list_insert(&shsurf->view->geometry.transformation_list,
1982 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001983 shsurf->saved_rotation_valid = false;
1984 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001985}
1986
Philip Withnallbecb77e2013-11-25 18:01:30 +00001987static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001988set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001989{
1990 struct shell_surface *shsurf;
1991 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001992 struct weston_view *view;
1993
1994 view = get_default_view(surface);
1995 if (!view)
1996 return;
1997
1998 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1999
2000 shsurf = get_shell_surface(surface);
2001 current_ws = get_current_workspace(shsurf->shell);
2002
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002003 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002004 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002005
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002006 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002007 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002008
2009 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002010 weston_view_damage_below(view);
2011}
2012
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002013
Tiago Vignattibe143262012-04-16 17:31:41 +03002014static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002015shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002016{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002017 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002018}
2019
Pekka Paalanen8274d902014-08-06 19:36:51 +03002020static int
2021black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2022{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002023 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002024 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002025 int n;
2026 int rem;
2027 int ret;
2028
2029 n = snprintf(buf, len, "black background surface for ");
2030 if (n < 0)
2031 return n;
2032
2033 rem = (int)len - n;
2034 if (rem < 0)
2035 rem = 0;
2036
2037 if (fs_surface->get_label)
2038 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2039 else
2040 ret = snprintf(buf + n, rem, "<unknown>");
2041
2042 if (ret < 0)
2043 return n;
2044
2045 return n + ret;
2046}
2047
Alex Wu21858432012-04-01 20:13:08 +08002048static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002049black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002050
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002052create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002053 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002054 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002055{
2056 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002058
2059 surface = weston_surface_create(ec);
2060 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002061 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002062 return NULL;
2063 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 view = weston_view_create(surface);
2065 if (surface == NULL) {
2066 weston_log("no memory\n");
2067 weston_surface_destroy(surface);
2068 return NULL;
2069 }
Alex Wu4539b082012-03-01 12:57:46 +08002070
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002071 surface->committed = black_surface_committed;
2072 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002073 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002074 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002075 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002076 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002077 pixman_region32_fini(&surface->input);
2078 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002079
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002080 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002081 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082
2083 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002084}
2085
Philip Withnalled8f1a92013-11-25 18:01:43 +00002086static void
2087shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2088{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002089 struct weston_surface *surface =
2090 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002091 struct weston_output *output = shsurf->fullscreen_output;
2092
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002093 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002094
2095 if (!shsurf->fullscreen.black_view)
2096 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002097 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002098 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002099 output->x, output->y,
2100 output->width,
2101 output->height);
2102
2103 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002104 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2105 weston_layer_entry_insert(&shsurf->view->layer_link,
2106 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002107 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002108 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002109
Armin Krezović4663aca2016-06-30 06:04:29 +02002110 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002111 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002112}
2113
Alex Wu4539b082012-03-01 12:57:46 +08002114/* Create black surface and append it to the associated fullscreen surface.
2115 * Handle size dismatch and positioning according to the method. */
2116static void
2117shell_configure_fullscreen(struct shell_surface *shsurf)
2118{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002119 struct weston_surface *surface =
2120 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002121 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002122
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002123 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002124 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002125 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2126 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002127
Philip Withnalled8f1a92013-11-25 18:01:43 +00002128 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002129
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002130 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002131 &surf_width, &surf_height);
2132
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002133 if (surface->buffer_ref.buffer)
2134 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002135}
2136
Alex Wu4539b082012-03-01 12:57:46 +08002137static void
2138shell_map_fullscreen(struct shell_surface *shsurf)
2139{
Alex Wubd3354b2012-04-17 17:20:49 +08002140 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002141}
2142
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002143static struct weston_output *
2144get_focused_output(struct weston_compositor *compositor)
2145{
2146 struct weston_seat *seat;
2147 struct weston_output *output = NULL;
2148
2149 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002150 struct weston_touch *touch = weston_seat_get_touch(seat);
2151 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2152 struct weston_keyboard *keyboard =
2153 weston_seat_get_keyboard(seat);
2154
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002155 /* Priority has touch focus, then pointer and
2156 * then keyboard focus. We should probably have
2157 * three for loops and check frist for touch,
2158 * then for pointer, etc. but unless somebody has some
2159 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002160 if (touch && touch->focus)
2161 output = touch->focus->output;
2162 else if (pointer && pointer->focus)
2163 output = pointer->focus->output;
2164 else if (keyboard && keyboard->focus)
2165 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002166
2167 if (output)
2168 break;
2169 }
2170
2171 return output;
2172}
2173
2174static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002175destroy_shell_seat(struct wl_listener *listener, void *data)
2176{
2177 struct shell_seat *shseat =
2178 container_of(listener,
2179 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002180
2181 wl_list_remove(&shseat->seat_destroy_listener.link);
2182 free(shseat);
2183}
2184
Jason Ekstrand024177c2014-04-21 19:42:58 -05002185static void
2186shell_seat_caps_changed(struct wl_listener *listener, void *data)
2187{
Derek Foreman1281a362015-07-31 16:55:32 -05002188 struct weston_keyboard *keyboard;
2189 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190 struct shell_seat *seat;
2191
2192 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002193 keyboard = weston_seat_get_keyboard(seat->seat);
2194 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195
Derek Foreman1281a362015-07-31 16:55:32 -05002196 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002198 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002199 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002200 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002201 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002202 wl_list_init(&seat->keyboard_focus_listener.link);
2203 }
2204
Derek Foreman1281a362015-07-31 16:55:32 -05002205 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002207 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002208 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002209 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002210 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002211 wl_list_init(&seat->pointer_focus_listener.link);
2212 }
2213}
2214
Giulio Camuffo5085a752013-03-25 21:42:45 +01002215static struct shell_seat *
2216create_shell_seat(struct weston_seat *seat)
2217{
2218 struct shell_seat *shseat;
2219
2220 shseat = calloc(1, sizeof *shseat);
2221 if (!shseat) {
2222 weston_log("no memory to allocate shell seat\n");
2223 return NULL;
2224 }
2225
2226 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002227
2228 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2229 wl_signal_add(&seat->destroy_signal,
2230 &shseat->seat_destroy_listener);
2231
Jason Ekstrand024177c2014-04-21 19:42:58 -05002232 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2233 wl_list_init(&shseat->keyboard_focus_listener.link);
2234
2235 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2236 wl_list_init(&shseat->pointer_focus_listener.link);
2237
2238 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2239 wl_signal_add(&seat->updated_caps_signal,
2240 &shseat->caps_changed_listener);
2241 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2242
Giulio Camuffo5085a752013-03-25 21:42:45 +01002243 return shseat;
2244}
2245
2246static struct shell_seat *
2247get_shell_seat(struct weston_seat *seat)
2248{
2249 struct wl_listener *listener;
2250
2251 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002252 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002253
2254 return container_of(listener,
2255 struct shell_seat, seat_destroy_listener);
2256}
2257
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002258static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002259fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002260{
2261 struct shell_surface *shsurf = data;
2262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002263 weston_surface_destroy(shsurf->view->surface);
2264 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002265}
2266
2267static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002268fade_out_done(struct weston_view_animation *animation, void *data)
2269{
2270 struct shell_surface *shsurf = data;
2271 struct wl_event_loop *loop;
2272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002275 if (weston_view_is_mapped(shsurf->view)) {
2276 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002277 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002278 }
2279}
2280
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002281struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002282get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002283{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002284 if (weston_surface_is_desktop_surface(surface)) {
2285 struct weston_desktop_surface *desktop_surface =
2286 weston_surface_get_desktop_surface(surface);
2287 return weston_desktop_surface_get_user_data(desktop_surface);
2288 }
2289 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002290}
2291
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002292/*
2293 * libweston-desktop
2294 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002295
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002296static void
2297desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2298 void *shell)
2299{
2300 struct weston_desktop_client *client =
2301 weston_desktop_surface_get_client(desktop_surface);
2302 struct wl_client *wl_client =
2303 weston_desktop_client_get_client(client);
2304 struct weston_view *view;
2305 struct shell_surface *shsurf;
2306 struct weston_surface *surface =
2307 weston_desktop_surface_get_surface(desktop_surface);
2308
2309 view = weston_desktop_surface_create_view(desktop_surface);
2310 if (!view)
2311 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002312
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002313 shsurf = calloc(1, sizeof *shsurf);
2314 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002315 if (wl_client)
2316 wl_client_post_no_memory(wl_client);
2317 else
2318 weston_log("no memory to allocate shell surface\n");
2319 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002320 }
2321
Pekka Paalanen8274d902014-08-06 19:36:51 +03002322 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002323
Tiago Vignattibc052c92012-04-19 16:18:18 +03002324 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002325 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002326 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002327 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002328 shsurf->desktop_surface = desktop_surface;
2329 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002331 wl_list_init(&shsurf->fullscreen.transform.link);
2332
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002333 shsurf->output = get_default_output(shsurf->shell->compositor);
2334
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002335 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002336
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337 /* empty when not in use */
2338 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002339 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002340
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002341 wl_list_init(&shsurf->workspace_transform.link);
2342
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002343 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2344 weston_desktop_surface_set_activated(desktop_surface,
2345 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002346}
2347
Tiago Vignattibc052c92012-04-19 16:18:18 +03002348static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002349desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2350 void *shell)
2351{
2352 struct shell_surface *shsurf =
2353 weston_desktop_surface_get_user_data(desktop_surface);
2354 struct weston_surface *surface =
2355 weston_desktop_surface_get_surface(desktop_surface);
2356
2357 if (!shsurf)
2358 return;
2359
2360 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2361
2362 if (shsurf->fullscreen.black_view)
2363 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2364
2365 weston_surface_set_label_func(surface, NULL);
2366 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2367 shsurf->desktop_surface = NULL;
2368
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002369 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002370 if (weston_surface_is_mapped(surface) &&
2371 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2372 pixman_region32_fini(&surface->pending.input);
2373 pixman_region32_init(&surface->pending.input);
2374 pixman_region32_fini(&surface->input);
2375 pixman_region32_init(&surface->input);
2376 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2377 fade_out_done, shsurf);
2378 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002379 weston_view_destroy(shsurf->view);
2380 free(shsurf);
2381 }
2382}
2383
2384static void
2385set_maximized_position(struct desktop_shell *shell,
2386 struct shell_surface *shsurf)
2387{
2388 pixman_rectangle32_t area;
2389 struct weston_geometry geometry;
2390
2391 get_output_work_area(shell, shsurf->output, &area);
2392 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2393
2394 weston_view_set_position(shsurf->view,
2395 area.x - geometry.x,
2396 area.y - geometry.y);
2397}
2398
2399static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002400set_position_from_xwayland(struct shell_surface *shsurf)
2401{
2402 struct weston_geometry geometry;
2403 float x;
2404 float y;
2405
2406 assert(shsurf->xwayland.is_set);
2407
2408 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2409 x = shsurf->xwayland.x - geometry.x;
2410 y = shsurf->xwayland.y - geometry.y;
2411
2412 weston_view_set_position(shsurf->view, x, y);
2413
2414#ifdef WM_DEBUG
2415 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2416 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2417 geometry.x, geometry.y, x, y);
2418#endif
2419}
2420
2421static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002422map(struct desktop_shell *shell, struct shell_surface *shsurf,
2423 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002424{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002425 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002426 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2427 struct weston_compositor *compositor = shell->compositor;
2428 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002429
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002430 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002431 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002432 center_on_output(shsurf->view, shsurf->fullscreen_output);
2433 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002434 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002436 } else if (shsurf->xwayland.is_set) {
2437 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002438 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002439 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002440 }
2441
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002442 /* Surface stacking order, see also activate(). */
2443 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002444
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002445 weston_view_update_transform(shsurf->view);
2446 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002447 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002448 surface->output = shsurf->output;
2449 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002450 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002451
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452 if (!shell->locked) {
2453 wl_list_for_each(seat, &compositor->seat_list, link)
2454 activate(shell, shsurf->view, seat,
2455 WESTON_ACTIVATE_FLAG_CONFIGURE);
2456 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002457
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002458 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002459 switch (shell->win_animation_type) {
2460 case ANIMATION_FADE:
2461 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2462 break;
2463 case ANIMATION_ZOOM:
2464 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2465 break;
2466 case ANIMATION_NONE:
2467 default:
2468 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002469 }
2470 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002471}
2472
2473static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2475 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002476{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002477 struct shell_surface *shsurf =
2478 weston_desktop_surface_get_user_data(desktop_surface);
2479 struct weston_surface *surface =
2480 weston_desktop_surface_get_surface(desktop_surface);
2481 struct weston_view *view = shsurf->view;
2482 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002483 bool was_fullscreen;
2484 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002485
2486 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002487 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002488
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002489 was_fullscreen = shsurf->state.fullscreen;
2490 was_maximized = shsurf->state.maximized;
2491
2492 shsurf->state.fullscreen =
2493 weston_desktop_surface_get_fullscreen(desktop_surface);
2494 shsurf->state.maximized =
2495 weston_desktop_surface_get_maximized(desktop_surface);
2496
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002497 if (!weston_surface_is_mapped(surface)) {
2498 map(shell, shsurf, sx, sy);
2499 surface->is_mapped = true;
2500 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2501 ++surface->ref_count;
2502 return;
2503 }
2504
2505 if (sx == 0 && sy == 0 &&
2506 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002507 shsurf->last_height == surface->height &&
2508 was_fullscreen == shsurf->state.fullscreen &&
2509 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002510 return;
2511
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002512 if (was_fullscreen)
2513 unset_fullscreen(shsurf);
2514 if (was_maximized)
2515 unset_maximized(shsurf);
2516
Quentin Glidic920cf042016-08-16 14:26:20 +02002517 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2518 !shsurf->saved_position_valid) {
2519 shsurf->saved_x = shsurf->view->geometry.x;
2520 shsurf->saved_y = shsurf->view->geometry.y;
2521 shsurf->saved_position_valid = true;
2522
2523 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2524 wl_list_remove(&shsurf->rotation.transform.link);
2525 wl_list_init(&shsurf->rotation.transform.link);
2526 weston_view_geometry_dirty(shsurf->view);
2527 shsurf->saved_rotation_valid = true;
2528 }
2529 }
2530
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002531 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002532 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002533 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002534 set_maximized_position(shell, shsurf);
2535 surface->output = shsurf->output;
2536 } else {
2537 float from_x, from_y;
2538 float to_x, to_y;
2539 float x, y;
2540
2541 if (shsurf->resize_edges) {
2542 sx = 0;
2543 sy = 0;
2544 }
2545
2546 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2547 sx = shsurf->last_width - surface->width;
2548 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2549 sy = shsurf->last_height - surface->height;
2550
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002551 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2552 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2553 x = shsurf->view->geometry.x + to_x - from_x;
2554 y = shsurf->view->geometry.y + to_y - from_y;
2555
2556 weston_view_set_position(shsurf->view, x, y);
2557 }
2558
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002559 shsurf->last_width = surface->width;
2560 shsurf->last_height = surface->height;
2561
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002562 /* XXX: would a fullscreen surface need the same handling? */
2563 if (surface->output) {
2564 wl_list_for_each(view, &surface->views, surface_link)
2565 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002566 }
2567}
2568
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002569static void
2570set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2571 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002572{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002573 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2574 struct weston_surface *surface =
2575 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2576 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002577
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002578 if (fullscreen) {
2579 /* handle clients launching in fullscreen */
2580 if (output == NULL && !weston_surface_is_mapped(surface)) {
2581 /* Set the output to the one that has focus currently. */
2582 output = get_focused_output(surface->compositor);
2583 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002584
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 shell_surface_set_output(shsurf, output);
2586 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002587
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002588 width = shsurf->output->width;
2589 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002590 }
2591 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2592 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002593}
2594
2595static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002596desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2597 struct weston_seat *seat, uint32_t serial, void *shell)
2598{
2599 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2600 struct weston_touch *touch = weston_seat_get_touch(seat);
2601 struct shell_surface *shsurf =
2602 weston_desktop_surface_get_user_data(desktop_surface);
2603 struct weston_surface *surface =
2604 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2605 struct wl_resource *resource = surface->resource;
2606 struct weston_surface *focus;
2607
2608 if (pointer &&
2609 pointer->focus &&
2610 pointer->button_count > 0 &&
2611 pointer->grab_serial == serial) {
2612 focus = weston_surface_get_main_surface(pointer->focus->surface);
2613 if ((focus == surface) &&
2614 (surface_move(shsurf, pointer, true) < 0))
2615 wl_resource_post_no_memory(resource);
2616 } else if (touch &&
2617 touch->focus &&
2618 touch->grab_serial == serial) {
2619 focus = weston_surface_get_main_surface(touch->focus->surface);
2620 if ((focus == surface) &&
2621 (surface_touch_move(shsurf, touch) < 0))
2622 wl_resource_post_no_memory(resource);
2623 }
2624}
2625
2626static void
2627desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2628 struct weston_seat *seat, uint32_t serial,
2629 enum weston_desktop_surface_edge edges, void *shell)
2630{
2631 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2632 struct shell_surface *shsurf =
2633 weston_desktop_surface_get_user_data(desktop_surface);
2634 struct weston_surface *surface =
2635 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2636 struct wl_resource *resource = surface->resource;
2637 struct weston_surface *focus;
2638
2639 if (!pointer ||
2640 pointer->button_count == 0 ||
2641 pointer->grab_serial != serial ||
2642 pointer->focus == NULL)
2643 return;
2644
2645 focus = weston_surface_get_main_surface(pointer->focus->surface);
2646 if (focus != surface)
2647 return;
2648
2649 if (surface_resize(shsurf, pointer, edges) < 0)
2650 wl_resource_post_no_memory(resource);
2651}
2652
2653static void
2654desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2655 bool fullscreen,
2656 struct weston_output *output, void *shell)
2657{
2658 struct shell_surface *shsurf =
2659 weston_desktop_surface_get_user_data(desktop_surface);
2660
2661 set_fullscreen(shsurf, fullscreen, output);
2662}
2663
2664static void
2665set_maximized(struct shell_surface *shsurf, bool maximized)
2666{
2667 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2668 struct weston_surface *surface =
2669 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2670 int32_t width = 0, height = 0;
2671
2672 if (maximized) {
2673 struct weston_output *output;
2674 struct desktop_shell *shell;
2675 pixman_rectangle32_t area;
2676
2677 if (!weston_surface_is_mapped(surface))
2678 output = get_focused_output(surface->compositor);
2679 else
2680 output = surface->output;
2681
2682 shell_surface_set_output(shsurf, output);
2683
2684 shell = shell_surface_get_shell(shsurf);
2685 get_output_work_area(shell, shsurf->output, &area);
2686
2687 width = area.width;
2688 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002689 }
2690 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2691 weston_desktop_surface_set_size(desktop_surface, width, height);
2692}
2693
2694static void
2695desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2696 bool maximized, void *shell)
2697{
2698 struct shell_surface *shsurf =
2699 weston_desktop_surface_get_user_data(desktop_surface);
2700
2701 set_maximized(shsurf, maximized);
2702}
2703
2704static void
2705desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2706 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002707{
2708 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002709 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002710
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711 /* apply compositor's own minimization logic (hide) */
2712 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002713}
2714
Rafael Antognollie2a34552013-12-03 15:35:45 -02002715static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002716set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002717{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002718 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002719
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002720 if (pointer->grab->interface == &busy_cursor_grab_interface)
2721 return;
2722
2723 grab = malloc(sizeof *grab);
2724 if (!grab)
2725 return;
2726
2727 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2728 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2729 /* Mark the shsurf as ungrabbed so that button binding is able
2730 * to move it. */
2731 shsurf->grabbed = 0;
2732}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002733
2734static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002735end_busy_cursor(struct weston_compositor *compositor,
2736 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002737{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002738 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002739 struct shell_grab *grab;
2740 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002741
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002742 wl_list_for_each(seat, &compositor->seat_list, link) {
2743 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2744 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002745
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002746 if (!pointer)
2747 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002749 if (pointer->grab->interface != &busy_cursor_grab_interface)
2750 continue;
2751
2752 grab = (struct shell_grab *) pointer->grab;
2753 shsurf = grab->shsurf;
2754 if (!shsurf)
2755 continue;
2756
2757 grab_client =
2758 weston_desktop_surface_get_client(shsurf->desktop_surface);
2759 if (grab_client == desktop_client) {
2760 shell_grab_end(grab);
2761 free(grab);
2762 }
2763 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002764}
2765
2766static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002767desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2768 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002769{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770 struct shell_surface *shsurf =
2771 weston_desktop_surface_get_user_data(desktop_surface);
2772 bool *unresponsive = user_data;
2773
2774 shsurf->unresponsive = *unresponsive;
2775}
2776
2777static void
2778desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2779 void *shell_)
2780{
2781 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002782 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002783 struct weston_seat *seat;
2784 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002785
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002786 weston_desktop_client_for_each_surface(desktop_client,
2787 desktop_surface_set_unresponsive,
2788 &unresponsive);
2789
2790
2791 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2792 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2793 struct weston_desktop_client *grab_client;
2794
2795 if (!pointer || !pointer->focus)
2796 continue;
2797
2798 shsurf = get_shell_surface(pointer->focus->surface);
2799 if (!shsurf)
2800 continue;
2801
2802 grab_client =
2803 weston_desktop_surface_get_client(shsurf->desktop_surface);
2804 if (grab_client == desktop_client)
2805 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002806 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002807}
2808
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002809static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810desktop_surface_pong(struct weston_desktop_client *desktop_client,
2811 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002812{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002813 struct desktop_shell *shell = shell_;
2814 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002815
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002816 weston_desktop_client_for_each_surface(desktop_client,
2817 desktop_surface_set_unresponsive,
2818 &unresponsive);
2819 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002820}
2821
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002822static void
2823desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2824 int32_t x, int32_t y, void *shell_)
2825{
2826 struct shell_surface *shsurf =
2827 weston_desktop_surface_get_user_data(surface);
2828
2829 shsurf->xwayland.x = x;
2830 shsurf->xwayland.y = y;
2831 shsurf->xwayland.is_set = true;
2832}
2833
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002834static const struct weston_desktop_api shell_desktop_api = {
2835 .struct_size = sizeof(struct weston_desktop_api),
2836 .surface_added = desktop_surface_added,
2837 .surface_removed = desktop_surface_removed,
2838 .committed = desktop_surface_committed,
2839 .move = desktop_surface_move,
2840 .resize = desktop_surface_resize,
2841 .fullscreen_requested = desktop_surface_fullscreen_requested,
2842 .maximized_requested = desktop_surface_maximized_requested,
2843 .minimized_requested = desktop_surface_minimized_requested,
2844 .ping_timeout = desktop_surface_ping_timeout,
2845 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002846 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002847};
2848
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002849/* ************************ *
2850 * end of libweston-desktop *
2851 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002852static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002853configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002854{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002856
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002857 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 if (v->output == ev->output && v != ev) {
2859 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002860 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002861 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002862 }
2863 }
2864
Quentin Glidice8bf9592016-06-23 18:55:20 +02002865 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002866 ev->surface->is_mapped = true;
2867 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002868
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002869 if (wl_list_empty(&ev->layer_link.link)) {
2870 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002872 }
2873}
2874
David Fort50d962f2016-06-09 17:55:52 +02002875
2876static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877find_shell_output_from_weston_output(struct desktop_shell *shell,
2878 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002879{
2880 struct shell_output *shell_output;
2881
2882 wl_list_for_each(shell_output, &shell->output_list, link) {
2883 if (shell_output->output == output)
2884 return shell_output;
2885 }
2886
2887 return NULL;
2888}
2889
Pekka Paalanen8274d902014-08-06 19:36:51 +03002890static int
2891background_get_label(struct weston_surface *surface, char *buf, size_t len)
2892{
2893 return snprintf(buf, len, "background for output %s",
2894 surface->output->name);
2895}
2896
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002897static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002898background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002899{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002900 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002902
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 view = container_of(es->views.next, struct weston_view, surface_link);
2904
Quentin Glidice8bf9592016-06-23 18:55:20 +02002905 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002906}
2907
2908static void
David Fort50d962f2016-06-09 17:55:52 +02002909handle_background_surface_destroy(struct wl_listener *listener, void *data)
2910{
2911 struct shell_output *output =
2912 container_of(listener, struct shell_output, background_surface_listener);
2913
2914 weston_log("background surface gone\n");
2915 output->background_surface = NULL;
2916}
2917
2918static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002919desktop_shell_set_background(struct wl_client *client,
2920 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002921 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002922 struct wl_resource *surface_resource)
2923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002924 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002925 struct weston_surface *surface =
2926 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002927 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002930 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002931 wl_resource_post_error(surface_resource,
2932 WL_DISPLAY_ERROR_INVALID_OBJECT,
2933 "surface role already assigned");
2934 return;
2935 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002936
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2938 weston_view_destroy(view);
2939 view = weston_view_create(surface);
2940
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002941 surface->committed = background_committed;
2942 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002943 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03002944 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002946 weston_desktop_shell_send_configure(resource, 0,
2947 surface_resource,
2948 surface->output->width,
2949 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002950
2951 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2952 sh_output->background_surface = surface;
2953
2954 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2955 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002956}
2957
Pekka Paalanen8274d902014-08-06 19:36:51 +03002958static int
2959panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2960{
2961 return snprintf(buf, len, "panel for output %s",
2962 surface->output->name);
2963}
2964
Kristian Høgsberg75840622011-09-06 13:48:16 -04002965static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002966panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002967{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002968 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002970 int width, height;
2971 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002972
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 view = container_of(es->views.next, struct weston_view, surface_link);
2974
Quentin Glidice8bf9592016-06-23 18:55:20 +02002975 get_panel_size(shell, view, &width, &height);
2976 switch (shell->panel_position) {
2977 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2978 break;
2979 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2980 y = view->output->height - height;
2981 break;
2982 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2983 break;
2984 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2985 x = view->output->width - width;
2986 break;
2987 }
2988
2989 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002990}
2991
2992static void
David Fort50d962f2016-06-09 17:55:52 +02002993handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2994{
2995 struct shell_output *output =
2996 container_of(listener, struct shell_output, panel_surface_listener);
2997
2998 weston_log("panel surface gone\n");
2999 output->panel_surface = NULL;
3000}
3001
3002
3003static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004desktop_shell_set_panel(struct wl_client *client,
3005 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003006 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003007 struct wl_resource *surface_resource)
3008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003010 struct weston_surface *surface =
3011 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003013 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003014
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003015 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003016 wl_resource_post_error(surface_resource,
3017 WL_DISPLAY_ERROR_INVALID_OBJECT,
3018 "surface role already assigned");
3019 return;
3020 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003021
Jason Ekstranda7af7042013-10-12 22:38:11 -05003022 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3023 weston_view_destroy(view);
3024 view = weston_view_create(surface);
3025
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003026 surface->committed = panel_committed;
3027 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003028 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03003029 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003031 weston_desktop_shell_send_configure(resource, 0,
3032 surface_resource,
3033 surface->output->width,
3034 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003035
3036 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3037 sh_output->panel_surface = surface;
3038
3039 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3040 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003041}
3042
Pekka Paalanen8274d902014-08-06 19:36:51 +03003043static int
3044lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3045{
3046 return snprintf(buf, len, "lock window");
3047}
3048
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003049static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003050lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003051{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003052 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003053 struct weston_view *view;
3054
3055 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003056
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003057 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003058 return;
3059
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003061
3062 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003063 weston_layer_entry_insert(&shell->lock_layer.view_list,
3064 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003066 surface->is_mapped = true;
3067 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003068 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003069 }
3070}
3071
3072static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003073handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003074{
Tiago Vignattibe143262012-04-16 17:31:41 +03003075 struct desktop_shell *shell =
3076 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003077
Martin Minarik6d118362012-06-07 18:01:59 +02003078 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003079 shell->lock_surface = NULL;
3080}
3081
3082static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003083desktop_shell_set_lock_surface(struct wl_client *client,
3084 struct wl_resource *resource,
3085 struct wl_resource *surface_resource)
3086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003088 struct weston_surface *surface =
3089 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003090
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003091 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003092
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003093 if (!shell->locked)
3094 return;
3095
Pekka Paalanen98262232011-12-01 10:42:22 +02003096 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003097
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003098 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003099 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003100 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003101
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003102 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003103 surface->committed = lock_surface_committed;
3104 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003105 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003106}
3107
3108static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003109resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003110{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003111 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003112
Quentin Glidic82681572016-12-17 13:40:51 +01003113 weston_layer_unset_position(&shell->lock_layer);
3114
3115 if (shell->showing_input_panels)
3116 weston_layer_set_position(&shell->input_panel_layer,
3117 WESTON_LAYER_POSITION_TOP_UI);
3118 weston_layer_set_position(&shell->fullscreen_layer,
3119 WESTON_LAYER_POSITION_FULLSCREEN);
3120 weston_layer_set_position(&shell->panel_layer,
3121 WESTON_LAYER_POSITION_UI);
3122 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003123
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003124 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003125
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003126 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003127 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003128 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003129}
3130
3131static void
3132desktop_shell_unlock(struct wl_client *client,
3133 struct wl_resource *resource)
3134{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003135 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003136
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003137 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003138
3139 if (shell->locked)
3140 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003141}
3142
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003144desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003145 struct wl_resource *resource,
3146 struct wl_resource *surface_resource)
3147{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003148 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003149
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003150 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003151 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003152}
3153
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003154static void
3155desktop_shell_desktop_ready(struct wl_client *client,
3156 struct wl_resource *resource)
3157{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003158 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003159
3160 shell_fade_startup(shell);
3161}
3162
Jonny Lamb765760d2014-08-20 15:53:19 +02003163static void
3164desktop_shell_set_panel_position(struct wl_client *client,
3165 struct wl_resource *resource,
3166 uint32_t position)
3167{
3168 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3169
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003170 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3171 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3172 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3173 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003174 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003175 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003176 "bad position argument");
3177 return;
3178 }
3179
3180 shell->panel_position = position;
3181}
3182
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003183static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003184 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003185 desktop_shell_set_panel,
3186 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003187 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003188 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003189 desktop_shell_desktop_ready,
3190 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003191};
3192
3193static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003194move_binding(struct weston_pointer *pointer, uint32_t time,
3195 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003196{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003197 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003198 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003199 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003200
Derek Foreman8ae2db52015-07-15 13:00:36 -05003201 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003202 return;
3203
Derek Foreman8ae2db52015-07-15 13:00:36 -05003204 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003205
Pekka Paalanen01388e22013-04-25 13:57:44 +03003206 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003207 if (surface == NULL)
3208 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003209
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003210 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003211 if (shsurf == NULL ||
3212 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3213 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003214 return;
3215
Derek Foreman794fa0e2015-07-15 13:00:38 -05003216 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003217}
3218
3219static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003220maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3221 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003222{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003223 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003224 struct weston_surface *surface;
3225 struct shell_surface *shsurf;
3226
3227 surface = weston_surface_get_main_surface(focus);
3228 if (surface == NULL)
3229 return;
3230
3231 shsurf = get_shell_surface(surface);
3232 if (shsurf == NULL)
3233 return;
3234
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003235 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003236}
3237
3238static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003239fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3240 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003241{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003242 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003243 struct weston_surface *surface;
3244 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003245 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003246
3247 surface = weston_surface_get_main_surface(focus);
3248 if (surface == NULL)
3249 return;
3250
3251 shsurf = get_shell_surface(surface);
3252 if (shsurf == NULL)
3253 return;
3254
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003255 fullscreen =
3256 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003257
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003258 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003259}
3260
3261static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003262touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003263{
Derek Foreman362656b2014-09-04 10:23:05 -05003264 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003265 struct weston_surface *surface;
3266 struct shell_surface *shsurf;
3267
Derek Foreman8ae2db52015-07-15 13:00:36 -05003268 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003269 return;
3270
Derek Foreman8ae2db52015-07-15 13:00:36 -05003271 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003272 surface = weston_surface_get_main_surface(focus);
3273 if (surface == NULL)
3274 return;
3275
3276 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003277 if (shsurf == NULL ||
3278 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3279 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003280 return;
3281
Derek Foremanb7674ae2015-07-15 13:00:37 -05003282 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003283}
3284
3285static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003286resize_binding(struct weston_pointer *pointer, uint32_t time,
3287 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003288{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003289 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003290 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003291 uint32_t edges = 0;
3292 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003293 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003294
Derek Foreman8ae2db52015-07-15 13:00:36 -05003295 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003296 return;
3297
Derek Foreman8ae2db52015-07-15 13:00:36 -05003298 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003299
Pekka Paalanen01388e22013-04-25 13:57:44 +03003300 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003301 if (surface == NULL)
3302 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003303
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003304 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003305 if (shsurf == NULL ||
3306 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3307 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003308 return;
3309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003311 wl_fixed_to_int(pointer->grab_x),
3312 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003314
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003315 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003316 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003317 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003318 edges |= 0;
3319 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003320 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003321
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003323 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003324 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003325 edges |= 0;
3326 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003327 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003328
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003329 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003330}
3331
3332static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003333surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003334 struct weston_pointer_axis_event *event,
3335 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003336{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003337 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003338 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003339 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003340 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003341
Pekka Paalanen01388e22013-04-25 13:57:44 +03003342 /* XXX: broken for windows containing sub-surfaces */
3343 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003344 if (surface == NULL)
3345 return;
3346
3347 shsurf = get_shell_surface(surface);
3348 if (!shsurf)
3349 return;
3350
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003351 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003352
Jason Ekstranda7af7042013-10-12 22:38:11 -05003353 if (shsurf->view->alpha > 1.0)
3354 shsurf->view->alpha = 1.0;
3355 if (shsurf->view->alpha < step)
3356 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003357
Jason Ekstranda7af7042013-10-12 22:38:11 -05003358 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003359 weston_surface_damage(surface);
3360}
3361
3362static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003363do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003364 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003365{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003366 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003367 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003368 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003369 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003370
Derek Foreman1281a362015-07-31 16:55:32 -05003371 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003372 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3373 return;
3374 }
3375
Scott Moreauccbf29d2012-02-22 14:21:41 -07003376 wl_list_for_each(output, &compositor->output_list, link) {
3377 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003378 wl_fixed_to_double(pointer->x),
3379 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003380 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003381 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003382 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003383 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003384 increment = -output->zoom.increment;
3385 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003386 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003387 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003388 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003389 else
3390 increment = 0;
3391
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003392 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003393
Scott Moreaue6603982012-06-11 13:07:51 -06003394 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003395 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003396 else if (output->zoom.level > output->zoom.max_level)
3397 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003398
3399 if (!output->zoom.active) {
3400 if (output->zoom.level <= 0.0)
3401 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003402 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003403 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003404
Scott Moreaue6603982012-06-11 13:07:51 -06003405 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003406
Jason Ekstranda7af7042013-10-12 22:38:11 -05003407 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003408 }
3409 }
3410}
3411
Scott Moreauccbf29d2012-02-22 14:21:41 -07003412static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003413zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003414 struct weston_pointer_axis_event *event,
3415 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003416{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003417 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003418}
3419
3420static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003421zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3422 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003423{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003424 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003425}
3426
3427static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003428terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3429 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003431 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003432
Daniel Stone325fc2d2012-05-30 16:31:58 +01003433 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003434}
3435
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003436static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003437rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003438 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003439{
3440 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003441 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003442 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003443 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003444 struct weston_surface *surface =
3445 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003446 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003447
Jonas Ådahld2510102014-10-05 21:39:14 +02003448 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003449
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003450 if (!shsurf)
3451 return;
3452
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003453 cx = 0.5f * surface->width;
3454 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003455
Daniel Stone37816df2012-05-16 18:45:18 +01003456 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3457 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003458 r = sqrtf(dx * dx + dy * dy);
3459
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003460 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003462
3463 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003464 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003465 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003466
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003467 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003468 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003469
3470 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003471 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003472 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003473 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003474 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003475
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003476 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003477 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003478 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003479 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003480 wl_list_init(&shsurf->rotation.transform.link);
3481 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003482 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003483 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003484
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003485 /* We need to adjust the position of the surface
3486 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 cposx = shsurf->view->geometry.x + cx;
3488 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003489 dposx = rotate->center.x - cposx;
3490 dposy = rotate->center.y - cposy;
3491 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 weston_view_set_position(shsurf->view,
3493 shsurf->view->geometry.x + dposx,
3494 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003495 }
3496
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003497 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003498 * lazily applies the damage due to rotation update.
3499 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003500 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003501}
3502
3503static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003504rotate_grab_button(struct weston_pointer_grab *grab,
3505 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506{
3507 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003508 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003509 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003510 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003511 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003512
Daniel Stone4dbadb12012-05-30 16:31:51 +01003513 if (pointer->button_count == 0 &&
3514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003515 if (shsurf)
3516 weston_matrix_multiply(&shsurf->rotation.rotation,
3517 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003518 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003519 free(rotate);
3520 }
3521}
3522
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003523static void
3524rotate_grab_cancel(struct weston_pointer_grab *grab)
3525{
3526 struct rotate_grab *rotate =
3527 container_of(grab, struct rotate_grab, base.grab);
3528
3529 shell_grab_end(&rotate->base);
3530 free(rotate);
3531}
3532
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003533static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003534 noop_grab_focus,
3535 rotate_grab_motion,
3536 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003537 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003538 noop_grab_axis_source,
3539 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003540 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003541};
3542
3543static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003544surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003545{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003546 struct weston_surface *surface =
3547 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003549 float dx, dy;
3550 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003551
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552 rotate = malloc(sizeof *rotate);
3553 if (!rotate)
3554 return;
3555
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003556 weston_view_to_global_float(shsurf->view,
3557 surface->width * 0.5f,
3558 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003559 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003560
Derek Foreman74de4692015-07-15 13:00:39 -05003561 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3562 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003563 r = sqrtf(dx * dx + dy * dy);
3564 if (r > 20.0f) {
3565 struct weston_matrix inverse;
3566
3567 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003568 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003569 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003570
3571 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003572 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003573 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003574 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003575 weston_matrix_init(&rotate->rotation);
3576 }
3577
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003578 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003579 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580}
3581
3582static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003583rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003584 void *data)
3585{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003586 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003587 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003588 struct shell_surface *surface;
3589
Derek Foreman8ae2db52015-07-15 13:00:36 -05003590 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003591 return;
3592
Derek Foreman8ae2db52015-07-15 13:00:36 -05003593 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003594
Pekka Paalanen01388e22013-04-25 13:57:44 +03003595 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003596 if (base_surface == NULL)
3597 return;
3598
3599 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003600 if (surface == NULL ||
3601 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3602 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003603 return;
3604
Derek Foreman74de4692015-07-15 13:00:39 -05003605 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003606}
3607
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003608/* Move all fullscreen layers down to the current workspace and hide their
3609 * black views. The surfaces' state is set to both fullscreen and lowered,
3610 * and this is reversed when such a surface is re-configured, see
3611 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3612 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003613 * lowering_output = NULL - Lower on all outputs, else only lower on the
3614 * specified output.
3615 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003616 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003617 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003618void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003619lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003620 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003621{
3622 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003624
3625 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003627 &shell->fullscreen_layer.view_list.link,
3628 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003629 struct shell_surface *shsurf = get_shell_surface(view->surface);
3630
3631 if (!shsurf)
3632 continue;
3633
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003634 /* Only lower surfaces which have lowering_output as their fullscreen
3635 * output, unless a NULL output asks for lowering on all outputs.
3636 */
3637 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3638 continue;
3639
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003640 /* We can have a non-fullscreen popup for a fullscreen surface
3641 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003642 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003643 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003644 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3645 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003646 weston_view_damage_below(shsurf->fullscreen.black_view);
3647
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003648 }
3649
3650 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003651 weston_layer_entry_remove(&view->layer_link);
3652 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003653 weston_view_damage_below(view);
3654 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003655
3656 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003657 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003658}
3659
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003660void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003661activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003662 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003663{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003664 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003665 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003666 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003667 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003668 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003669 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003670
Pekka Paalanen01388e22013-04-25 13:57:44 +03003671 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003672 shsurf = get_shell_surface(main_surface);
3673 assert(shsurf);
3674
3675 /* Only demote fullscreen surfaces on the output of activated shsurf.
3676 * Leave fullscreen surfaces on unrelated outputs alone. */
3677 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003678
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003679 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003680
Jonas Ådahl8538b222012-08-29 22:13:03 +02003681 state = ensure_focus_state(shell, seat);
3682 if (state == NULL)
3683 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003684
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003685 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003686 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003687
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003688 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3689 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003690 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003691
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003692 /* Update the surface’s layer. This brings it to the top of the stacking
3693 * order as appropriate. */
3694 shell_surface_update_layer(shsurf);
3695
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003696 if (shell->focus_animation_type != ANIMATION_NONE) {
3697 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003698 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003699 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003700}
3701
Alex Wu21858432012-04-01 20:13:08 +08003702/* no-op func for checking black surface */
3703static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003704black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003705{
3706}
3707
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003708static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003709is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003710{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003711 struct weston_surface *surface = view->surface;
3712
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003713 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003714 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003715 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003716 return true;
3717 }
3718 return false;
3719}
3720
Kristian Høgsberg75840622011-09-06 13:48:16 -04003721static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003722activate_binding(struct weston_seat *seat,
3723 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003724 struct weston_view *focus_view,
3725 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003726{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003727 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003728 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003729
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003730 if (!focus_view)
3731 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003732
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003733 if (is_black_surface_view(focus_view, &main_view))
3734 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003735
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003736 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003737 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003738 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003739
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003740 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003741}
3742
3743static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003744click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3745 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003746{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003747 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003748 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003749 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003750 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003751
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003752 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003753 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003754 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003755}
3756
3757static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003758touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3759 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003760{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003761 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003762 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003763 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003764 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003765
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003766 activate_binding(touch->seat, data, touch->focus,
3767 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003768}
3769
3770static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003771unfocus_all_seats(struct desktop_shell *shell)
3772{
3773 struct weston_seat *seat, *next;
3774
3775 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003776 struct weston_keyboard *keyboard =
3777 weston_seat_get_keyboard(seat);
3778
3779 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003780 continue;
3781
Derek Foreman1281a362015-07-31 16:55:32 -05003782 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003783 }
3784}
3785
3786static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003787lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003788{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003789 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003790
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003791 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003792 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003793 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003794 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003795
3796 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003797
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003798 /* Hide all surfaces by removing the fullscreen, panel and
3799 * toplevel layers. This way nothing else can show or receive
3800 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003801
Quentin Glidic82681572016-12-17 13:40:51 +01003802 weston_layer_unset_position(&shell->panel_layer);
3803 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003804 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003805 weston_layer_unset_position(&shell->input_panel_layer);
3806 weston_layer_unset_position(&ws->layer);
3807
3808 weston_layer_set_position(&shell->lock_layer,
3809 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003810
Derek Foreman004b4a12015-07-20 16:28:13 -05003811 weston_compositor_sleep(shell->compositor);
3812
Neil Robertsb4a91702014-04-09 16:33:32 +01003813 /* Remove the keyboard focus on all seats. This will be
3814 * restored to the workspace's saved state via
3815 * restore_focus_state when the compositor is unlocked */
3816 unfocus_all_seats(shell);
3817
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003818 /* TODO: disable bindings that should not work while locked. */
3819
3820 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003821}
3822
3823static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003824unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003825{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003826 struct wl_resource *shell_resource;
3827
Pekka Paalanend81c2162011-11-16 13:47:34 +02003828 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003829 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003830 return;
3831 }
3832
3833 /* If desktop-shell client has gone away, unlock immediately. */
3834 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003835 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003836 return;
3837 }
3838
3839 if (shell->prepare_event_sent)
3840 return;
3841
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003842 shell_resource = shell->child.desktop_shell;
3843 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003844 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003845}
3846
3847static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003848shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003849{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003850 struct shell_output *shell_output = data;
3851 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003852
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003853 shell_output->fade.animation = NULL;
3854 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003855 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003856 weston_surface_destroy(shell_output->fade.view->surface);
3857 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003858 break;
3859 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003860 lock(shell);
3861 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003862 default:
3863 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003864 }
3865}
3866
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003868shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003869{
3870 struct weston_compositor *compositor = shell->compositor;
3871 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003872 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003873
3874 surface = weston_surface_create(compositor);
3875 if (!surface)
3876 return NULL;
3877
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 view = weston_view_create(surface);
3879 if (!view) {
3880 weston_surface_destroy(surface);
3881 return NULL;
3882 }
3883
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003884 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3885 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003886 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003887 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3888 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003889 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003890 surface->is_mapped = true;
3891 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003892
Jason Ekstranda7af7042013-10-12 22:38:11 -05003893 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003894}
3895
3896static void
3897shell_fade(struct desktop_shell *shell, enum fade_type type)
3898{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003899 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003900 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003901
3902 switch (type) {
3903 case FADE_IN:
3904 tint = 0.0;
3905 break;
3906 case FADE_OUT:
3907 tint = 1.0;
3908 break;
3909 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003910 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003911 return;
3912 }
3913
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003914 /* Create a separate fade surface for each output */
3915 wl_list_for_each(shell_output, &shell->output_list, link) {
3916 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003917
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003918 if (shell_output->fade.view == NULL) {
3919 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3920 if (!shell_output->fade.view)
3921 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003922
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003923 shell_output->fade.view->alpha = 1.0 - tint;
3924 weston_view_update_transform(shell_output->fade.view);
3925 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003926
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003927 if (shell_output->fade.view->output == NULL) {
3928 /* If the black view gets a NULL output, we lost the
3929 * last output and we'll just cancel the fade. This
3930 * happens when you close the last window under the
3931 * X11 or Wayland backends. */
3932 shell->locked = false;
3933 weston_surface_destroy(shell_output->fade.view->surface);
3934 shell_output->fade.view = NULL;
3935 } else if (shell_output->fade.animation) {
3936 weston_fade_update(shell_output->fade.animation, tint);
3937 } else {
3938 shell_output->fade.animation =
3939 weston_fade_run(shell_output->fade.view,
3940 1.0 - tint, tint, 300.0,
3941 shell_fade_done_for_output, shell_output);
3942 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003943 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003944}
3945
3946static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003947do_shell_fade_startup(void *data)
3948{
3949 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003950 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003951
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003952 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003953 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003954 } else {
3955 weston_log("desktop shell: "
3956 "unexpected fade-in animation type %d\n",
3957 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003958 wl_list_for_each(shell_output, &shell->output_list, link) {
3959 weston_surface_destroy(shell_output->fade.view->surface);
3960 shell_output->fade.view = NULL;
3961 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003962 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003963}
3964
3965static void
3966shell_fade_startup(struct desktop_shell *shell)
3967{
3968 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003969 struct shell_output *shell_output;
3970 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003971
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003972 wl_list_for_each(shell_output, &shell->output_list, link) {
3973 if (!shell_output->fade.startup_timer)
3974 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003975
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003976 wl_event_source_remove(shell_output->fade.startup_timer);
3977 shell_output->fade.startup_timer = NULL;
3978 has_fade = true;
3979 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003980
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003981 if (has_fade) {
3982 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3983 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3984 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003985}
3986
3987static int
3988fade_startup_timeout(void *data)
3989{
3990 struct desktop_shell *shell = data;
3991
3992 shell_fade_startup(shell);
3993 return 0;
3994}
3995
3996static void
3997shell_fade_init(struct desktop_shell *shell)
3998{
3999 /* Make compositor output all black, and wait for the desktop-shell
4000 * client to signal it is ready, then fade in. The timer triggers a
4001 * fade-in, in case the desktop-shell client takes too long.
4002 */
4003
4004 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004005 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004006
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004007 if (shell->startup_animation_type == ANIMATION_NONE)
4008 return;
4009
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004010 wl_list_for_each(shell_output, &shell->output_list, link) {
4011 if (shell_output->fade.view != NULL) {
4012 weston_log("%s: warning: fade surface already exists\n",
4013 __func__);
4014 continue;
4015 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004016
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004017 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4018 if (!shell_output->fade.view)
4019 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004020
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004021 weston_view_update_transform(shell_output->fade.view);
4022 weston_surface_damage(shell_output->fade.view->surface);
4023
4024 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4025 shell_output->fade.startup_timer =
4026 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4027 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4028 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004029}
4030
4031static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004032idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004033{
4034 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004035 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004036
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004037 struct weston_seat *seat;
4038
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004039 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4040 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004041
4042 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004043 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004044}
4045
4046static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004047wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004048{
4049 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004050 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004051
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004052 unlock(shell);
4053}
4054
4055static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004056transform_handler(struct wl_listener *listener, void *data)
4057{
4058 struct weston_surface *surface = data;
4059 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004060 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004061 int x, y;
4062
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004063 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004064 return;
4065
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004066 api = shsurf->shell->xwayland_surface_api;
4067 if (!api) {
4068 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4069 shsurf->shell->xwayland_surface_api = api;
4070 }
4071
4072 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004073 return;
4074
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004075 if (!weston_view_is_mapped(shsurf->view))
4076 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004077
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004078 x = shsurf->view->geometry.x;
4079 y = shsurf->view->geometry.y;
4080
4081 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004082}
4083
4084static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004085center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004086{
Giulio Camuffob8366642013-04-25 13:57:46 +03004087 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004088 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004089
Jason Ekstranda7af7042013-10-12 22:38:11 -05004090 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004091
4092 x = output->x + (output->width - width) / 2 - surf_x / 2;
4093 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004094
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004095 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004096}
4097
4098static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004099weston_view_set_initial_position(struct weston_view *view,
4100 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004101{
4102 struct weston_compositor *compositor = shell->compositor;
4103 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004104 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004105 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004106 struct weston_output *output, *target_output = NULL;
4107 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004108 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004109
4110 /* As a heuristic place the new window on the same output as the
4111 * pointer. Falling back to the output containing 0, 0.
4112 *
4113 * TODO: Do something clever for touch too?
4114 */
4115 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004116 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4117
4118 if (pointer) {
4119 ix = wl_fixed_to_int(pointer->x);
4120 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004121 break;
4122 }
4123 }
4124
4125 wl_list_for_each(output, &compositor->output_list, link) {
4126 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4127 target_output = output;
4128 break;
4129 }
4130 }
4131
4132 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004133 weston_view_set_position(view, 10 + random() % 400,
4134 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004135 return;
4136 }
4137
4138 /* Valid range within output where the surface will still be onscreen.
4139 * If this is negative it means that the surface is bigger than
4140 * output.
4141 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004142 get_output_work_area(shell, target_output, &area);
4143
Derek Foremanf814c5d2015-04-15 12:23:54 -05004144 x = area.x;
4145 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004146 range_x = area.width - view->surface->width;
4147 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004148
Rob Bradford4cb88c72012-08-13 15:18:44 +01004149 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004150 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004151
4152 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004153 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004154
Jason Ekstranda7af7042013-10-12 22:38:11 -05004155 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004156}
4157
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004158static bool
4159check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4160{
4161 struct timespec now;
4162
4163 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4164 return false;
4165
4166 /*
4167 * If the shell helper client dies before the session has been
4168 * up for roughly 30 seconds, better just make Weston shut down,
4169 * because the user likely has no way to interact with the desktop
4170 * anyway.
4171 */
4172 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4173 weston_log("Error: %s apparently cannot run at all.\n",
4174 shell->client);
4175 weston_log_continue(STAMP_SPACE "Quitting...");
4176 wl_display_terminate(shell->compositor->wl_display);
4177
4178 return true;
4179 }
4180
4181 return false;
4182}
4183
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004184static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004185
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004186static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004187respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004188{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004189 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004190
4191 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4192 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004193 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004194 shell->child.deathstamp = time;
4195 shell->child.deathcount = 0;
4196 }
4197
4198 shell->child.deathcount++;
4199 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004200 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004201 return;
4202 }
4203
Pekka Paalanen826dc142014-08-27 12:11:53 +03004204 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004205 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004206}
4207
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004208static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004209desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4210{
4211 struct desktop_shell *shell;
4212
4213 shell = container_of(listener, struct desktop_shell,
4214 child.client_destroy_listener);
4215
Pekka Paalanen826dc142014-08-27 12:11:53 +03004216 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004217 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004218 /*
4219 * unbind_desktop_shell() will reset shell->child.desktop_shell
4220 * before the respawned process has a chance to create a new
4221 * desktop_shell object, because we are being called from the
4222 * wl_client destructor which destroys all wl_resources before
4223 * returning.
4224 */
4225
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004226 if (!check_desktop_shell_crash_too_early(shell))
4227 respawn_desktop_shell_process(shell);
4228
Pekka Paalanen826dc142014-08-27 12:11:53 +03004229 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004230}
4231
4232static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004233launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004234{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004235 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004236
Pekka Paalanen826dc142014-08-27 12:11:53 +03004237 shell->child.client = weston_client_start(shell->compositor,
4238 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004239
Arnaud Vrac42631192014-08-25 20:56:46 +02004240 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004241 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004242 return;
4243 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004244
4245 shell->child.client_destroy_listener.notify =
4246 desktop_shell_client_destroy;
4247 wl_client_add_destroy_listener(shell->child.client,
4248 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004249}
4250
4251static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004252unbind_desktop_shell(struct wl_resource *resource)
4253{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004254 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004255
4256 if (shell->locked)
4257 resume_desktop(shell);
4258
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004259 shell->child.desktop_shell = NULL;
4260 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004261}
4262
4263static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004264bind_desktop_shell(struct wl_client *client,
4265 void *data, uint32_t version, uint32_t id)
4266{
Tiago Vignattibe143262012-04-16 17:31:41 +03004267 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004268 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004269
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004270 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4271 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004272
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004273 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004274 wl_resource_set_implementation(resource,
4275 &desktop_shell_implementation,
4276 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004277 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004278 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004279 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004280
4281 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4282 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004283}
4284
Kristian Høgsberg07045392012-02-19 18:52:44 -05004285struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004286 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004287 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004288 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004289 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004290 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004291};
4292
4293static void
4294switcher_next(struct switcher *switcher)
4295{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004296 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004297 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004298 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004299 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004300
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004301 /* temporary re-display minimized surfaces */
4302 struct weston_view *tmp;
4303 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004304 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4305 weston_layer_entry_remove(&view->layer_link);
4306 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004307 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4308 *minimized = view;
4309 }
4310
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004311 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004312 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004313 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004314 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004315 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004316 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004317 next = view;
4318 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 view->alpha = 0.25;
4320 weston_view_geometry_dirty(view);
4321 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004322 }
Alex Wu1659daa2012-04-01 20:13:09 +08004323
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004324 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 view->alpha = 0.25;
4326 weston_view_geometry_dirty(view);
4327 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004328 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004329 }
4330
4331 if (next == NULL)
4332 next = first;
4333
Alex Wu07b26062012-03-12 16:06:01 +08004334 if (next == NULL)
4335 return;
4336
Kristian Høgsberg07045392012-02-19 18:52:44 -05004337 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004338 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004339
4340 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004341 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004343
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004344 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004345 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004346 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347}
4348
4349static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004350switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004351{
4352 struct switcher *switcher =
4353 container_of(listener, struct switcher, listener);
4354
4355 switcher_next(switcher);
4356}
4357
4358static void
Daniel Stone351eb612012-05-31 15:27:47 -04004359switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004360{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004362 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004363 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004364
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004365 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004366 if (is_focus_view(view))
4367 continue;
4368
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 view->alpha = 1.0;
4370 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004371 }
4372
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004373 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004374 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004375 keyboard->seat,
4376 WESTON_ACTIVATE_FLAG_CONFIGURE);
4377 }
4378
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004380 weston_keyboard_end_grab(keyboard);
4381 if (keyboard->input_method_resource)
4382 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004383
4384 /* re-hide surfaces that were temporary shown during the switch */
4385 struct weston_view **minimized;
4386 wl_array_for_each(minimized, &switcher->minimized_array) {
4387 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004388 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004389 weston_layer_entry_remove(&(*minimized)->layer_link);
4390 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004391 weston_view_damage_below(*minimized);
4392 }
4393 }
4394 wl_array_release(&switcher->minimized_array);
4395
Kristian Høgsberg07045392012-02-19 18:52:44 -05004396 free(switcher);
4397}
4398
4399static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004400switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004401 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004402{
4403 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004404 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004405
Daniel Stonec9785ea2012-05-30 16:31:52 +01004406 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004407 switcher_next(switcher);
4408}
4409
4410static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004411switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004412 uint32_t mods_depressed, uint32_t mods_latched,
4413 uint32_t mods_locked, uint32_t group)
4414{
4415 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004416 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004417
Daniel Stone351eb612012-05-31 15:27:47 -04004418 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4419 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004420}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004421
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004422static void
4423switcher_cancel(struct weston_keyboard_grab *grab)
4424{
4425 struct switcher *switcher = container_of(grab, struct switcher, grab);
4426
4427 switcher_destroy(switcher);
4428}
4429
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004430static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004431 switcher_key,
4432 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004433 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004434};
4435
4436static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004437switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4438 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004439{
Tiago Vignattibe143262012-04-16 17:31:41 +03004440 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004441 struct switcher *switcher;
4442
4443 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004444 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004445 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004446 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004447 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004448 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004449
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004450 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004451 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004452 weston_keyboard_start_grab(keyboard, &switcher->grab);
4453 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004454 switcher_next(switcher);
4455}
4456
Pekka Paalanen3c647232011-12-22 13:43:43 +02004457static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004458backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4459 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004460{
4461 struct weston_compositor *compositor = data;
4462 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004463 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004464
4465 /* TODO: we're limiting to simple use cases, where we assume just
4466 * control on the primary display. We'd have to extend later if we
4467 * ever get support for setting backlights on random desktop LCD
4468 * panels though */
4469 output = get_default_output(compositor);
4470 if (!output)
4471 return;
4472
4473 if (!output->set_backlight)
4474 return;
4475
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004476 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4477 backlight_new = output->backlight_current - 25;
4478 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4479 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004480
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004481 if (backlight_new < 5)
4482 backlight_new = 5;
4483 if (backlight_new > 255)
4484 backlight_new = 255;
4485
4486 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004487 output->set_backlight(output, output->backlight_current);
4488}
4489
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004490static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004491force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4492 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004493{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004494 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004495 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004496 struct desktop_shell *shell = data;
4497 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004498 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004499
Derek Foreman8ae2db52015-07-15 13:00:36 -05004500 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004501 if (!focus_surface)
4502 return;
4503
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004504 wl_signal_emit(&compositor->kill_signal, focus_surface);
4505
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004506 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004507 wl_client_get_credentials(client, &pid, NULL, NULL);
4508
4509 /* Skip clients that we launched ourselves (the credentials of
4510 * the socketpair is ours) */
4511 if (pid == getpid())
4512 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004513
Daniel Stone325fc2d2012-05-30 16:31:58 +01004514 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004515}
4516
4517static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004518workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004519 uint32_t key, void *data)
4520{
4521 struct desktop_shell *shell = data;
4522 unsigned int new_index = shell->workspaces.current;
4523
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004524 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004525 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004526 if (new_index != 0)
4527 new_index--;
4528
4529 change_workspace(shell, new_index);
4530}
4531
4532static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004533workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004534 uint32_t key, void *data)
4535{
4536 struct desktop_shell *shell = data;
4537 unsigned int new_index = shell->workspaces.current;
4538
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004539 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004540 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004541 if (new_index < shell->workspaces.num - 1)
4542 new_index++;
4543
4544 change_workspace(shell, new_index);
4545}
4546
4547static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004548workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004549 uint32_t key, void *data)
4550{
4551 struct desktop_shell *shell = data;
4552 unsigned int new_index;
4553
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004554 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004555 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004556 new_index = key - KEY_F1;
4557 if (new_index >= shell->workspaces.num)
4558 new_index = shell->workspaces.num - 1;
4559
4560 change_workspace(shell, new_index);
4561}
4562
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004563static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004564workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4565 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004566{
4567 struct desktop_shell *shell = data;
4568 unsigned int new_index = shell->workspaces.current;
4569
4570 if (shell->locked)
4571 return;
4572
4573 if (new_index != 0)
4574 new_index--;
4575
Derek Foreman8ae2db52015-07-15 13:00:36 -05004576 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004577}
4578
4579static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004580workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4581 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004582{
4583 struct desktop_shell *shell = data;
4584 unsigned int new_index = shell->workspaces.current;
4585
4586 if (shell->locked)
4587 return;
4588
4589 if (new_index < shell->workspaces.num - 1)
4590 new_index++;
4591
Derek Foreman8ae2db52015-07-15 13:00:36 -05004592 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004593}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004594
4595static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004596shell_reposition_view_on_output_destroy(struct weston_view *view)
4597{
4598 struct weston_output *output, *first_output;
4599 struct weston_compositor *ec = view->surface->compositor;
4600 struct shell_surface *shsurf;
4601 float x, y;
4602 int visible;
4603
4604 x = view->geometry.x;
4605 y = view->geometry.y;
4606
4607 /* At this point the destroyed output is not in the list anymore.
4608 * If the view is still visible somewhere, we leave where it is,
4609 * otherwise, move it to the first output. */
4610 visible = 0;
4611 wl_list_for_each(output, &ec->output_list, link) {
4612 if (pixman_region32_contains_point(&output->region,
4613 x, y, NULL)) {
4614 visible = 1;
4615 break;
4616 }
4617 }
4618
4619 if (!visible) {
4620 first_output = container_of(ec->output_list.next,
4621 struct weston_output, link);
4622
4623 x = first_output->x + first_output->width / 4;
4624 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004625
4626 weston_view_set_position(view, x, y);
4627 } else {
4628 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004629 }
4630
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004631
4632 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004633 if (!shsurf)
4634 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004635
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004636 shsurf->saved_position_valid = false;
4637 set_maximized(shsurf, false);
4638 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004639}
4640
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004641void
4642shell_for_each_layer(struct desktop_shell *shell,
4643 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004644{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004645 struct workspace **ws;
4646
4647 func(shell, &shell->fullscreen_layer, data);
4648 func(shell, &shell->panel_layer, data);
4649 func(shell, &shell->background_layer, data);
4650 func(shell, &shell->lock_layer, data);
4651 func(shell, &shell->input_panel_layer, data);
4652
4653 wl_array_for_each(ws, &shell->workspaces.array)
4654 func(shell, &(*ws)->layer, data);
4655}
4656
4657static void
4658shell_output_destroy_move_layer(struct desktop_shell *shell,
4659 struct weston_layer *layer,
4660 void *data)
4661{
4662 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004663 struct weston_view *view;
4664
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004665 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004666 if (view->output != output)
4667 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004668
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004669 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004670 }
4671}
4672
4673static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004674handle_output_destroy(struct wl_listener *listener, void *data)
4675{
4676 struct shell_output *output_listener =
4677 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004678 struct weston_output *output = output_listener->output;
4679 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004680
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004681 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004682
Quentin Glidic561201c2016-08-20 18:19:51 +02004683 wl_list_remove(&output_listener->panel_surface_listener.link);
4684 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004685 wl_list_remove(&output_listener->destroy_listener.link);
4686 wl_list_remove(&output_listener->link);
4687 free(output_listener);
4688}
4689
4690static void
David Fort50d962f2016-06-09 17:55:52 +02004691shell_resize_surface_to_output(struct desktop_shell *shell,
4692 struct weston_surface *surface,
4693 const struct weston_output *output)
4694{
4695 if (!surface)
4696 return;
4697
4698 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4699 surface->resource,
4700 output->width,
4701 output->height);
4702}
4703
4704
4705static void
4706handle_output_resized(struct wl_listener *listener, void *data)
4707{
4708 struct desktop_shell *shell =
4709 container_of(listener, struct desktop_shell, resized_listener);
4710 struct weston_output *output = (struct weston_output *)data;
4711 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4712
4713 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4714 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4715}
4716
4717static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004718create_shell_output(struct desktop_shell *shell,
4719 struct weston_output *output)
4720{
4721 struct shell_output *shell_output;
4722
4723 shell_output = zalloc(sizeof *shell_output);
4724 if (shell_output == NULL)
4725 return;
4726
4727 shell_output->output = output;
4728 shell_output->shell = shell;
4729 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004730 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004731 wl_signal_add(&output->destroy_signal,
4732 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004733 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004734}
4735
4736static void
4737handle_output_create(struct wl_listener *listener, void *data)
4738{
4739 struct desktop_shell *shell =
4740 container_of(listener, struct desktop_shell, output_create_listener);
4741 struct weston_output *output = (struct weston_output *)data;
4742
4743 create_shell_output(shell, output);
4744}
4745
4746static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004747handle_output_move_layer(struct desktop_shell *shell,
4748 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004749{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004750 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004751 struct weston_view *view;
4752 float x, y;
4753
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004754 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004755 if (view->output != output)
4756 continue;
4757
4758 x = view->geometry.x + output->move_x;
4759 y = view->geometry.y + output->move_y;
4760 weston_view_set_position(view, x, y);
4761 }
4762}
4763
4764static void
4765handle_output_move(struct wl_listener *listener, void *data)
4766{
4767 struct desktop_shell *shell;
4768
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004769 shell = container_of(listener, struct desktop_shell,
4770 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004771
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004772 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004773}
4774
4775static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004776setup_output_destroy_handler(struct weston_compositor *ec,
4777 struct desktop_shell *shell)
4778{
4779 struct weston_output *output;
4780
4781 wl_list_init(&shell->output_list);
4782 wl_list_for_each(output, &ec->output_list, link)
4783 create_shell_output(shell, output);
4784
4785 shell->output_create_listener.notify = handle_output_create;
4786 wl_signal_add(&ec->output_created_signal,
4787 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004788
4789 shell->output_move_listener.notify = handle_output_move;
4790 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004791}
4792
4793static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004794shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004795{
Tiago Vignattibe143262012-04-16 17:31:41 +03004796 struct desktop_shell *shell =
4797 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004798 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004799 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004800
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004801 /* Force state to unlocked so we don't try to fade */
4802 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004803
4804 if (shell->child.client) {
4805 /* disable respawn */
4806 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004807 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004808 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004809
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004810 wl_list_remove(&shell->idle_listener.link);
4811 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004812 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004813
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004814 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004815 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004816
Xiong Zhang6b481422013-10-23 13:58:32 +08004817 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4818 wl_list_remove(&shell_output->destroy_listener.link);
4819 wl_list_remove(&shell_output->link);
4820 free(shell_output);
4821 }
4822
4823 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004824 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004825 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004826
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004827 wl_array_for_each(ws, &shell->workspaces.array)
4828 workspace_destroy(*ws);
4829 wl_array_release(&shell->workspaces.array);
4830
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004831 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004832 free(shell);
4833}
4834
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004835static void
4836shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4837{
4838 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004839 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004840
Bob Ham744e6532016-01-12 10:21:48 +00004841 if (shell->allow_zap)
4842 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4843 MODIFIER_CTRL | MODIFIER_ALT,
4844 terminate_binding, ec);
4845
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004846 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004847 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4848 click_to_activate_binding,
4849 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004850 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4851 click_to_activate_binding,
4852 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004853 weston_compositor_add_touch_binding(ec, 0,
4854 touch_to_activate_binding,
4855 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004856 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4857 MODIFIER_SUPER | MODIFIER_ALT,
4858 surface_opacity_binding, NULL);
4859 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4860 MODIFIER_SUPER, zoom_axis_binding,
4861 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004862 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4863 backlight_binding, ec);
4864 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4865 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004866
4867 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004868 if (shell->exposay_modifier)
4869 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4870 exposay_binding, shell);
4871
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004872 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004873 if (!mod)
4874 return;
4875
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4877 zoom_key_binding, NULL);
4878 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4879 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004880 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4881 maximize_binding, NULL);
4882 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4883 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004884 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4885 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004886 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004887 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004888 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004889 weston_compositor_add_button_binding(ec, BTN_LEFT,
4890 mod | MODIFIER_SHIFT,
4891 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004892
4893 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004894 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004895 rotate_binding, NULL);
4896
Daniel Stone325fc2d2012-05-30 16:31:58 +01004897 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4898 shell);
4899 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4900 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004901 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4902 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004903 weston_compositor_add_key_binding(ec, KEY_K, mod,
4904 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004905 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4906 workspace_up_binding, shell);
4907 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4908 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004909 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4910 workspace_move_surface_up_binding,
4911 shell);
4912 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4913 workspace_move_surface_down_binding,
4914 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004915
4916 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4917 if (shell->workspaces.num > 1) {
4918 num_workspace_bindings = shell->workspaces.num;
4919 if (num_workspace_bindings > 6)
4920 num_workspace_bindings = 6;
4921 for (i = 0; i < num_workspace_bindings; i++)
4922 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4923 workspace_f_binding,
4924 shell);
4925 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004926
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004927 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004928}
4929
Jason Ekstrand024177c2014-04-21 19:42:58 -05004930static void
4931handle_seat_created(struct wl_listener *listener, void *data)
4932{
4933 struct weston_seat *seat = data;
4934
4935 create_shell_seat(seat);
4936}
4937
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004938WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004939wet_shell_init(struct weston_compositor *ec,
4940 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004941{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004942 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004943 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004944 struct workspace **pws;
4945 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004946 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004947
Peter Huttererf3d62272013-08-08 11:57:05 +10004948 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004949 if (shell == NULL)
4950 return -1;
4951
Kristian Høgsberg75840622011-09-06 13:48:16 -04004952 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004953
4954 shell->destroy_listener.notify = shell_destroy;
4955 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004956 shell->idle_listener.notify = idle_handler;
4957 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4958 shell->wake_listener.notify = wake_handler;
4959 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004960 shell->transform_listener.notify = transform_handler;
4961 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004962
Quentin Glidic82681572016-12-17 13:40:51 +01004963 weston_layer_init(&shell->fullscreen_layer, ec);
4964 weston_layer_init(&shell->panel_layer, ec);
4965 weston_layer_init(&shell->background_layer, ec);
4966 weston_layer_init(&shell->lock_layer, ec);
4967 weston_layer_init(&shell->input_panel_layer, ec);
4968
4969 weston_layer_set_position(&shell->fullscreen_layer,
4970 WESTON_LAYER_POSITION_FULLSCREEN);
4971 weston_layer_set_position(&shell->panel_layer,
4972 WESTON_LAYER_POSITION_UI);
4973 weston_layer_set_position(&shell->background_layer,
4974 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004975
4976 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004977 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004978
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004979 if (input_panel_setup(shell) < 0)
4980 return -1;
4981
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004982 shell->text_backend = text_backend_init(ec);
4983 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004984 return -1;
4985
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004986 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004987
Daniel Stonedf8133b2013-11-19 11:37:14 +01004988 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4989 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4990
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004991 for (i = 0; i < shell->workspaces.num; i++) {
4992 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4993 if (pws == NULL)
4994 return -1;
4995
Quentin Glidic82681572016-12-17 13:40:51 +01004996 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004997 if (*pws == NULL)
4998 return -1;
4999 }
5000 activate_workspace(shell, 0);
5001
Quentin Glidic82681572016-12-17 13:40:51 +01005002 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005003
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005004 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005005 wl_list_init(&shell->workspaces.animation.link);
5006 shell->workspaces.animation.frame = animate_workspace_change_frame;
5007
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005008 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5009 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005010 return -1;
5011
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005012 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005013 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005014 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005015 return -1;
5016
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005017 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005018
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005019 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005020
Xiong Zhang6b481422013-10-23 13:58:32 +08005021 setup_output_destroy_handler(ec, shell);
5022
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005023 loop = wl_display_get_event_loop(ec->wl_display);
5024 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005025
Jason Ekstrand024177c2014-04-21 19:42:58 -05005026 wl_list_for_each(seat, &ec->seat_list, link)
5027 handle_seat_created(NULL, seat);
5028 shell->seat_create_listener.notify = handle_seat_created;
5029 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005030
David Fort50d962f2016-06-09 17:55:52 +02005031 shell->resized_listener.notify = handle_output_resized;
5032 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5033
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005034 screenshooter_create(ec);
5035
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005036 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005037
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005038 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005039
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005040 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5041
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005042 return 0;
5043}