blob: bcde3fbf2edcc657247980fd84fcbe1db2fac072 [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 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800110 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500113 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600114 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200115 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700116 struct wl_listener resource_destroy_listener;
117
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400118 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000119 struct wl_list children_list; /* child surfaces of this one */
120 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300121 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200122
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800123 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400124 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500125 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200126 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800127 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800129 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700130 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800131 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100132
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200134 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500135 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 } rotation;
137
138 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100139 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500140 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400142 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 } popup;
144
Alex Wu4539b082012-03-01 12:57:46 +0800145 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300146 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400147 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 } transient;
149
150 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800151 enum wl_shell_surface_fullscreen_method type;
152 struct weston_transform transform; /* matrix from x, y */
153 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500154 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800155 } fullscreen;
156
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200157 struct weston_transform workspace_transform;
158
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500159 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700165 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400175 int32_t x, y, width, height;
176 } geometry, next_geometry;
177 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700178
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200180};
181
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300182struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400183 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300184 struct shell_surface *shsurf;
185 struct wl_listener shsurf_destroy_listener;
186};
187
Rusty Lynch1084da52013-08-15 09:10:08 -0700188struct shell_touch_grab {
189 struct weston_touch_grab grab;
190 struct shell_surface *shsurf;
191 struct wl_listener shsurf_destroy_listener;
192 struct weston_touch *touch;
193};
194
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300195struct weston_move_grab {
196 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100197 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700198 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199};
200
Rusty Lynch1084da52013-08-15 09:10:08 -0700201struct weston_touch_move_grab {
202 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800203 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700204 wl_fixed_t dx, dy;
205};
206
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500209 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200211 float x;
212 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 } center;
214};
215
Giulio Camuffo5085a752013-03-25 21:42:45 +0100216struct shell_seat {
217 struct weston_seat *seat;
218 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500219 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220
Jason Ekstrand024177c2014-04-21 19:42:58 -0500221 struct wl_listener caps_changed_listener;
222 struct wl_listener pointer_focus_listener;
223 struct wl_listener keyboard_focus_listener;
224
Giulio Camuffo5085a752013-03-25 21:42:45 +0100225 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400226 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200227 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100228 struct wl_list surfaces_list;
229 struct wl_client *client;
230 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200231 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 } popup_grab;
233};
234
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800235struct shell_client {
236 struct wl_resource *resource;
237 struct wl_client *client;
238 struct desktop_shell *shell;
239 struct wl_listener destroy_listener;
240 struct wl_event_source *ping_timer;
241 uint32_t ping_serial;
242 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800243 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800244};
245
Alex Wubd3354b2012-04-17 17:20:49 +0800246static struct desktop_shell *
247shell_surface_get_shell(struct shell_surface *shsurf);
248
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500249static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400250surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500251
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300252static void
253shell_fade_startup(struct desktop_shell *shell);
254
Philip Withnallbecb77e2013-11-25 18:01:30 +0000255static struct shell_seat *
256get_shell_seat(struct weston_seat *seat);
257
Jonny Lambd73c6942014-08-20 15:53:20 +0200258static void
259get_output_panel_size(struct desktop_shell *shell,
260 struct weston_output *output,
261 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700262
Philip Withnall648a4dd2013-11-25 18:01:44 +0000263static void
264shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
265
Alex Wubd3354b2012-04-17 17:20:49 +0800266static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200267shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
268
269static bool
270shell_surface_is_xdg_surface(struct shell_surface *shsurf);
271
272static bool
273shell_surface_is_xdg_popup(struct shell_surface *shsurf);
274
275static void
276shell_surface_set_parent(struct shell_surface *shsurf,
277 struct weston_surface *parent);
278
Pekka Paalanen8274d902014-08-06 19:36:51 +0300279static int
280shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
281{
282 struct shell_surface *shsurf;
283 const char *typestr[] = {
284 [SHELL_SURFACE_NONE] = "unidentified",
285 [SHELL_SURFACE_TOPLEVEL] = "top-level",
286 [SHELL_SURFACE_POPUP] = "popup",
287 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
288 };
289 const char *t, *c;
290
291 shsurf = get_shell_surface(surface);
292 if (!shsurf)
293 return snprintf(buf, len, "unidentified window");
294
295 t = shsurf->title;
296 c = shsurf->class;
297
298 return snprintf(buf, len, "%s window%s%s%s%s%s",
299 typestr[shsurf->type],
300 t ? " '" : "", t ?: "", t ? "'" : "",
301 c ? " of " : "", c ?: "");
302}
303
Rafael Antognollie2a34552013-12-03 15:35:45 -0200304static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800305shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
306{
307 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500308 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800309
310 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100311
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300312 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800313 return false;
314
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300315 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500316 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800319}
320
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500321static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400322destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323{
324 struct shell_grab *grab;
325
326 grab = container_of(listener, struct shell_grab,
327 shsurf_destroy_listener);
328
329 grab->shsurf = NULL;
330}
331
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800332struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333get_default_view(struct weston_surface *surface)
334{
335 struct shell_surface *shsurf;
336 struct weston_view *view;
337
338 if (!surface || wl_list_empty(&surface->views))
339 return NULL;
340
341 shsurf = get_shell_surface(surface);
342 if (shsurf)
343 return shsurf->view;
344
345 wl_list_for_each(view, &surface->views, surface_link)
346 if (weston_view_is_mapped(view))
347 return view;
348
349 return container_of(surface->views.next, struct weston_view, surface_link);
350}
351
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400353popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200354static void
355touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400356
357static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300358shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400359 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300360 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400361 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300363{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364 struct desktop_shell *shell = shsurf->shell;
365
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400366 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200367 if (pointer->seat->touch)
368 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400369
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300370 grab->grab.interface = interface;
371 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400372 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500373 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400374 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300375
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400378 if (shell->child.desktop_shell) {
379 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
380 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500381 weston_pointer_set_focus(pointer,
382 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400383 wl_fixed_from_int(0),
384 wl_fixed_from_int(0));
385 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386}
387
Jonny Lambd73c6942014-08-20 15:53:20 +0200388static void
389get_output_panel_size(struct desktop_shell *shell,
390 struct weston_output *output,
391 int *width,
392 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700393{
394 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200395
396 *width = 0;
397 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700398
399 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200400 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700401
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300402 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200403 float x, y;
404
405 if (view->surface->output != output)
406 continue;
407
408 switch (shell->panel_position) {
409 case DESKTOP_SHELL_PANEL_POSITION_TOP:
410 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
411
412 weston_view_to_global_float(view,
413 view->surface->width, 0,
414 &x, &y);
415
416 *width = (int) x;
417 *height = view->surface->height + (int) y;
418 return;
419
420 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
421 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
422 weston_view_to_global_float(view,
423 0, view->surface->height,
424 &x, &y);
425
426 *width = view->surface->width + (int) x;
427 *height = (int) y;
428 return;
429
430 default:
431 /* we've already set width and height to
432 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700433 break;
434 }
435 }
436
Jonny Lambd73c6942014-08-20 15:53:20 +0200437 /* the correct view wasn't found */
438}
439
440static void
441get_output_work_area(struct desktop_shell *shell,
442 struct weston_output *output,
443 pixman_rectangle32_t *area)
444{
445 int32_t panel_width = 0, panel_height = 0;
446
447 area->x = 0;
448 area->y = 0;
449
450 get_output_panel_size(shell, output, &panel_width, &panel_height);
451
452 switch (shell->panel_position) {
453 case DESKTOP_SHELL_PANEL_POSITION_TOP:
454 default:
455 area->y = panel_height;
456 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
457 area->width = output->width;
458 area->height = output->height - panel_height;
459 break;
460 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
461 area->x = panel_width;
462 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
463 area->width = output->width - panel_width;
464 area->height = output->height;
465 break;
466 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700467}
468
469static void
470send_configure_for_surface(struct shell_surface *shsurf)
471{
472 int32_t width, height;
473 struct surface_state *state;
474
475 if (shsurf->state_requested)
476 state = &shsurf->requested_state;
477 else if (shsurf->state_changed)
478 state = &shsurf->next_state;
479 else
480 state = &shsurf->state;
481
482 if (state->fullscreen) {
483 width = shsurf->output->width;
484 height = shsurf->output->height;
485 } else if (state->maximized) {
486 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200487 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700488
489 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200490 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700491
Jonny Lambd73c6942014-08-20 15:53:20 +0200492 width = area.width;
493 height = area.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700494 } else {
495 width = 0;
496 height = 0;
497 }
498
499 shsurf->client->send_configure(shsurf->surface, width, height);
500}
501
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300502static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400503shell_surface_state_changed(struct shell_surface *shsurf)
504{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700505 if (shell_surface_is_xdg_surface(shsurf))
506 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400507}
508
509static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300510shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300511{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700512 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400513 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700514 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400515
516 if (grab->shsurf->resize_edges) {
517 grab->shsurf->resize_edges = 0;
518 shell_surface_state_changed(grab->shsurf);
519 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700520 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300521
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700522 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300523}
524
525static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700526shell_touch_grab_start(struct shell_touch_grab *grab,
527 const struct weston_touch_grab_interface *interface,
528 struct shell_surface *shsurf,
529 struct weston_touch *touch)
530{
531 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800532
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200533 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700534 if (touch->seat->pointer)
535 popup_grab_end(touch->seat->pointer);
536
Rusty Lynch1084da52013-08-15 09:10:08 -0700537 grab->grab.interface = interface;
538 grab->shsurf = shsurf;
539 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
540 wl_signal_add(&shsurf->destroy_signal,
541 &grab->shsurf_destroy_listener);
542
543 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700544 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700545
546 weston_touch_start_grab(touch, &grab->grab);
547 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500548 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500549 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700550}
551
552static void
553shell_touch_grab_end(struct shell_touch_grab *grab)
554{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700555 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700556 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700557 grab->shsurf->grabbed = 0;
558 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700559
560 weston_touch_end_grab(grab->touch);
561}
562
563static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500564center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800565 struct weston_output *output);
566
Daniel Stone496ca172012-05-30 16:31:42 +0100567static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300568get_modifier(char *modifier)
569{
570 if (!modifier)
571 return MODIFIER_SUPER;
572
573 if (!strcmp("ctrl", modifier))
574 return MODIFIER_CTRL;
575 else if (!strcmp("alt", modifier))
576 return MODIFIER_ALT;
577 else if (!strcmp("super", modifier))
578 return MODIFIER_SUPER;
579 else
580 return MODIFIER_SUPER;
581}
582
Juan Zhaoe10d2792012-04-25 19:09:52 +0800583static enum animation_type
584get_animation_type(char *animation)
585{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800586 if (!animation)
587 return ANIMATION_NONE;
588
Juan Zhaoe10d2792012-04-25 19:09:52 +0800589 if (!strcmp("zoom", animation))
590 return ANIMATION_ZOOM;
591 else if (!strcmp("fade", animation))
592 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100593 else if (!strcmp("dim-layer", animation))
594 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800595 else
596 return ANIMATION_NONE;
597}
598
Alex Wu4539b082012-03-01 12:57:46 +0800599static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400600shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200601{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400602 struct weston_config_section *section;
603 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500604 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300605 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200606
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400607 section = weston_config_get_section(shell->compositor->config,
608 "screensaver", NULL, NULL);
609 weston_config_section_get_string(section,
610 "path", &shell->screensaver.path, NULL);
611 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200612 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400613
614 section = weston_config_get_section(shell->compositor->config,
615 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300616 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
617 WESTON_SHELL_CLIENT);
618 if (ret < 0)
619 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400620 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500621 "client", &s, client);
622 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100623 shell->client = s;
624 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400625 "binding-modifier", &s, "super");
626 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200627 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800628
629 weston_config_section_get_string(section,
630 "exposay-modifier", &s, "none");
631 if (strcmp(s, "none") == 0)
632 shell->exposay_modifier = 0;
633 else
634 shell->exposay_modifier = get_modifier(s);
635 free(s);
636
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400637 weston_config_section_get_string(section, "animation", &s, "none");
638 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200639 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200640 weston_config_section_get_string(section, "close-animation", &s, "fade");
641 shell->win_close_animation_type = get_animation_type(s);
642 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700643 weston_config_section_get_string(section,
644 "startup-animation", &s, "fade");
645 shell->startup_animation_type = get_animation_type(s);
646 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700647 if (shell->startup_animation_type == ANIMATION_ZOOM)
648 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100649 weston_config_section_get_string(section, "focus-animation", &s, "none");
650 shell->focus_animation_type = get_animation_type(s);
651 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400652 weston_config_section_get_uint(section, "num-workspaces",
653 &shell->workspaces.num,
654 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200655}
656
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800657struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100658get_default_output(struct weston_compositor *compositor)
659{
660 return container_of(compositor->output_list.next,
661 struct weston_output, link);
662}
663
Pekka Paalanen8274d902014-08-06 19:36:51 +0300664static int
665focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
666{
667 return snprintf(buf, len, "focus highlight effect for output %s",
668 surface->output->name);
669}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100670
671/* no-op func for checking focus surface */
672static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600673focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100674{
675}
676
677static struct focus_surface *
678get_focus_surface(struct weston_surface *surface)
679{
680 if (surface->configure == focus_surface_configure)
681 return surface->configure_private;
682 else
683 return NULL;
684}
685
686static bool
687is_focus_surface (struct weston_surface *es)
688{
689 return (es->configure == focus_surface_configure);
690}
691
692static bool
693is_focus_view (struct weston_view *view)
694{
695 return is_focus_surface (view->surface);
696}
697
698static struct focus_surface *
699create_focus_surface(struct weston_compositor *ec,
700 struct weston_output *output)
701{
702 struct focus_surface *fsurf = NULL;
703 struct weston_surface *surface = NULL;
704
705 fsurf = malloc(sizeof *fsurf);
706 if (!fsurf)
707 return NULL;
708
709 fsurf->surface = weston_surface_create(ec);
710 surface = fsurf->surface;
711 if (surface == NULL) {
712 free(fsurf);
713 return NULL;
714 }
715
716 surface->configure = focus_surface_configure;
717 surface->output = output;
718 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300719 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100720
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800721 fsurf->view = weston_view_create(surface);
722 if (fsurf->view == NULL) {
723 weston_surface_destroy(surface);
724 free(fsurf);
725 return NULL;
726 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100727 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100728
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600729 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600730 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100731 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
732 pixman_region32_fini(&surface->opaque);
733 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
734 output->width, output->height);
735 pixman_region32_fini(&surface->input);
736 pixman_region32_init(&surface->input);
737
738 wl_list_init(&fsurf->workspace_transform.link);
739
740 return fsurf;
741}
742
743static void
744focus_surface_destroy(struct focus_surface *fsurf)
745{
746 weston_surface_destroy(fsurf->surface);
747 free(fsurf);
748}
749
750static void
751focus_animation_done(struct weston_view_animation *animation, void *data)
752{
753 struct workspace *ws = data;
754
755 ws->focus_animation = NULL;
756}
757
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200758static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200759focus_state_destroy(struct focus_state *state)
760{
761 wl_list_remove(&state->seat_destroy_listener.link);
762 wl_list_remove(&state->surface_destroy_listener.link);
763 free(state);
764}
765
766static void
767focus_state_seat_destroy(struct wl_listener *listener, void *data)
768{
769 struct focus_state *state = container_of(listener,
770 struct focus_state,
771 seat_destroy_listener);
772
773 wl_list_remove(&state->link);
774 focus_state_destroy(state);
775}
776
777static void
778focus_state_surface_destroy(struct wl_listener *listener, void *data)
779{
780 struct focus_state *state = container_of(listener,
781 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400782 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400783 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500784 struct weston_surface *main_surface, *next;
785 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200786
Pekka Paalanen01388e22013-04-25 13:57:44 +0300787 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
788
Kristian Høgsberge3778222012-07-31 17:29:30 -0400789 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300790 wl_list_for_each(view,
791 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400793 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100794 if (is_focus_view(view))
795 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400796
Jason Ekstranda7af7042013-10-12 22:38:11 -0500797 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400798 break;
799 }
800
Pekka Paalanen01388e22013-04-25 13:57:44 +0300801 /* if the focus was a sub-surface, activate its main surface */
802 if (main_surface != state->keyboard_focus)
803 next = main_surface;
804
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100805 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400806 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100807 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100808 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400809 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100810 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
811 if (state->ws->focus_animation)
812 weston_view_animation_destroy(state->ws->focus_animation);
813
814 state->ws->focus_animation = weston_fade_run(
815 state->ws->fsurf_front->view,
816 state->ws->fsurf_front->view->alpha, 0.0, 300,
817 focus_animation_done, state->ws);
818 }
819
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 wl_list_remove(&state->link);
821 focus_state_destroy(state);
822 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200823}
824
825static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400826focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200827{
Jonas Ådahl04769742012-06-13 00:01:24 +0200828 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200829
830 state = malloc(sizeof *state);
831 if (state == NULL)
832 return NULL;
833
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400835 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200836 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400837 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200838
839 state->seat_destroy_listener.notify = focus_state_seat_destroy;
840 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400841 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200842 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400843 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200844
845 return state;
846}
847
Jonas Ådahl8538b222012-08-29 22:13:03 +0200848static struct focus_state *
849ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
850{
851 struct workspace *ws = get_current_workspace(shell);
852 struct focus_state *state;
853
854 wl_list_for_each(state, &ws->focus_list, link)
855 if (state->seat == seat)
856 break;
857
858 if (&state->link == &ws->focus_list)
859 state = focus_state_create(seat, ws);
860
861 return state;
862}
863
Jonas Ådahl04769742012-06-13 00:01:24 +0200864static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800865focus_state_set_focus(struct focus_state *state,
866 struct weston_surface *surface)
867{
868 if (state->keyboard_focus) {
869 wl_list_remove(&state->surface_destroy_listener.link);
870 wl_list_init(&state->surface_destroy_listener.link);
871 }
872
873 state->keyboard_focus = surface;
874 if (surface)
875 wl_signal_add(&surface->destroy_signal,
876 &state->surface_destroy_listener);
877}
878
879static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400880restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200881{
882 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400883 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100884 struct wl_list pending_seat_list;
885 struct weston_seat *seat, *next_seat;
886
887 /* Temporarily steal the list of seats so that we can keep
888 * track of the seats we've already processed */
889 wl_list_init(&pending_seat_list);
890 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
891 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200892
893 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100894 wl_list_remove(&state->seat->link);
895 wl_list_insert(&shell->compositor->seat_list,
896 &state->seat->link);
897
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800898 if (state->seat->keyboard == NULL)
899 continue;
900
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400901 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200902
Kristian Høgsberge3148752013-05-06 23:19:49 -0400903 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200904 }
Neil Roberts4237f502014-04-09 16:33:31 +0100905
906 /* For any remaining seats that we don't have a focus state
907 * for we'll reset the keyboard focus to NULL */
908 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
909 wl_list_insert(&shell->compositor->seat_list, &seat->link);
910
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300911 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100912 continue;
913
914 weston_keyboard_set_focus(seat->keyboard, NULL);
915 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200916}
917
918static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200919replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
920 struct weston_seat *seat)
921{
922 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200923
924 wl_list_for_each(state, &ws->focus_list, link) {
925 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800926 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927 return;
928 }
929 }
930}
931
932static void
933drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
934 struct weston_surface *surface)
935{
936 struct focus_state *state;
937
938 wl_list_for_each(state, &ws->focus_list, link)
939 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800940 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200941}
942
943static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100944animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
945 struct weston_view *from, struct weston_view *to)
946{
947 struct weston_output *output;
948 bool focus_surface_created = false;
949
950 /* FIXME: Only support dim animation using two layers */
951 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
952 return;
953
954 output = get_default_output(shell->compositor);
955 if (ws->fsurf_front == NULL && (from || to)) {
956 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800957 if (ws->fsurf_front == NULL)
958 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100959 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800960
961 ws->fsurf_back = create_focus_surface(shell->compositor, output);
962 if (ws->fsurf_back == NULL) {
963 focus_surface_destroy(ws->fsurf_front);
964 return;
965 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800967
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 focus_surface_created = true;
969 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300970 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
971 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 }
973
974 if (ws->focus_animation) {
975 weston_view_animation_destroy(ws->focus_animation);
976 ws->focus_animation = NULL;
977 }
978
979 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300980 weston_layer_entry_insert(&to->layer_link,
981 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100982 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300983 weston_layer_entry_insert(&ws->layer.view_list,
984 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985
986 if (focus_surface_created) {
987 ws->focus_animation = weston_fade_run(
988 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200989 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 focus_animation_done, ws);
991 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 weston_layer_entry_insert(&from->layer_link,
993 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994 ws->focus_animation = weston_stable_fade_run(
995 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200996 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 focus_animation_done, ws);
998 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300999 weston_layer_entry_insert(&ws->layer.view_list,
1000 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 ws->focus_animation = weston_stable_fade_run(
1002 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001003 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 focus_animation_done, ws);
1005 }
1006}
1007
1008static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001009workspace_destroy(struct workspace *ws)
1010{
Jonas Ådahl04769742012-06-13 00:01:24 +02001011 struct focus_state *state, *next;
1012
1013 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1014 focus_state_destroy(state);
1015
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 if (ws->fsurf_front)
1017 focus_surface_destroy(ws->fsurf_front);
1018 if (ws->fsurf_back)
1019 focus_surface_destroy(ws->fsurf_back);
1020
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001021 free(ws);
1022}
1023
Jonas Ådahl04769742012-06-13 00:01:24 +02001024static void
1025seat_destroyed(struct wl_listener *listener, void *data)
1026{
1027 struct weston_seat *seat = data;
1028 struct focus_state *state, *next;
1029 struct workspace *ws = container_of(listener,
1030 struct workspace,
1031 seat_destroyed_listener);
1032
1033 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1034 if (state->seat == seat)
1035 wl_list_remove(&state->link);
1036}
1037
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001038static struct workspace *
1039workspace_create(void)
1040{
1041 struct workspace *ws = malloc(sizeof *ws);
1042 if (ws == NULL)
1043 return NULL;
1044
1045 weston_layer_init(&ws->layer, NULL);
1046
Jonas Ådahl04769742012-06-13 00:01:24 +02001047 wl_list_init(&ws->focus_list);
1048 wl_list_init(&ws->seat_destroyed_listener.link);
1049 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001050 ws->fsurf_front = NULL;
1051 ws->fsurf_back = NULL;
1052 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001053
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001054 return ws;
1055}
1056
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057static int
1058workspace_is_empty(struct workspace *ws)
1059{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001060 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061}
1062
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001063static struct workspace *
1064get_workspace(struct desktop_shell *shell, unsigned int index)
1065{
1066 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001067 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001068 pws += index;
1069 return *pws;
1070}
1071
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001072struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001073get_current_workspace(struct desktop_shell *shell)
1074{
1075 return get_workspace(shell, shell->workspaces.current);
1076}
1077
1078static void
1079activate_workspace(struct desktop_shell *shell, unsigned int index)
1080{
1081 struct workspace *ws;
1082
1083 ws = get_workspace(shell, index);
1084 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1085
1086 shell->workspaces.current = index;
1087}
1088
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089static unsigned int
1090get_output_height(struct weston_output *output)
1091{
1092 return abs(output->region.extents.y1 - output->region.extents.y2);
1093}
1094
1095static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001096view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098 struct weston_transform *transform;
1099
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001100 if (is_focus_view(view)) {
1101 struct focus_surface *fsurf = get_focus_surface(view->surface);
1102 transform = &fsurf->workspace_transform;
1103 } else {
1104 struct shell_surface *shsurf = get_shell_surface(view->surface);
1105 transform = &shsurf->workspace_transform;
1106 }
1107
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001110 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001112 weston_matrix_init(&transform->matrix);
1113 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116}
1117
1118static void
1119workspace_translate_out(struct workspace *ws, double fraction)
1120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 unsigned int height;
1123 double d;
1124
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001125 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127 d = height * fraction;
1128
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001129 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 }
1131}
1132
1133static void
1134workspace_translate_in(struct workspace *ws, double fraction)
1135{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137 unsigned int height;
1138 double d;
1139
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001140 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001142
1143 if (fraction > 0)
1144 d = -(height - height * fraction);
1145 else
1146 d = height + height * fraction;
1147
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001148 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 }
1150}
1151
1152static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001153broadcast_current_workspace_state(struct desktop_shell *shell)
1154{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001155 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001156
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001157 wl_resource_for_each(resource, &shell->workspaces.client_list)
1158 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001159 shell->workspaces.current,
1160 shell->workspaces.num);
1161}
1162
1163static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001164reverse_workspace_change_animation(struct desktop_shell *shell,
1165 unsigned int index,
1166 struct workspace *from,
1167 struct workspace *to)
1168{
1169 shell->workspaces.current = index;
1170
1171 shell->workspaces.anim_to = to;
1172 shell->workspaces.anim_from = from;
1173 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1174 shell->workspaces.anim_timestamp = 0;
1175
Scott Moreau4272e632012-08-13 09:58:41 -06001176 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001177}
1178
1179static void
1180workspace_deactivate_transforms(struct workspace *ws)
1181{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001185 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001186 if (is_focus_view(view)) {
1187 struct focus_surface *fsurf = get_focus_surface(view->surface);
1188 transform = &fsurf->workspace_transform;
1189 } else {
1190 struct shell_surface *shsurf = get_shell_surface(view->surface);
1191 transform = &shsurf->workspace_transform;
1192 }
1193
1194 if (!wl_list_empty(&transform->link)) {
1195 wl_list_remove(&transform->link);
1196 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199 }
1200}
1201
1202static void
1203finish_workspace_change_animation(struct desktop_shell *shell,
1204 struct workspace *from,
1205 struct workspace *to)
1206{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001207 struct weston_view *view;
1208
Scott Moreau4272e632012-08-13 09:58:41 -06001209 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001211 /* Views that extend past the bottom of the output are still
1212 * visible after the workspace animation ends but before its layer
1213 * is hidden. In that case, we need to damage below those views so
1214 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001215 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001216 weston_view_damage_below(view);
1217
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001218 wl_list_remove(&shell->workspaces.animation.link);
1219 workspace_deactivate_transforms(from);
1220 workspace_deactivate_transforms(to);
1221 shell->workspaces.anim_to = NULL;
1222
1223 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1224}
1225
1226static void
1227animate_workspace_change_frame(struct weston_animation *animation,
1228 struct weston_output *output, uint32_t msecs)
1229{
1230 struct desktop_shell *shell =
1231 container_of(animation, struct desktop_shell,
1232 workspaces.animation);
1233 struct workspace *from = shell->workspaces.anim_from;
1234 struct workspace *to = shell->workspaces.anim_to;
1235 uint32_t t;
1236 double x, y;
1237
1238 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1239 finish_workspace_change_animation(shell, from, to);
1240 return;
1241 }
1242
1243 if (shell->workspaces.anim_timestamp == 0) {
1244 if (shell->workspaces.anim_current == 0.0)
1245 shell->workspaces.anim_timestamp = msecs;
1246 else
1247 shell->workspaces.anim_timestamp =
1248 msecs -
1249 /* Invers of movement function 'y' below. */
1250 (asin(1.0 - shell->workspaces.anim_current) *
1251 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1252 M_2_PI);
1253 }
1254
1255 t = msecs - shell->workspaces.anim_timestamp;
1256
1257 /*
1258 * x = [0, π/2]
1259 * y(x) = sin(x)
1260 */
1261 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1262 y = sin(x);
1263
1264 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001265 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001266
1267 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1268 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1269 shell->workspaces.anim_current = y;
1270
Scott Moreau4272e632012-08-13 09:58:41 -06001271 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001272 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001273 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001274 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001275}
1276
1277static void
1278animate_workspace_change(struct desktop_shell *shell,
1279 unsigned int index,
1280 struct workspace *from,
1281 struct workspace *to)
1282{
1283 struct weston_output *output;
1284
1285 int dir;
1286
1287 if (index > shell->workspaces.current)
1288 dir = -1;
1289 else
1290 dir = 1;
1291
1292 shell->workspaces.current = index;
1293
1294 shell->workspaces.anim_dir = dir;
1295 shell->workspaces.anim_from = from;
1296 shell->workspaces.anim_to = to;
1297 shell->workspaces.anim_current = 0.0;
1298 shell->workspaces.anim_timestamp = 0;
1299
1300 output = container_of(shell->compositor->output_list.next,
1301 struct weston_output, link);
1302 wl_list_insert(&output->animation_list,
1303 &shell->workspaces.animation.link);
1304
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001305 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306
1307 workspace_translate_in(to, 0);
1308
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001309 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001310
Scott Moreau4272e632012-08-13 09:58:41 -06001311 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001312}
1313
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001314static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001315update_workspace(struct desktop_shell *shell, unsigned int index,
1316 struct workspace *from, struct workspace *to)
1317{
1318 shell->workspaces.current = index;
1319 wl_list_insert(&from->layer.link, &to->layer.link);
1320 wl_list_remove(&from->layer.link);
1321}
1322
1323static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001324change_workspace(struct desktop_shell *shell, unsigned int index)
1325{
1326 struct workspace *from;
1327 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001328 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001329
1330 if (index == shell->workspaces.current)
1331 return;
1332
1333 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001334 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001335 return;
1336
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001337 from = get_current_workspace(shell);
1338 to = get_workspace(shell, index);
1339
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001340 if (shell->workspaces.anim_from == to &&
1341 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001343 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001345 return;
1346 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001347
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001348 if (shell->workspaces.anim_to != NULL)
1349 finish_workspace_change_animation(shell,
1350 shell->workspaces.anim_from,
1351 shell->workspaces.anim_to);
1352
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001354
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001355 if (shell->focus_animation_type != ANIMATION_NONE) {
1356 wl_list_for_each(state, &from->focus_list, link)
1357 if (state->keyboard_focus)
1358 animate_focus_change(shell, from,
1359 get_default_view(state->keyboard_focus), NULL);
1360
1361 wl_list_for_each(state, &to->focus_list, link)
1362 if (state->keyboard_focus)
1363 animate_focus_change(shell, to,
1364 NULL, get_default_view(state->keyboard_focus));
1365 }
1366
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001367 if (workspace_is_empty(to) && workspace_is_empty(from))
1368 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001369 else
1370 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001371
1372 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001373}
1374
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001375static bool
1376workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1377{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001378 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001379 struct wl_list *e;
1380
1381 if (wl_list_empty(list))
1382 return false;
1383
1384 e = list->next;
1385
1386 if (e->next != list)
1387 return false;
1388
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001389 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001390}
1391
1392static void
Philip Withnall659163d2013-11-25 18:01:36 +00001393move_surface_to_workspace(struct desktop_shell *shell,
1394 struct shell_surface *shsurf,
1395 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001396{
1397 struct workspace *from;
1398 struct workspace *to;
1399 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001400 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001401 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001402
1403 if (workspace == shell->workspaces.current)
1404 return;
1405
Philip Withnall659163d2013-11-25 18:01:36 +00001406 view = get_default_view(shsurf->surface);
1407 if (!view)
1408 return;
1409
1410 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1411
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001412 if (workspace >= shell->workspaces.num)
1413 workspace = shell->workspaces.num - 1;
1414
Jonas Ådahle9d22502012-08-29 22:13:01 +02001415 from = get_current_workspace(shell);
1416 to = get_workspace(shell, workspace);
1417
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001418 weston_layer_entry_remove(&view->layer_link);
1419 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001420
Philip Withnall648a4dd2013-11-25 18:01:44 +00001421 shell_surface_update_child_surface_layers(shsurf);
1422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001424 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1425 if (!seat->keyboard)
1426 continue;
1427
1428 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001430 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001431 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001434}
1435
1436static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001437take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001438 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001439 unsigned int index)
1440{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001441 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443 struct shell_surface *shsurf;
1444 struct workspace *from;
1445 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001446 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001447
Pekka Paalanen01388e22013-04-25 13:57:44 +03001448 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 view = get_default_view(surface);
1450 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001451 index == shell->workspaces.current ||
1452 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453 return;
1454
1455 from = get_current_workspace(shell);
1456 to = get_workspace(shell, index);
1457
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001458 weston_layer_entry_remove(&view->layer_link);
1459 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001460
Philip Withnall659163d2013-11-25 18:01:36 +00001461 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001462 if (shsurf != NULL)
1463 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001464
Jonas Ådahle9d22502012-08-29 22:13:01 +02001465 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001466 drop_focus_state(shell, from, surface);
1467
1468 if (shell->workspaces.anim_from == to &&
1469 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001470 wl_list_remove(&to->layer.link);
1471 wl_list_insert(from->layer.link.prev, &to->layer.link);
1472
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001473 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474 broadcast_current_workspace_state(shell);
1475
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001476 return;
1477 }
1478
1479 if (shell->workspaces.anim_to != NULL)
1480 finish_workspace_change_animation(shell,
1481 shell->workspaces.anim_from,
1482 shell->workspaces.anim_to);
1483
1484 if (workspace_is_empty(from) &&
1485 workspace_has_only(to, surface))
1486 update_workspace(shell, index, from, to);
1487 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001488 if (shsurf != NULL &&
1489 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001490 wl_list_insert(&shell->workspaces.anim_sticky_list,
1491 &shsurf->workspace_transform.link);
1492
1493 animate_workspace_change(shell, index, from, to);
1494 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001495
1496 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001497
1498 state = ensure_focus_state(shell, seat);
1499 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001500 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001501}
1502
1503static void
1504workspace_manager_move_surface(struct wl_client *client,
1505 struct wl_resource *resource,
1506 struct wl_resource *surface_resource,
1507 uint32_t workspace)
1508{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001509 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001510 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001511 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001512 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001513 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001514
Pekka Paalanen01388e22013-04-25 13:57:44 +03001515 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001516 shell_surface = get_shell_surface(main_surface);
1517 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001519
1520 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001521}
1522
1523static const struct workspace_manager_interface workspace_manager_implementation = {
1524 workspace_manager_move_surface,
1525};
1526
1527static void
1528unbind_resource(struct wl_resource *resource)
1529{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001530 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001531}
1532
1533static void
1534bind_workspace_manager(struct wl_client *client,
1535 void *data, uint32_t version, uint32_t id)
1536{
1537 struct desktop_shell *shell = data;
1538 struct wl_resource *resource;
1539
Jason Ekstranda85118c2013-06-27 20:17:02 -05001540 resource = wl_resource_create(client,
1541 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001542
1543 if (resource == NULL) {
1544 weston_log("couldn't add workspace manager object");
1545 return;
1546 }
1547
Jason Ekstranda85118c2013-06-27 20:17:02 -05001548 wl_resource_set_implementation(resource,
1549 &workspace_manager_implementation,
1550 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001551 wl_list_insert(&shell->workspaces.client_list,
1552 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001553
1554 workspace_manager_send_state(resource,
1555 shell->workspaces.current,
1556 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001557}
1558
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001559static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001560touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1561 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1562{
1563}
1564
1565static void
1566touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1567{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001568 struct weston_touch_move_grab *move =
1569 (struct weston_touch_move_grab *) container_of(
1570 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001571
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001572 if (touch_id == 0)
1573 move->active = 0;
1574
Jonas Ådahl9484b692013-12-02 22:05:03 +01001575 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001576 shell_touch_grab_end(&move->base);
1577 free(move);
1578 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001579}
1580
1581static void
1582touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1583 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1584{
1585 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1586 struct shell_surface *shsurf = move->base.shsurf;
1587 struct weston_surface *es;
1588 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1589 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1590
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001591 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001592 return;
1593
1594 es = shsurf->surface;
1595
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001596 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001597
1598 weston_compositor_schedule_repaint(es->compositor);
1599}
1600
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001601static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001602touch_move_grab_frame(struct weston_touch_grab *grab)
1603{
1604}
1605
1606static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001607touch_move_grab_cancel(struct weston_touch_grab *grab)
1608{
1609 struct weston_touch_move_grab *move =
1610 (struct weston_touch_move_grab *) container_of(
1611 grab, struct shell_touch_grab, grab);
1612
1613 shell_touch_grab_end(&move->base);
1614 free(move);
1615}
1616
Rusty Lynch1084da52013-08-15 09:10:08 -07001617static const struct weston_touch_grab_interface touch_move_grab_interface = {
1618 touch_move_grab_down,
1619 touch_move_grab_up,
1620 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001621 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001622 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001623};
1624
1625static int
1626surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1627{
1628 struct weston_touch_move_grab *move;
1629
1630 if (!shsurf)
1631 return -1;
1632
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001633 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001634 return 0;
1635
1636 move = malloc(sizeof *move);
1637 if (!move)
1638 return -1;
1639
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001640 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001642 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001644 seat->touch->grab_y;
1645
1646 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1647 seat->touch);
1648
1649 return 0;
1650}
1651
1652static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001653noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001654{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001655}
1656
1657static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001658constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1659{
1660 struct shell_surface *shsurf = move->base.shsurf;
1661 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001662 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001663 const int safety = 50;
1664
1665 x = wl_fixed_to_int(pointer->x + move->dx);
1666 y = wl_fixed_to_int(pointer->y + move->dy);
1667
Jonny Lambd73c6942014-08-20 15:53:20 +02001668 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1669 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1670 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001671
Jonny Lambd73c6942014-08-20 15:53:20 +02001672 bottom = y + shsurf->geometry.height;
1673 if (bottom - panel_height < safety)
1674 y = panel_height + safety -
1675 shsurf->geometry.height;
1676
1677 if (move->client_initiated &&
1678 y + shsurf->geometry.y < panel_height)
1679 y = panel_height - shsurf->geometry.y;
1680 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001681
1682 *cx = x;
1683 *cy = y;
1684}
1685
1686static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001687move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1688 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001689{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001691 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001692 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001693 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001694
1695 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001696 if (!shsurf)
1697 return;
1698
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001699 constrain_position(move, &cx, &cy);
1700
1701 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001704}
1705
1706static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001707move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001708 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001709{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001710 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1711 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001712 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001713 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001714
Daniel Stone4dbadb12012-05-30 16:31:51 +01001715 if (pointer->button_count == 0 &&
1716 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001717 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001718 free(grab);
1719 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001720}
1721
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001722static void
1723move_grab_cancel(struct weston_pointer_grab *grab)
1724{
1725 struct shell_grab *shell_grab =
1726 container_of(grab, struct shell_grab, grab);
1727
1728 shell_grab_end(shell_grab);
1729 free(grab);
1730}
1731
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001732static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001733 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001734 move_grab_motion,
1735 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001736 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001737};
1738
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001740surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1741 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742{
1743 struct weston_move_grab *move;
1744
1745 if (!shsurf)
1746 return -1;
1747
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001748 if (shsurf->grabbed ||
1749 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750 return 0;
1751
1752 move = malloc(sizeof *move);
1753 if (!move)
1754 return -1;
1755
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001757 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001759 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001760 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761
1762 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001763 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764
1765 return 0;
1766}
1767
1768static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001769common_surface_move(struct wl_resource *resource,
1770 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001772 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001773 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001774 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001775
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001776 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001777 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001778 seat->pointer->button_count > 0 &&
1779 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001781 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001782 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001783 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001784 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001785 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001786 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001788 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001789 (surface_touch_move(shsurf, seat) < 0))
1790 wl_resource_post_no_memory(resource);
1791 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001792}
1793
Rafael Antognollie2a34552013-12-03 15:35:45 -02001794static void
1795shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1796 struct wl_resource *seat_resource, uint32_t serial)
1797{
1798 common_surface_move(resource, seat_resource, serial);
1799}
1800
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001801struct weston_resize_grab {
1802 struct shell_grab base;
1803 uint32_t edges;
1804 int32_t width, height;
1805};
1806
1807static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001808resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1809 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001810{
1811 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001812 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813 struct shell_surface *shsurf = resize->base.shsurf;
1814 int32_t width, height;
1815 wl_fixed_t from_x, from_y;
1816 wl_fixed_t to_x, to_y;
1817
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001818 weston_pointer_move(pointer, x, y);
1819
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001820 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821 return;
1822
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 weston_view_from_global_fixed(shsurf->view,
1824 pointer->grab_x, pointer->grab_y,
1825 &from_x, &from_y);
1826 weston_view_from_global_fixed(shsurf->view,
1827 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828
1829 width = resize->width;
1830 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1831 width += wl_fixed_to_int(from_x - to_x);
1832 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1833 width += wl_fixed_to_int(to_x - from_x);
1834 }
1835
1836 height = resize->height;
1837 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1838 height += wl_fixed_to_int(from_y - to_y);
1839 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1840 height += wl_fixed_to_int(to_y - from_y);
1841 }
1842
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001843 if (width < 1)
1844 width = 1;
1845 if (height < 1)
1846 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001847 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001848}
1849
1850static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001851send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001852{
1853 struct shell_surface *shsurf = get_shell_surface(surface);
1854
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001855 assert(shsurf);
1856
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001857 if (shsurf->resource)
1858 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001859 shsurf->resize_edges,
1860 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001861}
1862
1863static const struct weston_shell_client shell_client = {
1864 send_configure
1865};
1866
1867static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001868resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001869 uint32_t time, uint32_t button, uint32_t state_w)
1870{
1871 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001872 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001873 enum wl_pointer_button_state state = state_w;
1874
1875 if (pointer->button_count == 0 &&
1876 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1877 shell_grab_end(&resize->base);
1878 free(grab);
1879 }
1880}
1881
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001882static void
1883resize_grab_cancel(struct weston_pointer_grab *grab)
1884{
1885 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1886
1887 shell_grab_end(&resize->base);
1888 free(grab);
1889}
1890
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001891static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001892 noop_grab_focus,
1893 resize_grab_motion,
1894 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001895 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001896};
1897
Giulio Camuffob8366642013-04-25 13:57:46 +03001898/*
1899 * Returns the bounding box of a surface and all its sub-surfaces,
1900 * in the surface coordinates system. */
1901static void
1902surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1903 int32_t *y, int32_t *w, int32_t *h) {
1904 pixman_region32_t region;
1905 pixman_box32_t *box;
1906 struct weston_subsurface *subsurface;
1907
1908 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909 surface->width,
1910 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001911
1912 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1913 pixman_region32_union_rect(&region, &region,
1914 subsurface->position.x,
1915 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 subsurface->surface->width,
1917 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001918 }
1919
1920 box = pixman_region32_extents(&region);
1921 if (x)
1922 *x = box->x1;
1923 if (y)
1924 *y = box->y1;
1925 if (w)
1926 *w = box->x2 - box->x1;
1927 if (h)
1928 *h = box->y2 - box->y1;
1929
1930 pixman_region32_fini(&region);
1931}
1932
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933static int
1934surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001935 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001936{
1937 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001938 const unsigned resize_topbottom =
1939 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1940 const unsigned resize_leftright =
1941 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1942 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001943
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001944 if (shsurf->grabbed ||
1945 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946 return 0;
1947
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001948 /* Check for invalid edge combinations. */
1949 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1950 (edges & resize_topbottom) == resize_topbottom ||
1951 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001952 return 0;
1953
1954 resize = malloc(sizeof *resize);
1955 if (!resize)
1956 return -1;
1957
1958 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001959
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001960 resize->width = shsurf->geometry.width;
1961 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001962
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001963 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001964 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001965 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001966 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001967
1968 return 0;
1969}
1970
1971static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001972common_surface_resize(struct wl_resource *resource,
1973 struct wl_resource *seat_resource, uint32_t serial,
1974 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001975{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001976 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001977 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001978 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001979
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001980 if (seat->pointer == NULL ||
1981 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001982 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001983 seat->pointer->focus == NULL)
1984 return;
1985
1986 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1987 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001988 return;
1989
Kristian Høgsberge3148752013-05-06 23:19:49 -04001990 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001991 wl_resource_post_no_memory(resource);
1992}
1993
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001994static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001995shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1996 struct wl_resource *seat_resource, uint32_t serial,
1997 uint32_t edges)
1998{
1999 common_surface_resize(resource, seat_resource, serial, edges);
2000}
2001
2002static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002003busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002004{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002005 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002006 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002008 wl_fixed_t sx, sy;
2009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 view = weston_compositor_pick_view(pointer->seat->compositor,
2011 pointer->x, pointer->y,
2012 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002013
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002014 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2015 shell_grab_end(grab);
2016 free(grab);
2017 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002018}
2019
2020static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002021busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2022 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002023{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002024 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002025}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002026
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002028busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002029 uint32_t time, uint32_t button, uint32_t state)
2030{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002031 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002032 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002033 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002034
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002035 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002036 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002037 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002038 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002039 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002040 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002041 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042}
2043
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002044static void
2045busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2046{
2047 struct shell_grab *grab = (struct shell_grab *) base;
2048
2049 shell_grab_end(grab);
2050 free(grab);
2051}
2052
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002053static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002054 busy_cursor_grab_focus,
2055 busy_cursor_grab_motion,
2056 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002057 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058};
2059
2060static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002061set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062{
2063 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002064
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002065 if (pointer->grab->interface == &busy_cursor_grab_interface)
2066 return;
2067
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002068 grab = malloc(sizeof *grab);
2069 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002070 return;
2071
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002072 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2073 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002074 /* Mark the shsurf as ungrabbed so that button binding is able
2075 * to move it. */
2076 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002078
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002079static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002080end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002081{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002082 struct shell_grab *grab;
2083 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002084
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002085 wl_list_for_each(seat, &compositor->seat_list, link) {
2086 if (seat->pointer == NULL)
2087 continue;
2088
2089 grab = (struct shell_grab *) seat->pointer->grab;
2090 if (grab->grab.interface == &busy_cursor_grab_interface &&
2091 wl_resource_get_client(grab->shsurf->resource) == client) {
2092 shell_grab_end(grab);
2093 free(grab);
2094 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002095 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002096}
2097
Scott Moreau9521d5e2012-04-19 13:06:17 -06002098static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002099handle_shell_client_destroy(struct wl_listener *listener, void *data);
2100
2101static int
2102xdg_ping_timeout_handler(void *data)
2103{
2104 struct shell_client *sc = data;
2105 struct weston_seat *seat;
2106 struct shell_surface *shsurf;
2107
2108 /* Client is not responding */
2109 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002110 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2111 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2112 continue;
2113 if (seat->pointer->focus->surface->resource == NULL)
2114 continue;
2115
2116 shsurf = get_shell_surface(seat->pointer->focus->surface);
2117 if (shsurf &&
2118 wl_resource_get_client(shsurf->resource) == sc->client)
2119 set_busy_cursor(shsurf, seat->pointer);
2120 }
2121
2122 return 1;
2123}
2124
2125static void
2126handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2127{
2128 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002129 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002130 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002131 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002132
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002133 if (sc->unresponsive) {
2134 xdg_ping_timeout_handler(sc);
2135 return;
2136 }
2137
2138 sc->ping_serial = serial;
2139 loop = wl_display_get_event_loop(compositor->wl_display);
2140 if (sc->ping_timer == NULL)
2141 sc->ping_timer =
2142 wl_event_loop_add_timer(loop,
2143 xdg_ping_timeout_handler, sc);
2144 if (sc->ping_timer == NULL)
2145 return;
2146
2147 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2148
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002149 if (shell_surface_is_xdg_surface(shsurf) ||
2150 shell_surface_is_xdg_popup(shsurf))
2151 xdg_shell_send_ping(sc->resource, serial);
2152 else if (shell_surface_is_wl_shell_surface(shsurf))
2153 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002154}
2155
Scott Moreauff1db4a2012-04-17 19:06:18 -06002156static void
2157ping_handler(struct weston_surface *surface, uint32_t serial)
2158{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002159 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002160
2161 if (!shsurf)
2162 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002163 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002164 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002165 if (shsurf->surface == shsurf->shell->grab_surface)
2166 return;
2167
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002168 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002169}
2170
2171static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002172handle_pointer_focus(struct wl_listener *listener, void *data)
2173{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002174 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002176 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002177 uint32_t serial;
2178
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002180 return;
2181
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002183 serial = wl_display_next_serial(compositor->wl_display);
2184 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002185}
2186
2187static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002188shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2189{
2190 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002191 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002192}
2193
2194static void
2195shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2196{
2197 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002198 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002199}
2200
2201static void
2202handle_keyboard_focus(struct wl_listener *listener, void *data)
2203{
2204 struct weston_keyboard *keyboard = data;
2205 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2206
2207 if (seat->focused_surface) {
2208 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2209 if (shsurf)
2210 shell_surface_lose_keyboard_focus(shsurf);
2211 }
2212
2213 seat->focused_surface = keyboard->focus;
2214
2215 if (seat->focused_surface) {
2216 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2217 if (shsurf)
2218 shell_surface_gain_keyboard_focus(shsurf);
2219 }
2220}
2221
2222static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002223shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002224{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002225 if (sc->ping_serial != serial)
2226 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002227
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002228 sc->unresponsive = 0;
2229 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002230
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002231 if (sc->ping_timer) {
2232 wl_event_source_remove(sc->ping_timer);
2233 sc->ping_timer = NULL;
2234 }
2235
2236}
2237
2238static void
2239shell_surface_pong(struct wl_client *client,
2240 struct wl_resource *resource, uint32_t serial)
2241{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002242 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2243 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002244
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002245 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002246}
2247
2248static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002249set_title(struct shell_surface *shsurf, const char *title)
2250{
2251 free(shsurf->title);
2252 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002253 shsurf->surface->timeline.force_refresh = 1;
2254}
2255
2256static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002257set_pid(struct shell_surface *shsurf, pid_t pid)
2258{
2259 /* We have no use for it */
2260}
2261
2262static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002263set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2264{
2265 shsurf->type = t;
2266 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002267}
2268
2269static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002270set_window_geometry(struct shell_surface *shsurf,
2271 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002272{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002273 shsurf->next_geometry.x = x;
2274 shsurf->next_geometry.y = y;
2275 shsurf->next_geometry.width = width;
2276 shsurf->next_geometry.height = height;
2277 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002278}
2279
2280static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002281shell_surface_set_title(struct wl_client *client,
2282 struct wl_resource *resource, const char *title)
2283{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002284 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002285
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002286 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002287}
2288
2289static void
2290shell_surface_set_class(struct wl_client *client,
2291 struct wl_resource *resource, const char *class)
2292{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002293 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002294
2295 free(shsurf->class);
2296 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002297 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002298}
2299
Alex Wu4539b082012-03-01 12:57:46 +08002300static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002301restore_output_mode(struct weston_output *output)
2302{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002303 if (output->original_mode)
2304 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002305}
2306
2307static void
2308restore_all_output_modes(struct weston_compositor *compositor)
2309{
2310 struct weston_output *output;
2311
2312 wl_list_for_each(output, &compositor->output_list, link)
2313 restore_output_mode(output);
2314}
2315
Philip Withnall07926d92013-11-25 18:01:40 +00002316/* The surface will be inserted into the list immediately after the link
2317 * returned by this function (i.e. will be stacked immediately above the
2318 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002319static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002320shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2321{
2322 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002323 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002324
2325 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002326 case SHELL_SURFACE_XWAYLAND:
2327 return &shsurf->shell->fullscreen_layer.view_list;
2328
2329 case SHELL_SURFACE_NONE:
2330 return NULL;
2331
Kristian Høgsbergead52422014-01-01 13:51:52 -08002332 case SHELL_SURFACE_POPUP:
2333 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002334 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002335 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002336 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002337 /* Move the surface to its parent layer so
2338 * that surfaces which are transient for
2339 * fullscreen surfaces don't get hidden by the
2340 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002341
2342 /* TODO: Handle a parent with multiple views */
2343 parent = get_default_view(shsurf->parent);
2344 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002345 return container_of(parent->layer_link.link.prev,
2346 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002347 }
Philip Withnall07926d92013-11-25 18:01:40 +00002348
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002349 /* Move the surface to a normal workspace layer so that surfaces
2350 * which were previously fullscreen or transient are no longer
2351 * rendered on top. */
2352 ws = get_current_workspace(shsurf->shell);
2353 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002354 }
2355
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002356 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002357}
2358
Philip Withnall648a4dd2013-11-25 18:01:44 +00002359static void
2360shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2361{
2362 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002363 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002364
2365 /* Move the child layers to the same workspace as shsurf. They will be
2366 * stacked above shsurf. */
2367 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002368 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002369 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002370 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002371 prev = container_of(shsurf->view->layer_link.link.prev,
2372 struct weston_layer_entry, link);
2373 weston_layer_entry_remove(&child->view->layer_link);
2374 weston_layer_entry_insert(prev,
2375 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376 weston_view_geometry_dirty(child->view);
2377 weston_surface_damage(child->surface);
2378
2379 /* Recurse. We don’t expect this to recurse very far (if
2380 * at all) because that would imply we have transient
2381 * (or popup) children of transient surfaces, which
2382 * would be unusual. */
2383 shell_surface_update_child_surface_layers(child);
2384 }
2385 }
2386}
2387
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002388/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002389 * geometry to ensure the changes are redrawn.
2390 *
2391 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2392 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002393static void
2394shell_surface_update_layer(struct shell_surface *shsurf)
2395{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002396 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002397
2398 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2399
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002400 if (new_layer_link == NULL)
2401 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002402 if (new_layer_link == &shsurf->view->layer_link)
2403 return;
2404
2405 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002406 weston_layer_entry_remove(&shsurf->view->layer_link);
2407 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002408 weston_view_geometry_dirty(shsurf->view);
2409 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002410
2411 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002412}
2413
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002414static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002415shell_surface_set_parent(struct shell_surface *shsurf,
2416 struct weston_surface *parent)
2417{
2418 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002419
2420 wl_list_remove(&shsurf->children_link);
2421 wl_list_init(&shsurf->children_link);
2422
2423 /* Insert into the parent surface’s child list. */
2424 if (parent != NULL) {
2425 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2426 if (parent_shsurf != NULL)
2427 wl_list_insert(&parent_shsurf->children_list,
2428 &shsurf->children_link);
2429 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002430}
2431
2432static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002433shell_surface_set_output(struct shell_surface *shsurf,
2434 struct weston_output *output)
2435{
2436 struct weston_surface *es = shsurf->surface;
2437
2438 /* get the default output, if the client set it as NULL
2439 check whether the ouput is available */
2440 if (output)
2441 shsurf->output = output;
2442 else if (es->output)
2443 shsurf->output = es->output;
2444 else
2445 shsurf->output = get_default_output(es->compositor);
2446}
2447
2448static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002449surface_clear_next_states(struct shell_surface *shsurf)
2450{
2451 shsurf->next_state.maximized = false;
2452 shsurf->next_state.fullscreen = false;
2453
2454 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2455 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2456 shsurf->state_changed = true;
2457}
2458
2459static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460set_toplevel(struct shell_surface *shsurf)
2461{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002462 shell_surface_set_parent(shsurf, NULL);
2463 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002464 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002465
2466 /* The layer_link is updated in set_surface_type(),
2467 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468}
2469
2470static void
2471shell_surface_set_toplevel(struct wl_client *client,
2472 struct wl_resource *resource)
2473{
2474 struct shell_surface *surface = wl_resource_get_user_data(resource);
2475
2476 set_toplevel(surface);
2477}
2478
2479static void
2480set_transient(struct shell_surface *shsurf,
2481 struct weston_surface *parent, int x, int y, uint32_t flags)
2482{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002483 assert(parent != NULL);
2484
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002485 shell_surface_set_parent(shsurf, parent);
2486
2487 surface_clear_next_states(shsurf);
2488
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489 shsurf->transient.x = x;
2490 shsurf->transient.y = y;
2491 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002492
Rafael Antognollied207b42013-12-03 15:35:43 -02002493 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002494 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002495 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002496
2497 /* The layer_link is updated in set_surface_type(),
2498 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002499}
2500
2501static void
2502shell_surface_set_transient(struct wl_client *client,
2503 struct wl_resource *resource,
2504 struct wl_resource *parent_resource,
2505 int x, int y, uint32_t flags)
2506{
2507 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2508 struct weston_surface *parent =
2509 wl_resource_get_user_data(parent_resource);
2510
2511 set_transient(shsurf, parent, x, y, flags);
2512}
2513
2514static void
2515set_fullscreen(struct shell_surface *shsurf,
2516 uint32_t method,
2517 uint32_t framerate,
2518 struct weston_output *output)
2519{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002520 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002521 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522
2523 shsurf->fullscreen_output = shsurf->output;
2524 shsurf->fullscreen.type = method;
2525 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002526
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002527 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002528}
2529
2530static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002531weston_view_set_initial_position(struct weston_view *view,
2532 struct desktop_shell *shell);
2533
2534static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002535unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002536{
Philip Withnallf85fe842013-11-25 18:01:37 +00002537 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002538 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2539 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002540 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002541 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002542
Alex Wu4539b082012-03-01 12:57:46 +08002543 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2544 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002545
Alex Wu4539b082012-03-01 12:57:46 +08002546 wl_list_remove(&shsurf->fullscreen.transform.link);
2547 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002548
Jason Ekstranda7af7042013-10-12 22:38:11 -05002549 if (shsurf->fullscreen.black_view)
2550 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2551 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002552
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002553 if (shsurf->saved_position_valid)
2554 weston_view_set_position(shsurf->view,
2555 shsurf->saved_x, shsurf->saved_y);
2556 else
2557 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2558
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002559 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002561 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002562 shsurf->saved_rotation_valid = false;
2563 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002564
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002565 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002566}
2567
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002568static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002569shell_surface_set_fullscreen(struct wl_client *client,
2570 struct wl_resource *resource,
2571 uint32_t method,
2572 uint32_t framerate,
2573 struct wl_resource *output_resource)
2574{
2575 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2576 struct weston_output *output;
2577
2578 if (output_resource)
2579 output = wl_resource_get_user_data(output_resource);
2580 else
2581 output = NULL;
2582
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002583 shell_surface_set_parent(shsurf, NULL);
2584
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002586 shsurf->next_state.fullscreen = true;
2587 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002588 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002589}
2590
2591static void
2592set_popup(struct shell_surface *shsurf,
2593 struct weston_surface *parent,
2594 struct weston_seat *seat,
2595 uint32_t serial,
2596 int32_t x,
2597 int32_t y)
2598{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002599 assert(parent != NULL);
2600
Philip Withnallbecb77e2013-11-25 18:01:30 +00002601 shsurf->popup.shseat = get_shell_seat(seat);
2602 shsurf->popup.serial = serial;
2603 shsurf->popup.x = x;
2604 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002605
Pekka Paalanenb5026542014-11-12 15:09:24 +02002606 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002607}
2608
2609static void
2610shell_surface_set_popup(struct wl_client *client,
2611 struct wl_resource *resource,
2612 struct wl_resource *seat_resource,
2613 uint32_t serial,
2614 struct wl_resource *parent_resource,
2615 int32_t x, int32_t y, uint32_t flags)
2616{
2617 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002618 struct weston_surface *parent =
2619 wl_resource_get_user_data(parent_resource);
2620
2621 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002622
Rafael Antognolli03b16592013-12-03 15:35:42 -02002623 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002625 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002626 wl_resource_get_user_data(seat_resource),
2627 serial, x, y);
2628}
2629
2630static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002631unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002632{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002633 /* undo all maximized things here */
2634 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002635
2636 if (shsurf->saved_position_valid)
2637 weston_view_set_position(shsurf->view,
2638 shsurf->saved_x, shsurf->saved_y);
2639 else
2640 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002641
2642 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002643 wl_list_insert(&shsurf->view->geometry.transformation_list,
2644 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645 shsurf->saved_rotation_valid = false;
2646 }
2647
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002648 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002649}
2650
Philip Withnallbecb77e2013-11-25 18:01:30 +00002651static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002652set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002653{
2654 struct shell_surface *shsurf;
2655 struct workspace *current_ws;
2656 struct weston_seat *seat;
2657 struct weston_surface *focus;
2658 struct weston_view *view;
2659
2660 view = get_default_view(surface);
2661 if (!view)
2662 return;
2663
2664 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2665
2666 shsurf = get_shell_surface(surface);
2667 current_ws = get_current_workspace(shsurf->shell);
2668
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002669 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002670 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002671
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002672 drop_focus_state(shsurf->shell, current_ws, view->surface);
2673 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2674 if (!seat->keyboard)
2675 continue;
2676 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2677 if (focus == view->surface)
2678 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002679 }
2680
2681 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002682 weston_view_damage_below(view);
2683}
2684
2685static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002686shell_surface_set_maximized(struct wl_client *client,
2687 struct wl_resource *resource,
2688 struct wl_resource *output_resource)
2689{
2690 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2691 struct weston_output *output;
2692
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002693 surface_clear_next_states(shsurf);
2694 shsurf->next_state.maximized = true;
2695 shsurf->state_changed = true;
2696
Pekka Paalanenb5026542014-11-12 15:09:24 +02002697 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002698 shell_surface_set_parent(shsurf, NULL);
2699
Philip Withnallbecb77e2013-11-25 18:01:30 +00002700 if (output_resource)
2701 output = wl_resource_get_user_data(output_resource);
2702 else
2703 output = NULL;
2704
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002705 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002706
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002707 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002708}
2709
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002710/* This is only ever called from set_surface_type(), so there’s no need to
2711 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002712static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002713reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002714{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002715 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002716 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002717 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002718 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002719
Pekka Paalanen98262232011-12-01 10:42:22 +02002720 return 0;
2721}
2722
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002723static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002724set_full_output(struct shell_surface *shsurf)
2725{
2726 shsurf->saved_x = shsurf->view->geometry.x;
2727 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002728 shsurf->saved_width = shsurf->surface->width;
2729 shsurf->saved_height = shsurf->surface->height;
2730 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002731 shsurf->saved_position_valid = true;
2732
2733 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2734 wl_list_remove(&shsurf->rotation.transform.link);
2735 wl_list_init(&shsurf->rotation.transform.link);
2736 weston_view_geometry_dirty(shsurf->view);
2737 shsurf->saved_rotation_valid = true;
2738 }
2739}
2740
2741static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002742set_surface_type(struct shell_surface *shsurf)
2743{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002744 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002746
Philip Withnallbecb77e2013-11-25 18:01:30 +00002747 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002748
Rafael Antognolli03b16592013-12-03 15:35:42 -02002749 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002750 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002751
2752 switch (shsurf->type) {
2753 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002754 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002755 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002756 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 weston_view_set_position(shsurf->view,
2758 pev->geometry.x + shsurf->transient.x,
2759 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002760 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002761 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002762
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002763 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764 weston_view_set_position(shsurf->view, shsurf->transient.x,
2765 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002766 break;
2767
Philip Withnall0f640e22013-11-25 18:01:31 +00002768 case SHELL_SURFACE_POPUP:
2769 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002770 default:
2771 break;
2772 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002773
2774 /* Update the surface’s layer. */
2775 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002776}
2777
Tiago Vignattibe143262012-04-16 17:31:41 +03002778static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002779shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002780{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002781 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002782}
2783
Pekka Paalanen8274d902014-08-06 19:36:51 +03002784static int
2785black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2786{
2787 struct weston_surface *fs_surface = surface->configure_private;
2788 int n;
2789 int rem;
2790 int ret;
2791
2792 n = snprintf(buf, len, "black background surface for ");
2793 if (n < 0)
2794 return n;
2795
2796 rem = (int)len - n;
2797 if (rem < 0)
2798 rem = 0;
2799
2800 if (fs_surface->get_label)
2801 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2802 else
2803 ret = snprintf(buf + n, rem, "<unknown>");
2804
2805 if (ret < 0)
2806 return n;
2807
2808 return n + ret;
2809}
2810
Alex Wu21858432012-04-01 20:13:08 +08002811static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002812black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002813
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002815create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002816 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002817 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002818{
2819 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002821
2822 surface = weston_surface_create(ec);
2823 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002824 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002825 return NULL;
2826 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 view = weston_view_create(surface);
2828 if (surface == NULL) {
2829 weston_log("no memory\n");
2830 weston_surface_destroy(surface);
2831 return NULL;
2832 }
Alex Wu4539b082012-03-01 12:57:46 +08002833
Alex Wu21858432012-04-01 20:13:08 +08002834 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002835 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002836 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002837 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002838 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002839 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002840 pixman_region32_fini(&surface->input);
2841 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002842
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002843 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002844 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002845
2846 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002847}
2848
Philip Withnalled8f1a92013-11-25 18:01:43 +00002849static void
2850shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2851{
2852 struct weston_output *output = shsurf->fullscreen_output;
2853
Rafael Antognolli03b16592013-12-03 15:35:42 -02002854 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002855
2856 if (!shsurf->fullscreen.black_view)
2857 shsurf->fullscreen.black_view =
2858 create_black_surface(shsurf->surface->compositor,
2859 shsurf->surface,
2860 output->x, output->y,
2861 output->width,
2862 output->height);
2863
2864 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002865 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2866 weston_layer_entry_insert(&shsurf->view->layer_link,
2867 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002868 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2869 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002870
2871 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002872}
2873
Alex Wu4539b082012-03-01 12:57:46 +08002874/* Create black surface and append it to the associated fullscreen surface.
2875 * Handle size dismatch and positioning according to the method. */
2876static void
2877shell_configure_fullscreen(struct shell_surface *shsurf)
2878{
2879 struct weston_output *output = shsurf->fullscreen_output;
2880 struct weston_surface *surface = shsurf->surface;
2881 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002882 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002883 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002884
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002885 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2886 restore_output_mode(output);
2887
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002888 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002889 weston_layer_entry_remove(&shsurf->view->layer_link);
2890 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002891
Philip Withnalled8f1a92013-11-25 18:01:43 +00002892 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002893
Jason Ekstranda7af7042013-10-12 22:38:11 -05002894 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002895 &surf_width, &surf_height);
2896
Alex Wu4539b082012-03-01 12:57:46 +08002897 switch (shsurf->fullscreen.type) {
2898 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002899 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002901 break;
2902 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002903 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002904 if (output->width == surf_width &&
2905 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 weston_view_set_position(shsurf->view,
2907 output->x - surf_x,
2908 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002909 break;
2910 }
2911
Alex Wu4539b082012-03-01 12:57:46 +08002912 matrix = &shsurf->fullscreen.transform.matrix;
2913 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002914
Scott Moreau1bad5db2012-08-18 01:04:05 -06002915 output_aspect = (float) output->width /
2916 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 /* XXX: Use surf_width and surf_height here? */
2918 surface_aspect = (float) surface->width /
2919 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002920 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002921 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002922 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002923 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002924 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002925 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002926
Alex Wu4539b082012-03-01 12:57:46 +08002927 weston_matrix_scale(matrix, scale, scale, 1);
2928 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002930 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002931 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2932 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002934
Alex Wu4539b082012-03-01 12:57:46 +08002935 break;
2936 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002937 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002938 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002939 surf_width * surface->buffer_viewport.buffer.scale,
2940 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002941 shsurf->fullscreen.framerate};
2942
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002943 if (weston_output_mode_switch_to_temporary(output, &mode,
2944 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 weston_view_set_position(shsurf->view,
2946 output->x - surf_x,
2947 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002948 shsurf->fullscreen.black_view->surface->width = output->width;
2949 shsurf->fullscreen.black_view->surface->height = output->height;
2950 weston_view_set_position(shsurf->fullscreen.black_view,
2951 output->x - surf_x,
2952 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002953 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002954 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002955 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002957 }
Alex Wubd3354b2012-04-17 17:20:49 +08002958 }
Alex Wu4539b082012-03-01 12:57:46 +08002959 break;
2960 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002961 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002962 break;
2963 default:
2964 break;
2965 }
2966}
2967
Alex Wu4539b082012-03-01 12:57:46 +08002968static void
2969shell_map_fullscreen(struct shell_surface *shsurf)
2970{
Alex Wubd3354b2012-04-17 17:20:49 +08002971 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002972}
2973
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002974static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002975set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2976{
2977 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002978 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002979 shsurf->transient.x = x;
2980 shsurf->transient.y = y;
2981 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002982
2983 shell_surface_set_parent(shsurf, NULL);
2984
Pekka Paalanenb5026542014-11-12 15:09:24 +02002985 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002986 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002987}
2988
Kristian Høgsberg47792412014-05-04 13:47:06 -07002989static void
2990shell_interface_set_fullscreen(struct shell_surface *shsurf,
2991 uint32_t method,
2992 uint32_t framerate,
2993 struct weston_output *output)
2994{
2995 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002996 shsurf->next_state.fullscreen = true;
2997 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002998
2999 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003000}
3001
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003002static struct weston_output *
3003get_focused_output(struct weston_compositor *compositor)
3004{
3005 struct weston_seat *seat;
3006 struct weston_output *output = NULL;
3007
3008 wl_list_for_each(seat, &compositor->seat_list, link) {
3009 /* Priority has touch focus, then pointer and
3010 * then keyboard focus. We should probably have
3011 * three for loops and check frist for touch,
3012 * then for pointer, etc. but unless somebody has some
3013 * objections, I think this is sufficient. */
3014 if (seat->touch && seat->touch->focus)
3015 output = seat->touch->focus->output;
3016 else if (seat->pointer && seat->pointer->focus)
3017 output = seat->pointer->focus->output;
3018 else if (seat->keyboard && seat->keyboard->focus)
3019 output = seat->keyboard->focus->output;
3020
3021 if (output)
3022 break;
3023 }
3024
3025 return output;
3026}
3027
3028static void
3029shell_interface_set_maximized(struct shell_surface *shsurf)
3030{
3031 struct weston_output *output;
3032
3033 surface_clear_next_states(shsurf);
3034 shsurf->next_state.maximized = true;
3035 shsurf->state_changed = true;
3036 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3037
3038 if (!weston_surface_is_mapped(shsurf->surface))
3039 output = get_focused_output(shsurf->surface->compositor);
3040 else
3041 output = shsurf->surface->output;
3042
3043 shell_surface_set_output(shsurf, output);
3044 send_configure_for_surface(shsurf);
3045}
3046
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003047static int
3048shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3049{
3050 return surface_move(shsurf, ws, 1);
3051}
3052
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003053static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003054
3055static void
3056destroy_shell_seat(struct wl_listener *listener, void *data)
3057{
3058 struct shell_seat *shseat =
3059 container_of(listener,
3060 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003061 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003062
3063 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003064 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065 shseat->popup_grab.client = NULL;
3066
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003067 wl_list_for_each_safe(shsurf, next,
3068 &shseat->popup_grab.surfaces_list,
3069 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003071 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 }
3074
3075 wl_list_remove(&shseat->seat_destroy_listener.link);
3076 free(shseat);
3077}
3078
Jason Ekstrand024177c2014-04-21 19:42:58 -05003079static void
3080shell_seat_caps_changed(struct wl_listener *listener, void *data)
3081{
3082 struct shell_seat *seat;
3083
3084 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3085
3086 if (seat->seat->keyboard &&
3087 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3088 wl_signal_add(&seat->seat->keyboard->focus_signal,
3089 &seat->keyboard_focus_listener);
3090 } else if (!seat->seat->keyboard) {
3091 wl_list_init(&seat->keyboard_focus_listener.link);
3092 }
3093
3094 if (seat->seat->pointer &&
3095 wl_list_empty(&seat->pointer_focus_listener.link)) {
3096 wl_signal_add(&seat->seat->pointer->focus_signal,
3097 &seat->pointer_focus_listener);
3098 } else if (!seat->seat->pointer) {
3099 wl_list_init(&seat->pointer_focus_listener.link);
3100 }
3101}
3102
Giulio Camuffo5085a752013-03-25 21:42:45 +01003103static struct shell_seat *
3104create_shell_seat(struct weston_seat *seat)
3105{
3106 struct shell_seat *shseat;
3107
3108 shseat = calloc(1, sizeof *shseat);
3109 if (!shseat) {
3110 weston_log("no memory to allocate shell seat\n");
3111 return NULL;
3112 }
3113
3114 shseat->seat = seat;
3115 wl_list_init(&shseat->popup_grab.surfaces_list);
3116
3117 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3118 wl_signal_add(&seat->destroy_signal,
3119 &shseat->seat_destroy_listener);
3120
Jason Ekstrand024177c2014-04-21 19:42:58 -05003121 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3122 wl_list_init(&shseat->keyboard_focus_listener.link);
3123
3124 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3125 wl_list_init(&shseat->pointer_focus_listener.link);
3126
3127 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3128 wl_signal_add(&seat->updated_caps_signal,
3129 &shseat->caps_changed_listener);
3130 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3131
Giulio Camuffo5085a752013-03-25 21:42:45 +01003132 return shseat;
3133}
3134
3135static struct shell_seat *
3136get_shell_seat(struct weston_seat *seat)
3137{
3138 struct wl_listener *listener;
3139
3140 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003141 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003142
3143 return container_of(listener,
3144 struct shell_seat, seat_destroy_listener);
3145}
3146
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003147static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003148popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003149{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003150 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003151 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003152 struct shell_seat *shseat =
3153 container_of(grab, struct shell_seat, popup_grab.grab);
3154 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003155 wl_fixed_t sx, sy;
3156
Jason Ekstranda7af7042013-10-12 22:38:11 -05003157 view = weston_compositor_pick_view(pointer->seat->compositor,
3158 pointer->x, pointer->y,
3159 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003160
Jason Ekstranda7af7042013-10-12 22:38:11 -05003161 if (view && view->surface->resource &&
3162 wl_resource_get_client(view->surface->resource) == client) {
3163 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003164 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003165 weston_pointer_set_focus(pointer, NULL,
3166 wl_fixed_from_int(0),
3167 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003168 }
3169}
3170
3171static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003172popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3173 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003174{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003175 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003176 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003177 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003178
Jonas Ådahl61917c82014-08-11 22:44:02 +02003179 if (pointer->focus) {
3180 weston_view_from_global_fixed(pointer->focus, x, y,
3181 &pointer->sx, &pointer->sy);
3182 }
3183
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003184 weston_pointer_move(pointer, x, y);
3185
Neil Roberts96d790e2013-09-19 17:32:00 +01003186 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003187 weston_view_from_global_fixed(pointer->focus,
3188 pointer->x, pointer->y,
3189 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003190 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003191 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003192}
3193
3194static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003195popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003196 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003197{
3198 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003199 struct shell_seat *shseat =
3200 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003201 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003202 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003203 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003204 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003205
Neil Roberts96d790e2013-09-19 17:32:00 +01003206 resource_list = &grab->pointer->focus_resource_list;
3207 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003208 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003209 wl_resource_for_each(resource, resource_list) {
3210 wl_pointer_send_button(resource, serial,
3211 time, button, state);
3212 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003213 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003214 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003215 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003216 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003217 }
3218
Daniel Stone4dbadb12012-05-30 16:31:51 +01003219 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003220 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003221}
3222
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003223static void
3224popup_grab_cancel(struct weston_pointer_grab *grab)
3225{
3226 popup_grab_end(grab->pointer);
3227}
3228
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003229static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003230 popup_grab_focus,
3231 popup_grab_motion,
3232 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003233 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003234};
3235
3236static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003237touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3238 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3239{
3240 struct wl_resource *resource;
3241 struct shell_seat *shseat =
3242 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3243 struct wl_display *display = shseat->seat->compositor->wl_display;
3244 uint32_t serial;
3245 struct wl_list *resource_list;
3246
3247 resource_list = &grab->touch->focus_resource_list;
3248 if (!wl_list_empty(resource_list)) {
3249 serial = wl_display_get_serial(display);
3250 wl_resource_for_each(resource, resource_list) {
3251 wl_touch_send_down(resource, serial, time,
3252 grab->touch->focus->surface->resource,
3253 touch_id, sx, sy);
3254 }
3255 }
3256}
3257
3258static void
3259touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3260{
3261 struct wl_resource *resource;
3262 struct shell_seat *shseat =
3263 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3264 struct wl_display *display = shseat->seat->compositor->wl_display;
3265 uint32_t serial;
3266 struct wl_list *resource_list;
3267
3268 resource_list = &grab->touch->focus_resource_list;
3269 if (!wl_list_empty(resource_list)) {
3270 serial = wl_display_get_serial(display);
3271 wl_resource_for_each(resource, resource_list) {
3272 wl_touch_send_up(resource, serial, time, touch_id);
3273 }
3274 }
3275}
3276
3277static void
3278touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3279 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3280{
3281 struct wl_resource *resource;
3282 struct wl_list *resource_list;
3283
3284 resource_list = &grab->touch->focus_resource_list;
3285 if (!wl_list_empty(resource_list)) {
3286 wl_resource_for_each(resource, resource_list) {
3287 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3288 }
3289 }
3290}
3291
3292static void
3293touch_popup_grab_frame(struct weston_touch_grab *grab)
3294{
3295}
3296
3297static void
3298touch_popup_grab_cancel(struct weston_touch_grab *grab)
3299{
3300 touch_popup_grab_end(grab->touch);
3301}
3302
3303static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3304 touch_popup_grab_down,
3305 touch_popup_grab_up,
3306 touch_popup_grab_motion,
3307 touch_popup_grab_frame,
3308 touch_popup_grab_cancel,
3309};
3310
3311static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003312shell_surface_send_popup_done(struct shell_surface *shsurf)
3313{
3314 if (shell_surface_is_wl_shell_surface(shsurf))
3315 wl_shell_surface_send_popup_done(shsurf->resource);
3316 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003317 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003318}
3319
3320static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003321popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003322{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003323 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003324 struct shell_seat *shseat =
3325 container_of(grab, struct shell_seat, popup_grab.grab);
3326 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003327 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003328
3329 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003330 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003331 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003332 shseat->popup_grab.grab.interface = NULL;
3333 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003334 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003335 wl_list_for_each_safe(shsurf, next,
3336 &shseat->popup_grab.surfaces_list,
3337 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003338 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003339 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003340 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003342 wl_list_init(&shseat->popup_grab.surfaces_list);
3343 }
3344}
3345
3346static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003347touch_popup_grab_end(struct weston_touch *touch)
3348{
3349 struct weston_touch_grab *grab = touch->grab;
3350 struct shell_seat *shseat =
3351 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3352 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003353 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003354
3355 if (touch->grab->interface == &touch_popup_grab_interface) {
3356 weston_touch_end_grab(grab->touch);
3357 shseat->popup_grab.client = NULL;
3358 shseat->popup_grab.touch_grab.interface = NULL;
3359 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3360 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003361 wl_list_for_each_safe(shsurf, next,
3362 &shseat->popup_grab.surfaces_list,
3363 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003364 shell_surface_send_popup_done(shsurf);
3365 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003366 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003368 wl_list_init(&shseat->popup_grab.surfaces_list);
3369 }
3370}
3371
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003372static struct shell_surface *
3373get_top_popup(struct shell_seat *shseat)
3374{
3375 struct shell_surface *shsurf;
3376
3377 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3378 return NULL;
3379 } else {
3380 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3381 struct shell_surface,
3382 popup.grab_link);
3383 return shsurf;
3384 }
3385}
3386
3387static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003388add_popup_grab(struct shell_surface *shsurf,
3389 struct shell_seat *shseat,
3390 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003391{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003392 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003393 struct shell_surface *parent, *top_surface;
3394
3395 parent = get_shell_surface(shsurf->parent);
3396 top_surface = get_top_popup(shseat);
3397 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003398 (!parent ||
3399 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003400 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003401 wl_resource_post_error(shsurf->owner_resource,
3402 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003403 "xdg_popup was not created on the "
3404 "topmost popup");
3405 return -1;
3406 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003407
3408 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003409 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003410 shseat->popup_grab.client =
3411 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003412
3413 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003414 shseat->popup_grab.grab.interface =
3415 &popup_grab_interface;
3416
3417 /* We must make sure here that this popup was opened
3418 * after a mouse press, and not just by moving around
3419 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003420 if (shseat->seat->pointer->button_count > 0)
3421 shseat->popup_grab.initial_up = 0;
3422 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003423 shseat->popup_grab.touch_grab.interface =
3424 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003425 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003426
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003427 wl_list_insert(&shseat->popup_grab.surfaces_list,
3428 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003429
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003430 if (type == POINTER) {
3431 weston_pointer_start_grab(seat->pointer,
3432 &shseat->popup_grab.grab);
3433 } else if (type == TOUCH) {
3434 weston_touch_start_grab(seat->touch,
3435 &shseat->popup_grab.touch_grab);
3436 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003437 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003438 wl_list_insert(&shseat->popup_grab.surfaces_list,
3439 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003440 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003441
3442 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003443}
3444
3445static void
3446remove_popup_grab(struct shell_surface *shsurf)
3447{
3448 struct shell_seat *shseat = shsurf->popup.shseat;
3449
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003450 if (shell_surface_is_xdg_popup(shsurf) &&
3451 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003452 wl_resource_post_error(shsurf->owner_resource,
3453 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003454 "xdg_popup was destroyed while it was "
3455 "not the topmost popup.");
3456 return;
3457 }
3458
Giulio Camuffo5085a752013-03-25 21:42:45 +01003459 wl_list_remove(&shsurf->popup.grab_link);
3460 wl_list_init(&shsurf->popup.grab_link);
3461 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462 if (shseat->popup_grab.type == POINTER) {
3463 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3464 shseat->popup_grab.grab.interface = NULL;
3465 } else if (shseat->popup_grab.type == TOUCH) {
3466 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3467 shseat->popup_grab.touch_grab.interface = NULL;
3468 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003469 }
3470}
3471
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003472static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003473shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003474{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003475 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003476 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003477
Jason Ekstranda7af7042013-10-12 22:38:11 -05003478 shsurf->surface->output = parent_view->output;
3479 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003480
Jason Ekstranda7af7042013-10-12 22:38:11 -05003481 weston_view_set_transform_parent(shsurf->view, parent_view);
3482 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3483 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003484
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003485 if (shseat->seat->pointer &&
3486 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003487 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3488 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003489 } else if (shseat->seat->touch &&
3490 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003491 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3492 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003493 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003494 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003495 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003496 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003497
3498 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003499}
3500
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003501static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003502 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003503 shell_surface_move,
3504 shell_surface_resize,
3505 shell_surface_set_toplevel,
3506 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003507 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003508 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003509 shell_surface_set_maximized,
3510 shell_surface_set_title,
3511 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003512};
3513
3514static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003515destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003516{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003517 struct shell_surface *child, *next;
3518
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003519 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3520
Giulio Camuffo5085a752013-03-25 21:42:45 +01003521 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3522 remove_popup_grab(shsurf);
3523 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003524
Alex Wubd3354b2012-04-17 17:20:49 +08003525 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003526 shell_surface_is_top_fullscreen(shsurf))
3527 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003528
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 if (shsurf->fullscreen.black_view)
3530 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003531
Alex Wubd3354b2012-04-17 17:20:49 +08003532 /* As destroy_resource() use wl_list_for_each_safe(),
3533 * we can always remove the listener.
3534 */
3535 wl_list_remove(&shsurf->surface_destroy_listener.link);
3536 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003537 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003538 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003539
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 weston_view_destroy(shsurf->view);
3541
Philip Withnall648a4dd2013-11-25 18:01:44 +00003542 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003543 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3544 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003545
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003546 wl_list_remove(&shsurf->link);
3547 free(shsurf);
3548}
3549
3550static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003551shell_destroy_shell_surface(struct wl_resource *resource)
3552{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003553 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003554
Bryan Caina46b9462014-04-04 17:41:24 -05003555 if (!wl_list_empty(&shsurf->popup.grab_link))
3556 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003557 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003558 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003559}
3560
3561static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003562shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003563{
3564 struct shell_surface *shsurf = container_of(listener,
3565 struct shell_surface,
3566 surface_destroy_listener);
3567
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003568 if (shsurf->resource)
3569 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003570
3571 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003572}
3573
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003574static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003575fade_out_done(struct weston_view_animation *animation, void *data)
3576{
3577 struct shell_surface *shsurf = data;
3578
3579 weston_surface_destroy(shsurf->surface);
3580}
3581
3582static void
3583handle_resource_destroy(struct wl_listener *listener, void *data)
3584{
3585 struct shell_surface *shsurf =
3586 container_of(listener, struct shell_surface,
3587 resource_destroy_listener);
3588
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003589 if (!weston_surface_is_mapped(shsurf->surface))
3590 return;
3591
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003592 shsurf->surface->ref_count++;
3593
3594 pixman_region32_fini(&shsurf->surface->pending.input);
3595 pixman_region32_init(&shsurf->surface->pending.input);
3596 pixman_region32_fini(&shsurf->surface->input);
3597 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003598 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3599 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3600 fade_out_done, shsurf);
3601 } else {
3602 weston_surface_destroy(shsurf->surface);
3603 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003604}
3605
3606static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003607shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003608
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003609struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003610get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003611{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003612 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003613 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003614 else
3615 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003616}
3617
Rafael Antognollie2a34552013-12-03 15:35:45 -02003618static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003619create_common_surface(struct shell_client *owner, void *shell,
3620 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003621 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003622{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003623 struct shell_surface *shsurf;
3624
Pekka Paalanen50b67472014-10-01 15:02:41 +03003625 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003626
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003627 shsurf = calloc(1, sizeof *shsurf);
3628 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003629 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003630 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003631 }
3632
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 shsurf->view = weston_view_create(surface);
3634 if (!shsurf->view) {
3635 weston_log("no memory to allocate shell surface\n");
3636 free(shsurf);
3637 return NULL;
3638 }
3639
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003640 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003641 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003642 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003643
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003644 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3645 wl_resource_add_destroy_listener(surface->resource,
3646 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003647 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003648
Tiago Vignattibc052c92012-04-19 16:18:18 +03003649 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003650 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003651 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003652 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003653 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003654 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003655 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3656 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003657 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003658 wl_list_init(&shsurf->fullscreen.transform.link);
3659
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003660 shsurf->output = get_default_output(shsurf->shell->compositor);
3661
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003662 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003663 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003664 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003665 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003666
3667 /* init link so its safe to always remove it in destroy_shell_surface */
3668 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003669 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003670
Pekka Paalanen460099f2012-01-20 16:48:25 +02003671 /* empty when not in use */
3672 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003673 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003674
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003675 wl_list_init(&shsurf->workspace_transform.link);
3676
Philip Withnall648a4dd2013-11-25 18:01:44 +00003677 wl_list_init(&shsurf->children_link);
3678 wl_list_init(&shsurf->children_list);
3679 shsurf->parent = NULL;
3680
Pekka Paalanenb5026542014-11-12 15:09:24 +02003681 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003682
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003683 shsurf->client = client;
3684
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003685 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003686}
3687
Rafael Antognollie2a34552013-12-03 15:35:45 -02003688static struct shell_surface *
3689create_shell_surface(void *shell, struct weston_surface *surface,
3690 const struct weston_shell_client *client)
3691{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003692 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003693}
3694
Jason Ekstranda7af7042013-10-12 22:38:11 -05003695static struct weston_view *
3696get_primary_view(void *shell, struct shell_surface *shsurf)
3697{
3698 return shsurf->view;
3699}
3700
Tiago Vignattibc052c92012-04-19 16:18:18 +03003701static void
3702shell_get_shell_surface(struct wl_client *client,
3703 struct wl_resource *resource,
3704 uint32_t id,
3705 struct wl_resource *surface_resource)
3706{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003707 struct weston_surface *surface =
3708 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003709 struct shell_client *sc = wl_resource_get_user_data(resource);
3710 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003711 struct shell_surface *shsurf;
3712
Pekka Paalanen50b67472014-10-01 15:02:41 +03003713 if (weston_surface_set_role(surface, "wl_shell_surface",
3714 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003715 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003716
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003717 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003718 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003719 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003720 return;
3721 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003722
Jason Ekstranda85118c2013-06-27 20:17:02 -05003723 shsurf->resource =
3724 wl_resource_create(client,
3725 &wl_shell_surface_interface, 1, id);
3726 wl_resource_set_implementation(shsurf->resource,
3727 &shell_surface_implementation,
3728 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003729 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003730}
3731
Rafael Antognollie2a34552013-12-03 15:35:45 -02003732static bool
3733shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3734{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003735 /* A shell surface without a resource is created from xwayland
3736 * and is considered a wl_shell surface for now. */
3737
3738 return shsurf->resource == NULL ||
3739 wl_resource_instance_of(shsurf->resource,
3740 &wl_shell_surface_interface,
3741 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003742}
3743
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003744static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003745 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003746};
3747
Rafael Antognollie2a34552013-12-03 15:35:45 -02003748/****************************
3749 * xdg-shell implementation */
3750
3751static void
3752xdg_surface_destroy(struct wl_client *client,
3753 struct wl_resource *resource)
3754{
3755 wl_resource_destroy(resource);
3756}
3757
3758static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003759xdg_surface_set_parent(struct wl_client *client,
3760 struct wl_resource *resource,
3761 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003762{
3763 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003764 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003765
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003766 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003767 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003768 shell_surface_set_parent(shsurf, parent->surface);
3769 } else {
3770 shell_surface_set_parent(shsurf, NULL);
3771 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003772}
3773
3774static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003775xdg_surface_set_app_id(struct wl_client *client,
3776 struct wl_resource *resource,
3777 const char *app_id)
3778{
3779 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3780
3781 free(shsurf->class);
3782 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003783 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003784}
3785
3786static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003787xdg_surface_show_window_menu(struct wl_client *client,
3788 struct wl_resource *surface_resource,
3789 struct wl_resource *seat_resource,
3790 uint32_t serial,
3791 int32_t x,
3792 int32_t y)
3793{
3794 /* TODO */
3795}
3796
3797static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003798xdg_surface_set_title(struct wl_client *client,
3799 struct wl_resource *resource, const char *title)
3800{
3801 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3802
3803 set_title(shsurf, title);
3804}
3805
3806static void
3807xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3808 struct wl_resource *seat_resource, uint32_t serial)
3809{
3810 common_surface_move(resource, seat_resource, serial);
3811}
3812
3813static void
3814xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3815 struct wl_resource *seat_resource, uint32_t serial,
3816 uint32_t edges)
3817{
3818 common_surface_resize(resource, seat_resource, serial, edges);
3819}
3820
3821static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003822xdg_surface_ack_configure(struct wl_client *client,
3823 struct wl_resource *resource,
3824 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003825{
3826 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3827
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003828 if (shsurf->state_requested) {
3829 shsurf->next_state = shsurf->requested_state;
3830 shsurf->state_changed = true;
3831 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003832 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833}
3834
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003835static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003836xdg_surface_set_window_geometry(struct wl_client *client,
3837 struct wl_resource *resource,
3838 int32_t x,
3839 int32_t y,
3840 int32_t width,
3841 int32_t height)
3842{
3843 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3844
3845 set_window_geometry(shsurf, x, y, width, height);
3846}
3847
3848static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003849xdg_surface_set_maximized(struct wl_client *client,
3850 struct wl_resource *resource)
3851{
3852 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003853 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003854
3855 shsurf->state_requested = true;
3856 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003857
3858 if (!weston_surface_is_mapped(shsurf->surface))
3859 output = get_focused_output(shsurf->surface->compositor);
3860 else
3861 output = shsurf->surface->output;
3862
3863 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003864 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003865}
3866
3867static void
3868xdg_surface_unset_maximized(struct wl_client *client,
3869 struct wl_resource *resource)
3870{
3871 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3872
3873 shsurf->state_requested = true;
3874 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003875 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003876}
3877
3878static void
3879xdg_surface_set_fullscreen(struct wl_client *client,
3880 struct wl_resource *resource,
3881 struct wl_resource *output_resource)
3882{
3883 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3884 struct weston_output *output;
3885
3886 shsurf->state_requested = true;
3887 shsurf->requested_state.fullscreen = true;
3888
3889 if (output_resource)
3890 output = wl_resource_get_user_data(output_resource);
3891 else
3892 output = NULL;
3893
Marek Chalupa052917a2014-09-02 11:35:12 +02003894 /* handle clients launching in fullscreen */
3895 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3896 /* Set the output to the one that has focus currently. */
3897 assert(shsurf->surface);
3898 output = get_focused_output(shsurf->surface->compositor);
3899 }
3900
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003901 shell_surface_set_output(shsurf, output);
3902 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003903
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003904 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003905}
3906
3907static void
3908xdg_surface_unset_fullscreen(struct wl_client *client,
3909 struct wl_resource *resource)
3910{
3911 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3912
3913 shsurf->state_requested = true;
3914 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003915 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003916}
3917
3918static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003919xdg_surface_set_minimized(struct wl_client *client,
3920 struct wl_resource *resource)
3921{
3922 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3923
3924 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3925 return;
3926
3927 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003928 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003929}
3930
Rafael Antognollie2a34552013-12-03 15:35:45 -02003931static const struct xdg_surface_interface xdg_surface_implementation = {
3932 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003933 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003934 xdg_surface_set_title,
3935 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003936 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003937 xdg_surface_move,
3938 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003939 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003940 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003941 xdg_surface_set_maximized,
3942 xdg_surface_unset_maximized,
3943 xdg_surface_set_fullscreen,
3944 xdg_surface_unset_fullscreen,
3945 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003946};
3947
3948static void
3949xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003950 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003951{
3952 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003953 uint32_t *s;
3954 struct wl_array states;
3955 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003956
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003957 assert(shsurf);
3958
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003959 if (!shsurf->resource)
3960 return;
3961
3962 wl_array_init(&states);
3963 if (shsurf->requested_state.fullscreen) {
3964 s = wl_array_add(&states, sizeof *s);
3965 *s = XDG_SURFACE_STATE_FULLSCREEN;
3966 } else if (shsurf->requested_state.maximized) {
3967 s = wl_array_add(&states, sizeof *s);
3968 *s = XDG_SURFACE_STATE_MAXIMIZED;
3969 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003970 if (shsurf->resize_edges != 0) {
3971 s = wl_array_add(&states, sizeof *s);
3972 *s = XDG_SURFACE_STATE_RESIZING;
3973 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003974 if (shsurf->focus_count > 0) {
3975 s = wl_array_add(&states, sizeof *s);
3976 *s = XDG_SURFACE_STATE_ACTIVATED;
3977 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003978
3979 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3980 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3981
3982 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003983}
3984
3985static const struct weston_shell_client xdg_client = {
3986 xdg_send_configure
3987};
3988
3989static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08003990xdg_shell_destroy(struct wl_client *client,
3991 struct wl_resource *resource)
3992{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003993 struct shell_client *sc = wl_resource_get_user_data(resource);
3994 struct wl_resource *shsurf_resource;
3995 struct shell_surface *shsurf;
3996
3997 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
3998 shsurf = wl_resource_get_user_data(shsurf_resource);
3999 if (shsurf->owner_resource == resource) {
4000 wl_resource_post_error(
4001 resource,
4002 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4003 "not all child surface objects destroyed");
4004 return;
4005 }
4006 }
4007
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004008 wl_resource_destroy(resource);
4009}
4010
4011static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004012xdg_use_unstable_version(struct wl_client *client,
4013 struct wl_resource *resource,
4014 int32_t version)
4015{
4016 if (version > 1) {
4017 wl_resource_post_error(resource,
4018 1,
4019 "xdg-shell:: version not implemented yet.");
4020 return;
4021 }
4022}
4023
4024static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004025create_xdg_surface(struct shell_client *owner, void *shell,
4026 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004027 const struct weston_shell_client *client)
4028{
4029 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004030
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004031 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004032 if (!shsurf)
4033 return NULL;
4034
Pekka Paalanenb5026542014-11-12 15:09:24 +02004035 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004036
4037 return shsurf;
4038}
4039
4040static void
4041xdg_get_xdg_surface(struct wl_client *client,
4042 struct wl_resource *resource,
4043 uint32_t id,
4044 struct wl_resource *surface_resource)
4045{
4046 struct weston_surface *surface =
4047 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004048 struct shell_client *sc = wl_resource_get_user_data(resource);
4049 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004050 struct shell_surface *shsurf;
4051
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004052 shsurf = get_shell_surface(surface);
4053 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4054 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4055 "This wl_surface is already an "
4056 "xdg_surface");
4057 return;
4058 }
4059
Pekka Paalanen50b67472014-10-01 15:02:41 +03004060 if (weston_surface_set_role(surface, "xdg_surface",
4061 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004062 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004063
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004064 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004065 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004066 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004067 return;
4068 }
4069
4070 shsurf->resource =
4071 wl_resource_create(client,
4072 &xdg_surface_interface, 1, id);
4073 wl_resource_set_implementation(shsurf->resource,
4074 &xdg_surface_implementation,
4075 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004076 shsurf->owner_resource = resource;
4077 wl_list_insert(&sc->surface_list,
4078 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004079}
4080
4081static bool
4082shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4083{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004084 return shsurf->resource &&
4085 wl_resource_instance_of(shsurf->resource,
4086 &xdg_surface_interface,
4087 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004088}
4089
4090/* xdg-popup implementation */
4091
4092static void
4093xdg_popup_destroy(struct wl_client *client,
4094 struct wl_resource *resource)
4095{
4096 wl_resource_destroy(resource);
4097}
4098
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099static const struct xdg_popup_interface xdg_popup_implementation = {
4100 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101};
4102
4103static void
4104xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004105 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004106{
4107}
4108
4109static const struct weston_shell_client xdg_popup_client = {
4110 xdg_popup_send_configure
4111};
4112
4113static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004114create_xdg_popup(struct shell_client *owner, void *shell,
4115 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004116 const struct weston_shell_client *client,
4117 struct weston_surface *parent,
4118 struct shell_seat *seat,
4119 uint32_t serial,
4120 int32_t x, int32_t y)
4121{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004122 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004123
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004124 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004125 if (!shsurf)
4126 return NULL;
4127
Pekka Paalanenb5026542014-11-12 15:09:24 +02004128 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004129 shsurf->popup.shseat = seat;
4130 shsurf->popup.serial = serial;
4131 shsurf->popup.x = x;
4132 shsurf->popup.y = y;
4133 shell_surface_set_parent(shsurf, parent);
4134
4135 return shsurf;
4136}
4137
4138static void
4139xdg_get_xdg_popup(struct wl_client *client,
4140 struct wl_resource *resource,
4141 uint32_t id,
4142 struct wl_resource *surface_resource,
4143 struct wl_resource *parent_resource,
4144 struct wl_resource *seat_resource,
4145 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004146 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004147{
4148 struct weston_surface *surface =
4149 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004150 struct shell_client *sc = wl_resource_get_user_data(resource);
4151 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004152 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004153 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004154 struct weston_surface *parent;
4155 struct shell_seat *seat;
4156
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004157 shsurf = get_shell_surface(surface);
4158 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4159 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4160 "This wl_surface is already an "
4161 "xdg_popup");
4162 return;
4163 }
4164
Pekka Paalanen50b67472014-10-01 15:02:41 +03004165 if (weston_surface_set_role(surface, "xdg_popup",
4166 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004167 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168
4169 if (!parent_resource) {
4170 wl_resource_post_error(surface_resource,
4171 WL_DISPLAY_ERROR_INVALID_OBJECT,
4172 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004173 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174 }
4175
4176 parent = wl_resource_get_user_data(parent_resource);
4177 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4178
Jonas Ådahlee45a552015-03-18 16:37:47 +08004179 /* Verify that we are creating the top most popup when mapping,
4180 * as it's not until then we know whether it was mapped as most
4181 * top level or not. */
4182
4183 parent_shsurf = get_shell_surface(parent);
4184 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4185 !shell_surface_is_xdg_surface(parent_shsurf)) {
4186 wl_resource_post_error(resource,
4187 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4188 "xdg_popup parent was invalid");
4189 return;
4190 }
4191
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004192 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193 parent, seat, serial, x, y);
4194 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004195 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004196 return;
4197 }
4198
4199 shsurf->resource =
4200 wl_resource_create(client,
4201 &xdg_popup_interface, 1, id);
4202 wl_resource_set_implementation(shsurf->resource,
4203 &xdg_popup_implementation,
4204 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004205 shsurf->owner_resource = resource;
4206 wl_list_insert(&sc->surface_list,
4207 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004208}
4209
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004210static void
4211xdg_pong(struct wl_client *client,
4212 struct wl_resource *resource, uint32_t serial)
4213{
4214 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004215
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004216 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004217}
4218
Rafael Antognollie2a34552013-12-03 15:35:45 -02004219static bool
4220shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4221{
4222 return wl_resource_instance_of(shsurf->resource,
4223 &xdg_popup_interface,
4224 &xdg_popup_implementation);
4225}
4226
4227static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004228 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 xdg_use_unstable_version,
4230 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004231 xdg_get_xdg_popup,
4232 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004233};
4234
4235static int
4236xdg_shell_unversioned_dispatch(const void *implementation,
4237 void *_target, uint32_t opcode,
4238 const struct wl_message *message,
4239 union wl_argument *args)
4240{
4241 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004242 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004243
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004244 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245 wl_resource_post_error(resource,
4246 WL_DISPLAY_ERROR_INVALID_OBJECT,
4247 "must call use_unstable_version first");
4248 return 0;
4249 }
4250
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004251#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004253 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4254 "shell implementation doesn't match protocol version");
4255
Rafael Antognollie2a34552013-12-03 15:35:45 -02004256 if (args[0].i != XDG_SERVER_VERSION) {
4257 wl_resource_post_error(resource,
4258 WL_DISPLAY_ERROR_INVALID_OBJECT,
4259 "incompatible version, server is %d "
4260 "client wants %d",
4261 XDG_SERVER_VERSION, args[0].i);
4262 return 0;
4263 }
4264
4265 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004266 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004267
4268 return 1;
4269}
4270
4271/* end of xdg-shell implementation */
4272/***********************************/
4273
Kristian Høgsberg07937562011-04-12 17:25:42 -04004274static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004275shell_fade(struct desktop_shell *shell, enum fade_type type);
4276
4277static int
4278screensaver_timeout(void *data)
4279{
4280 struct desktop_shell *shell = data;
4281
4282 shell_fade(shell, FADE_OUT);
4283
4284 return 1;
4285}
4286
4287static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004288handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004289{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004290 struct desktop_shell *shell =
4291 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004292
Pekka Paalanen18027e52011-12-02 16:31:49 +02004293 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004294
4295 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004296 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004297}
4298
4299static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004300launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004301{
4302 if (shell->screensaver.binding)
4303 return;
4304
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004305 if (!shell->screensaver.path) {
4306 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004307 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004308 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004309
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004310 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004311 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004312 return;
4313 }
4314
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004315 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004316 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004317 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004318 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004319}
4320
4321static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004322terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004323{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004324 if (shell->screensaver.process.pid == 0)
4325 return;
4326
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004327 /* Disarm the screensaver timer, otherwise it may fire when the
4328 * compositor is not in the idle state. In that case, the screen will
4329 * be locked, but the wake_signal won't fire on user input, making the
4330 * system unresponsive. */
4331 wl_event_source_timer_update(shell->screensaver.timer, 0);
4332
Pekka Paalanen18027e52011-12-02 16:31:49 +02004333 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004334}
4335
4336static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004337configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004338{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004340
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004341 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 if (v->output == ev->output && v != ev) {
4343 weston_view_unmap(v);
4344 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004345 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004346 }
4347 }
4348
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004349 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004350
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004351 if (wl_list_empty(&ev->layer_link.link)) {
4352 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004354 }
4355}
4356
Pekka Paalanen8274d902014-08-06 19:36:51 +03004357static int
4358background_get_label(struct weston_surface *surface, char *buf, size_t len)
4359{
4360 return snprintf(buf, len, "background for output %s",
4361 surface->output->name);
4362}
4363
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004365background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004366{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004367 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004369
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 view = container_of(es->views.next, struct weston_view, surface_link);
4371
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004372 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004373}
4374
4375static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004376desktop_shell_set_background(struct wl_client *client,
4377 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004378 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004379 struct wl_resource *surface_resource)
4380{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004381 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004382 struct weston_surface *surface =
4383 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004384 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004385
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004386 if (surface->configure) {
4387 wl_resource_post_error(surface_resource,
4388 WL_DISPLAY_ERROR_INVALID_OBJECT,
4389 "surface role already assigned");
4390 return;
4391 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004392
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4394 weston_view_destroy(view);
4395 view = weston_view_create(surface);
4396
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004397 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004398 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004399 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004400 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004402 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004403 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004404 surface->output->width,
4405 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004406}
4407
Pekka Paalanen8274d902014-08-06 19:36:51 +03004408static int
4409panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4410{
4411 return snprintf(buf, len, "panel for output %s",
4412 surface->output->name);
4413}
4414
Kristian Høgsberg75840622011-09-06 13:48:16 -04004415static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004416panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004417{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004418 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004419 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004420
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421 view = container_of(es->views.next, struct weston_view, surface_link);
4422
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004423 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004424}
4425
4426static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004427desktop_shell_set_panel(struct wl_client *client,
4428 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004429 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004430 struct wl_resource *surface_resource)
4431{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004432 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004433 struct weston_surface *surface =
4434 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004436
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004437 if (surface->configure) {
4438 wl_resource_post_error(surface_resource,
4439 WL_DISPLAY_ERROR_INVALID_OBJECT,
4440 "surface role already assigned");
4441 return;
4442 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004443
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4445 weston_view_destroy(view);
4446 view = weston_view_create(surface);
4447
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004448 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004449 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004450 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004451 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004452 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004453 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004454 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004455 surface->output->width,
4456 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457}
4458
Pekka Paalanen8274d902014-08-06 19:36:51 +03004459static int
4460lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4461{
4462 return snprintf(buf, len, "lock window");
4463}
4464
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004465static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004466lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004467{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004468 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004469 struct weston_view *view;
4470
4471 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004472
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004473 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004474 return;
4475
Jason Ekstranda7af7042013-10-12 22:38:11 -05004476 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004477
4478 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004479 weston_layer_entry_insert(&shell->lock_layer.view_list,
4480 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004481 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004482 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004483 }
4484}
4485
4486static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004487handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004488{
Tiago Vignattibe143262012-04-16 17:31:41 +03004489 struct desktop_shell *shell =
4490 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004491
Martin Minarik6d118362012-06-07 18:01:59 +02004492 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004493 shell->lock_surface = NULL;
4494}
4495
4496static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004497desktop_shell_set_lock_surface(struct wl_client *client,
4498 struct wl_resource *resource,
4499 struct wl_resource *surface_resource)
4500{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004501 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004502 struct weston_surface *surface =
4503 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004504
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004505 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004506
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004507 if (!shell->locked)
4508 return;
4509
Pekka Paalanen98262232011-12-01 10:42:22 +02004510 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004511
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004512 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004513 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004514 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004515
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004516 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004517 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004518 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004519 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004520}
4521
4522static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004523resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004524{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004525 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004526
Pekka Paalanen77346a62011-11-30 16:26:35 +02004527 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004528
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004529 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004530 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004531 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004532 &shell->input_panel_layer.link);
4533 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004534 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004535 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004536 wl_list_insert(&shell->compositor->cursor_layer.link,
4537 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004538 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004539 wl_list_insert(&shell->fullscreen_layer.link,
4540 &shell->panel_layer.link);
4541 wl_list_insert(&shell->panel_layer.link,
4542 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004543
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004544 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004545
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004547 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004548 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004549}
4550
4551static void
4552desktop_shell_unlock(struct wl_client *client,
4553 struct wl_resource *resource)
4554{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004555 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004556
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004558
4559 if (shell->locked)
4560 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004561}
4562
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004563static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004564desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004565 struct wl_resource *resource,
4566 struct wl_resource *surface_resource)
4567{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004568 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004569
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004570 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004571 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004572}
4573
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574static void
4575desktop_shell_desktop_ready(struct wl_client *client,
4576 struct wl_resource *resource)
4577{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004578 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004579
4580 shell_fade_startup(shell);
4581}
4582
Jonny Lamb765760d2014-08-20 15:53:19 +02004583static void
4584desktop_shell_set_panel_position(struct wl_client *client,
4585 struct wl_resource *resource,
4586 uint32_t position)
4587{
4588 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4589
4590 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4591 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4592 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4593 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4594 wl_resource_post_error(resource,
4595 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4596 "bad position argument");
4597 return;
4598 }
4599
4600 shell->panel_position = position;
4601}
4602
Kristian Høgsberg75840622011-09-06 13:48:16 -04004603static const struct desktop_shell_interface desktop_shell_implementation = {
4604 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004605 desktop_shell_set_panel,
4606 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004607 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004608 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004609 desktop_shell_desktop_ready,
4610 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004611};
4612
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004613static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004614get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004615{
4616 struct shell_surface *shsurf;
4617
4618 shsurf = get_shell_surface(surface);
4619 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004620 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004621 return shsurf->type;
4622}
4623
Kristian Høgsberg75840622011-09-06 13:48:16 -04004624static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004625move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004626{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004627 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004628 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004629 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004630
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004631 if (seat->pointer->focus == NULL)
4632 return;
4633
4634 focus = seat->pointer->focus->surface;
4635
Pekka Paalanen01388e22013-04-25 13:57:44 +03004636 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004637 if (surface == NULL)
4638 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004639
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004640 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004641 if (shsurf == NULL || shsurf->state.fullscreen ||
4642 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004643 return;
4644
Derek Foreman8aeeac82015-01-30 13:24:36 -06004645 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004646}
4647
4648static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004649maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4650{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004651 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004652 struct weston_surface *surface;
4653 struct shell_surface *shsurf;
4654
4655 surface = weston_surface_get_main_surface(focus);
4656 if (surface == NULL)
4657 return;
4658
4659 shsurf = get_shell_surface(surface);
4660 if (shsurf == NULL)
4661 return;
4662
4663 if (!shell_surface_is_xdg_surface(shsurf))
4664 return;
4665
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004666 shsurf->state_requested = true;
4667 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004668 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004669}
4670
4671static void
4672fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4673{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004674 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004675 struct weston_surface *surface;
4676 struct shell_surface *shsurf;
4677
4678 surface = weston_surface_get_main_surface(focus);
4679 if (surface == NULL)
4680 return;
4681
4682 shsurf = get_shell_surface(surface);
4683 if (shsurf == NULL)
4684 return;
4685
4686 if (!shell_surface_is_xdg_surface(shsurf))
4687 return;
4688
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004689 shsurf->state_requested = true;
4690 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004691 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004692 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004693}
4694
4695static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004696touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4697{
Derek Foreman362656b2014-09-04 10:23:05 -05004698 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004699 struct weston_surface *surface;
4700 struct shell_surface *shsurf;
4701
Derek Foreman362656b2014-09-04 10:23:05 -05004702 if (seat->touch->focus == NULL)
4703 return;
4704
4705 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004706 surface = weston_surface_get_main_surface(focus);
4707 if (surface == NULL)
4708 return;
4709
4710 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004711 if (shsurf == NULL || shsurf->state.fullscreen ||
4712 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004713 return;
4714
Derek Foreman8aeeac82015-01-30 13:24:36 -06004715 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004716}
4717
4718static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004719resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004720{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004721 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004722 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004723 uint32_t edges = 0;
4724 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004725 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004726
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004727 if (seat->pointer->focus == NULL)
4728 return;
4729
4730 focus = seat->pointer->focus->surface;
4731
Pekka Paalanen01388e22013-04-25 13:57:44 +03004732 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004733 if (surface == NULL)
4734 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004735
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004736 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004737 if (shsurf == NULL || shsurf->state.fullscreen ||
4738 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004739 return;
4740
Jason Ekstranda7af7042013-10-12 22:38:11 -05004741 weston_view_from_global(shsurf->view,
4742 wl_fixed_to_int(seat->pointer->grab_x),
4743 wl_fixed_to_int(seat->pointer->grab_y),
4744 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004745
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004746 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004747 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004748 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004749 edges |= 0;
4750 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004751 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004752
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004753 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004754 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004755 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004756 edges |= 0;
4757 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004758 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004759
Derek Foreman8aeeac82015-01-30 13:24:36 -06004760 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004761}
4762
4763static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004764surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004765 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004766{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004767 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004768 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004769 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004770 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004771
Pekka Paalanen01388e22013-04-25 13:57:44 +03004772 /* XXX: broken for windows containing sub-surfaces */
4773 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004774 if (surface == NULL)
4775 return;
4776
4777 shsurf = get_shell_surface(surface);
4778 if (!shsurf)
4779 return;
4780
Jason Ekstranda7af7042013-10-12 22:38:11 -05004781 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004782
Jason Ekstranda7af7042013-10-12 22:38:11 -05004783 if (shsurf->view->alpha > 1.0)
4784 shsurf->view->alpha = 1.0;
4785 if (shsurf->view->alpha < step)
4786 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004787
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004789 weston_surface_damage(surface);
4790}
4791
4792static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004793do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004794 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004795{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004796 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004797 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004798 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004799
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004800 if (!seat->pointer) {
4801 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4802 return;
4803 }
4804
Scott Moreauccbf29d2012-02-22 14:21:41 -07004805 wl_list_for_each(output, &compositor->output_list, link) {
4806 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004807 wl_fixed_to_double(seat->pointer->x),
4808 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004809 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004810 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004811 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004812 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004813 increment = -output->zoom.increment;
4814 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004815 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004816 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004817 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004818 else
4819 increment = 0;
4820
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004821 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004822
Scott Moreaue6603982012-06-11 13:07:51 -06004823 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004824 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004825 else if (output->zoom.level > output->zoom.max_level)
4826 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004827 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004828 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004829 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004830
Scott Moreaue6603982012-06-11 13:07:51 -06004831 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004832
Jason Ekstranda7af7042013-10-12 22:38:11 -05004833 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004834 }
4835 }
4836}
4837
Scott Moreauccbf29d2012-02-22 14:21:41 -07004838static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004839zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004840 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004841{
4842 do_zoom(seat, time, 0, axis, value);
4843}
4844
4845static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004846zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004847 void *data)
4848{
4849 do_zoom(seat, time, key, 0, 0);
4850}
4851
4852static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004853terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004854 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004855{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004856 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004857
Daniel Stone325fc2d2012-05-30 16:31:58 +01004858 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004859}
4860
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004861static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004862rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4863 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004864{
4865 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004867 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004868 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004869 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004870
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004871 weston_pointer_move(pointer, x, y);
4872
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004873 if (!shsurf)
4874 return;
4875
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004876 cx = 0.5f * shsurf->surface->width;
4877 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004878
Daniel Stone37816df2012-05-16 18:45:18 +01004879 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4880 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004881 r = sqrtf(dx * dx + dy * dy);
4882
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004883 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004884 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004885
4886 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004887 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004888 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004889
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004890 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004891 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004892
4893 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004894 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004895 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004896 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004897 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004898
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004899 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004903 wl_list_init(&shsurf->rotation.transform.link);
4904 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004905 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004906 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004907
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004908 /* We need to adjust the position of the surface
4909 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004910 cposx = shsurf->view->geometry.x + cx;
4911 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004912 dposx = rotate->center.x - cposx;
4913 dposy = rotate->center.y - cposy;
4914 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004915 weston_view_set_position(shsurf->view,
4916 shsurf->view->geometry.x + dposx,
4917 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004918 }
4919
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004920 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004921 * lazily applies the damage due to rotation update.
4922 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004923 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004924}
4925
4926static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004927rotate_grab_button(struct weston_pointer_grab *grab,
4928 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004929{
4930 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004931 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004932 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004933 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004934 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935
Daniel Stone4dbadb12012-05-30 16:31:51 +01004936 if (pointer->button_count == 0 &&
4937 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004938 if (shsurf)
4939 weston_matrix_multiply(&shsurf->rotation.rotation,
4940 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004941 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942 free(rotate);
4943 }
4944}
4945
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004946static void
4947rotate_grab_cancel(struct weston_pointer_grab *grab)
4948{
4949 struct rotate_grab *rotate =
4950 container_of(grab, struct rotate_grab, base.grab);
4951
4952 shell_grab_end(&rotate->base);
4953 free(rotate);
4954}
4955
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004956static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004957 noop_grab_focus,
4958 rotate_grab_motion,
4959 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004960 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004961};
4962
4963static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004964surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004965{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004966 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004967 float dx, dy;
4968 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970 rotate = malloc(sizeof *rotate);
4971 if (!rotate)
4972 return;
4973
Jason Ekstranda7af7042013-10-12 22:38:11 -05004974 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004975 surface->surface->width * 0.5f,
4976 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004977 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004978
Daniel Stone37816df2012-05-16 18:45:18 +01004979 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4980 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004981 r = sqrtf(dx * dx + dy * dy);
4982 if (r > 20.0f) {
4983 struct weston_matrix inverse;
4984
4985 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004986 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004987 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004988
4989 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004990 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004991 } else {
4992 weston_matrix_init(&surface->rotation.rotation);
4993 weston_matrix_init(&rotate->rotation);
4994 }
4995
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004996 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4997 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004998}
4999
5000static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005001rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005002 void *data)
5003{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005004 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005005 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005006 struct shell_surface *surface;
5007
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005008 if (seat->pointer->focus == NULL)
5009 return;
5010
5011 focus = seat->pointer->focus->surface;
5012
Pekka Paalanen01388e22013-04-25 13:57:44 +03005013 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005014 if (base_surface == NULL)
5015 return;
5016
5017 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005018 if (surface == NULL || surface->state.fullscreen ||
5019 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005020 return;
5021
5022 surface_rotate(surface, seat);
5023}
5024
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005025/* Move all fullscreen layers down to the current workspace and hide their
5026 * black views. The surfaces' state is set to both fullscreen and lowered,
5027 * and this is reversed when such a surface is re-configured, see
5028 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5029 *
5030 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005031 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005032void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005033lower_fullscreen_layer(struct desktop_shell *shell)
5034{
5035 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005036 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005037
5038 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005040 &shell->fullscreen_layer.view_list.link,
5041 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005042 struct shell_surface *shsurf = get_shell_surface(view->surface);
5043
5044 if (!shsurf)
5045 continue;
5046
5047 /* We can have a non-fullscreen popup for a fullscreen surface
5048 * in the fullscreen layer. */
5049 if (shsurf->state.fullscreen) {
5050 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005051 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5052 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005053 weston_view_damage_below(shsurf->fullscreen.black_view);
5054
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005055 }
5056
5057 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005058 weston_layer_entry_remove(&view->layer_link);
5059 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005060 weston_view_damage_below(view);
5061 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005062
5063 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005064 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005065}
5066
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005067void
Tiago Vignattibe143262012-04-16 17:31:41 +03005068activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005069 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005070{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005071 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005072 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005073 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005074 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005075 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005076
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005077 lower_fullscreen_layer(shell);
5078
Pekka Paalanen01388e22013-04-25 13:57:44 +03005079 main_surface = weston_surface_get_main_surface(es);
5080
Daniel Stone37816df2012-05-16 18:45:18 +01005081 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005082
Jonas Ådahl8538b222012-08-29 22:13:03 +02005083 state = ensure_focus_state(shell, seat);
5084 if (state == NULL)
5085 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005086
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005087 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005088 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005089
Rafael Antognolli03b16592013-12-03 15:35:42 -02005090 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005091 assert(shsurf);
5092
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005093 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005094 shell_configure_fullscreen(shsurf);
5095 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005096 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005097
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005098 /* Update the surface’s layer. This brings it to the top of the stacking
5099 * order as appropriate. */
5100 shell_surface_update_layer(shsurf);
5101
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005102 if (shell->focus_animation_type != ANIMATION_NONE) {
5103 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005104 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005105 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005106}
5107
Alex Wu21858432012-04-01 20:13:08 +08005108/* no-op func for checking black surface */
5109static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005110black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005111{
5112}
5113
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005114static bool
Alex Wu21858432012-04-01 20:13:08 +08005115is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5116{
5117 if (es->configure == black_surface_configure) {
5118 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005119 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005120 return true;
5121 }
5122 return false;
5123}
5124
Kristian Høgsberg75840622011-09-06 13:48:16 -04005125static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005126activate_binding(struct weston_seat *seat,
5127 struct desktop_shell *shell,
5128 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005129{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005130 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005131
Alex Wu9c35e6b2012-03-05 14:13:13 +08005132 if (!focus)
5133 return;
5134
Pekka Paalanen01388e22013-04-25 13:57:44 +03005135 if (is_black_surface(focus, &main_surface))
5136 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005137
Pekka Paalanen01388e22013-04-25 13:57:44 +03005138 main_surface = weston_surface_get_main_surface(focus);
5139 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005140 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005141
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005142 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005143}
5144
5145static void
5146click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5147 void *data)
5148{
5149 if (seat->pointer->grab != &seat->pointer->default_grab)
5150 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005151 if (seat->pointer->focus == NULL)
5152 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005153
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005154 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005155}
5156
5157static void
5158touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5159{
5160 if (seat->touch->grab != &seat->touch->default_grab)
5161 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005162 if (seat->touch->focus == NULL)
5163 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005164
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005165 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005166}
5167
5168static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005169unfocus_all_seats(struct desktop_shell *shell)
5170{
5171 struct weston_seat *seat, *next;
5172
5173 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5174 if (seat->keyboard == NULL)
5175 continue;
5176
5177 weston_keyboard_set_focus(seat->keyboard, NULL);
5178 }
5179}
5180
5181static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005182lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005183{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005184 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005185
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005186 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005187 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005188 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005189 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005190
5191 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005192
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005193 /* Hide all surfaces by removing the fullscreen, panel and
5194 * toplevel layers. This way nothing else can show or receive
5195 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005196
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005197 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005198 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005199 if (shell->showing_input_panels)
5200 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005201 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005202 wl_list_insert(&shell->compositor->cursor_layer.link,
5203 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005204
Pekka Paalanen77346a62011-11-30 16:26:35 +02005205 launch_screensaver(shell);
5206
Neil Robertsb4a91702014-04-09 16:33:32 +01005207 /* Remove the keyboard focus on all seats. This will be
5208 * restored to the workspace's saved state via
5209 * restore_focus_state when the compositor is unlocked */
5210 unfocus_all_seats(shell);
5211
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005212 /* TODO: disable bindings that should not work while locked. */
5213
5214 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005215}
5216
5217static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005218unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005219{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005220 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005221 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005222 return;
5223 }
5224
5225 /* If desktop-shell client has gone away, unlock immediately. */
5226 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005227 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005228 return;
5229 }
5230
5231 if (shell->prepare_event_sent)
5232 return;
5233
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005234 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005235 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005236}
5237
5238static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005239shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005240{
5241 struct desktop_shell *shell = data;
5242
5243 shell->fade.animation = NULL;
5244
5245 switch (shell->fade.type) {
5246 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247 weston_surface_destroy(shell->fade.view->surface);
5248 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005249 break;
5250 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005251 lock(shell);
5252 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005253 default:
5254 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005255 }
5256}
5257
Jason Ekstranda7af7042013-10-12 22:38:11 -05005258static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005259shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005260{
5261 struct weston_compositor *compositor = shell->compositor;
5262 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005263 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005264
5265 surface = weston_surface_create(compositor);
5266 if (!surface)
5267 return NULL;
5268
Jason Ekstranda7af7042013-10-12 22:38:11 -05005269 view = weston_view_create(surface);
5270 if (!view) {
5271 weston_surface_destroy(surface);
5272 return NULL;
5273 }
5274
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005275 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005276 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005277 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005278 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5279 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005280 pixman_region32_init(&surface->input);
5281
Jason Ekstranda7af7042013-10-12 22:38:11 -05005282 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005283}
5284
5285static void
5286shell_fade(struct desktop_shell *shell, enum fade_type type)
5287{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005288 float tint;
5289
5290 switch (type) {
5291 case FADE_IN:
5292 tint = 0.0;
5293 break;
5294 case FADE_OUT:
5295 tint = 1.0;
5296 break;
5297 default:
5298 weston_log("shell: invalid fade type\n");
5299 return;
5300 }
5301
5302 shell->fade.type = type;
5303
Jason Ekstranda7af7042013-10-12 22:38:11 -05005304 if (shell->fade.view == NULL) {
5305 shell->fade.view = shell_fade_create_surface(shell);
5306 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005307 return;
5308
Jason Ekstranda7af7042013-10-12 22:38:11 -05005309 shell->fade.view->alpha = 1.0 - tint;
5310 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005311 }
5312
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005313 if (shell->fade.view->output == NULL) {
5314 /* If the black view gets a NULL output, we lost the
5315 * last output and we'll just cancel the fade. This
5316 * happens when you close the last window under the
5317 * X11 or Wayland backends. */
5318 shell->locked = false;
5319 weston_surface_destroy(shell->fade.view->surface);
5320 shell->fade.view = NULL;
5321 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005322 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005323 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005324 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005326 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005327 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005328 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005329}
5330
5331static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005332do_shell_fade_startup(void *data)
5333{
5334 struct desktop_shell *shell = data;
5335
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005336 if (shell->startup_animation_type == ANIMATION_FADE)
5337 shell_fade(shell, FADE_IN);
5338 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005339 weston_surface_destroy(shell->fade.view->surface);
5340 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005341 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005342}
5343
5344static void
5345shell_fade_startup(struct desktop_shell *shell)
5346{
5347 struct wl_event_loop *loop;
5348
5349 if (!shell->fade.startup_timer)
5350 return;
5351
5352 wl_event_source_remove(shell->fade.startup_timer);
5353 shell->fade.startup_timer = NULL;
5354
5355 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5356 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5357}
5358
5359static int
5360fade_startup_timeout(void *data)
5361{
5362 struct desktop_shell *shell = data;
5363
5364 shell_fade_startup(shell);
5365 return 0;
5366}
5367
5368static void
5369shell_fade_init(struct desktop_shell *shell)
5370{
5371 /* Make compositor output all black, and wait for the desktop-shell
5372 * client to signal it is ready, then fade in. The timer triggers a
5373 * fade-in, in case the desktop-shell client takes too long.
5374 */
5375
5376 struct wl_event_loop *loop;
5377
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379 weston_log("%s: warning: fade surface already exists\n",
5380 __func__);
5381 return;
5382 }
5383
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 shell->fade.view = shell_fade_create_surface(shell);
5385 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005386 return;
5387
Jason Ekstranda7af7042013-10-12 22:38:11 -05005388 weston_view_update_transform(shell->fade.view);
5389 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005390
5391 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5392 shell->fade.startup_timer =
5393 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5394 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5395}
5396
5397static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005398idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005399{
5400 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005401 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005402 struct weston_seat *seat;
5403
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005404 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005405 if (seat->pointer)
5406 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005407 if (seat->touch)
5408 touch_popup_grab_end(seat->touch);
5409 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005410
5411 shell_fade(shell, FADE_OUT);
5412 /* lock() is called from shell_fade_done() */
5413}
5414
5415static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005416wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005417{
5418 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005419 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005420
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005421 unlock(shell);
5422}
5423
5424static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005426{
Giulio Camuffob8366642013-04-25 13:57:46 +03005427 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005428 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005429
Jason Ekstranda7af7042013-10-12 22:38:11 -05005430 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005431
5432 x = output->x + (output->width - width) / 2 - surf_x / 2;
5433 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005434
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005435 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005436}
5437
5438static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005439weston_view_set_initial_position(struct weston_view *view,
5440 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005441{
5442 struct weston_compositor *compositor = shell->compositor;
5443 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005444 int32_t range_x, range_y;
5445 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005446 struct weston_output *output, *target_output = NULL;
5447 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005448 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005449
5450 /* As a heuristic place the new window on the same output as the
5451 * pointer. Falling back to the output containing 0, 0.
5452 *
5453 * TODO: Do something clever for touch too?
5454 */
5455 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005456 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005457 ix = wl_fixed_to_int(seat->pointer->x);
5458 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005459 break;
5460 }
5461 }
5462
5463 wl_list_for_each(output, &compositor->output_list, link) {
5464 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5465 target_output = output;
5466 break;
5467 }
5468 }
5469
5470 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005471 weston_view_set_position(view, 10 + random() % 400,
5472 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005473 return;
5474 }
5475
5476 /* Valid range within output where the surface will still be onscreen.
5477 * If this is negative it means that the surface is bigger than
5478 * output.
5479 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005480 get_output_work_area(shell, target_output, &area);
5481
5482 dx = area.x;
5483 dy = area.y;
5484 range_x = area.width - view->surface->width;
5485 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005486
Rob Bradford4cb88c72012-08-13 15:18:44 +01005487 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005488 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005489
5490 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005491 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005492
5493 x = target_output->x + dx;
5494 y = target_output->y + dy;
5495
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005497}
5498
5499static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005500set_maximized_position(struct desktop_shell *shell,
5501 struct shell_surface *shsurf)
5502{
5503 int32_t surf_x, surf_y;
5504 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005505 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005506
Jonny Lambd73c6942014-08-20 15:53:20 +02005507 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005508 if (shsurf->has_set_geometry) {
5509 surf_x = shsurf->geometry.x;
5510 surf_y = shsurf->geometry.y;
5511 } else {
5512 surface_subsurfaces_boundingbox(shsurf->surface,
5513 &surf_x, &surf_y, NULL, NULL);
5514 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005515 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005516
5517 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005518 e->x1 + area.x - surf_x,
5519 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005520}
5521
5522static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005523map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005524 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005526 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005527 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005528
Pekka Paalanen77346a62011-11-30 16:26:35 +02005529 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005530 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005531 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005532 if (shsurf->state.fullscreen) {
5533 center_on_output(shsurf->view, shsurf->fullscreen_output);
5534 shell_map_fullscreen(shsurf);
5535 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005536 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005537 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005538 weston_view_set_initial_position(shsurf->view, shell);
5539 }
Juan Zhao96879df2012-02-07 08:45:41 +08005540 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005541 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005542 if (shell_map_popup(shsurf) != 0)
5543 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005544 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005545 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005546 weston_view_set_position(shsurf->view,
5547 shsurf->view->geometry.x + sx,
5548 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005549 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005550 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005551 default:
5552 ;
5553 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005554
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005555 /* Surface stacking order, see also activate(). */
5556 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005557
Jason Ekstranda7af7042013-10-12 22:38:11 -05005558 if (shsurf->type != SHELL_SURFACE_NONE) {
5559 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005560 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005561 shsurf->surface->output = shsurf->output;
5562 shsurf->view->output = shsurf->output;
5563 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005564 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005565
Jason Ekstranda7af7042013-10-12 22:38:11 -05005566 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005567 /* XXX: xwayland's using the same fields for transient type */
5568 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005569 if (shsurf->transient.flags ==
5570 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5571 break;
5572 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005573 if (shsurf->state.relative &&
5574 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5575 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005576 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005577 break;
5578 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005579 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005580 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005581 case SHELL_SURFACE_POPUP:
5582 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005583 default:
5584 break;
5585 }
5586
Rafael Antognolli03b16592013-12-03 15:35:42 -02005587 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5588 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005589 {
5590 switch (shell->win_animation_type) {
5591 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005592 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005593 break;
5594 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005595 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005596 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005597 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005598 default:
5599 break;
5600 }
5601 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005602}
5603
5604static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005605configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005606 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005607{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005608 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005609 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005610
Pekka Paalanen77346a62011-11-30 16:26:35 +02005611 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005612
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005613 assert(shsurf);
5614
Rafael Antognolli03b16592013-12-03 15:35:42 -02005615 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005616 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005617 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005618 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005619 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005620 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005621 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005622
Alex Wu4539b082012-03-01 12:57:46 +08005623 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005624 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005625 wl_list_for_each(view, &surface->views, surface_link)
5626 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005627
Rafael Antognolli03b16592013-12-03 15:35:42 -02005628 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005629 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005630 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005631}
5632
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005633static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005634shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005635{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005636 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005637 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005638 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005639
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005640 assert(shsurf);
5641
5642 shell = shsurf->shell;
5643
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005644 if (!weston_surface_is_mapped(es) &&
5645 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005646 remove_popup_grab(shsurf);
5647 }
5648
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005649 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005650 return;
5651
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005652 if (shsurf->has_next_geometry) {
5653 shsurf->geometry = shsurf->next_geometry;
5654 shsurf->has_next_geometry = false;
5655 shsurf->has_set_geometry = true;
5656 } else if (!shsurf->has_set_geometry) {
5657 surface_subsurfaces_boundingbox(shsurf->surface,
5658 &shsurf->geometry.x,
5659 &shsurf->geometry.y,
5660 &shsurf->geometry.width,
5661 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005662 }
5663
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005664 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005665 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005666 type_changed = 1;
5667 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005668
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005669 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005670 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005671 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005672 shsurf->last_width != es->width ||
5673 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005674 float from_x, from_y;
5675 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005676
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005677 if (shsurf->resize_edges) {
5678 sx = 0;
5679 sy = 0;
5680 }
5681
5682 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5683 sx = shsurf->last_width - es->width;
5684 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5685 sy = shsurf->last_height - es->height;
5686
5687 shsurf->last_width = es->width;
5688 shsurf->last_height = es->height;
5689
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5691 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005692 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005693 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005694 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005695 }
5696}
5697
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005698static bool
5699check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5700{
5701 struct timespec now;
5702
5703 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5704 return false;
5705
5706 /*
5707 * If the shell helper client dies before the session has been
5708 * up for roughly 30 seconds, better just make Weston shut down,
5709 * because the user likely has no way to interact with the desktop
5710 * anyway.
5711 */
5712 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5713 weston_log("Error: %s apparently cannot run at all.\n",
5714 shell->client);
5715 weston_log_continue(STAMP_SPACE "Quitting...");
5716 wl_display_terminate(shell->compositor->wl_display);
5717
5718 return true;
5719 }
5720
5721 return false;
5722}
5723
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005724static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005725
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005726static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005727respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005728{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005729 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005730
5731 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5732 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005733 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005734 shell->child.deathstamp = time;
5735 shell->child.deathcount = 0;
5736 }
5737
5738 shell->child.deathcount++;
5739 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005740 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005741 return;
5742 }
5743
Pekka Paalanen826dc142014-08-27 12:11:53 +03005744 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005745 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005746}
5747
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005748static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005749desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5750{
5751 struct desktop_shell *shell;
5752
5753 shell = container_of(listener, struct desktop_shell,
5754 child.client_destroy_listener);
5755
Pekka Paalanen826dc142014-08-27 12:11:53 +03005756 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005757 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005758 /*
5759 * unbind_desktop_shell() will reset shell->child.desktop_shell
5760 * before the respawned process has a chance to create a new
5761 * desktop_shell object, because we are being called from the
5762 * wl_client destructor which destroys all wl_resources before
5763 * returning.
5764 */
5765
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005766 if (!check_desktop_shell_crash_too_early(shell))
5767 respawn_desktop_shell_process(shell);
5768
Pekka Paalanen826dc142014-08-27 12:11:53 +03005769 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005770}
5771
5772static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005773launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005774{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005775 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005776
Pekka Paalanen826dc142014-08-27 12:11:53 +03005777 shell->child.client = weston_client_start(shell->compositor,
5778 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005779
Arnaud Vrac42631192014-08-25 20:56:46 +02005780 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005781 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005782 return;
5783 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005784
5785 shell->child.client_destroy_listener.notify =
5786 desktop_shell_client_destroy;
5787 wl_client_add_destroy_listener(shell->child.client,
5788 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005789}
5790
5791static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005792handle_shell_client_destroy(struct wl_listener *listener, void *data)
5793{
5794 struct shell_client *sc =
5795 container_of(listener, struct shell_client, destroy_listener);
5796
5797 if (sc->ping_timer)
5798 wl_event_source_remove(sc->ping_timer);
5799 free(sc);
5800}
5801
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005802static struct shell_client *
5803shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5804 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005805{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005807
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005808 sc = zalloc(sizeof *sc);
5809 if (sc == NULL) {
5810 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005811 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005812 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005813
5814 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005815 if (sc->resource == NULL) {
5816 free(sc);
5817 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005818 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005819 }
5820
5821 sc->client = client;
5822 sc->shell = shell;
5823 sc->destroy_listener.notify = handle_shell_client_destroy;
5824 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005825 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005826
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005827 return sc;
5828}
5829
5830static void
5831bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5832{
5833 struct desktop_shell *shell = data;
5834 struct shell_client *sc;
5835
5836 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5837 if (sc)
5838 wl_resource_set_implementation(sc->resource,
5839 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005840 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005841}
5842
5843static void
5844bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5845{
5846 struct desktop_shell *shell = data;
5847 struct shell_client *sc;
5848
5849 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5850 if (sc)
5851 wl_resource_set_dispatcher(sc->resource,
5852 xdg_shell_unversioned_dispatch,
5853 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005854}
5855
5856static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005857unbind_desktop_shell(struct wl_resource *resource)
5858{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005859 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005860
5861 if (shell->locked)
5862 resume_desktop(shell);
5863
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005864 shell->child.desktop_shell = NULL;
5865 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005866}
5867
5868static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005869bind_desktop_shell(struct wl_client *client,
5870 void *data, uint32_t version, uint32_t id)
5871{
Tiago Vignattibe143262012-04-16 17:31:41 +03005872 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005873 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005874
Jason Ekstranda85118c2013-06-27 20:17:02 -05005875 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005876 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005877
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005878 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005879 wl_resource_set_implementation(resource,
5880 &desktop_shell_implementation,
5881 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005882 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005883
5884 if (version < 2)
5885 shell_fade_startup(shell);
5886
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005887 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005888 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005889
5890 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5891 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005892}
5893
Pekka Paalanen8274d902014-08-06 19:36:51 +03005894static int
5895screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5896{
5897 return snprintf(buf, len, "screensaver for output %s",
5898 surface->output->name);
5899}
5900
Pekka Paalanen6e168112011-11-24 11:34:05 +02005901static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005902screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005903{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005904 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005905 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005906 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005907
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005908 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005909 return;
5910
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005911 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005912 if (!shell->locked)
5913 return;
5914
Jason Ekstranda7af7042013-10-12 22:38:11 -05005915 view = container_of(surface->views.next, struct weston_view, surface_link);
5916 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005917
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005918 if (wl_list_empty(&view->layer_link.link)) {
5919 prev = container_of(shell->lock_layer.view_list.link.prev,
5920 struct weston_layer_entry, link);
5921 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005922 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005923 wl_event_source_timer_update(shell->screensaver.timer,
5924 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005925 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005926 }
5927}
5928
5929static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005930screensaver_set_surface(struct wl_client *client,
5931 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005932 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005933 struct wl_resource *output_resource)
5934{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005935 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005936 struct weston_surface *surface =
5937 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005938 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005939 struct weston_view *view, *next;
5940
5941 /* Make sure we only have one view */
5942 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5943 weston_view_destroy(view);
5944 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005945
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005946 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005947 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005948 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005949 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005950}
5951
5952static const struct screensaver_interface screensaver_implementation = {
5953 screensaver_set_surface
5954};
5955
5956static void
5957unbind_screensaver(struct wl_resource *resource)
5958{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005959 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005960
Pekka Paalanen77346a62011-11-30 16:26:35 +02005961 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005962}
5963
5964static void
5965bind_screensaver(struct wl_client *client,
5966 void *data, uint32_t version, uint32_t id)
5967{
Tiago Vignattibe143262012-04-16 17:31:41 +03005968 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005969 struct wl_resource *resource;
5970
Jason Ekstranda85118c2013-06-27 20:17:02 -05005971 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005972
Pekka Paalanen77346a62011-11-30 16:26:35 +02005973 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005974 wl_resource_set_implementation(resource,
5975 &screensaver_implementation,
5976 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005977 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005978 return;
5979 }
5980
5981 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5982 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005983}
5984
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005986 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005987 struct weston_surface *current;
5988 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005989 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005991};
5992
5993static void
5994switcher_next(struct switcher *switcher)
5995{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005996 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005997 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005998 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005999 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006000
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006001 /* temporary re-display minimized surfaces */
6002 struct weston_view *tmp;
6003 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006004 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6005 weston_layer_entry_remove(&view->layer_link);
6006 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006007 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6008 *minimized = view;
6009 }
6010
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006011 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006012 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006013 if (shsurf &&
6014 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6015 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006017 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006019 next = view->surface;
6020 prev = view->surface;
6021 view->alpha = 0.25;
6022 weston_view_geometry_dirty(view);
6023 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024 }
Alex Wu1659daa2012-04-01 20:13:09 +08006025
Jason Ekstranda7af7042013-10-12 22:38:11 -05006026 if (is_black_surface(view->surface, NULL)) {
6027 view->alpha = 0.25;
6028 weston_view_geometry_dirty(view);
6029 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006030 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006031 }
6032
6033 if (next == NULL)
6034 next = first;
6035
Alex Wu07b26062012-03-12 16:06:01 +08006036 if (next == NULL)
6037 return;
6038
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006040 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041
6042 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006043 wl_list_for_each(view, &next->views, surface_link)
6044 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006045
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006046 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006047 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006048 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049}
6050
6051static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006052switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053{
6054 struct switcher *switcher =
6055 container_of(listener, struct switcher, listener);
6056
6057 switcher_next(switcher);
6058}
6059
6060static void
Daniel Stone351eb612012-05-31 15:27:47 -04006061switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006063 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006064 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006065 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006066
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006067 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006068 if (is_focus_view(view))
6069 continue;
6070
Jason Ekstranda7af7042013-10-12 22:38:11 -05006071 view->alpha = 1.0;
6072 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006073 }
6074
Alex Wu07b26062012-03-12 16:06:01 +08006075 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006076 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006077 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006078 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006079 weston_keyboard_end_grab(keyboard);
6080 if (keyboard->input_method_resource)
6081 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006082
6083 /* re-hide surfaces that were temporary shown during the switch */
6084 struct weston_view **minimized;
6085 wl_array_for_each(minimized, &switcher->minimized_array) {
6086 /* with the exception of the current selected */
6087 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006088 weston_layer_entry_remove(&(*minimized)->layer_link);
6089 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006090 weston_view_damage_below(*minimized);
6091 }
6092 }
6093 wl_array_release(&switcher->minimized_array);
6094
Kristian Høgsberg07045392012-02-19 18:52:44 -05006095 free(switcher);
6096}
6097
6098static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006099switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006100 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006101{
6102 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006103 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006104
Daniel Stonec9785ea2012-05-30 16:31:52 +01006105 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006106 switcher_next(switcher);
6107}
6108
6109static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006110switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006111 uint32_t mods_depressed, uint32_t mods_latched,
6112 uint32_t mods_locked, uint32_t group)
6113{
6114 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006115 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006116
Daniel Stone351eb612012-05-31 15:27:47 -04006117 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6118 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006119}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006121static void
6122switcher_cancel(struct weston_keyboard_grab *grab)
6123{
6124 struct switcher *switcher = container_of(grab, struct switcher, grab);
6125
6126 switcher_destroy(switcher);
6127}
6128
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006129static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006130 switcher_key,
6131 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006132 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133};
6134
6135static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006136switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006137 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138{
Tiago Vignattibe143262012-04-16 17:31:41 +03006139 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 struct switcher *switcher;
6141
6142 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006143 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006144 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006145 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006146 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006147 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006149 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006150 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006151 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006152 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6153 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006154 switcher_next(switcher);
6155}
6156
Pekka Paalanen3c647232011-12-22 13:43:43 +02006157static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006158backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006159 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006160{
6161 struct weston_compositor *compositor = data;
6162 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006163 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006164
6165 /* TODO: we're limiting to simple use cases, where we assume just
6166 * control on the primary display. We'd have to extend later if we
6167 * ever get support for setting backlights on random desktop LCD
6168 * panels though */
6169 output = get_default_output(compositor);
6170 if (!output)
6171 return;
6172
6173 if (!output->set_backlight)
6174 return;
6175
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006176 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6177 backlight_new = output->backlight_current - 25;
6178 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6179 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006180
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006181 if (backlight_new < 5)
6182 backlight_new = 5;
6183 if (backlight_new > 255)
6184 backlight_new = 255;
6185
6186 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006187 output->set_backlight(output, output->backlight_current);
6188}
6189
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006190static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006191force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006192 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006193{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006194 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006195 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006196 struct desktop_shell *shell = data;
6197 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006198 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006199
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006200 focus_surface = seat->keyboard->focus;
6201 if (!focus_surface)
6202 return;
6203
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006204 wl_signal_emit(&compositor->kill_signal, focus_surface);
6205
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006206 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006207 wl_client_get_credentials(client, &pid, NULL, NULL);
6208
6209 /* Skip clients that we launched ourselves (the credentials of
6210 * the socketpair is ours) */
6211 if (pid == getpid())
6212 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006213
Daniel Stone325fc2d2012-05-30 16:31:58 +01006214 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006215}
6216
6217static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006218workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006219 uint32_t key, void *data)
6220{
6221 struct desktop_shell *shell = data;
6222 unsigned int new_index = shell->workspaces.current;
6223
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006224 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006225 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006226 if (new_index != 0)
6227 new_index--;
6228
6229 change_workspace(shell, new_index);
6230}
6231
6232static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006233workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006234 uint32_t key, void *data)
6235{
6236 struct desktop_shell *shell = data;
6237 unsigned int new_index = shell->workspaces.current;
6238
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006239 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006240 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006241 if (new_index < shell->workspaces.num - 1)
6242 new_index++;
6243
6244 change_workspace(shell, new_index);
6245}
6246
6247static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006248workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006249 uint32_t key, void *data)
6250{
6251 struct desktop_shell *shell = data;
6252 unsigned int new_index;
6253
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006254 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006255 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006256 new_index = key - KEY_F1;
6257 if (new_index >= shell->workspaces.num)
6258 new_index = shell->workspaces.num - 1;
6259
6260 change_workspace(shell, new_index);
6261}
6262
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006263static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006264workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006265 uint32_t key, void *data)
6266{
6267 struct desktop_shell *shell = data;
6268 unsigned int new_index = shell->workspaces.current;
6269
6270 if (shell->locked)
6271 return;
6272
6273 if (new_index != 0)
6274 new_index--;
6275
6276 take_surface_to_workspace_by_seat(shell, seat, new_index);
6277}
6278
6279static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006280workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006281 uint32_t key, void *data)
6282{
6283 struct desktop_shell *shell = data;
6284 unsigned int new_index = shell->workspaces.current;
6285
6286 if (shell->locked)
6287 return;
6288
6289 if (new_index < shell->workspaces.num - 1)
6290 new_index++;
6291
6292 take_surface_to_workspace_by_seat(shell, seat, new_index);
6293}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006294
6295static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006296shell_reposition_view_on_output_destroy(struct weston_view *view)
6297{
6298 struct weston_output *output, *first_output;
6299 struct weston_compositor *ec = view->surface->compositor;
6300 struct shell_surface *shsurf;
6301 float x, y;
6302 int visible;
6303
6304 x = view->geometry.x;
6305 y = view->geometry.y;
6306
6307 /* At this point the destroyed output is not in the list anymore.
6308 * If the view is still visible somewhere, we leave where it is,
6309 * otherwise, move it to the first output. */
6310 visible = 0;
6311 wl_list_for_each(output, &ec->output_list, link) {
6312 if (pixman_region32_contains_point(&output->region,
6313 x, y, NULL)) {
6314 visible = 1;
6315 break;
6316 }
6317 }
6318
6319 if (!visible) {
6320 first_output = container_of(ec->output_list.next,
6321 struct weston_output, link);
6322
6323 x = first_output->x + first_output->width / 4;
6324 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006325
6326 weston_view_set_position(view, x, y);
6327 } else {
6328 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006329 }
6330
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006331
6332 shsurf = get_shell_surface(view->surface);
6333
6334 if (shsurf) {
6335 shsurf->saved_position_valid = false;
6336 shsurf->next_state.maximized = false;
6337 shsurf->next_state.fullscreen = false;
6338 shsurf->state_changed = true;
6339 }
6340}
6341
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006342void
6343shell_for_each_layer(struct desktop_shell *shell,
6344 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006345{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006346 struct workspace **ws;
6347
6348 func(shell, &shell->fullscreen_layer, data);
6349 func(shell, &shell->panel_layer, data);
6350 func(shell, &shell->background_layer, data);
6351 func(shell, &shell->lock_layer, data);
6352 func(shell, &shell->input_panel_layer, data);
6353
6354 wl_array_for_each(ws, &shell->workspaces.array)
6355 func(shell, &(*ws)->layer, data);
6356}
6357
6358static void
6359shell_output_destroy_move_layer(struct desktop_shell *shell,
6360 struct weston_layer *layer,
6361 void *data)
6362{
6363 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006364 struct weston_view *view;
6365
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006366 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006367 if (view->output != output)
6368 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006369
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006370 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006371 }
6372}
6373
6374static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006375handle_output_destroy(struct wl_listener *listener, void *data)
6376{
6377 struct shell_output *output_listener =
6378 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006379 struct weston_output *output = output_listener->output;
6380 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006381
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006382 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006383
Xiong Zhang6b481422013-10-23 13:58:32 +08006384 wl_list_remove(&output_listener->destroy_listener.link);
6385 wl_list_remove(&output_listener->link);
6386 free(output_listener);
6387}
6388
6389static void
6390create_shell_output(struct desktop_shell *shell,
6391 struct weston_output *output)
6392{
6393 struct shell_output *shell_output;
6394
6395 shell_output = zalloc(sizeof *shell_output);
6396 if (shell_output == NULL)
6397 return;
6398
6399 shell_output->output = output;
6400 shell_output->shell = shell;
6401 shell_output->destroy_listener.notify = handle_output_destroy;
6402 wl_signal_add(&output->destroy_signal,
6403 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006404 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006405}
6406
6407static void
6408handle_output_create(struct wl_listener *listener, void *data)
6409{
6410 struct desktop_shell *shell =
6411 container_of(listener, struct desktop_shell, output_create_listener);
6412 struct weston_output *output = (struct weston_output *)data;
6413
6414 create_shell_output(shell, output);
6415}
6416
6417static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006418handle_output_move_layer(struct desktop_shell *shell,
6419 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006420{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006421 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006422 struct weston_view *view;
6423 float x, y;
6424
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006425 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 if (view->output != output)
6427 continue;
6428
6429 x = view->geometry.x + output->move_x;
6430 y = view->geometry.y + output->move_y;
6431 weston_view_set_position(view, x, y);
6432 }
6433}
6434
6435static void
6436handle_output_move(struct wl_listener *listener, void *data)
6437{
6438 struct desktop_shell *shell;
6439
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006440 shell = container_of(listener, struct desktop_shell,
6441 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006442
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006443 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006444}
6445
6446static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006447setup_output_destroy_handler(struct weston_compositor *ec,
6448 struct desktop_shell *shell)
6449{
6450 struct weston_output *output;
6451
6452 wl_list_init(&shell->output_list);
6453 wl_list_for_each(output, &ec->output_list, link)
6454 create_shell_output(shell, output);
6455
6456 shell->output_create_listener.notify = handle_output_create;
6457 wl_signal_add(&ec->output_created_signal,
6458 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006459
6460 shell->output_move_listener.notify = handle_output_move;
6461 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006462}
6463
6464static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006465shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006466{
Tiago Vignattibe143262012-04-16 17:31:41 +03006467 struct desktop_shell *shell =
6468 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006469 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006470 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006471
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006472 /* Force state to unlocked so we don't try to fade */
6473 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006474
6475 if (shell->child.client) {
6476 /* disable respawn */
6477 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006478 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006479 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006480
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09006481 wl_event_source_remove(shell->screensaver.timer);
6482
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006483 wl_list_remove(&shell->idle_listener.link);
6484 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006485
6486 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006487
Xiong Zhang6b481422013-10-23 13:58:32 +08006488 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6489 wl_list_remove(&shell_output->destroy_listener.link);
6490 wl_list_remove(&shell_output->link);
6491 free(shell_output);
6492 }
6493
6494 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006495 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006496
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006497 wl_array_for_each(ws, &shell->workspaces.array)
6498 workspace_destroy(*ws);
6499 wl_array_release(&shell->workspaces.array);
6500
Pekka Paalanen3c647232011-12-22 13:43:43 +02006501 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006502 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006503 free(shell);
6504}
6505
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006506static void
6507shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6508{
6509 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006510 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006511
6512 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006513 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6514 MODIFIER_CTRL | MODIFIER_ALT,
6515 terminate_binding, ec);
6516 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6517 click_to_activate_binding,
6518 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006519 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6520 click_to_activate_binding,
6521 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006522 weston_compositor_add_touch_binding(ec, 0,
6523 touch_to_activate_binding,
6524 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006525 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6526 MODIFIER_SUPER | MODIFIER_ALT,
6527 surface_opacity_binding, NULL);
6528 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6529 MODIFIER_SUPER, zoom_axis_binding,
6530 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006531
6532 /* configurable bindings */
6533 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006534 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6535 zoom_key_binding, NULL);
6536 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6537 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006538 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6539 maximize_binding, NULL);
6540 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6541 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006542 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6543 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006544 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006545 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6546 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006547 weston_compositor_add_button_binding(ec, BTN_LEFT,
6548 mod | MODIFIER_SHIFT,
6549 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006550
6551 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6552 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6553 rotate_binding, NULL);
6554
Daniel Stone325fc2d2012-05-30 16:31:58 +01006555 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6556 shell);
6557 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6558 ec);
6559 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6560 backlight_binding, ec);
6561 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6562 ec);
6563 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6564 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006565 weston_compositor_add_key_binding(ec, KEY_K, mod,
6566 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006567 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6568 workspace_up_binding, shell);
6569 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6570 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006571 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6572 workspace_move_surface_up_binding,
6573 shell);
6574 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6575 workspace_move_surface_down_binding,
6576 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006577
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006578 if (shell->exposay_modifier)
6579 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6580 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006581
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006582 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6583 if (shell->workspaces.num > 1) {
6584 num_workspace_bindings = shell->workspaces.num;
6585 if (num_workspace_bindings > 6)
6586 num_workspace_bindings = 6;
6587 for (i = 0; i < num_workspace_bindings; i++)
6588 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6589 workspace_f_binding,
6590 shell);
6591 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006592
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006593 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006594}
6595
Jason Ekstrand024177c2014-04-21 19:42:58 -05006596static void
6597handle_seat_created(struct wl_listener *listener, void *data)
6598{
6599 struct weston_seat *seat = data;
6600
6601 create_shell_seat(seat);
6602}
6603
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006604WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006605module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006606 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006607{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006608 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006609 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006610 struct workspace **pws;
6611 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006612 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006613
Peter Huttererf3d62272013-08-08 11:57:05 +10006614 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006615 if (shell == NULL)
6616 return -1;
6617
Kristian Høgsberg75840622011-09-06 13:48:16 -04006618 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006619
6620 shell->destroy_listener.notify = shell_destroy;
6621 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006622 shell->idle_listener.notify = idle_handler;
6623 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6624 shell->wake_listener.notify = wake_handler;
6625 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006626
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006627 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006628 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006629 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006630 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006631 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006632 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006633 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006634 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006635 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006636 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006637 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006638 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006639 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006640
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006641 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6642 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006643 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6644 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006645 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006646
6647 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006648 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006649
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006650 if (input_panel_setup(shell) < 0)
6651 return -1;
6652
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006653 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006654
Daniel Stonedf8133b2013-11-19 11:37:14 +01006655 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6656 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6657
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006658 for (i = 0; i < shell->workspaces.num; i++) {
6659 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6660 if (pws == NULL)
6661 return -1;
6662
6663 *pws = workspace_create();
6664 if (*pws == NULL)
6665 return -1;
6666 }
6667 activate_workspace(shell, 0);
6668
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006669 weston_layer_init(&shell->minimized_layer, NULL);
6670
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006671 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006672 wl_list_init(&shell->workspaces.animation.link);
6673 shell->workspaces.animation.frame = animate_workspace_change_frame;
6674
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006675 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006676 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006677 return -1;
6678
Rafael Antognollie2a34552013-12-03 15:35:45 -02006679 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6680 shell, bind_xdg_shell) == NULL)
6681 return -1;
6682
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006683 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006684 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006685 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006686 return -1;
6687
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006688 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6689 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006690 return -1;
6691
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006692 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6693 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006694 return -1;
6695
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006696 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006697
Jonny Lamb765760d2014-08-20 15:53:19 +02006698 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6699
Xiong Zhang6b481422013-10-23 13:58:32 +08006700 setup_output_destroy_handler(ec, shell);
6701
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006702 loop = wl_display_get_event_loop(ec->wl_display);
6703 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006704
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006705 shell->screensaver.timer =
6706 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6707
Jason Ekstrand024177c2014-04-21 19:42:58 -05006708 wl_list_for_each(seat, &ec->seat_list, link)
6709 handle_seat_created(NULL, seat);
6710 shell->seat_create_listener.notify = handle_seat_created;
6711 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006712
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006713 screenshooter_create(ec);
6714
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006715 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006716
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006717 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006718
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006719 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6720
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006721 return 0;
6722}