blob: c05a5c75152f7efb7077898371023fedc828cf04 [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;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900494 } else if (shsurf->resize_edges) {
495 width = shsurf->geometry.width;
496 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700497 } else {
498 width = 0;
499 height = 0;
500 }
501
502 shsurf->client->send_configure(shsurf->surface, width, height);
503}
504
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300505static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400506shell_surface_state_changed(struct shell_surface *shsurf)
507{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700508 if (shell_surface_is_xdg_surface(shsurf))
509 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400510}
511
512static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300513shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300514{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700515 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400516 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700517 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400518
519 if (grab->shsurf->resize_edges) {
520 grab->shsurf->resize_edges = 0;
521 shell_surface_state_changed(grab->shsurf);
522 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700523 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300524
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700525 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300526}
527
528static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700529shell_touch_grab_start(struct shell_touch_grab *grab,
530 const struct weston_touch_grab_interface *interface,
531 struct shell_surface *shsurf,
532 struct weston_touch *touch)
533{
534 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800535
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200536 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700537 if (touch->seat->pointer)
538 popup_grab_end(touch->seat->pointer);
539
Rusty Lynch1084da52013-08-15 09:10:08 -0700540 grab->grab.interface = interface;
541 grab->shsurf = shsurf;
542 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
543 wl_signal_add(&shsurf->destroy_signal,
544 &grab->shsurf_destroy_listener);
545
546 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700547 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700548
549 weston_touch_start_grab(touch, &grab->grab);
550 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500551 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700553}
554
555static void
556shell_touch_grab_end(struct shell_touch_grab *grab)
557{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700558 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700559 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700560 grab->shsurf->grabbed = 0;
561 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700562
563 weston_touch_end_grab(grab->touch);
564}
565
566static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500567center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800568 struct weston_output *output);
569
Daniel Stone496ca172012-05-30 16:31:42 +0100570static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300571get_modifier(char *modifier)
572{
573 if (!modifier)
574 return MODIFIER_SUPER;
575
576 if (!strcmp("ctrl", modifier))
577 return MODIFIER_CTRL;
578 else if (!strcmp("alt", modifier))
579 return MODIFIER_ALT;
580 else if (!strcmp("super", modifier))
581 return MODIFIER_SUPER;
582 else
583 return MODIFIER_SUPER;
584}
585
Juan Zhaoe10d2792012-04-25 19:09:52 +0800586static enum animation_type
587get_animation_type(char *animation)
588{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800589 if (!animation)
590 return ANIMATION_NONE;
591
Juan Zhaoe10d2792012-04-25 19:09:52 +0800592 if (!strcmp("zoom", animation))
593 return ANIMATION_ZOOM;
594 else if (!strcmp("fade", animation))
595 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100596 else if (!strcmp("dim-layer", animation))
597 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800598 else
599 return ANIMATION_NONE;
600}
601
Alex Wu4539b082012-03-01 12:57:46 +0800602static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400603shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200604{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400605 struct weston_config_section *section;
606 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500607 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300608 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200609
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400610 section = weston_config_get_section(shell->compositor->config,
611 "screensaver", NULL, NULL);
612 weston_config_section_get_string(section,
613 "path", &shell->screensaver.path, NULL);
614 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200615 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400616
617 section = weston_config_get_section(shell->compositor->config,
618 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300619 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
620 WESTON_SHELL_CLIENT);
621 if (ret < 0)
622 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400623 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500624 "client", &s, client);
625 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100626 shell->client = s;
627 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400628 "binding-modifier", &s, "super");
629 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200630 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800631
632 weston_config_section_get_string(section,
633 "exposay-modifier", &s, "none");
634 if (strcmp(s, "none") == 0)
635 shell->exposay_modifier = 0;
636 else
637 shell->exposay_modifier = get_modifier(s);
638 free(s);
639
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400640 weston_config_section_get_string(section, "animation", &s, "none");
641 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200642 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200643 weston_config_section_get_string(section, "close-animation", &s, "fade");
644 shell->win_close_animation_type = get_animation_type(s);
645 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700646 weston_config_section_get_string(section,
647 "startup-animation", &s, "fade");
648 shell->startup_animation_type = get_animation_type(s);
649 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700650 if (shell->startup_animation_type == ANIMATION_ZOOM)
651 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100652 weston_config_section_get_string(section, "focus-animation", &s, "none");
653 shell->focus_animation_type = get_animation_type(s);
654 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400655 weston_config_section_get_uint(section, "num-workspaces",
656 &shell->workspaces.num,
657 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200658}
659
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800660struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100661get_default_output(struct weston_compositor *compositor)
662{
663 return container_of(compositor->output_list.next,
664 struct weston_output, link);
665}
666
Pekka Paalanen8274d902014-08-06 19:36:51 +0300667static int
668focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
669{
670 return snprintf(buf, len, "focus highlight effect for output %s",
671 surface->output->name);
672}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673
674/* no-op func for checking focus surface */
675static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600676focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100677{
678}
679
680static struct focus_surface *
681get_focus_surface(struct weston_surface *surface)
682{
683 if (surface->configure == focus_surface_configure)
684 return surface->configure_private;
685 else
686 return NULL;
687}
688
689static bool
690is_focus_surface (struct weston_surface *es)
691{
692 return (es->configure == focus_surface_configure);
693}
694
695static bool
696is_focus_view (struct weston_view *view)
697{
698 return is_focus_surface (view->surface);
699}
700
701static struct focus_surface *
702create_focus_surface(struct weston_compositor *ec,
703 struct weston_output *output)
704{
705 struct focus_surface *fsurf = NULL;
706 struct weston_surface *surface = NULL;
707
708 fsurf = malloc(sizeof *fsurf);
709 if (!fsurf)
710 return NULL;
711
712 fsurf->surface = weston_surface_create(ec);
713 surface = fsurf->surface;
714 if (surface == NULL) {
715 free(fsurf);
716 return NULL;
717 }
718
719 surface->configure = focus_surface_configure;
720 surface->output = output;
721 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300722 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100723
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800724 fsurf->view = weston_view_create(surface);
725 if (fsurf->view == NULL) {
726 weston_surface_destroy(surface);
727 free(fsurf);
728 return NULL;
729 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100730 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100731
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600732 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600733 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100734 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
735 pixman_region32_fini(&surface->opaque);
736 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
737 output->width, output->height);
738 pixman_region32_fini(&surface->input);
739 pixman_region32_init(&surface->input);
740
741 wl_list_init(&fsurf->workspace_transform.link);
742
743 return fsurf;
744}
745
746static void
747focus_surface_destroy(struct focus_surface *fsurf)
748{
749 weston_surface_destroy(fsurf->surface);
750 free(fsurf);
751}
752
753static void
754focus_animation_done(struct weston_view_animation *animation, void *data)
755{
756 struct workspace *ws = data;
757
758 ws->focus_animation = NULL;
759}
760
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200761static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200762focus_state_destroy(struct focus_state *state)
763{
764 wl_list_remove(&state->seat_destroy_listener.link);
765 wl_list_remove(&state->surface_destroy_listener.link);
766 free(state);
767}
768
769static void
770focus_state_seat_destroy(struct wl_listener *listener, void *data)
771{
772 struct focus_state *state = container_of(listener,
773 struct focus_state,
774 seat_destroy_listener);
775
776 wl_list_remove(&state->link);
777 focus_state_destroy(state);
778}
779
780static void
781focus_state_surface_destroy(struct wl_listener *listener, void *data)
782{
783 struct focus_state *state = container_of(listener,
784 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400785 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400786 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500787 struct weston_surface *main_surface, *next;
788 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200789
Pekka Paalanen01388e22013-04-25 13:57:44 +0300790 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
791
Kristian Høgsberge3778222012-07-31 17:29:30 -0400792 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300793 wl_list_for_each(view,
794 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400796 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100797 if (is_focus_view(view))
798 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400799
Jason Ekstranda7af7042013-10-12 22:38:11 -0500800 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400801 break;
802 }
803
Pekka Paalanen01388e22013-04-25 13:57:44 +0300804 /* if the focus was a sub-surface, activate its main surface */
805 if (main_surface != state->keyboard_focus)
806 next = main_surface;
807
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100808 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400809 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100810 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100811 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400812 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100813 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
814 if (state->ws->focus_animation)
815 weston_view_animation_destroy(state->ws->focus_animation);
816
817 state->ws->focus_animation = weston_fade_run(
818 state->ws->fsurf_front->view,
819 state->ws->fsurf_front->view->alpha, 0.0, 300,
820 focus_animation_done, state->ws);
821 }
822
Kristian Høgsberge3778222012-07-31 17:29:30 -0400823 wl_list_remove(&state->link);
824 focus_state_destroy(state);
825 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200826}
827
828static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400829focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200830{
Jonas Ådahl04769742012-06-13 00:01:24 +0200831 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200832
833 state = malloc(sizeof *state);
834 if (state == NULL)
835 return NULL;
836
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400838 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200839 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400840 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200841
842 state->seat_destroy_listener.notify = focus_state_seat_destroy;
843 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400844 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200845 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200847
848 return state;
849}
850
Jonas Ådahl8538b222012-08-29 22:13:03 +0200851static struct focus_state *
852ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
853{
854 struct workspace *ws = get_current_workspace(shell);
855 struct focus_state *state;
856
857 wl_list_for_each(state, &ws->focus_list, link)
858 if (state->seat == seat)
859 break;
860
861 if (&state->link == &ws->focus_list)
862 state = focus_state_create(seat, ws);
863
864 return state;
865}
866
Jonas Ådahl04769742012-06-13 00:01:24 +0200867static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800868focus_state_set_focus(struct focus_state *state,
869 struct weston_surface *surface)
870{
871 if (state->keyboard_focus) {
872 wl_list_remove(&state->surface_destroy_listener.link);
873 wl_list_init(&state->surface_destroy_listener.link);
874 }
875
876 state->keyboard_focus = surface;
877 if (surface)
878 wl_signal_add(&surface->destroy_signal,
879 &state->surface_destroy_listener);
880}
881
882static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400883restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200884{
885 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400886 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100887 struct wl_list pending_seat_list;
888 struct weston_seat *seat, *next_seat;
889
890 /* Temporarily steal the list of seats so that we can keep
891 * track of the seats we've already processed */
892 wl_list_init(&pending_seat_list);
893 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
894 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200895
896 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100897 wl_list_remove(&state->seat->link);
898 wl_list_insert(&shell->compositor->seat_list,
899 &state->seat->link);
900
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800901 if (state->seat->keyboard == NULL)
902 continue;
903
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400904 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200905
Kristian Høgsberge3148752013-05-06 23:19:49 -0400906 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200907 }
Neil Roberts4237f502014-04-09 16:33:31 +0100908
909 /* For any remaining seats that we don't have a focus state
910 * for we'll reset the keyboard focus to NULL */
911 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
912 wl_list_insert(&shell->compositor->seat_list, &seat->link);
913
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300914 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100915 continue;
916
917 weston_keyboard_set_focus(seat->keyboard, NULL);
918 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200919}
920
921static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200922replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
923 struct weston_seat *seat)
924{
925 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926
927 wl_list_for_each(state, &ws->focus_list, link) {
928 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800929 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930 return;
931 }
932 }
933}
934
935static void
936drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
937 struct weston_surface *surface)
938{
939 struct focus_state *state;
940
941 wl_list_for_each(state, &ws->focus_list, link)
942 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800943 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200944}
945
946static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
948 struct weston_view *from, struct weston_view *to)
949{
950 struct weston_output *output;
951 bool focus_surface_created = false;
952
953 /* FIXME: Only support dim animation using two layers */
954 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
955 return;
956
957 output = get_default_output(shell->compositor);
958 if (ws->fsurf_front == NULL && (from || to)) {
959 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800960 if (ws->fsurf_front == NULL)
961 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100962 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800963
964 ws->fsurf_back = create_focus_surface(shell->compositor, output);
965 if (ws->fsurf_back == NULL) {
966 focus_surface_destroy(ws->fsurf_front);
967 return;
968 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800970
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971 focus_surface_created = true;
972 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300973 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
974 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100975 }
976
977 if (ws->focus_animation) {
978 weston_view_animation_destroy(ws->focus_animation);
979 ws->focus_animation = NULL;
980 }
981
982 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300983 weston_layer_entry_insert(&to->layer_link,
984 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300986 weston_layer_entry_insert(&ws->layer.view_list,
987 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988
989 if (focus_surface_created) {
990 ws->focus_animation = weston_fade_run(
991 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200992 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 focus_animation_done, ws);
994 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300995 weston_layer_entry_insert(&from->layer_link,
996 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 ws->focus_animation = weston_stable_fade_run(
998 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200999 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 focus_animation_done, ws);
1001 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001002 weston_layer_entry_insert(&ws->layer.view_list,
1003 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 ws->focus_animation = weston_stable_fade_run(
1005 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001006 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 focus_animation_done, ws);
1008 }
1009}
1010
1011static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001012workspace_destroy(struct workspace *ws)
1013{
Jonas Ådahl04769742012-06-13 00:01:24 +02001014 struct focus_state *state, *next;
1015
1016 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1017 focus_state_destroy(state);
1018
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001019 if (ws->fsurf_front)
1020 focus_surface_destroy(ws->fsurf_front);
1021 if (ws->fsurf_back)
1022 focus_surface_destroy(ws->fsurf_back);
1023
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001024 free(ws);
1025}
1026
Jonas Ådahl04769742012-06-13 00:01:24 +02001027static void
1028seat_destroyed(struct wl_listener *listener, void *data)
1029{
1030 struct weston_seat *seat = data;
1031 struct focus_state *state, *next;
1032 struct workspace *ws = container_of(listener,
1033 struct workspace,
1034 seat_destroyed_listener);
1035
1036 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1037 if (state->seat == seat)
1038 wl_list_remove(&state->link);
1039}
1040
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001041static struct workspace *
1042workspace_create(void)
1043{
1044 struct workspace *ws = malloc(sizeof *ws);
1045 if (ws == NULL)
1046 return NULL;
1047
1048 weston_layer_init(&ws->layer, NULL);
1049
Jonas Ådahl04769742012-06-13 00:01:24 +02001050 wl_list_init(&ws->focus_list);
1051 wl_list_init(&ws->seat_destroyed_listener.link);
1052 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001053 ws->fsurf_front = NULL;
1054 ws->fsurf_back = NULL;
1055 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001056
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001057 return ws;
1058}
1059
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060static int
1061workspace_is_empty(struct workspace *ws)
1062{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001063 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064}
1065
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001066static struct workspace *
1067get_workspace(struct desktop_shell *shell, unsigned int index)
1068{
1069 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001070 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001071 pws += index;
1072 return *pws;
1073}
1074
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001075struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001076get_current_workspace(struct desktop_shell *shell)
1077{
1078 return get_workspace(shell, shell->workspaces.current);
1079}
1080
1081static void
1082activate_workspace(struct desktop_shell *shell, unsigned int index)
1083{
1084 struct workspace *ws;
1085
1086 ws = get_workspace(shell, index);
1087 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1088
1089 shell->workspaces.current = index;
1090}
1091
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001092static unsigned int
1093get_output_height(struct weston_output *output)
1094{
1095 return abs(output->region.extents.y1 - output->region.extents.y2);
1096}
1097
1098static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001099view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 struct weston_transform *transform;
1102
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001103 if (is_focus_view(view)) {
1104 struct focus_surface *fsurf = get_focus_surface(view->surface);
1105 transform = &fsurf->workspace_transform;
1106 } else {
1107 struct shell_surface *shsurf = get_shell_surface(view->surface);
1108 transform = &shsurf->workspace_transform;
1109 }
1110
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001113 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001115 weston_matrix_init(&transform->matrix);
1116 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119}
1120
1121static void
1122workspace_translate_out(struct workspace *ws, double fraction)
1123{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 unsigned int height;
1126 double d;
1127
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001128 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 d = height * fraction;
1131
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001132 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 }
1134}
1135
1136static void
1137workspace_translate_in(struct workspace *ws, double fraction)
1138{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140 unsigned int height;
1141 double d;
1142
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001143 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145
1146 if (fraction > 0)
1147 d = -(height - height * fraction);
1148 else
1149 d = height + height * fraction;
1150
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001151 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 }
1153}
1154
1155static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001156broadcast_current_workspace_state(struct desktop_shell *shell)
1157{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001158 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001159
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001160 wl_resource_for_each(resource, &shell->workspaces.client_list)
1161 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001162 shell->workspaces.current,
1163 shell->workspaces.num);
1164}
1165
1166static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167reverse_workspace_change_animation(struct desktop_shell *shell,
1168 unsigned int index,
1169 struct workspace *from,
1170 struct workspace *to)
1171{
1172 shell->workspaces.current = index;
1173
1174 shell->workspaces.anim_to = to;
1175 shell->workspaces.anim_from = from;
1176 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1177 shell->workspaces.anim_timestamp = 0;
1178
Scott Moreau4272e632012-08-13 09:58:41 -06001179 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001180}
1181
1182static void
1183workspace_deactivate_transforms(struct workspace *ws)
1184{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001186 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001188 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001189 if (is_focus_view(view)) {
1190 struct focus_surface *fsurf = get_focus_surface(view->surface);
1191 transform = &fsurf->workspace_transform;
1192 } else {
1193 struct shell_surface *shsurf = get_shell_surface(view->surface);
1194 transform = &shsurf->workspace_transform;
1195 }
1196
1197 if (!wl_list_empty(&transform->link)) {
1198 wl_list_remove(&transform->link);
1199 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 }
1203}
1204
1205static void
1206finish_workspace_change_animation(struct desktop_shell *shell,
1207 struct workspace *from,
1208 struct workspace *to)
1209{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001210 struct weston_view *view;
1211
Scott Moreau4272e632012-08-13 09:58:41 -06001212 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001213
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001214 /* Views that extend past the bottom of the output are still
1215 * visible after the workspace animation ends but before its layer
1216 * is hidden. In that case, we need to damage below those views so
1217 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001218 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001219 weston_view_damage_below(view);
1220
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221 wl_list_remove(&shell->workspaces.animation.link);
1222 workspace_deactivate_transforms(from);
1223 workspace_deactivate_transforms(to);
1224 shell->workspaces.anim_to = NULL;
1225
1226 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1227}
1228
1229static void
1230animate_workspace_change_frame(struct weston_animation *animation,
1231 struct weston_output *output, uint32_t msecs)
1232{
1233 struct desktop_shell *shell =
1234 container_of(animation, struct desktop_shell,
1235 workspaces.animation);
1236 struct workspace *from = shell->workspaces.anim_from;
1237 struct workspace *to = shell->workspaces.anim_to;
1238 uint32_t t;
1239 double x, y;
1240
1241 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1242 finish_workspace_change_animation(shell, from, to);
1243 return;
1244 }
1245
1246 if (shell->workspaces.anim_timestamp == 0) {
1247 if (shell->workspaces.anim_current == 0.0)
1248 shell->workspaces.anim_timestamp = msecs;
1249 else
1250 shell->workspaces.anim_timestamp =
1251 msecs -
1252 /* Invers of movement function 'y' below. */
1253 (asin(1.0 - shell->workspaces.anim_current) *
1254 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1255 M_2_PI);
1256 }
1257
1258 t = msecs - shell->workspaces.anim_timestamp;
1259
1260 /*
1261 * x = [0, π/2]
1262 * y(x) = sin(x)
1263 */
1264 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1265 y = sin(x);
1266
1267 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001268 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001269
1270 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1271 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1272 shell->workspaces.anim_current = y;
1273
Scott Moreau4272e632012-08-13 09:58:41 -06001274 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001275 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001276 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001277 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001278}
1279
1280static void
1281animate_workspace_change(struct desktop_shell *shell,
1282 unsigned int index,
1283 struct workspace *from,
1284 struct workspace *to)
1285{
1286 struct weston_output *output;
1287
1288 int dir;
1289
1290 if (index > shell->workspaces.current)
1291 dir = -1;
1292 else
1293 dir = 1;
1294
1295 shell->workspaces.current = index;
1296
1297 shell->workspaces.anim_dir = dir;
1298 shell->workspaces.anim_from = from;
1299 shell->workspaces.anim_to = to;
1300 shell->workspaces.anim_current = 0.0;
1301 shell->workspaces.anim_timestamp = 0;
1302
1303 output = container_of(shell->compositor->output_list.next,
1304 struct weston_output, link);
1305 wl_list_insert(&output->animation_list,
1306 &shell->workspaces.animation.link);
1307
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309
1310 workspace_translate_in(to, 0);
1311
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001312 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001313
Scott Moreau4272e632012-08-13 09:58:41 -06001314 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001315}
1316
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001317static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318update_workspace(struct desktop_shell *shell, unsigned int index,
1319 struct workspace *from, struct workspace *to)
1320{
1321 shell->workspaces.current = index;
1322 wl_list_insert(&from->layer.link, &to->layer.link);
1323 wl_list_remove(&from->layer.link);
1324}
1325
1326static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001327change_workspace(struct desktop_shell *shell, unsigned int index)
1328{
1329 struct workspace *from;
1330 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001331 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001332
1333 if (index == shell->workspaces.current)
1334 return;
1335
1336 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001337 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001338 return;
1339
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001340 from = get_current_workspace(shell);
1341 to = get_workspace(shell, index);
1342
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001343 if (shell->workspaces.anim_from == to &&
1344 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001345 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001346 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001347 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001348 return;
1349 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001350
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001351 if (shell->workspaces.anim_to != NULL)
1352 finish_workspace_change_animation(shell,
1353 shell->workspaces.anim_from,
1354 shell->workspaces.anim_to);
1355
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001356 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001357
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001358 if (shell->focus_animation_type != ANIMATION_NONE) {
1359 wl_list_for_each(state, &from->focus_list, link)
1360 if (state->keyboard_focus)
1361 animate_focus_change(shell, from,
1362 get_default_view(state->keyboard_focus), NULL);
1363
1364 wl_list_for_each(state, &to->focus_list, link)
1365 if (state->keyboard_focus)
1366 animate_focus_change(shell, to,
1367 NULL, get_default_view(state->keyboard_focus));
1368 }
1369
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001370 if (workspace_is_empty(to) && workspace_is_empty(from))
1371 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001372 else
1373 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001374
1375 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001376}
1377
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378static bool
1379workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1380{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001381 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001382 struct wl_list *e;
1383
1384 if (wl_list_empty(list))
1385 return false;
1386
1387 e = list->next;
1388
1389 if (e->next != list)
1390 return false;
1391
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001392 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001393}
1394
1395static void
Philip Withnall659163d2013-11-25 18:01:36 +00001396move_surface_to_workspace(struct desktop_shell *shell,
1397 struct shell_surface *shsurf,
1398 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001399{
1400 struct workspace *from;
1401 struct workspace *to;
1402 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001403 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001404 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405
1406 if (workspace == shell->workspaces.current)
1407 return;
1408
Philip Withnall659163d2013-11-25 18:01:36 +00001409 view = get_default_view(shsurf->surface);
1410 if (!view)
1411 return;
1412
1413 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1414
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001415 if (workspace >= shell->workspaces.num)
1416 workspace = shell->workspaces.num - 1;
1417
Jonas Ådahle9d22502012-08-29 22:13:01 +02001418 from = get_current_workspace(shell);
1419 to = get_workspace(shell, workspace);
1420
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001421 weston_layer_entry_remove(&view->layer_link);
1422 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001423
Philip Withnall648a4dd2013-11-25 18:01:44 +00001424 shell_surface_update_child_surface_layers(shsurf);
1425
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001427 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1428 if (!seat->keyboard)
1429 continue;
1430
1431 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001433 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001434 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001437}
1438
1439static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001440take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001441 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442 unsigned int index)
1443{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001444 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446 struct shell_surface *shsurf;
1447 struct workspace *from;
1448 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001449 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001450
Pekka Paalanen01388e22013-04-25 13:57:44 +03001451 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 view = get_default_view(surface);
1453 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001454 index == shell->workspaces.current ||
1455 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001456 return;
1457
1458 from = get_current_workspace(shell);
1459 to = get_workspace(shell, index);
1460
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001461 weston_layer_entry_remove(&view->layer_link);
1462 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001463
Philip Withnall659163d2013-11-25 18:01:36 +00001464 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001465 if (shsurf != NULL)
1466 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001467
Jonas Ådahle9d22502012-08-29 22:13:01 +02001468 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001469 drop_focus_state(shell, from, surface);
1470
1471 if (shell->workspaces.anim_from == to &&
1472 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001473 wl_list_remove(&to->layer.link);
1474 wl_list_insert(from->layer.link.prev, &to->layer.link);
1475
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001476 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477 broadcast_current_workspace_state(shell);
1478
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001479 return;
1480 }
1481
1482 if (shell->workspaces.anim_to != NULL)
1483 finish_workspace_change_animation(shell,
1484 shell->workspaces.anim_from,
1485 shell->workspaces.anim_to);
1486
1487 if (workspace_is_empty(from) &&
1488 workspace_has_only(to, surface))
1489 update_workspace(shell, index, from, to);
1490 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001491 if (shsurf != NULL &&
1492 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001493 wl_list_insert(&shell->workspaces.anim_sticky_list,
1494 &shsurf->workspace_transform.link);
1495
1496 animate_workspace_change(shell, index, from, to);
1497 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001498
1499 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001500
1501 state = ensure_focus_state(shell, seat);
1502 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001503 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001504}
1505
1506static void
1507workspace_manager_move_surface(struct wl_client *client,
1508 struct wl_resource *resource,
1509 struct wl_resource *surface_resource,
1510 uint32_t workspace)
1511{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001512 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001513 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001514 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001515 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001516 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001517
Pekka Paalanen01388e22013-04-25 13:57:44 +03001518 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001519 shell_surface = get_shell_surface(main_surface);
1520 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001522
1523 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001524}
1525
1526static const struct workspace_manager_interface workspace_manager_implementation = {
1527 workspace_manager_move_surface,
1528};
1529
1530static void
1531unbind_resource(struct wl_resource *resource)
1532{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001533 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001534}
1535
1536static void
1537bind_workspace_manager(struct wl_client *client,
1538 void *data, uint32_t version, uint32_t id)
1539{
1540 struct desktop_shell *shell = data;
1541 struct wl_resource *resource;
1542
Jason Ekstranda85118c2013-06-27 20:17:02 -05001543 resource = wl_resource_create(client,
1544 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001545
1546 if (resource == NULL) {
1547 weston_log("couldn't add workspace manager object");
1548 return;
1549 }
1550
Jason Ekstranda85118c2013-06-27 20:17:02 -05001551 wl_resource_set_implementation(resource,
1552 &workspace_manager_implementation,
1553 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001554 wl_list_insert(&shell->workspaces.client_list,
1555 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001556
1557 workspace_manager_send_state(resource,
1558 shell->workspaces.current,
1559 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001560}
1561
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001562static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001563touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1564 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1565{
1566}
1567
1568static void
1569touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1570{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001571 struct weston_touch_move_grab *move =
1572 (struct weston_touch_move_grab *) container_of(
1573 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001574
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001575 if (touch_id == 0)
1576 move->active = 0;
1577
Jonas Ådahl9484b692013-12-02 22:05:03 +01001578 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001579 shell_touch_grab_end(&move->base);
1580 free(move);
1581 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001582}
1583
1584static void
1585touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1586 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1587{
1588 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1589 struct shell_surface *shsurf = move->base.shsurf;
1590 struct weston_surface *es;
1591 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1592 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1593
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001594 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001595 return;
1596
1597 es = shsurf->surface;
1598
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001599 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001600
1601 weston_compositor_schedule_repaint(es->compositor);
1602}
1603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001605touch_move_grab_frame(struct weston_touch_grab *grab)
1606{
1607}
1608
1609static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001610touch_move_grab_cancel(struct weston_touch_grab *grab)
1611{
1612 struct weston_touch_move_grab *move =
1613 (struct weston_touch_move_grab *) container_of(
1614 grab, struct shell_touch_grab, grab);
1615
1616 shell_touch_grab_end(&move->base);
1617 free(move);
1618}
1619
Rusty Lynch1084da52013-08-15 09:10:08 -07001620static const struct weston_touch_grab_interface touch_move_grab_interface = {
1621 touch_move_grab_down,
1622 touch_move_grab_up,
1623 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001624 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001625 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001626};
1627
1628static int
1629surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1630{
1631 struct weston_touch_move_grab *move;
1632
1633 if (!shsurf)
1634 return -1;
1635
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001636 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001637 return 0;
1638
1639 move = malloc(sizeof *move);
1640 if (!move)
1641 return -1;
1642
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001643 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001645 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001647 seat->touch->grab_y;
1648
1649 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1650 seat->touch);
1651
1652 return 0;
1653}
1654
1655static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001656noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001657{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001658}
1659
1660static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001661constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1662{
1663 struct shell_surface *shsurf = move->base.shsurf;
1664 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001665 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001666 const int safety = 50;
1667
1668 x = wl_fixed_to_int(pointer->x + move->dx);
1669 y = wl_fixed_to_int(pointer->y + move->dy);
1670
Jonny Lambd73c6942014-08-20 15:53:20 +02001671 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1672 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1673 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001674
Jonny Lambd73c6942014-08-20 15:53:20 +02001675 bottom = y + shsurf->geometry.height;
1676 if (bottom - panel_height < safety)
1677 y = panel_height + safety -
1678 shsurf->geometry.height;
1679
1680 if (move->client_initiated &&
1681 y + shsurf->geometry.y < panel_height)
1682 y = panel_height - shsurf->geometry.y;
1683 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001684
1685 *cx = x;
1686 *cy = y;
1687}
1688
1689static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001690move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1691 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001692{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001693 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001694 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001695 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001696 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001697
1698 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001699 if (!shsurf)
1700 return;
1701
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001702 constrain_position(move, &cx, &cy);
1703
1704 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001705
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001707}
1708
1709static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001710move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001711 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001712{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001713 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1714 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001715 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001716 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001717
Daniel Stone4dbadb12012-05-30 16:31:51 +01001718 if (pointer->button_count == 0 &&
1719 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001720 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001721 free(grab);
1722 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001723}
1724
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001725static void
1726move_grab_cancel(struct weston_pointer_grab *grab)
1727{
1728 struct shell_grab *shell_grab =
1729 container_of(grab, struct shell_grab, grab);
1730
1731 shell_grab_end(shell_grab);
1732 free(grab);
1733}
1734
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001735static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001736 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001737 move_grab_motion,
1738 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001739 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001740};
1741
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001743surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1744 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745{
1746 struct weston_move_grab *move;
1747
1748 if (!shsurf)
1749 return -1;
1750
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001751 if (shsurf->grabbed ||
1752 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753 return 0;
1754
1755 move = malloc(sizeof *move);
1756 if (!move)
1757 return -1;
1758
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001760 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001762 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001763 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764
1765 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001766 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767
1768 return 0;
1769}
1770
1771static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001772common_surface_move(struct wl_resource *resource,
1773 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001775 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001776 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001777 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001779 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001780 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001781 seat->pointer->button_count > 0 &&
1782 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001784 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001785 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001786 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001787 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001788 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001789 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001791 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001792 (surface_touch_move(shsurf, seat) < 0))
1793 wl_resource_post_no_memory(resource);
1794 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001795}
1796
Rafael Antognollie2a34552013-12-03 15:35:45 -02001797static void
1798shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1799 struct wl_resource *seat_resource, uint32_t serial)
1800{
1801 common_surface_move(resource, seat_resource, serial);
1802}
1803
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804struct weston_resize_grab {
1805 struct shell_grab base;
1806 uint32_t edges;
1807 int32_t width, height;
1808};
1809
1810static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001811resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1812 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813{
1814 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001815 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001816 struct shell_surface *shsurf = resize->base.shsurf;
1817 int32_t width, height;
1818 wl_fixed_t from_x, from_y;
1819 wl_fixed_t to_x, to_y;
1820
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001821 weston_pointer_move(pointer, x, y);
1822
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001823 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001824 return;
1825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 weston_view_from_global_fixed(shsurf->view,
1827 pointer->grab_x, pointer->grab_y,
1828 &from_x, &from_y);
1829 weston_view_from_global_fixed(shsurf->view,
1830 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831
1832 width = resize->width;
1833 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1834 width += wl_fixed_to_int(from_x - to_x);
1835 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1836 width += wl_fixed_to_int(to_x - from_x);
1837 }
1838
1839 height = resize->height;
1840 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1841 height += wl_fixed_to_int(from_y - to_y);
1842 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1843 height += wl_fixed_to_int(to_y - from_y);
1844 }
1845
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001846 if (width < 1)
1847 width = 1;
1848 if (height < 1)
1849 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001850 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851}
1852
1853static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001854send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855{
1856 struct shell_surface *shsurf = get_shell_surface(surface);
1857
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001858 assert(shsurf);
1859
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001860 if (shsurf->resource)
1861 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001862 shsurf->resize_edges,
1863 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001864}
1865
1866static const struct weston_shell_client shell_client = {
1867 send_configure
1868};
1869
1870static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001871resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001872 uint32_t time, uint32_t button, uint32_t state_w)
1873{
1874 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001875 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001876 enum wl_pointer_button_state state = state_w;
1877
1878 if (pointer->button_count == 0 &&
1879 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1880 shell_grab_end(&resize->base);
1881 free(grab);
1882 }
1883}
1884
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001885static void
1886resize_grab_cancel(struct weston_pointer_grab *grab)
1887{
1888 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1889
1890 shell_grab_end(&resize->base);
1891 free(grab);
1892}
1893
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001894static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895 noop_grab_focus,
1896 resize_grab_motion,
1897 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001898 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001899};
1900
Giulio Camuffob8366642013-04-25 13:57:46 +03001901/*
1902 * Returns the bounding box of a surface and all its sub-surfaces,
1903 * in the surface coordinates system. */
1904static void
1905surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1906 int32_t *y, int32_t *w, int32_t *h) {
1907 pixman_region32_t region;
1908 pixman_box32_t *box;
1909 struct weston_subsurface *subsurface;
1910
1911 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001912 surface->width,
1913 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001914
1915 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1916 pixman_region32_union_rect(&region, &region,
1917 subsurface->position.x,
1918 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 subsurface->surface->width,
1920 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001921 }
1922
1923 box = pixman_region32_extents(&region);
1924 if (x)
1925 *x = box->x1;
1926 if (y)
1927 *y = box->y1;
1928 if (w)
1929 *w = box->x2 - box->x1;
1930 if (h)
1931 *h = box->y2 - box->y1;
1932
1933 pixman_region32_fini(&region);
1934}
1935
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001936static int
1937surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001938 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939{
1940 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001941 const unsigned resize_topbottom =
1942 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1943 const unsigned resize_leftright =
1944 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1945 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001947 if (shsurf->grabbed ||
1948 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949 return 0;
1950
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001951 /* Check for invalid edge combinations. */
1952 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1953 (edges & resize_topbottom) == resize_topbottom ||
1954 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001955 return 0;
1956
1957 resize = malloc(sizeof *resize);
1958 if (!resize)
1959 return -1;
1960
1961 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001962
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001963 resize->width = shsurf->geometry.width;
1964 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001965
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001966 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001967 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001968 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001969 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001970
1971 return 0;
1972}
1973
1974static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001975common_surface_resize(struct wl_resource *resource,
1976 struct wl_resource *seat_resource, uint32_t serial,
1977 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001978{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001979 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001980 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001981 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001982
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001983 if (seat->pointer == NULL ||
1984 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001985 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001986 seat->pointer->focus == NULL)
1987 return;
1988
1989 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1990 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001991 return;
1992
Kristian Høgsberge3148752013-05-06 23:19:49 -04001993 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001994 wl_resource_post_no_memory(resource);
1995}
1996
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001997static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001998shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1999 struct wl_resource *seat_resource, uint32_t serial,
2000 uint32_t edges)
2001{
2002 common_surface_resize(resource, seat_resource, serial, edges);
2003}
2004
2005static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002006busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002007{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002008 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002009 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002011 wl_fixed_t sx, sy;
2012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 view = weston_compositor_pick_view(pointer->seat->compositor,
2014 pointer->x, pointer->y,
2015 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002016
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002017 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2018 shell_grab_end(grab);
2019 free(grab);
2020 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002021}
2022
2023static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002024busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2025 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002026{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002027 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002029
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002031busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002032 uint32_t time, uint32_t button, uint32_t state)
2033{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002034 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002035 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002036 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002037
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002038 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002039 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002040 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002041 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002042 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002043 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002044 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002045}
2046
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002047static void
2048busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2049{
2050 struct shell_grab *grab = (struct shell_grab *) base;
2051
2052 shell_grab_end(grab);
2053 free(grab);
2054}
2055
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002056static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057 busy_cursor_grab_focus,
2058 busy_cursor_grab_motion,
2059 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002060 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061};
2062
2063static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002064set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065{
2066 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002067
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002068 if (pointer->grab->interface == &busy_cursor_grab_interface)
2069 return;
2070
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071 grab = malloc(sizeof *grab);
2072 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002073 return;
2074
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002075 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2076 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002077 /* Mark the shsurf as ungrabbed so that button binding is able
2078 * to move it. */
2079 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002080}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002081
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002082static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002083end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002084{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002085 struct shell_grab *grab;
2086 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002087
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002088 wl_list_for_each(seat, &compositor->seat_list, link) {
2089 if (seat->pointer == NULL)
2090 continue;
2091
2092 grab = (struct shell_grab *) seat->pointer->grab;
2093 if (grab->grab.interface == &busy_cursor_grab_interface &&
2094 wl_resource_get_client(grab->shsurf->resource) == client) {
2095 shell_grab_end(grab);
2096 free(grab);
2097 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002098 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002099}
2100
Scott Moreau9521d5e2012-04-19 13:06:17 -06002101static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002102handle_shell_client_destroy(struct wl_listener *listener, void *data);
2103
2104static int
2105xdg_ping_timeout_handler(void *data)
2106{
2107 struct shell_client *sc = data;
2108 struct weston_seat *seat;
2109 struct shell_surface *shsurf;
2110
2111 /* Client is not responding */
2112 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002113 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2114 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2115 continue;
2116 if (seat->pointer->focus->surface->resource == NULL)
2117 continue;
2118
2119 shsurf = get_shell_surface(seat->pointer->focus->surface);
2120 if (shsurf &&
2121 wl_resource_get_client(shsurf->resource) == sc->client)
2122 set_busy_cursor(shsurf, seat->pointer);
2123 }
2124
2125 return 1;
2126}
2127
2128static void
2129handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2130{
2131 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002132 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002133 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002134 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002135
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002136 if (sc->unresponsive) {
2137 xdg_ping_timeout_handler(sc);
2138 return;
2139 }
2140
2141 sc->ping_serial = serial;
2142 loop = wl_display_get_event_loop(compositor->wl_display);
2143 if (sc->ping_timer == NULL)
2144 sc->ping_timer =
2145 wl_event_loop_add_timer(loop,
2146 xdg_ping_timeout_handler, sc);
2147 if (sc->ping_timer == NULL)
2148 return;
2149
2150 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2151
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002152 if (shell_surface_is_xdg_surface(shsurf) ||
2153 shell_surface_is_xdg_popup(shsurf))
2154 xdg_shell_send_ping(sc->resource, serial);
2155 else if (shell_surface_is_wl_shell_surface(shsurf))
2156 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002157}
2158
Scott Moreauff1db4a2012-04-17 19:06:18 -06002159static void
2160ping_handler(struct weston_surface *surface, uint32_t serial)
2161{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002162 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002163
2164 if (!shsurf)
2165 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002166 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002167 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002168 if (shsurf->surface == shsurf->shell->grab_surface)
2169 return;
2170
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002171 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002172}
2173
2174static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002175handle_pointer_focus(struct wl_listener *listener, void *data)
2176{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002177 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002179 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002180 uint32_t serial;
2181
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183 return;
2184
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002186 serial = wl_display_next_serial(compositor->wl_display);
2187 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002188}
2189
2190static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002191shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2192{
2193 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002194 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002195}
2196
2197static void
2198shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2199{
2200 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002201 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002202}
2203
2204static void
2205handle_keyboard_focus(struct wl_listener *listener, void *data)
2206{
2207 struct weston_keyboard *keyboard = data;
2208 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2209
2210 if (seat->focused_surface) {
2211 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2212 if (shsurf)
2213 shell_surface_lose_keyboard_focus(shsurf);
2214 }
2215
2216 seat->focused_surface = keyboard->focus;
2217
2218 if (seat->focused_surface) {
2219 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2220 if (shsurf)
2221 shell_surface_gain_keyboard_focus(shsurf);
2222 }
2223}
2224
2225static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002226shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002227{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002228 if (sc->ping_serial != serial)
2229 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002230
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002231 sc->unresponsive = 0;
2232 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002233
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002234 if (sc->ping_timer) {
2235 wl_event_source_remove(sc->ping_timer);
2236 sc->ping_timer = NULL;
2237 }
2238
2239}
2240
2241static void
2242shell_surface_pong(struct wl_client *client,
2243 struct wl_resource *resource, uint32_t serial)
2244{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002245 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2246 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002247
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002248 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002249}
2250
2251static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002252set_title(struct shell_surface *shsurf, const char *title)
2253{
2254 free(shsurf->title);
2255 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002256 shsurf->surface->timeline.force_refresh = 1;
2257}
2258
2259static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002260set_pid(struct shell_surface *shsurf, pid_t pid)
2261{
2262 /* We have no use for it */
2263}
2264
2265static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002266set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2267{
2268 shsurf->type = t;
2269 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002270}
2271
2272static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002273set_window_geometry(struct shell_surface *shsurf,
2274 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002275{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002276 shsurf->next_geometry.x = x;
2277 shsurf->next_geometry.y = y;
2278 shsurf->next_geometry.width = width;
2279 shsurf->next_geometry.height = height;
2280 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002281}
2282
2283static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002284shell_surface_set_title(struct wl_client *client,
2285 struct wl_resource *resource, const char *title)
2286{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002287 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002288
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002289 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002290}
2291
2292static void
2293shell_surface_set_class(struct wl_client *client,
2294 struct wl_resource *resource, const char *class)
2295{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002296 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002297
2298 free(shsurf->class);
2299 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002300 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002301}
2302
Alex Wu4539b082012-03-01 12:57:46 +08002303static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002304restore_output_mode(struct weston_output *output)
2305{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002306 if (output->original_mode)
2307 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002308}
2309
2310static void
2311restore_all_output_modes(struct weston_compositor *compositor)
2312{
2313 struct weston_output *output;
2314
2315 wl_list_for_each(output, &compositor->output_list, link)
2316 restore_output_mode(output);
2317}
2318
Philip Withnall07926d92013-11-25 18:01:40 +00002319/* The surface will be inserted into the list immediately after the link
2320 * returned by this function (i.e. will be stacked immediately above the
2321 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002322static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002323shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2324{
2325 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002326 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002327
2328 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002329 case SHELL_SURFACE_XWAYLAND:
2330 return &shsurf->shell->fullscreen_layer.view_list;
2331
2332 case SHELL_SURFACE_NONE:
2333 return NULL;
2334
Kristian Høgsbergead52422014-01-01 13:51:52 -08002335 case SHELL_SURFACE_POPUP:
2336 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002337 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002338 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002339 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002340 /* Move the surface to its parent layer so
2341 * that surfaces which are transient for
2342 * fullscreen surfaces don't get hidden by the
2343 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002344
2345 /* TODO: Handle a parent with multiple views */
2346 parent = get_default_view(shsurf->parent);
2347 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002348 return container_of(parent->layer_link.link.prev,
2349 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002350 }
Philip Withnall07926d92013-11-25 18:01:40 +00002351
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002352 /* Move the surface to a normal workspace layer so that surfaces
2353 * which were previously fullscreen or transient are no longer
2354 * rendered on top. */
2355 ws = get_current_workspace(shsurf->shell);
2356 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002357 }
2358
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002359 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002360}
2361
Philip Withnall648a4dd2013-11-25 18:01:44 +00002362static void
2363shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2364{
2365 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002366 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002367
2368 /* Move the child layers to the same workspace as shsurf. They will be
2369 * stacked above shsurf. */
2370 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002371 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002372 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002373 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 prev = container_of(shsurf->view->layer_link.link.prev,
2375 struct weston_layer_entry, link);
2376 weston_layer_entry_remove(&child->view->layer_link);
2377 weston_layer_entry_insert(prev,
2378 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002379 weston_view_geometry_dirty(child->view);
2380 weston_surface_damage(child->surface);
2381
2382 /* Recurse. We don’t expect this to recurse very far (if
2383 * at all) because that would imply we have transient
2384 * (or popup) children of transient surfaces, which
2385 * would be unusual. */
2386 shell_surface_update_child_surface_layers(child);
2387 }
2388 }
2389}
2390
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002391/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002392 * geometry to ensure the changes are redrawn.
2393 *
2394 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2395 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002396static void
2397shell_surface_update_layer(struct shell_surface *shsurf)
2398{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002399 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400
2401 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2402
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002403 if (new_layer_link == NULL)
2404 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002405 if (new_layer_link == &shsurf->view->layer_link)
2406 return;
2407
2408 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409 weston_layer_entry_remove(&shsurf->view->layer_link);
2410 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002411 weston_view_geometry_dirty(shsurf->view);
2412 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002413
2414 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002415}
2416
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002417static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002418shell_surface_set_parent(struct shell_surface *shsurf,
2419 struct weston_surface *parent)
2420{
2421 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002422
2423 wl_list_remove(&shsurf->children_link);
2424 wl_list_init(&shsurf->children_link);
2425
2426 /* Insert into the parent surface’s child list. */
2427 if (parent != NULL) {
2428 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2429 if (parent_shsurf != NULL)
2430 wl_list_insert(&parent_shsurf->children_list,
2431 &shsurf->children_link);
2432 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002433}
2434
2435static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002436shell_surface_set_output(struct shell_surface *shsurf,
2437 struct weston_output *output)
2438{
2439 struct weston_surface *es = shsurf->surface;
2440
2441 /* get the default output, if the client set it as NULL
2442 check whether the ouput is available */
2443 if (output)
2444 shsurf->output = output;
2445 else if (es->output)
2446 shsurf->output = es->output;
2447 else
2448 shsurf->output = get_default_output(es->compositor);
2449}
2450
2451static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002452surface_clear_next_states(struct shell_surface *shsurf)
2453{
2454 shsurf->next_state.maximized = false;
2455 shsurf->next_state.fullscreen = false;
2456
2457 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2458 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2459 shsurf->state_changed = true;
2460}
2461
2462static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463set_toplevel(struct shell_surface *shsurf)
2464{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002465 shell_surface_set_parent(shsurf, NULL);
2466 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002467 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002468
2469 /* The layer_link is updated in set_surface_type(),
2470 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471}
2472
2473static void
2474shell_surface_set_toplevel(struct wl_client *client,
2475 struct wl_resource *resource)
2476{
2477 struct shell_surface *surface = wl_resource_get_user_data(resource);
2478
2479 set_toplevel(surface);
2480}
2481
2482static void
2483set_transient(struct shell_surface *shsurf,
2484 struct weston_surface *parent, int x, int y, uint32_t flags)
2485{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002486 assert(parent != NULL);
2487
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002488 shell_surface_set_parent(shsurf, parent);
2489
2490 surface_clear_next_states(shsurf);
2491
Philip Withnallbecb77e2013-11-25 18:01:30 +00002492 shsurf->transient.x = x;
2493 shsurf->transient.y = y;
2494 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002495
Rafael Antognollied207b42013-12-03 15:35:43 -02002496 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002497 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002498 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002499
2500 /* The layer_link is updated in set_surface_type(),
2501 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502}
2503
2504static void
2505shell_surface_set_transient(struct wl_client *client,
2506 struct wl_resource *resource,
2507 struct wl_resource *parent_resource,
2508 int x, int y, uint32_t flags)
2509{
2510 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2511 struct weston_surface *parent =
2512 wl_resource_get_user_data(parent_resource);
2513
2514 set_transient(shsurf, parent, x, y, flags);
2515}
2516
2517static void
2518set_fullscreen(struct shell_surface *shsurf,
2519 uint32_t method,
2520 uint32_t framerate,
2521 struct weston_output *output)
2522{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002523 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002524 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002525
2526 shsurf->fullscreen_output = shsurf->output;
2527 shsurf->fullscreen.type = method;
2528 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002529
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002530 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002531}
2532
2533static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002534weston_view_set_initial_position(struct weston_view *view,
2535 struct desktop_shell *shell);
2536
2537static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002538unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002539{
Philip Withnallf85fe842013-11-25 18:01:37 +00002540 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002541 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2542 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002543 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002544 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002545
Alex Wu4539b082012-03-01 12:57:46 +08002546 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2547 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002548
Alex Wu4539b082012-03-01 12:57:46 +08002549 wl_list_remove(&shsurf->fullscreen.transform.link);
2550 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002551
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 if (shsurf->fullscreen.black_view)
2553 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2554 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002555
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002556 if (shsurf->saved_position_valid)
2557 weston_view_set_position(shsurf->view,
2558 shsurf->saved_x, shsurf->saved_y);
2559 else
2560 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2561
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002562 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002564 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002565 shsurf->saved_rotation_valid = false;
2566 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002567
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002568 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002569}
2570
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002571static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572shell_surface_set_fullscreen(struct wl_client *client,
2573 struct wl_resource *resource,
2574 uint32_t method,
2575 uint32_t framerate,
2576 struct wl_resource *output_resource)
2577{
2578 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2579 struct weston_output *output;
2580
2581 if (output_resource)
2582 output = wl_resource_get_user_data(output_resource);
2583 else
2584 output = NULL;
2585
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002586 shell_surface_set_parent(shsurf, NULL);
2587
Rafael Antognolli03b16592013-12-03 15:35:42 -02002588 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002589 shsurf->next_state.fullscreen = true;
2590 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002591 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592}
2593
2594static void
2595set_popup(struct shell_surface *shsurf,
2596 struct weston_surface *parent,
2597 struct weston_seat *seat,
2598 uint32_t serial,
2599 int32_t x,
2600 int32_t y)
2601{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002602 assert(parent != NULL);
2603
Philip Withnallbecb77e2013-11-25 18:01:30 +00002604 shsurf->popup.shseat = get_shell_seat(seat);
2605 shsurf->popup.serial = serial;
2606 shsurf->popup.x = x;
2607 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002608
Pekka Paalanenb5026542014-11-12 15:09:24 +02002609 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002610}
2611
2612static void
2613shell_surface_set_popup(struct wl_client *client,
2614 struct wl_resource *resource,
2615 struct wl_resource *seat_resource,
2616 uint32_t serial,
2617 struct wl_resource *parent_resource,
2618 int32_t x, int32_t y, uint32_t flags)
2619{
2620 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002621 struct weston_surface *parent =
2622 wl_resource_get_user_data(parent_resource);
2623
2624 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002625
Rafael Antognolli03b16592013-12-03 15:35:42 -02002626 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002627 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002628 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002629 wl_resource_get_user_data(seat_resource),
2630 serial, x, y);
2631}
2632
2633static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002634unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002635{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002636 /* undo all maximized things here */
2637 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002638
2639 if (shsurf->saved_position_valid)
2640 weston_view_set_position(shsurf->view,
2641 shsurf->saved_x, shsurf->saved_y);
2642 else
2643 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644
2645 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646 wl_list_insert(&shsurf->view->geometry.transformation_list,
2647 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002648 shsurf->saved_rotation_valid = false;
2649 }
2650
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002651 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002652}
2653
Philip Withnallbecb77e2013-11-25 18:01:30 +00002654static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002655set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002656{
2657 struct shell_surface *shsurf;
2658 struct workspace *current_ws;
2659 struct weston_seat *seat;
2660 struct weston_surface *focus;
2661 struct weston_view *view;
2662
2663 view = get_default_view(surface);
2664 if (!view)
2665 return;
2666
2667 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2668
2669 shsurf = get_shell_surface(surface);
2670 current_ws = get_current_workspace(shsurf->shell);
2671
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002672 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002673 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002674
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002675 drop_focus_state(shsurf->shell, current_ws, view->surface);
2676 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2677 if (!seat->keyboard)
2678 continue;
2679 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2680 if (focus == view->surface)
2681 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002682 }
2683
2684 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002685 weston_view_damage_below(view);
2686}
2687
2688static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002689shell_surface_set_maximized(struct wl_client *client,
2690 struct wl_resource *resource,
2691 struct wl_resource *output_resource)
2692{
2693 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2694 struct weston_output *output;
2695
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002696 surface_clear_next_states(shsurf);
2697 shsurf->next_state.maximized = true;
2698 shsurf->state_changed = true;
2699
Pekka Paalanenb5026542014-11-12 15:09:24 +02002700 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002701 shell_surface_set_parent(shsurf, NULL);
2702
Philip Withnallbecb77e2013-11-25 18:01:30 +00002703 if (output_resource)
2704 output = wl_resource_get_user_data(output_resource);
2705 else
2706 output = NULL;
2707
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002708 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002709
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002710 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711}
2712
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002713/* This is only ever called from set_surface_type(), so there’s no need to
2714 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002715static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002716reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002717{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002718 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002719 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002720 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002721 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002722
Pekka Paalanen98262232011-12-01 10:42:22 +02002723 return 0;
2724}
2725
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002726static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002727set_full_output(struct shell_surface *shsurf)
2728{
2729 shsurf->saved_x = shsurf->view->geometry.x;
2730 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002731 shsurf->saved_width = shsurf->surface->width;
2732 shsurf->saved_height = shsurf->surface->height;
2733 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002734 shsurf->saved_position_valid = true;
2735
2736 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2737 wl_list_remove(&shsurf->rotation.transform.link);
2738 wl_list_init(&shsurf->rotation.transform.link);
2739 weston_view_geometry_dirty(shsurf->view);
2740 shsurf->saved_rotation_valid = true;
2741 }
2742}
2743
2744static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002745set_surface_type(struct shell_surface *shsurf)
2746{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002747 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002749
Philip Withnallbecb77e2013-11-25 18:01:30 +00002750 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002751
Rafael Antognolli03b16592013-12-03 15:35:42 -02002752 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002753 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002754
2755 switch (shsurf->type) {
2756 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002757 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002758 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002759 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760 weston_view_set_position(shsurf->view,
2761 pev->geometry.x + shsurf->transient.x,
2762 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002763 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002764 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002765
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002766 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002767 weston_view_set_position(shsurf->view, shsurf->transient.x,
2768 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002769 break;
2770
Philip Withnall0f640e22013-11-25 18:01:31 +00002771 case SHELL_SURFACE_POPUP:
2772 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002773 default:
2774 break;
2775 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002776
2777 /* Update the surface’s layer. */
2778 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002779}
2780
Tiago Vignattibe143262012-04-16 17:31:41 +03002781static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002782shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002783{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002784 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002785}
2786
Pekka Paalanen8274d902014-08-06 19:36:51 +03002787static int
2788black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2789{
2790 struct weston_surface *fs_surface = surface->configure_private;
2791 int n;
2792 int rem;
2793 int ret;
2794
2795 n = snprintf(buf, len, "black background surface for ");
2796 if (n < 0)
2797 return n;
2798
2799 rem = (int)len - n;
2800 if (rem < 0)
2801 rem = 0;
2802
2803 if (fs_surface->get_label)
2804 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2805 else
2806 ret = snprintf(buf + n, rem, "<unknown>");
2807
2808 if (ret < 0)
2809 return n;
2810
2811 return n + ret;
2812}
2813
Alex Wu21858432012-04-01 20:13:08 +08002814static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002815black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002816
Jason Ekstranda7af7042013-10-12 22:38:11 -05002817static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002818create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002819 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002820 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002821{
2822 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002824
2825 surface = weston_surface_create(ec);
2826 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002827 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002828 return NULL;
2829 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002830 view = weston_view_create(surface);
2831 if (surface == NULL) {
2832 weston_log("no memory\n");
2833 weston_surface_destroy(surface);
2834 return NULL;
2835 }
Alex Wu4539b082012-03-01 12:57:46 +08002836
Alex Wu21858432012-04-01 20:13:08 +08002837 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002838 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002839 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002840 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002841 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002842 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002843 pixman_region32_fini(&surface->input);
2844 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002845
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002846 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002847 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002848
2849 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002850}
2851
Philip Withnalled8f1a92013-11-25 18:01:43 +00002852static void
2853shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2854{
2855 struct weston_output *output = shsurf->fullscreen_output;
2856
Rafael Antognolli03b16592013-12-03 15:35:42 -02002857 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002858
2859 if (!shsurf->fullscreen.black_view)
2860 shsurf->fullscreen.black_view =
2861 create_black_surface(shsurf->surface->compositor,
2862 shsurf->surface,
2863 output->x, output->y,
2864 output->width,
2865 output->height);
2866
2867 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002868 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2869 weston_layer_entry_insert(&shsurf->view->layer_link,
2870 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002871 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2872 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002873
2874 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002875}
2876
Alex Wu4539b082012-03-01 12:57:46 +08002877/* Create black surface and append it to the associated fullscreen surface.
2878 * Handle size dismatch and positioning according to the method. */
2879static void
2880shell_configure_fullscreen(struct shell_surface *shsurf)
2881{
2882 struct weston_output *output = shsurf->fullscreen_output;
2883 struct weston_surface *surface = shsurf->surface;
2884 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002885 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002886 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002887
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002888 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2889 restore_output_mode(output);
2890
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002891 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002892 weston_layer_entry_remove(&shsurf->view->layer_link);
2893 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002894
Philip Withnalled8f1a92013-11-25 18:01:43 +00002895 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002896
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002898 &surf_width, &surf_height);
2899
Alex Wu4539b082012-03-01 12:57:46 +08002900 switch (shsurf->fullscreen.type) {
2901 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002902 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002904 break;
2905 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002906 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002907 if (output->width == surf_width &&
2908 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002909 weston_view_set_position(shsurf->view,
2910 output->x - surf_x,
2911 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002912 break;
2913 }
2914
Alex Wu4539b082012-03-01 12:57:46 +08002915 matrix = &shsurf->fullscreen.transform.matrix;
2916 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002917
Scott Moreau1bad5db2012-08-18 01:04:05 -06002918 output_aspect = (float) output->width /
2919 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 /* XXX: Use surf_width and surf_height here? */
2921 surface_aspect = (float) surface->width /
2922 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002923 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002924 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002925 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002926 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002927 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002928 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002929
Alex Wu4539b082012-03-01 12:57:46 +08002930 weston_matrix_scale(matrix, scale, scale, 1);
2931 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002933 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002934 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2935 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002937
Alex Wu4539b082012-03-01 12:57:46 +08002938 break;
2939 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002940 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002941 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002942 surf_width * surface->buffer_viewport.buffer.scale,
2943 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002944 shsurf->fullscreen.framerate};
2945
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002946 if (weston_output_mode_switch_to_temporary(output, &mode,
2947 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 weston_view_set_position(shsurf->view,
2949 output->x - surf_x,
2950 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002951 shsurf->fullscreen.black_view->surface->width = output->width;
2952 shsurf->fullscreen.black_view->surface->height = output->height;
2953 weston_view_set_position(shsurf->fullscreen.black_view,
2954 output->x - surf_x,
2955 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002956 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002957 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002958 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002960 }
Alex Wubd3354b2012-04-17 17:20:49 +08002961 }
Alex Wu4539b082012-03-01 12:57:46 +08002962 break;
2963 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002965 break;
2966 default:
2967 break;
2968 }
2969}
2970
Alex Wu4539b082012-03-01 12:57:46 +08002971static void
2972shell_map_fullscreen(struct shell_surface *shsurf)
2973{
Alex Wubd3354b2012-04-17 17:20:49 +08002974 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002975}
2976
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002977static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002978set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2979{
2980 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002981 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002982 shsurf->transient.x = x;
2983 shsurf->transient.y = y;
2984 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002985
2986 shell_surface_set_parent(shsurf, NULL);
2987
Pekka Paalanenb5026542014-11-12 15:09:24 +02002988 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002989 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002990}
2991
Kristian Høgsberg47792412014-05-04 13:47:06 -07002992static void
2993shell_interface_set_fullscreen(struct shell_surface *shsurf,
2994 uint32_t method,
2995 uint32_t framerate,
2996 struct weston_output *output)
2997{
2998 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002999 shsurf->next_state.fullscreen = true;
3000 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003001
3002 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003003}
3004
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003005static struct weston_output *
3006get_focused_output(struct weston_compositor *compositor)
3007{
3008 struct weston_seat *seat;
3009 struct weston_output *output = NULL;
3010
3011 wl_list_for_each(seat, &compositor->seat_list, link) {
3012 /* Priority has touch focus, then pointer and
3013 * then keyboard focus. We should probably have
3014 * three for loops and check frist for touch,
3015 * then for pointer, etc. but unless somebody has some
3016 * objections, I think this is sufficient. */
3017 if (seat->touch && seat->touch->focus)
3018 output = seat->touch->focus->output;
3019 else if (seat->pointer && seat->pointer->focus)
3020 output = seat->pointer->focus->output;
3021 else if (seat->keyboard && seat->keyboard->focus)
3022 output = seat->keyboard->focus->output;
3023
3024 if (output)
3025 break;
3026 }
3027
3028 return output;
3029}
3030
3031static void
3032shell_interface_set_maximized(struct shell_surface *shsurf)
3033{
3034 struct weston_output *output;
3035
3036 surface_clear_next_states(shsurf);
3037 shsurf->next_state.maximized = true;
3038 shsurf->state_changed = true;
3039 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3040
3041 if (!weston_surface_is_mapped(shsurf->surface))
3042 output = get_focused_output(shsurf->surface->compositor);
3043 else
3044 output = shsurf->surface->output;
3045
3046 shell_surface_set_output(shsurf, output);
3047 send_configure_for_surface(shsurf);
3048}
3049
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003050static int
3051shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3052{
3053 return surface_move(shsurf, ws, 1);
3054}
3055
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003056static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003057
3058static void
3059destroy_shell_seat(struct wl_listener *listener, void *data)
3060{
3061 struct shell_seat *shseat =
3062 container_of(listener,
3063 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003064 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065
3066 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003067 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068 shseat->popup_grab.client = NULL;
3069
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003070 wl_list_for_each_safe(shsurf, next,
3071 &shseat->popup_grab.surfaces_list,
3072 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003074 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076 }
3077
3078 wl_list_remove(&shseat->seat_destroy_listener.link);
3079 free(shseat);
3080}
3081
Jason Ekstrand024177c2014-04-21 19:42:58 -05003082static void
3083shell_seat_caps_changed(struct wl_listener *listener, void *data)
3084{
3085 struct shell_seat *seat;
3086
3087 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3088
3089 if (seat->seat->keyboard &&
3090 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3091 wl_signal_add(&seat->seat->keyboard->focus_signal,
3092 &seat->keyboard_focus_listener);
3093 } else if (!seat->seat->keyboard) {
3094 wl_list_init(&seat->keyboard_focus_listener.link);
3095 }
3096
3097 if (seat->seat->pointer &&
3098 wl_list_empty(&seat->pointer_focus_listener.link)) {
3099 wl_signal_add(&seat->seat->pointer->focus_signal,
3100 &seat->pointer_focus_listener);
3101 } else if (!seat->seat->pointer) {
3102 wl_list_init(&seat->pointer_focus_listener.link);
3103 }
3104}
3105
Giulio Camuffo5085a752013-03-25 21:42:45 +01003106static struct shell_seat *
3107create_shell_seat(struct weston_seat *seat)
3108{
3109 struct shell_seat *shseat;
3110
3111 shseat = calloc(1, sizeof *shseat);
3112 if (!shseat) {
3113 weston_log("no memory to allocate shell seat\n");
3114 return NULL;
3115 }
3116
3117 shseat->seat = seat;
3118 wl_list_init(&shseat->popup_grab.surfaces_list);
3119
3120 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3121 wl_signal_add(&seat->destroy_signal,
3122 &shseat->seat_destroy_listener);
3123
Jason Ekstrand024177c2014-04-21 19:42:58 -05003124 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3125 wl_list_init(&shseat->keyboard_focus_listener.link);
3126
3127 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3128 wl_list_init(&shseat->pointer_focus_listener.link);
3129
3130 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3131 wl_signal_add(&seat->updated_caps_signal,
3132 &shseat->caps_changed_listener);
3133 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3134
Giulio Camuffo5085a752013-03-25 21:42:45 +01003135 return shseat;
3136}
3137
3138static struct shell_seat *
3139get_shell_seat(struct weston_seat *seat)
3140{
3141 struct wl_listener *listener;
3142
3143 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003144 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003145
3146 return container_of(listener,
3147 struct shell_seat, seat_destroy_listener);
3148}
3149
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003150static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003151popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003152{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003153 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003155 struct shell_seat *shseat =
3156 container_of(grab, struct shell_seat, popup_grab.grab);
3157 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003158 wl_fixed_t sx, sy;
3159
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 view = weston_compositor_pick_view(pointer->seat->compositor,
3161 pointer->x, pointer->y,
3162 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163
Jason Ekstranda7af7042013-10-12 22:38:11 -05003164 if (view && view->surface->resource &&
3165 wl_resource_get_client(view->surface->resource) == client) {
3166 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003167 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003168 weston_pointer_set_focus(pointer, NULL,
3169 wl_fixed_from_int(0),
3170 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171 }
3172}
3173
3174static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003175popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3176 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003178 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003179 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003180 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181
Jonas Ådahl61917c82014-08-11 22:44:02 +02003182 if (pointer->focus) {
3183 weston_view_from_global_fixed(pointer->focus, x, y,
3184 &pointer->sx, &pointer->sy);
3185 }
3186
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003187 weston_pointer_move(pointer, x, y);
3188
Neil Roberts96d790e2013-09-19 17:32:00 +01003189 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003190 weston_view_from_global_fixed(pointer->focus,
3191 pointer->x, pointer->y,
3192 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003193 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003194 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003195}
3196
3197static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003198popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003199 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003200{
3201 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003202 struct shell_seat *shseat =
3203 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003204 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003205 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003206 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003207 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003208
Neil Roberts96d790e2013-09-19 17:32:00 +01003209 resource_list = &grab->pointer->focus_resource_list;
3210 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003211 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003212 wl_resource_for_each(resource, resource_list) {
3213 wl_pointer_send_button(resource, serial,
3214 time, button, state);
3215 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003216 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003217 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003218 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003219 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003220 }
3221
Daniel Stone4dbadb12012-05-30 16:31:51 +01003222 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003223 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224}
3225
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003226static void
3227popup_grab_cancel(struct weston_pointer_grab *grab)
3228{
3229 popup_grab_end(grab->pointer);
3230}
3231
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003232static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233 popup_grab_focus,
3234 popup_grab_motion,
3235 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003236 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237};
3238
3239static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003240touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3241 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3242{
3243 struct wl_resource *resource;
3244 struct shell_seat *shseat =
3245 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3246 struct wl_display *display = shseat->seat->compositor->wl_display;
3247 uint32_t serial;
3248 struct wl_list *resource_list;
3249
3250 resource_list = &grab->touch->focus_resource_list;
3251 if (!wl_list_empty(resource_list)) {
3252 serial = wl_display_get_serial(display);
3253 wl_resource_for_each(resource, resource_list) {
3254 wl_touch_send_down(resource, serial, time,
3255 grab->touch->focus->surface->resource,
3256 touch_id, sx, sy);
3257 }
3258 }
3259}
3260
3261static void
3262touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3263{
3264 struct wl_resource *resource;
3265 struct shell_seat *shseat =
3266 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3267 struct wl_display *display = shseat->seat->compositor->wl_display;
3268 uint32_t serial;
3269 struct wl_list *resource_list;
3270
3271 resource_list = &grab->touch->focus_resource_list;
3272 if (!wl_list_empty(resource_list)) {
3273 serial = wl_display_get_serial(display);
3274 wl_resource_for_each(resource, resource_list) {
3275 wl_touch_send_up(resource, serial, time, touch_id);
3276 }
3277 }
3278}
3279
3280static void
3281touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3282 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3283{
3284 struct wl_resource *resource;
3285 struct wl_list *resource_list;
3286
3287 resource_list = &grab->touch->focus_resource_list;
3288 if (!wl_list_empty(resource_list)) {
3289 wl_resource_for_each(resource, resource_list) {
3290 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3291 }
3292 }
3293}
3294
3295static void
3296touch_popup_grab_frame(struct weston_touch_grab *grab)
3297{
3298}
3299
3300static void
3301touch_popup_grab_cancel(struct weston_touch_grab *grab)
3302{
3303 touch_popup_grab_end(grab->touch);
3304}
3305
3306static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3307 touch_popup_grab_down,
3308 touch_popup_grab_up,
3309 touch_popup_grab_motion,
3310 touch_popup_grab_frame,
3311 touch_popup_grab_cancel,
3312};
3313
3314static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003315shell_surface_send_popup_done(struct shell_surface *shsurf)
3316{
3317 if (shell_surface_is_wl_shell_surface(shsurf))
3318 wl_shell_surface_send_popup_done(shsurf->resource);
3319 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003320 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321}
3322
3323static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003324popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003325{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003326 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003327 struct shell_seat *shseat =
3328 container_of(grab, struct shell_seat, popup_grab.grab);
3329 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003330 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003331
3332 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003333 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003334 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003335 shseat->popup_grab.grab.interface = NULL;
3336 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003337 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003338 wl_list_for_each_safe(shsurf, next,
3339 &shseat->popup_grab.surfaces_list,
3340 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003341 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003342 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003343 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003344 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003345 wl_list_init(&shseat->popup_grab.surfaces_list);
3346 }
3347}
3348
3349static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003350touch_popup_grab_end(struct weston_touch *touch)
3351{
3352 struct weston_touch_grab *grab = touch->grab;
3353 struct shell_seat *shseat =
3354 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3355 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003356 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003357
3358 if (touch->grab->interface == &touch_popup_grab_interface) {
3359 weston_touch_end_grab(grab->touch);
3360 shseat->popup_grab.client = NULL;
3361 shseat->popup_grab.touch_grab.interface = NULL;
3362 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3363 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003364 wl_list_for_each_safe(shsurf, next,
3365 &shseat->popup_grab.surfaces_list,
3366 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367 shell_surface_send_popup_done(shsurf);
3368 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003369 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003370 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003371 wl_list_init(&shseat->popup_grab.surfaces_list);
3372 }
3373}
3374
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003375static struct shell_surface *
3376get_top_popup(struct shell_seat *shseat)
3377{
3378 struct shell_surface *shsurf;
3379
3380 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3381 return NULL;
3382 } else {
3383 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3384 struct shell_surface,
3385 popup.grab_link);
3386 return shsurf;
3387 }
3388}
3389
3390static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003391add_popup_grab(struct shell_surface *shsurf,
3392 struct shell_seat *shseat,
3393 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003394{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003395 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003396 struct shell_surface *parent, *top_surface;
3397
3398 parent = get_shell_surface(shsurf->parent);
3399 top_surface = get_top_popup(shseat);
3400 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003401 (!parent ||
3402 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003403 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003404 wl_resource_post_error(shsurf->owner_resource,
3405 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003406 "xdg_popup was not created on the "
3407 "topmost popup");
3408 return -1;
3409 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003410
3411 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003412 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003413 shseat->popup_grab.client =
3414 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003415
3416 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003417 shseat->popup_grab.grab.interface =
3418 &popup_grab_interface;
3419
3420 /* We must make sure here that this popup was opened
3421 * after a mouse press, and not just by moving around
3422 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003423 if (shseat->seat->pointer->button_count > 0)
3424 shseat->popup_grab.initial_up = 0;
3425 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003426 shseat->popup_grab.touch_grab.interface =
3427 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003428 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003429
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003430 wl_list_insert(&shseat->popup_grab.surfaces_list,
3431 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003432
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003433 if (type == POINTER) {
3434 weston_pointer_start_grab(seat->pointer,
3435 &shseat->popup_grab.grab);
3436 } else if (type == TOUCH) {
3437 weston_touch_start_grab(seat->touch,
3438 &shseat->popup_grab.touch_grab);
3439 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003440 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003441 wl_list_insert(&shseat->popup_grab.surfaces_list,
3442 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003443 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003444
3445 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003446}
3447
3448static void
3449remove_popup_grab(struct shell_surface *shsurf)
3450{
3451 struct shell_seat *shseat = shsurf->popup.shseat;
3452
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003453 if (shell_surface_is_xdg_popup(shsurf) &&
3454 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003455 wl_resource_post_error(shsurf->owner_resource,
3456 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003457 "xdg_popup was destroyed while it was "
3458 "not the topmost popup.");
3459 return;
3460 }
3461
Giulio Camuffo5085a752013-03-25 21:42:45 +01003462 wl_list_remove(&shsurf->popup.grab_link);
3463 wl_list_init(&shsurf->popup.grab_link);
3464 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003465 if (shseat->popup_grab.type == POINTER) {
3466 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3467 shseat->popup_grab.grab.interface = NULL;
3468 } else if (shseat->popup_grab.type == TOUCH) {
3469 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3470 shseat->popup_grab.touch_grab.interface = NULL;
3471 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003472 }
3473}
3474
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003475static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003476shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003477{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003478 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003479 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003480
Jason Ekstranda7af7042013-10-12 22:38:11 -05003481 shsurf->surface->output = parent_view->output;
3482 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003483
Jason Ekstranda7af7042013-10-12 22:38:11 -05003484 weston_view_set_transform_parent(shsurf->view, parent_view);
3485 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3486 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003487
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003488 if (shseat->seat->pointer &&
3489 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3491 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003492 } else if (shseat->seat->touch &&
3493 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3495 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003496 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003497 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003498 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003499 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003500
3501 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003502}
3503
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003504static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003505 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003506 shell_surface_move,
3507 shell_surface_resize,
3508 shell_surface_set_toplevel,
3509 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003510 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003511 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003512 shell_surface_set_maximized,
3513 shell_surface_set_title,
3514 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003515};
3516
3517static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003518destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003519{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003520 struct shell_surface *child, *next;
3521
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003522 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3523
Giulio Camuffo5085a752013-03-25 21:42:45 +01003524 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3525 remove_popup_grab(shsurf);
3526 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527
Alex Wubd3354b2012-04-17 17:20:49 +08003528 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003529 shell_surface_is_top_fullscreen(shsurf))
3530 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003531
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 if (shsurf->fullscreen.black_view)
3533 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003534
Alex Wubd3354b2012-04-17 17:20:49 +08003535 /* As destroy_resource() use wl_list_for_each_safe(),
3536 * we can always remove the listener.
3537 */
3538 wl_list_remove(&shsurf->surface_destroy_listener.link);
3539 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003540 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003541 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003542
Jason Ekstranda7af7042013-10-12 22:38:11 -05003543 weston_view_destroy(shsurf->view);
3544
Philip Withnall648a4dd2013-11-25 18:01:44 +00003545 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003546 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3547 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003548
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003549 wl_list_remove(&shsurf->link);
3550 free(shsurf);
3551}
3552
3553static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003554shell_destroy_shell_surface(struct wl_resource *resource)
3555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003556 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003557
Bryan Caina46b9462014-04-04 17:41:24 -05003558 if (!wl_list_empty(&shsurf->popup.grab_link))
3559 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003560 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003561 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003562}
3563
3564static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003565shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003566{
3567 struct shell_surface *shsurf = container_of(listener,
3568 struct shell_surface,
3569 surface_destroy_listener);
3570
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003571 if (shsurf->resource)
3572 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003573
3574 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003575}
3576
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003577static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003578fade_out_done(struct weston_view_animation *animation, void *data)
3579{
3580 struct shell_surface *shsurf = data;
3581
3582 weston_surface_destroy(shsurf->surface);
3583}
3584
3585static void
3586handle_resource_destroy(struct wl_listener *listener, void *data)
3587{
3588 struct shell_surface *shsurf =
3589 container_of(listener, struct shell_surface,
3590 resource_destroy_listener);
3591
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003592 if (!weston_surface_is_mapped(shsurf->surface))
3593 return;
3594
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003595 shsurf->surface->ref_count++;
3596
3597 pixman_region32_fini(&shsurf->surface->pending.input);
3598 pixman_region32_init(&shsurf->surface->pending.input);
3599 pixman_region32_fini(&shsurf->surface->input);
3600 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003601 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3602 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3603 fade_out_done, shsurf);
3604 } else {
3605 weston_surface_destroy(shsurf->surface);
3606 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003607}
3608
3609static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003610shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003611
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003612struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003613get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003614{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003615 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003616 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003617 else
3618 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003619}
3620
Rafael Antognollie2a34552013-12-03 15:35:45 -02003621static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003622create_common_surface(struct shell_client *owner, void *shell,
3623 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003624 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003625{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003626 struct shell_surface *shsurf;
3627
Pekka Paalanen50b67472014-10-01 15:02:41 +03003628 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003629
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003630 shsurf = calloc(1, sizeof *shsurf);
3631 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003632 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003633 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003634 }
3635
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 shsurf->view = weston_view_create(surface);
3637 if (!shsurf->view) {
3638 weston_log("no memory to allocate shell surface\n");
3639 free(shsurf);
3640 return NULL;
3641 }
3642
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003643 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003644 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003645 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003646
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003647 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3648 wl_resource_add_destroy_listener(surface->resource,
3649 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003650 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003651
Tiago Vignattibc052c92012-04-19 16:18:18 +03003652 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003653 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003654 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003655 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003656 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003657 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003658 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3659 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003661 wl_list_init(&shsurf->fullscreen.transform.link);
3662
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003663 shsurf->output = get_default_output(shsurf->shell->compositor);
3664
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003665 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003666 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003667 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003668 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003669
3670 /* init link so its safe to always remove it in destroy_shell_surface */
3671 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003672 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003673
Pekka Paalanen460099f2012-01-20 16:48:25 +02003674 /* empty when not in use */
3675 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003676 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003677
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003678 wl_list_init(&shsurf->workspace_transform.link);
3679
Philip Withnall648a4dd2013-11-25 18:01:44 +00003680 wl_list_init(&shsurf->children_link);
3681 wl_list_init(&shsurf->children_list);
3682 shsurf->parent = NULL;
3683
Pekka Paalanenb5026542014-11-12 15:09:24 +02003684 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003685
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003686 shsurf->client = client;
3687
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003688 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003689}
3690
Rafael Antognollie2a34552013-12-03 15:35:45 -02003691static struct shell_surface *
3692create_shell_surface(void *shell, struct weston_surface *surface,
3693 const struct weston_shell_client *client)
3694{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003695 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003696}
3697
Jason Ekstranda7af7042013-10-12 22:38:11 -05003698static struct weston_view *
3699get_primary_view(void *shell, struct shell_surface *shsurf)
3700{
3701 return shsurf->view;
3702}
3703
Tiago Vignattibc052c92012-04-19 16:18:18 +03003704static void
3705shell_get_shell_surface(struct wl_client *client,
3706 struct wl_resource *resource,
3707 uint32_t id,
3708 struct wl_resource *surface_resource)
3709{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003710 struct weston_surface *surface =
3711 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003712 struct shell_client *sc = wl_resource_get_user_data(resource);
3713 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003714 struct shell_surface *shsurf;
3715
Pekka Paalanen50b67472014-10-01 15:02:41 +03003716 if (weston_surface_set_role(surface, "wl_shell_surface",
3717 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003718 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003719
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003720 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003721 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003722 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003723 return;
3724 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003725
Jason Ekstranda85118c2013-06-27 20:17:02 -05003726 shsurf->resource =
3727 wl_resource_create(client,
3728 &wl_shell_surface_interface, 1, id);
3729 wl_resource_set_implementation(shsurf->resource,
3730 &shell_surface_implementation,
3731 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003732 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003733}
3734
Rafael Antognollie2a34552013-12-03 15:35:45 -02003735static bool
3736shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3737{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003738 /* A shell surface without a resource is created from xwayland
3739 * and is considered a wl_shell surface for now. */
3740
3741 return shsurf->resource == NULL ||
3742 wl_resource_instance_of(shsurf->resource,
3743 &wl_shell_surface_interface,
3744 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003745}
3746
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003747static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003748 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003749};
3750
Rafael Antognollie2a34552013-12-03 15:35:45 -02003751/****************************
3752 * xdg-shell implementation */
3753
3754static void
3755xdg_surface_destroy(struct wl_client *client,
3756 struct wl_resource *resource)
3757{
3758 wl_resource_destroy(resource);
3759}
3760
3761static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003762xdg_surface_set_parent(struct wl_client *client,
3763 struct wl_resource *resource,
3764 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003765{
3766 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003767 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003768
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003769 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003770 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003771 shell_surface_set_parent(shsurf, parent->surface);
3772 } else {
3773 shell_surface_set_parent(shsurf, NULL);
3774 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003775}
3776
3777static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778xdg_surface_set_app_id(struct wl_client *client,
3779 struct wl_resource *resource,
3780 const char *app_id)
3781{
3782 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3783
3784 free(shsurf->class);
3785 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003786 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003787}
3788
3789static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003790xdg_surface_show_window_menu(struct wl_client *client,
3791 struct wl_resource *surface_resource,
3792 struct wl_resource *seat_resource,
3793 uint32_t serial,
3794 int32_t x,
3795 int32_t y)
3796{
3797 /* TODO */
3798}
3799
3800static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003801xdg_surface_set_title(struct wl_client *client,
3802 struct wl_resource *resource, const char *title)
3803{
3804 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3805
3806 set_title(shsurf, title);
3807}
3808
3809static void
3810xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3811 struct wl_resource *seat_resource, uint32_t serial)
3812{
3813 common_surface_move(resource, seat_resource, serial);
3814}
3815
3816static void
3817xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3818 struct wl_resource *seat_resource, uint32_t serial,
3819 uint32_t edges)
3820{
3821 common_surface_resize(resource, seat_resource, serial, edges);
3822}
3823
3824static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003825xdg_surface_ack_configure(struct wl_client *client,
3826 struct wl_resource *resource,
3827 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003828{
3829 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3830
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003831 if (shsurf->state_requested) {
3832 shsurf->next_state = shsurf->requested_state;
3833 shsurf->state_changed = true;
3834 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003835 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003836}
3837
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003838static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003839xdg_surface_set_window_geometry(struct wl_client *client,
3840 struct wl_resource *resource,
3841 int32_t x,
3842 int32_t y,
3843 int32_t width,
3844 int32_t height)
3845{
3846 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3847
3848 set_window_geometry(shsurf, x, y, width, height);
3849}
3850
3851static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003852xdg_surface_set_maximized(struct wl_client *client,
3853 struct wl_resource *resource)
3854{
3855 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003856 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003857
3858 shsurf->state_requested = true;
3859 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003860
3861 if (!weston_surface_is_mapped(shsurf->surface))
3862 output = get_focused_output(shsurf->surface->compositor);
3863 else
3864 output = shsurf->surface->output;
3865
3866 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003867 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003868}
3869
3870static void
3871xdg_surface_unset_maximized(struct wl_client *client,
3872 struct wl_resource *resource)
3873{
3874 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3875
3876 shsurf->state_requested = true;
3877 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003878 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003879}
3880
3881static void
3882xdg_surface_set_fullscreen(struct wl_client *client,
3883 struct wl_resource *resource,
3884 struct wl_resource *output_resource)
3885{
3886 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3887 struct weston_output *output;
3888
3889 shsurf->state_requested = true;
3890 shsurf->requested_state.fullscreen = true;
3891
3892 if (output_resource)
3893 output = wl_resource_get_user_data(output_resource);
3894 else
3895 output = NULL;
3896
Marek Chalupa052917a2014-09-02 11:35:12 +02003897 /* handle clients launching in fullscreen */
3898 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3899 /* Set the output to the one that has focus currently. */
3900 assert(shsurf->surface);
3901 output = get_focused_output(shsurf->surface->compositor);
3902 }
3903
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003904 shell_surface_set_output(shsurf, output);
3905 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003906
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003907 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003908}
3909
3910static void
3911xdg_surface_unset_fullscreen(struct wl_client *client,
3912 struct wl_resource *resource)
3913{
3914 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3915
3916 shsurf->state_requested = true;
3917 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003918 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003919}
3920
3921static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003922xdg_surface_set_minimized(struct wl_client *client,
3923 struct wl_resource *resource)
3924{
3925 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3926
3927 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3928 return;
3929
3930 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003931 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003932}
3933
Rafael Antognollie2a34552013-12-03 15:35:45 -02003934static const struct xdg_surface_interface xdg_surface_implementation = {
3935 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003936 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003937 xdg_surface_set_title,
3938 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003939 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003940 xdg_surface_move,
3941 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003942 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003943 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003944 xdg_surface_set_maximized,
3945 xdg_surface_unset_maximized,
3946 xdg_surface_set_fullscreen,
3947 xdg_surface_unset_fullscreen,
3948 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003949};
3950
3951static void
3952xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003953 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003954{
3955 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003956 uint32_t *s;
3957 struct wl_array states;
3958 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003959
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003960 assert(shsurf);
3961
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003962 if (!shsurf->resource)
3963 return;
3964
3965 wl_array_init(&states);
3966 if (shsurf->requested_state.fullscreen) {
3967 s = wl_array_add(&states, sizeof *s);
3968 *s = XDG_SURFACE_STATE_FULLSCREEN;
3969 } else if (shsurf->requested_state.maximized) {
3970 s = wl_array_add(&states, sizeof *s);
3971 *s = XDG_SURFACE_STATE_MAXIMIZED;
3972 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003973 if (shsurf->resize_edges != 0) {
3974 s = wl_array_add(&states, sizeof *s);
3975 *s = XDG_SURFACE_STATE_RESIZING;
3976 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003977 if (shsurf->focus_count > 0) {
3978 s = wl_array_add(&states, sizeof *s);
3979 *s = XDG_SURFACE_STATE_ACTIVATED;
3980 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003981
3982 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3983 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3984
3985 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003986}
3987
3988static const struct weston_shell_client xdg_client = {
3989 xdg_send_configure
3990};
3991
3992static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08003993xdg_shell_destroy(struct wl_client *client,
3994 struct wl_resource *resource)
3995{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003996 struct shell_client *sc = wl_resource_get_user_data(resource);
3997 struct wl_resource *shsurf_resource;
3998 struct shell_surface *shsurf;
3999
4000 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4001 shsurf = wl_resource_get_user_data(shsurf_resource);
4002 if (shsurf->owner_resource == resource) {
4003 wl_resource_post_error(
4004 resource,
4005 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4006 "not all child surface objects destroyed");
4007 return;
4008 }
4009 }
4010
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004011 wl_resource_destroy(resource);
4012}
4013
4014static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004015xdg_use_unstable_version(struct wl_client *client,
4016 struct wl_resource *resource,
4017 int32_t version)
4018{
4019 if (version > 1) {
4020 wl_resource_post_error(resource,
4021 1,
4022 "xdg-shell:: version not implemented yet.");
4023 return;
4024 }
4025}
4026
4027static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004028create_xdg_surface(struct shell_client *owner, void *shell,
4029 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004030 const struct weston_shell_client *client)
4031{
4032 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004033
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004034 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004035 if (!shsurf)
4036 return NULL;
4037
Pekka Paalanenb5026542014-11-12 15:09:24 +02004038 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004039
4040 return shsurf;
4041}
4042
4043static void
4044xdg_get_xdg_surface(struct wl_client *client,
4045 struct wl_resource *resource,
4046 uint32_t id,
4047 struct wl_resource *surface_resource)
4048{
4049 struct weston_surface *surface =
4050 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004051 struct shell_client *sc = wl_resource_get_user_data(resource);
4052 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004053 struct shell_surface *shsurf;
4054
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004055 shsurf = get_shell_surface(surface);
4056 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4057 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4058 "This wl_surface is already an "
4059 "xdg_surface");
4060 return;
4061 }
4062
Pekka Paalanen50b67472014-10-01 15:02:41 +03004063 if (weston_surface_set_role(surface, "xdg_surface",
4064 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004065 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004066
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004067 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004068 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004069 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004070 return;
4071 }
4072
4073 shsurf->resource =
4074 wl_resource_create(client,
4075 &xdg_surface_interface, 1, id);
4076 wl_resource_set_implementation(shsurf->resource,
4077 &xdg_surface_implementation,
4078 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004079 shsurf->owner_resource = resource;
4080 wl_list_insert(&sc->surface_list,
4081 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004082}
4083
4084static bool
4085shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4086{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004087 return shsurf->resource &&
4088 wl_resource_instance_of(shsurf->resource,
4089 &xdg_surface_interface,
4090 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004091}
4092
4093/* xdg-popup implementation */
4094
4095static void
4096xdg_popup_destroy(struct wl_client *client,
4097 struct wl_resource *resource)
4098{
4099 wl_resource_destroy(resource);
4100}
4101
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102static const struct xdg_popup_interface xdg_popup_implementation = {
4103 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004104};
4105
4106static void
4107xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004108 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004109{
4110}
4111
4112static const struct weston_shell_client xdg_popup_client = {
4113 xdg_popup_send_configure
4114};
4115
4116static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004117create_xdg_popup(struct shell_client *owner, void *shell,
4118 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004119 const struct weston_shell_client *client,
4120 struct weston_surface *parent,
4121 struct shell_seat *seat,
4122 uint32_t serial,
4123 int32_t x, int32_t y)
4124{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004125 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004126
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004127 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004128 if (!shsurf)
4129 return NULL;
4130
Pekka Paalanenb5026542014-11-12 15:09:24 +02004131 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004132 shsurf->popup.shseat = seat;
4133 shsurf->popup.serial = serial;
4134 shsurf->popup.x = x;
4135 shsurf->popup.y = y;
4136 shell_surface_set_parent(shsurf, parent);
4137
4138 return shsurf;
4139}
4140
4141static void
4142xdg_get_xdg_popup(struct wl_client *client,
4143 struct wl_resource *resource,
4144 uint32_t id,
4145 struct wl_resource *surface_resource,
4146 struct wl_resource *parent_resource,
4147 struct wl_resource *seat_resource,
4148 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004149 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004150{
4151 struct weston_surface *surface =
4152 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004153 struct shell_client *sc = wl_resource_get_user_data(resource);
4154 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004155 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004156 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004157 struct weston_surface *parent;
4158 struct shell_seat *seat;
4159
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004160 shsurf = get_shell_surface(surface);
4161 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4162 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4163 "This wl_surface is already an "
4164 "xdg_popup");
4165 return;
4166 }
4167
Pekka Paalanen50b67472014-10-01 15:02:41 +03004168 if (weston_surface_set_role(surface, "xdg_popup",
4169 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004170 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004171
4172 if (!parent_resource) {
4173 wl_resource_post_error(surface_resource,
4174 WL_DISPLAY_ERROR_INVALID_OBJECT,
4175 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004176 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004177 }
4178
4179 parent = wl_resource_get_user_data(parent_resource);
4180 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4181
Jonas Ådahlee45a552015-03-18 16:37:47 +08004182 /* Verify that we are creating the top most popup when mapping,
4183 * as it's not until then we know whether it was mapped as most
4184 * top level or not. */
4185
4186 parent_shsurf = get_shell_surface(parent);
4187 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4188 !shell_surface_is_xdg_surface(parent_shsurf)) {
4189 wl_resource_post_error(resource,
4190 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4191 "xdg_popup parent was invalid");
4192 return;
4193 }
4194
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004195 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004196 parent, seat, serial, x, y);
4197 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004198 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004199 return;
4200 }
4201
4202 shsurf->resource =
4203 wl_resource_create(client,
4204 &xdg_popup_interface, 1, id);
4205 wl_resource_set_implementation(shsurf->resource,
4206 &xdg_popup_implementation,
4207 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004208 shsurf->owner_resource = resource;
4209 wl_list_insert(&sc->surface_list,
4210 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004211}
4212
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004213static void
4214xdg_pong(struct wl_client *client,
4215 struct wl_resource *resource, uint32_t serial)
4216{
4217 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004218
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004219 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004220}
4221
Rafael Antognollie2a34552013-12-03 15:35:45 -02004222static bool
4223shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4224{
4225 return wl_resource_instance_of(shsurf->resource,
4226 &xdg_popup_interface,
4227 &xdg_popup_implementation);
4228}
4229
4230static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004231 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232 xdg_use_unstable_version,
4233 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004234 xdg_get_xdg_popup,
4235 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004236};
4237
4238static int
4239xdg_shell_unversioned_dispatch(const void *implementation,
4240 void *_target, uint32_t opcode,
4241 const struct wl_message *message,
4242 union wl_argument *args)
4243{
4244 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004245 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004246
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004247 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004248 wl_resource_post_error(resource,
4249 WL_DISPLAY_ERROR_INVALID_OBJECT,
4250 "must call use_unstable_version first");
4251 return 0;
4252 }
4253
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004254#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004256 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4257 "shell implementation doesn't match protocol version");
4258
Rafael Antognollie2a34552013-12-03 15:35:45 -02004259 if (args[0].i != XDG_SERVER_VERSION) {
4260 wl_resource_post_error(resource,
4261 WL_DISPLAY_ERROR_INVALID_OBJECT,
4262 "incompatible version, server is %d "
4263 "client wants %d",
4264 XDG_SERVER_VERSION, args[0].i);
4265 return 0;
4266 }
4267
4268 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004269 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270
4271 return 1;
4272}
4273
4274/* end of xdg-shell implementation */
4275/***********************************/
4276
Kristian Høgsberg07937562011-04-12 17:25:42 -04004277static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004278shell_fade(struct desktop_shell *shell, enum fade_type type);
4279
4280static int
4281screensaver_timeout(void *data)
4282{
4283 struct desktop_shell *shell = data;
4284
4285 shell_fade(shell, FADE_OUT);
4286
4287 return 1;
4288}
4289
4290static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004291handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004292{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004293 struct desktop_shell *shell =
4294 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004295
Pekka Paalanen18027e52011-12-02 16:31:49 +02004296 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004297
4298 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004299 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004300}
4301
4302static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004303launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004304{
4305 if (shell->screensaver.binding)
4306 return;
4307
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004308 if (!shell->screensaver.path) {
4309 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004310 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004311 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004312
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004313 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004314 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004315 return;
4316 }
4317
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004318 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004319 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004320 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004321 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004322}
4323
4324static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004325terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004326{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004327 if (shell->screensaver.process.pid == 0)
4328 return;
4329
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004330 /* Disarm the screensaver timer, otherwise it may fire when the
4331 * compositor is not in the idle state. In that case, the screen will
4332 * be locked, but the wake_signal won't fire on user input, making the
4333 * system unresponsive. */
4334 wl_event_source_timer_update(shell->screensaver.timer, 0);
4335
Pekka Paalanen18027e52011-12-02 16:31:49 +02004336 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004337}
4338
4339static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004340configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004341{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004343
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004344 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 if (v->output == ev->output && v != ev) {
4346 weston_view_unmap(v);
4347 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004348 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004349 }
4350 }
4351
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004352 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004353
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004354 if (wl_list_empty(&ev->layer_link.link)) {
4355 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004357 }
4358}
4359
Pekka Paalanen8274d902014-08-06 19:36:51 +03004360static int
4361background_get_label(struct weston_surface *surface, char *buf, size_t len)
4362{
4363 return snprintf(buf, len, "background for output %s",
4364 surface->output->name);
4365}
4366
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004367static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004368background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004369{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004370 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004372
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 view = container_of(es->views.next, struct weston_view, surface_link);
4374
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004375 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004376}
4377
4378static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004379desktop_shell_set_background(struct wl_client *client,
4380 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004381 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382 struct wl_resource *surface_resource)
4383{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004384 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004385 struct weston_surface *surface =
4386 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004388
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004389 if (surface->configure) {
4390 wl_resource_post_error(surface_resource,
4391 WL_DISPLAY_ERROR_INVALID_OBJECT,
4392 "surface role already assigned");
4393 return;
4394 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004395
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4397 weston_view_destroy(view);
4398 view = weston_view_create(surface);
4399
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004400 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004401 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004402 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004403 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004405 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004406 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004407 surface->output->width,
4408 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004409}
4410
Pekka Paalanen8274d902014-08-06 19:36:51 +03004411static int
4412panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4413{
4414 return snprintf(buf, len, "panel for output %s",
4415 surface->output->name);
4416}
4417
Kristian Høgsberg75840622011-09-06 13:48:16 -04004418static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004419panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004420{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004421 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 view = container_of(es->views.next, struct weston_view, surface_link);
4425
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004426 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004427}
4428
4429static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004430desktop_shell_set_panel(struct wl_client *client,
4431 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004432 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004433 struct wl_resource *surface_resource)
4434{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004435 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004436 struct weston_surface *surface =
4437 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004439
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004440 if (surface->configure) {
4441 wl_resource_post_error(surface_resource,
4442 WL_DISPLAY_ERROR_INVALID_OBJECT,
4443 "surface role already assigned");
4444 return;
4445 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004446
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4448 weston_view_destroy(view);
4449 view = weston_view_create(surface);
4450
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004451 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004452 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004453 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004454 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004456 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004457 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004458 surface->output->width,
4459 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004460}
4461
Pekka Paalanen8274d902014-08-06 19:36:51 +03004462static int
4463lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4464{
4465 return snprintf(buf, len, "lock window");
4466}
4467
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004468static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004469lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004470{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004471 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 struct weston_view *view;
4473
4474 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004475
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004477 return;
4478
Jason Ekstranda7af7042013-10-12 22:38:11 -05004479 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004480
4481 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004482 weston_layer_entry_insert(&shell->lock_layer.view_list,
4483 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004485 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004486 }
4487}
4488
4489static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004490handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004491{
Tiago Vignattibe143262012-04-16 17:31:41 +03004492 struct desktop_shell *shell =
4493 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004494
Martin Minarik6d118362012-06-07 18:01:59 +02004495 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004496 shell->lock_surface = NULL;
4497}
4498
4499static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004500desktop_shell_set_lock_surface(struct wl_client *client,
4501 struct wl_resource *resource,
4502 struct wl_resource *surface_resource)
4503{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004504 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004505 struct weston_surface *surface =
4506 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004507
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004508 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004509
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004510 if (!shell->locked)
4511 return;
4512
Pekka Paalanen98262232011-12-01 10:42:22 +02004513 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004514
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004515 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004516 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004517 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004518
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004519 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004520 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004521 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004522 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004523}
4524
4525static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004526resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004527{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004528 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004529
Pekka Paalanen77346a62011-11-30 16:26:35 +02004530 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004531
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004532 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004533 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004534 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004535 &shell->input_panel_layer.link);
4536 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004537 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004538 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004539 wl_list_insert(&shell->compositor->cursor_layer.link,
4540 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004541 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004542 wl_list_insert(&shell->fullscreen_layer.link,
4543 &shell->panel_layer.link);
4544 wl_list_insert(&shell->panel_layer.link,
4545 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004547 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004548
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004549 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004550 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004551 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004552}
4553
4554static void
4555desktop_shell_unlock(struct wl_client *client,
4556 struct wl_resource *resource)
4557{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004558 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004559
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004560 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004561
4562 if (shell->locked)
4563 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004564}
4565
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004566static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004567desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004568 struct wl_resource *resource,
4569 struct wl_resource *surface_resource)
4570{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004571 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004572
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004573 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004574 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004575}
4576
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004577static void
4578desktop_shell_desktop_ready(struct wl_client *client,
4579 struct wl_resource *resource)
4580{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004581 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004582
4583 shell_fade_startup(shell);
4584}
4585
Jonny Lamb765760d2014-08-20 15:53:19 +02004586static void
4587desktop_shell_set_panel_position(struct wl_client *client,
4588 struct wl_resource *resource,
4589 uint32_t position)
4590{
4591 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4592
4593 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4594 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4595 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4596 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4597 wl_resource_post_error(resource,
4598 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4599 "bad position argument");
4600 return;
4601 }
4602
4603 shell->panel_position = position;
4604}
4605
Kristian Høgsberg75840622011-09-06 13:48:16 -04004606static const struct desktop_shell_interface desktop_shell_implementation = {
4607 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004608 desktop_shell_set_panel,
4609 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004610 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004611 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004612 desktop_shell_desktop_ready,
4613 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004614};
4615
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004616static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004617get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004618{
4619 struct shell_surface *shsurf;
4620
4621 shsurf = get_shell_surface(surface);
4622 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004623 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004624 return shsurf->type;
4625}
4626
Kristian Høgsberg75840622011-09-06 13:48:16 -04004627static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004628move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004629{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004630 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004631 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004632 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004633
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004634 if (seat->pointer->focus == NULL)
4635 return;
4636
4637 focus = seat->pointer->focus->surface;
4638
Pekka Paalanen01388e22013-04-25 13:57:44 +03004639 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004640 if (surface == NULL)
4641 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004642
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004643 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004644 if (shsurf == NULL || shsurf->state.fullscreen ||
4645 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004646 return;
4647
Derek Foreman8aeeac82015-01-30 13:24:36 -06004648 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004649}
4650
4651static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004652maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4653{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004654 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004655 struct weston_surface *surface;
4656 struct shell_surface *shsurf;
4657
4658 surface = weston_surface_get_main_surface(focus);
4659 if (surface == NULL)
4660 return;
4661
4662 shsurf = get_shell_surface(surface);
4663 if (shsurf == NULL)
4664 return;
4665
4666 if (!shell_surface_is_xdg_surface(shsurf))
4667 return;
4668
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004669 shsurf->state_requested = true;
4670 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004671 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004672}
4673
4674static void
4675fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4676{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004677 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004678 struct weston_surface *surface;
4679 struct shell_surface *shsurf;
4680
4681 surface = weston_surface_get_main_surface(focus);
4682 if (surface == NULL)
4683 return;
4684
4685 shsurf = get_shell_surface(surface);
4686 if (shsurf == NULL)
4687 return;
4688
4689 if (!shell_surface_is_xdg_surface(shsurf))
4690 return;
4691
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004692 shsurf->state_requested = true;
4693 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004694 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004695 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004696}
4697
4698static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004699touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4700{
Derek Foreman362656b2014-09-04 10:23:05 -05004701 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004702 struct weston_surface *surface;
4703 struct shell_surface *shsurf;
4704
Derek Foreman362656b2014-09-04 10:23:05 -05004705 if (seat->touch->focus == NULL)
4706 return;
4707
4708 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004709 surface = weston_surface_get_main_surface(focus);
4710 if (surface == NULL)
4711 return;
4712
4713 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004714 if (shsurf == NULL || shsurf->state.fullscreen ||
4715 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004716 return;
4717
Derek Foreman8aeeac82015-01-30 13:24:36 -06004718 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004719}
4720
4721static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004722resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004723{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004724 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004725 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004726 uint32_t edges = 0;
4727 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004728 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004729
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004730 if (seat->pointer->focus == NULL)
4731 return;
4732
4733 focus = seat->pointer->focus->surface;
4734
Pekka Paalanen01388e22013-04-25 13:57:44 +03004735 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004736 if (surface == NULL)
4737 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004738
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004739 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004740 if (shsurf == NULL || shsurf->state.fullscreen ||
4741 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004742 return;
4743
Jason Ekstranda7af7042013-10-12 22:38:11 -05004744 weston_view_from_global(shsurf->view,
4745 wl_fixed_to_int(seat->pointer->grab_x),
4746 wl_fixed_to_int(seat->pointer->grab_y),
4747 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004748
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004749 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004750 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004751 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004752 edges |= 0;
4753 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004754 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004755
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004756 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004757 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004758 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004759 edges |= 0;
4760 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004761 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004762
Derek Foreman8aeeac82015-01-30 13:24:36 -06004763 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004764}
4765
4766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004767surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004768 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004769{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004770 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004771 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004772 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004773 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004774
Pekka Paalanen01388e22013-04-25 13:57:44 +03004775 /* XXX: broken for windows containing sub-surfaces */
4776 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004777 if (surface == NULL)
4778 return;
4779
4780 shsurf = get_shell_surface(surface);
4781 if (!shsurf)
4782 return;
4783
Jason Ekstranda7af7042013-10-12 22:38:11 -05004784 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004785
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 if (shsurf->view->alpha > 1.0)
4787 shsurf->view->alpha = 1.0;
4788 if (shsurf->view->alpha < step)
4789 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004790
Jason Ekstranda7af7042013-10-12 22:38:11 -05004791 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004792 weston_surface_damage(surface);
4793}
4794
4795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004796do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004797 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004798{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004799 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004800 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004801 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004802
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004803 if (!seat->pointer) {
4804 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4805 return;
4806 }
4807
Scott Moreauccbf29d2012-02-22 14:21:41 -07004808 wl_list_for_each(output, &compositor->output_list, link) {
4809 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004810 wl_fixed_to_double(seat->pointer->x),
4811 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004812 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004813 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004814 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004815 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004816 increment = -output->zoom.increment;
4817 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004818 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004819 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004820 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004821 else
4822 increment = 0;
4823
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004824 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004825
Scott Moreaue6603982012-06-11 13:07:51 -06004826 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004827 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004828 else if (output->zoom.level > output->zoom.max_level)
4829 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004830 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004831 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004832 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004833
Scott Moreaue6603982012-06-11 13:07:51 -06004834 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004835
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004837 }
4838 }
4839}
4840
Scott Moreauccbf29d2012-02-22 14:21:41 -07004841static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004842zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004843 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004844{
4845 do_zoom(seat, time, 0, axis, value);
4846}
4847
4848static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004849zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004850 void *data)
4851{
4852 do_zoom(seat, time, key, 0, 0);
4853}
4854
4855static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004856terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004857 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004858{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004859 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004860
Daniel Stone325fc2d2012-05-30 16:31:58 +01004861 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004862}
4863
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004864static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004865rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4866 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004867{
4868 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004869 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004870 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004871 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004872 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004873
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004874 weston_pointer_move(pointer, x, y);
4875
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004876 if (!shsurf)
4877 return;
4878
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004879 cx = 0.5f * shsurf->surface->width;
4880 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004881
Daniel Stone37816df2012-05-16 18:45:18 +01004882 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4883 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004884 r = sqrtf(dx * dx + dy * dy);
4885
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004888
4889 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004890 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004891 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004892
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004893 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004894 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004895
4896 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004897 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004898 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004899 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004900 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004901
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004902 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004904 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004905 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004906 wl_list_init(&shsurf->rotation.transform.link);
4907 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004908 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004909 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004910
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004911 /* We need to adjust the position of the surface
4912 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004913 cposx = shsurf->view->geometry.x + cx;
4914 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004915 dposx = rotate->center.x - cposx;
4916 dposy = rotate->center.y - cposy;
4917 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 weston_view_set_position(shsurf->view,
4919 shsurf->view->geometry.x + dposx,
4920 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004921 }
4922
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004923 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004924 * lazily applies the damage due to rotation update.
4925 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004926 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004927}
4928
4929static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004930rotate_grab_button(struct weston_pointer_grab *grab,
4931 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004932{
4933 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004934 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004935 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004936 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004937 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004938
Daniel Stone4dbadb12012-05-30 16:31:51 +01004939 if (pointer->button_count == 0 &&
4940 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004941 if (shsurf)
4942 weston_matrix_multiply(&shsurf->rotation.rotation,
4943 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004944 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004945 free(rotate);
4946 }
4947}
4948
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004949static void
4950rotate_grab_cancel(struct weston_pointer_grab *grab)
4951{
4952 struct rotate_grab *rotate =
4953 container_of(grab, struct rotate_grab, base.grab);
4954
4955 shell_grab_end(&rotate->base);
4956 free(rotate);
4957}
4958
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004959static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004960 noop_grab_focus,
4961 rotate_grab_motion,
4962 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004963 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004964};
4965
4966static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004967surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004968{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004970 float dx, dy;
4971 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004972
Pekka Paalanen460099f2012-01-20 16:48:25 +02004973 rotate = malloc(sizeof *rotate);
4974 if (!rotate)
4975 return;
4976
Jason Ekstranda7af7042013-10-12 22:38:11 -05004977 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004978 surface->surface->width * 0.5f,
4979 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004980 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004981
Daniel Stone37816df2012-05-16 18:45:18 +01004982 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4983 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004984 r = sqrtf(dx * dx + dy * dy);
4985 if (r > 20.0f) {
4986 struct weston_matrix inverse;
4987
4988 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004989 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004990 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004991
4992 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004993 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004994 } else {
4995 weston_matrix_init(&surface->rotation.rotation);
4996 weston_matrix_init(&rotate->rotation);
4997 }
4998
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004999 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
5000 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005001}
5002
5003static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005004rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005005 void *data)
5006{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005007 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005008 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005009 struct shell_surface *surface;
5010
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005011 if (seat->pointer->focus == NULL)
5012 return;
5013
5014 focus = seat->pointer->focus->surface;
5015
Pekka Paalanen01388e22013-04-25 13:57:44 +03005016 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005017 if (base_surface == NULL)
5018 return;
5019
5020 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005021 if (surface == NULL || surface->state.fullscreen ||
5022 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005023 return;
5024
5025 surface_rotate(surface, seat);
5026}
5027
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005028/* Move all fullscreen layers down to the current workspace and hide their
5029 * black views. The surfaces' state is set to both fullscreen and lowered,
5030 * and this is reversed when such a surface is re-configured, see
5031 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5032 *
5033 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005034 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005035void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005036lower_fullscreen_layer(struct desktop_shell *shell)
5037{
5038 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005040
5041 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005043 &shell->fullscreen_layer.view_list.link,
5044 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005045 struct shell_surface *shsurf = get_shell_surface(view->surface);
5046
5047 if (!shsurf)
5048 continue;
5049
5050 /* We can have a non-fullscreen popup for a fullscreen surface
5051 * in the fullscreen layer. */
5052 if (shsurf->state.fullscreen) {
5053 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005054 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5055 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005056 weston_view_damage_below(shsurf->fullscreen.black_view);
5057
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005058 }
5059
5060 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005061 weston_layer_entry_remove(&view->layer_link);
5062 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005063 weston_view_damage_below(view);
5064 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005065
5066 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005067 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005068}
5069
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005070void
Tiago Vignattibe143262012-04-16 17:31:41 +03005071activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005072 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005073{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005074 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005075 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005076 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005077 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005078 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005079
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005080 lower_fullscreen_layer(shell);
5081
Pekka Paalanen01388e22013-04-25 13:57:44 +03005082 main_surface = weston_surface_get_main_surface(es);
5083
Daniel Stone37816df2012-05-16 18:45:18 +01005084 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005085
Jonas Ådahl8538b222012-08-29 22:13:03 +02005086 state = ensure_focus_state(shell, seat);
5087 if (state == NULL)
5088 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005089
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005090 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005091 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005092
Rafael Antognolli03b16592013-12-03 15:35:42 -02005093 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005094 assert(shsurf);
5095
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005096 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005097 shell_configure_fullscreen(shsurf);
5098 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005099 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005100
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005101 /* Update the surface’s layer. This brings it to the top of the stacking
5102 * order as appropriate. */
5103 shell_surface_update_layer(shsurf);
5104
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005105 if (shell->focus_animation_type != ANIMATION_NONE) {
5106 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005107 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005108 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005109}
5110
Alex Wu21858432012-04-01 20:13:08 +08005111/* no-op func for checking black surface */
5112static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005113black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005114{
5115}
5116
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005117static bool
Alex Wu21858432012-04-01 20:13:08 +08005118is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5119{
5120 if (es->configure == black_surface_configure) {
5121 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005122 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005123 return true;
5124 }
5125 return false;
5126}
5127
Kristian Høgsberg75840622011-09-06 13:48:16 -04005128static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005129activate_binding(struct weston_seat *seat,
5130 struct desktop_shell *shell,
5131 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005132{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005133 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005134
Alex Wu9c35e6b2012-03-05 14:13:13 +08005135 if (!focus)
5136 return;
5137
Pekka Paalanen01388e22013-04-25 13:57:44 +03005138 if (is_black_surface(focus, &main_surface))
5139 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005140
Pekka Paalanen01388e22013-04-25 13:57:44 +03005141 main_surface = weston_surface_get_main_surface(focus);
5142 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005143 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005144
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005145 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005146}
5147
5148static void
5149click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5150 void *data)
5151{
5152 if (seat->pointer->grab != &seat->pointer->default_grab)
5153 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005154 if (seat->pointer->focus == NULL)
5155 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005156
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005157 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005158}
5159
5160static void
5161touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5162{
5163 if (seat->touch->grab != &seat->touch->default_grab)
5164 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005165 if (seat->touch->focus == NULL)
5166 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005167
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005168 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005169}
5170
5171static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005172unfocus_all_seats(struct desktop_shell *shell)
5173{
5174 struct weston_seat *seat, *next;
5175
5176 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5177 if (seat->keyboard == NULL)
5178 continue;
5179
5180 weston_keyboard_set_focus(seat->keyboard, NULL);
5181 }
5182}
5183
5184static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005185lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005186{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005187 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005188
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005189 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005190 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005191 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005192 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005193
5194 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005195
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005196 /* Hide all surfaces by removing the fullscreen, panel and
5197 * toplevel layers. This way nothing else can show or receive
5198 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005199
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005200 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005201 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005202 if (shell->showing_input_panels)
5203 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005204 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005205 wl_list_insert(&shell->compositor->cursor_layer.link,
5206 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005207
Pekka Paalanen77346a62011-11-30 16:26:35 +02005208 launch_screensaver(shell);
5209
Neil Robertsb4a91702014-04-09 16:33:32 +01005210 /* Remove the keyboard focus on all seats. This will be
5211 * restored to the workspace's saved state via
5212 * restore_focus_state when the compositor is unlocked */
5213 unfocus_all_seats(shell);
5214
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005215 /* TODO: disable bindings that should not work while locked. */
5216
5217 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005218}
5219
5220static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005221unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005222{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005223 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005224 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005225 return;
5226 }
5227
5228 /* If desktop-shell client has gone away, unlock immediately. */
5229 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005230 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005231 return;
5232 }
5233
5234 if (shell->prepare_event_sent)
5235 return;
5236
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005237 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005238 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005239}
5240
5241static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005242shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005243{
5244 struct desktop_shell *shell = data;
5245
5246 shell->fade.animation = NULL;
5247
5248 switch (shell->fade.type) {
5249 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005250 weston_surface_destroy(shell->fade.view->surface);
5251 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005252 break;
5253 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005254 lock(shell);
5255 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005256 default:
5257 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005258 }
5259}
5260
Jason Ekstranda7af7042013-10-12 22:38:11 -05005261static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005262shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005263{
5264 struct weston_compositor *compositor = shell->compositor;
5265 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005266 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005267
5268 surface = weston_surface_create(compositor);
5269 if (!surface)
5270 return NULL;
5271
Jason Ekstranda7af7042013-10-12 22:38:11 -05005272 view = weston_view_create(surface);
5273 if (!view) {
5274 weston_surface_destroy(surface);
5275 return NULL;
5276 }
5277
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005278 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005279 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005280 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005281 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5282 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005283 pixman_region32_init(&surface->input);
5284
Jason Ekstranda7af7042013-10-12 22:38:11 -05005285 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005286}
5287
5288static void
5289shell_fade(struct desktop_shell *shell, enum fade_type type)
5290{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005291 float tint;
5292
5293 switch (type) {
5294 case FADE_IN:
5295 tint = 0.0;
5296 break;
5297 case FADE_OUT:
5298 tint = 1.0;
5299 break;
5300 default:
5301 weston_log("shell: invalid fade type\n");
5302 return;
5303 }
5304
5305 shell->fade.type = type;
5306
Jason Ekstranda7af7042013-10-12 22:38:11 -05005307 if (shell->fade.view == NULL) {
5308 shell->fade.view = shell_fade_create_surface(shell);
5309 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005310 return;
5311
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 shell->fade.view->alpha = 1.0 - tint;
5313 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005314 }
5315
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005316 if (shell->fade.view->output == NULL) {
5317 /* If the black view gets a NULL output, we lost the
5318 * last output and we'll just cancel the fade. This
5319 * happens when you close the last window under the
5320 * X11 or Wayland backends. */
5321 shell->locked = false;
5322 weston_surface_destroy(shell->fade.view->surface);
5323 shell->fade.view = NULL;
5324 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005325 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005326 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005327 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005328 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005329 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005330 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005331 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005332}
5333
5334static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005335do_shell_fade_startup(void *data)
5336{
5337 struct desktop_shell *shell = data;
5338
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005339 if (shell->startup_animation_type == ANIMATION_FADE)
5340 shell_fade(shell, FADE_IN);
5341 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005342 weston_surface_destroy(shell->fade.view->surface);
5343 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005344 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005345}
5346
5347static void
5348shell_fade_startup(struct desktop_shell *shell)
5349{
5350 struct wl_event_loop *loop;
5351
5352 if (!shell->fade.startup_timer)
5353 return;
5354
5355 wl_event_source_remove(shell->fade.startup_timer);
5356 shell->fade.startup_timer = NULL;
5357
5358 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5359 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5360}
5361
5362static int
5363fade_startup_timeout(void *data)
5364{
5365 struct desktop_shell *shell = data;
5366
5367 shell_fade_startup(shell);
5368 return 0;
5369}
5370
5371static void
5372shell_fade_init(struct desktop_shell *shell)
5373{
5374 /* Make compositor output all black, and wait for the desktop-shell
5375 * client to signal it is ready, then fade in. The timer triggers a
5376 * fade-in, in case the desktop-shell client takes too long.
5377 */
5378
5379 struct wl_event_loop *loop;
5380
Jason Ekstranda7af7042013-10-12 22:38:11 -05005381 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005382 weston_log("%s: warning: fade surface already exists\n",
5383 __func__);
5384 return;
5385 }
5386
Jason Ekstranda7af7042013-10-12 22:38:11 -05005387 shell->fade.view = shell_fade_create_surface(shell);
5388 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005389 return;
5390
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 weston_view_update_transform(shell->fade.view);
5392 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005393
5394 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5395 shell->fade.startup_timer =
5396 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5397 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5398}
5399
5400static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005401idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005402{
5403 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005404 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005405 struct weston_seat *seat;
5406
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005407 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005408 if (seat->pointer)
5409 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005410 if (seat->touch)
5411 touch_popup_grab_end(seat->touch);
5412 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005413
5414 shell_fade(shell, FADE_OUT);
5415 /* lock() is called from shell_fade_done() */
5416}
5417
5418static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005419wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005420{
5421 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005422 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005423
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005424 unlock(shell);
5425}
5426
5427static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005428center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005429{
Giulio Camuffob8366642013-04-25 13:57:46 +03005430 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005431 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005432
Jason Ekstranda7af7042013-10-12 22:38:11 -05005433 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005434
5435 x = output->x + (output->width - width) / 2 - surf_x / 2;
5436 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005437
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005438 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005439}
5440
5441static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005442weston_view_set_initial_position(struct weston_view *view,
5443 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005444{
5445 struct weston_compositor *compositor = shell->compositor;
5446 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005447 int32_t range_x, range_y;
5448 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005449 struct weston_output *output, *target_output = NULL;
5450 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005451 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005452
5453 /* As a heuristic place the new window on the same output as the
5454 * pointer. Falling back to the output containing 0, 0.
5455 *
5456 * TODO: Do something clever for touch too?
5457 */
5458 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005459 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005460 ix = wl_fixed_to_int(seat->pointer->x);
5461 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005462 break;
5463 }
5464 }
5465
5466 wl_list_for_each(output, &compositor->output_list, link) {
5467 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5468 target_output = output;
5469 break;
5470 }
5471 }
5472
5473 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005474 weston_view_set_position(view, 10 + random() % 400,
5475 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005476 return;
5477 }
5478
5479 /* Valid range within output where the surface will still be onscreen.
5480 * If this is negative it means that the surface is bigger than
5481 * output.
5482 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005483 get_output_work_area(shell, target_output, &area);
5484
5485 dx = area.x;
5486 dy = area.y;
5487 range_x = area.width - view->surface->width;
5488 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005489
Rob Bradford4cb88c72012-08-13 15:18:44 +01005490 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005491 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005492
5493 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005494 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005495
5496 x = target_output->x + dx;
5497 y = target_output->y + dy;
5498
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005500}
5501
5502static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005503set_maximized_position(struct desktop_shell *shell,
5504 struct shell_surface *shsurf)
5505{
5506 int32_t surf_x, surf_y;
5507 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005508 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005509
Jonny Lambd73c6942014-08-20 15:53:20 +02005510 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005511 if (shsurf->has_set_geometry) {
5512 surf_x = shsurf->geometry.x;
5513 surf_y = shsurf->geometry.y;
5514 } else {
5515 surface_subsurfaces_boundingbox(shsurf->surface,
5516 &surf_x, &surf_y, NULL, NULL);
5517 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005518 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005519
5520 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005521 e->x1 + area.x - surf_x,
5522 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005523}
5524
5525static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005526map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005527 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005529 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005530 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005531
Pekka Paalanen77346a62011-11-30 16:26:35 +02005532 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005533 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005534 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005535 if (shsurf->state.fullscreen) {
5536 center_on_output(shsurf->view, shsurf->fullscreen_output);
5537 shell_map_fullscreen(shsurf);
5538 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005539 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005540 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005541 weston_view_set_initial_position(shsurf->view, shell);
5542 }
Juan Zhao96879df2012-02-07 08:45:41 +08005543 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005544 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005545 if (shell_map_popup(shsurf) != 0)
5546 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005547 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005548 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005549 weston_view_set_position(shsurf->view,
5550 shsurf->view->geometry.x + sx,
5551 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005552 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005553 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005554 default:
5555 ;
5556 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005557
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005558 /* Surface stacking order, see also activate(). */
5559 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005560
Jason Ekstranda7af7042013-10-12 22:38:11 -05005561 if (shsurf->type != SHELL_SURFACE_NONE) {
5562 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005563 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005564 shsurf->surface->output = shsurf->output;
5565 shsurf->view->output = shsurf->output;
5566 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005567 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005568
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005570 /* XXX: xwayland's using the same fields for transient type */
5571 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005572 if (shsurf->transient.flags ==
5573 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5574 break;
5575 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005576 if (shsurf->state.relative &&
5577 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5578 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005579 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005580 break;
5581 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005582 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005583 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005584 case SHELL_SURFACE_POPUP:
5585 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005586 default:
5587 break;
5588 }
5589
Rafael Antognolli03b16592013-12-03 15:35:42 -02005590 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5591 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005592 {
5593 switch (shell->win_animation_type) {
5594 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005595 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005596 break;
5597 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005598 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005599 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005600 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005601 default:
5602 break;
5603 }
5604 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005605}
5606
5607static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005608configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005609 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005610{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005611 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005612 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005613
Pekka Paalanen77346a62011-11-30 16:26:35 +02005614 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005615
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005616 assert(shsurf);
5617
Rafael Antognolli03b16592013-12-03 15:35:42 -02005618 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005619 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005620 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005621 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005622 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005623 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005624 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005625
Alex Wu4539b082012-03-01 12:57:46 +08005626 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005627 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005628 wl_list_for_each(view, &surface->views, surface_link)
5629 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005630
Rafael Antognolli03b16592013-12-03 15:35:42 -02005631 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005632 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005633 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005634}
5635
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005636static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005637shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005638{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005639 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005640 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005641 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005642
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005643 assert(shsurf);
5644
5645 shell = shsurf->shell;
5646
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005647 if (!weston_surface_is_mapped(es) &&
5648 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005649 remove_popup_grab(shsurf);
5650 }
5651
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005652 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005653 return;
5654
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005655 if (shsurf->has_next_geometry) {
5656 shsurf->geometry = shsurf->next_geometry;
5657 shsurf->has_next_geometry = false;
5658 shsurf->has_set_geometry = true;
5659 } else if (!shsurf->has_set_geometry) {
5660 surface_subsurfaces_boundingbox(shsurf->surface,
5661 &shsurf->geometry.x,
5662 &shsurf->geometry.y,
5663 &shsurf->geometry.width,
5664 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005665 }
5666
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005667 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005668 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005669 type_changed = 1;
5670 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005671
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005672 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005673 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005674 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005675 shsurf->last_width != es->width ||
5676 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005677 float from_x, from_y;
5678 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005679
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005680 if (shsurf->resize_edges) {
5681 sx = 0;
5682 sy = 0;
5683 }
5684
5685 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5686 sx = shsurf->last_width - es->width;
5687 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5688 sy = shsurf->last_height - es->height;
5689
5690 shsurf->last_width = es->width;
5691 shsurf->last_height = es->height;
5692
Jason Ekstranda7af7042013-10-12 22:38:11 -05005693 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5694 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005695 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005696 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005697 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005698 }
5699}
5700
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005701static bool
5702check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5703{
5704 struct timespec now;
5705
5706 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5707 return false;
5708
5709 /*
5710 * If the shell helper client dies before the session has been
5711 * up for roughly 30 seconds, better just make Weston shut down,
5712 * because the user likely has no way to interact with the desktop
5713 * anyway.
5714 */
5715 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5716 weston_log("Error: %s apparently cannot run at all.\n",
5717 shell->client);
5718 weston_log_continue(STAMP_SPACE "Quitting...");
5719 wl_display_terminate(shell->compositor->wl_display);
5720
5721 return true;
5722 }
5723
5724 return false;
5725}
5726
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005727static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005728
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005729static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005730respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005731{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005732 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005733
5734 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5735 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005736 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005737 shell->child.deathstamp = time;
5738 shell->child.deathcount = 0;
5739 }
5740
5741 shell->child.deathcount++;
5742 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005743 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005744 return;
5745 }
5746
Pekka Paalanen826dc142014-08-27 12:11:53 +03005747 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005748 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005749}
5750
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005751static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005752desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5753{
5754 struct desktop_shell *shell;
5755
5756 shell = container_of(listener, struct desktop_shell,
5757 child.client_destroy_listener);
5758
Pekka Paalanen826dc142014-08-27 12:11:53 +03005759 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005760 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005761 /*
5762 * unbind_desktop_shell() will reset shell->child.desktop_shell
5763 * before the respawned process has a chance to create a new
5764 * desktop_shell object, because we are being called from the
5765 * wl_client destructor which destroys all wl_resources before
5766 * returning.
5767 */
5768
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005769 if (!check_desktop_shell_crash_too_early(shell))
5770 respawn_desktop_shell_process(shell);
5771
Pekka Paalanen826dc142014-08-27 12:11:53 +03005772 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005773}
5774
5775static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005776launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005777{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005778 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005779
Pekka Paalanen826dc142014-08-27 12:11:53 +03005780 shell->child.client = weston_client_start(shell->compositor,
5781 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005782
Arnaud Vrac42631192014-08-25 20:56:46 +02005783 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005784 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005785 return;
5786 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005787
5788 shell->child.client_destroy_listener.notify =
5789 desktop_shell_client_destroy;
5790 wl_client_add_destroy_listener(shell->child.client,
5791 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005792}
5793
5794static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005795handle_shell_client_destroy(struct wl_listener *listener, void *data)
5796{
5797 struct shell_client *sc =
5798 container_of(listener, struct shell_client, destroy_listener);
5799
5800 if (sc->ping_timer)
5801 wl_event_source_remove(sc->ping_timer);
5802 free(sc);
5803}
5804
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005805static struct shell_client *
5806shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5807 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005808{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005809 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005810
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005811 sc = zalloc(sizeof *sc);
5812 if (sc == NULL) {
5813 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005814 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005815 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005816
5817 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005818 if (sc->resource == NULL) {
5819 free(sc);
5820 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005821 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005822 }
5823
5824 sc->client = client;
5825 sc->shell = shell;
5826 sc->destroy_listener.notify = handle_shell_client_destroy;
5827 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005828 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005829
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005830 return sc;
5831}
5832
5833static void
5834bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5835{
5836 struct desktop_shell *shell = data;
5837 struct shell_client *sc;
5838
5839 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5840 if (sc)
5841 wl_resource_set_implementation(sc->resource,
5842 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005843 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005844}
5845
5846static void
5847bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5848{
5849 struct desktop_shell *shell = data;
5850 struct shell_client *sc;
5851
5852 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5853 if (sc)
5854 wl_resource_set_dispatcher(sc->resource,
5855 xdg_shell_unversioned_dispatch,
5856 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005857}
5858
5859static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005860unbind_desktop_shell(struct wl_resource *resource)
5861{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005862 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005863
5864 if (shell->locked)
5865 resume_desktop(shell);
5866
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005867 shell->child.desktop_shell = NULL;
5868 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005869}
5870
5871static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005872bind_desktop_shell(struct wl_client *client,
5873 void *data, uint32_t version, uint32_t id)
5874{
Tiago Vignattibe143262012-04-16 17:31:41 +03005875 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005876 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005877
Jason Ekstranda85118c2013-06-27 20:17:02 -05005878 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005879 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005880
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005881 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005882 wl_resource_set_implementation(resource,
5883 &desktop_shell_implementation,
5884 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005885 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005886
5887 if (version < 2)
5888 shell_fade_startup(shell);
5889
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005890 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005891 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005892
5893 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5894 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005895}
5896
Pekka Paalanen8274d902014-08-06 19:36:51 +03005897static int
5898screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5899{
5900 return snprintf(buf, len, "screensaver for output %s",
5901 surface->output->name);
5902}
5903
Pekka Paalanen6e168112011-11-24 11:34:05 +02005904static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005905screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005906{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005907 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005908 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005909 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005910
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005911 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005912 return;
5913
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005914 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005915 if (!shell->locked)
5916 return;
5917
Jason Ekstranda7af7042013-10-12 22:38:11 -05005918 view = container_of(surface->views.next, struct weston_view, surface_link);
5919 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005920
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005921 if (wl_list_empty(&view->layer_link.link)) {
5922 prev = container_of(shell->lock_layer.view_list.link.prev,
5923 struct weston_layer_entry, link);
5924 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005925 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005926 wl_event_source_timer_update(shell->screensaver.timer,
5927 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005928 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005929 }
5930}
5931
5932static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005933screensaver_set_surface(struct wl_client *client,
5934 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005935 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005936 struct wl_resource *output_resource)
5937{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005938 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005939 struct weston_surface *surface =
5940 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005941 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005942 struct weston_view *view, *next;
5943
5944 /* Make sure we only have one view */
5945 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5946 weston_view_destroy(view);
5947 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005948
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005949 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005950 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005951 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005952 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005953}
5954
5955static const struct screensaver_interface screensaver_implementation = {
5956 screensaver_set_surface
5957};
5958
5959static void
5960unbind_screensaver(struct wl_resource *resource)
5961{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005962 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005963
Pekka Paalanen77346a62011-11-30 16:26:35 +02005964 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005965}
5966
5967static void
5968bind_screensaver(struct wl_client *client,
5969 void *data, uint32_t version, uint32_t id)
5970{
Tiago Vignattibe143262012-04-16 17:31:41 +03005971 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005972 struct wl_resource *resource;
5973
Jason Ekstranda85118c2013-06-27 20:17:02 -05005974 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005975
Pekka Paalanen77346a62011-11-30 16:26:35 +02005976 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005977 wl_resource_set_implementation(resource,
5978 &screensaver_implementation,
5979 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005980 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005981 return;
5982 }
5983
5984 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5985 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005986}
5987
Kristian Høgsberg07045392012-02-19 18:52:44 -05005988struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005989 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005990 struct weston_surface *current;
5991 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005992 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005993 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005994};
5995
5996static void
5997switcher_next(struct switcher *switcher)
5998{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005999 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006000 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006001 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006002 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006003
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006004 /* temporary re-display minimized surfaces */
6005 struct weston_view *tmp;
6006 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006007 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6008 weston_layer_entry_remove(&view->layer_link);
6009 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006010 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6011 *minimized = view;
6012 }
6013
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006014 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006015 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006016 if (shsurf &&
6017 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6018 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006019 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006020 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006021 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006022 next = view->surface;
6023 prev = view->surface;
6024 view->alpha = 0.25;
6025 weston_view_geometry_dirty(view);
6026 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006027 }
Alex Wu1659daa2012-04-01 20:13:09 +08006028
Jason Ekstranda7af7042013-10-12 22:38:11 -05006029 if (is_black_surface(view->surface, NULL)) {
6030 view->alpha = 0.25;
6031 weston_view_geometry_dirty(view);
6032 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006033 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006034 }
6035
6036 if (next == NULL)
6037 next = first;
6038
Alex Wu07b26062012-03-12 16:06:01 +08006039 if (next == NULL)
6040 return;
6041
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006043 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044
6045 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006046 wl_list_for_each(view, &next->views, surface_link)
6047 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006048
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006049 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006050 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006051 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006052}
6053
6054static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006055switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056{
6057 struct switcher *switcher =
6058 container_of(listener, struct switcher, listener);
6059
6060 switcher_next(switcher);
6061}
6062
6063static void
Daniel Stone351eb612012-05-31 15:27:47 -04006064switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006065{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006066 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006067 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006068 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006069
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006070 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006071 if (is_focus_view(view))
6072 continue;
6073
Jason Ekstranda7af7042013-10-12 22:38:11 -05006074 view->alpha = 1.0;
6075 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006076 }
6077
Alex Wu07b26062012-03-12 16:06:01 +08006078 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006079 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006080 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006081 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006082 weston_keyboard_end_grab(keyboard);
6083 if (keyboard->input_method_resource)
6084 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006085
6086 /* re-hide surfaces that were temporary shown during the switch */
6087 struct weston_view **minimized;
6088 wl_array_for_each(minimized, &switcher->minimized_array) {
6089 /* with the exception of the current selected */
6090 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006091 weston_layer_entry_remove(&(*minimized)->layer_link);
6092 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006093 weston_view_damage_below(*minimized);
6094 }
6095 }
6096 wl_array_release(&switcher->minimized_array);
6097
Kristian Høgsberg07045392012-02-19 18:52:44 -05006098 free(switcher);
6099}
6100
6101static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006102switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006103 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006104{
6105 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006106 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006107
Daniel Stonec9785ea2012-05-30 16:31:52 +01006108 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006109 switcher_next(switcher);
6110}
6111
6112static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006113switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006114 uint32_t mods_depressed, uint32_t mods_latched,
6115 uint32_t mods_locked, uint32_t group)
6116{
6117 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006118 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006119
Daniel Stone351eb612012-05-31 15:27:47 -04006120 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6121 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006122}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006123
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006124static void
6125switcher_cancel(struct weston_keyboard_grab *grab)
6126{
6127 struct switcher *switcher = container_of(grab, struct switcher, grab);
6128
6129 switcher_destroy(switcher);
6130}
6131
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006132static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006133 switcher_key,
6134 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006135 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006136};
6137
6138static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006139switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006140 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006141{
Tiago Vignattibe143262012-04-16 17:31:41 +03006142 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006143 struct switcher *switcher;
6144
6145 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006146 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006147 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006148 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006149 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006150 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006151
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006152 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006153 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006154 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006155 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6156 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006157 switcher_next(switcher);
6158}
6159
Pekka Paalanen3c647232011-12-22 13:43:43 +02006160static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006161backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006162 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006163{
6164 struct weston_compositor *compositor = data;
6165 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006166 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006167
6168 /* TODO: we're limiting to simple use cases, where we assume just
6169 * control on the primary display. We'd have to extend later if we
6170 * ever get support for setting backlights on random desktop LCD
6171 * panels though */
6172 output = get_default_output(compositor);
6173 if (!output)
6174 return;
6175
6176 if (!output->set_backlight)
6177 return;
6178
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006179 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6180 backlight_new = output->backlight_current - 25;
6181 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6182 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006183
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006184 if (backlight_new < 5)
6185 backlight_new = 5;
6186 if (backlight_new > 255)
6187 backlight_new = 255;
6188
6189 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006190 output->set_backlight(output, output->backlight_current);
6191}
6192
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006193static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006194force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006195 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006196{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006197 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006198 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006199 struct desktop_shell *shell = data;
6200 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006201 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006202
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006203 focus_surface = seat->keyboard->focus;
6204 if (!focus_surface)
6205 return;
6206
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006207 wl_signal_emit(&compositor->kill_signal, focus_surface);
6208
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006209 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006210 wl_client_get_credentials(client, &pid, NULL, NULL);
6211
6212 /* Skip clients that we launched ourselves (the credentials of
6213 * the socketpair is ours) */
6214 if (pid == getpid())
6215 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006216
Daniel Stone325fc2d2012-05-30 16:31:58 +01006217 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006218}
6219
6220static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006221workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006222 uint32_t key, void *data)
6223{
6224 struct desktop_shell *shell = data;
6225 unsigned int new_index = shell->workspaces.current;
6226
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006227 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006228 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006229 if (new_index != 0)
6230 new_index--;
6231
6232 change_workspace(shell, new_index);
6233}
6234
6235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006236workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006237 uint32_t key, void *data)
6238{
6239 struct desktop_shell *shell = data;
6240 unsigned int new_index = shell->workspaces.current;
6241
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006242 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006243 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006244 if (new_index < shell->workspaces.num - 1)
6245 new_index++;
6246
6247 change_workspace(shell, new_index);
6248}
6249
6250static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006251workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006252 uint32_t key, void *data)
6253{
6254 struct desktop_shell *shell = data;
6255 unsigned int new_index;
6256
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006257 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006258 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006259 new_index = key - KEY_F1;
6260 if (new_index >= shell->workspaces.num)
6261 new_index = shell->workspaces.num - 1;
6262
6263 change_workspace(shell, new_index);
6264}
6265
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006266static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006267workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006268 uint32_t key, void *data)
6269{
6270 struct desktop_shell *shell = data;
6271 unsigned int new_index = shell->workspaces.current;
6272
6273 if (shell->locked)
6274 return;
6275
6276 if (new_index != 0)
6277 new_index--;
6278
6279 take_surface_to_workspace_by_seat(shell, seat, new_index);
6280}
6281
6282static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006283workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006284 uint32_t key, void *data)
6285{
6286 struct desktop_shell *shell = data;
6287 unsigned int new_index = shell->workspaces.current;
6288
6289 if (shell->locked)
6290 return;
6291
6292 if (new_index < shell->workspaces.num - 1)
6293 new_index++;
6294
6295 take_surface_to_workspace_by_seat(shell, seat, new_index);
6296}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006297
6298static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006299shell_reposition_view_on_output_destroy(struct weston_view *view)
6300{
6301 struct weston_output *output, *first_output;
6302 struct weston_compositor *ec = view->surface->compositor;
6303 struct shell_surface *shsurf;
6304 float x, y;
6305 int visible;
6306
6307 x = view->geometry.x;
6308 y = view->geometry.y;
6309
6310 /* At this point the destroyed output is not in the list anymore.
6311 * If the view is still visible somewhere, we leave where it is,
6312 * otherwise, move it to the first output. */
6313 visible = 0;
6314 wl_list_for_each(output, &ec->output_list, link) {
6315 if (pixman_region32_contains_point(&output->region,
6316 x, y, NULL)) {
6317 visible = 1;
6318 break;
6319 }
6320 }
6321
6322 if (!visible) {
6323 first_output = container_of(ec->output_list.next,
6324 struct weston_output, link);
6325
6326 x = first_output->x + first_output->width / 4;
6327 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006328
6329 weston_view_set_position(view, x, y);
6330 } else {
6331 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006332 }
6333
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006334
6335 shsurf = get_shell_surface(view->surface);
6336
6337 if (shsurf) {
6338 shsurf->saved_position_valid = false;
6339 shsurf->next_state.maximized = false;
6340 shsurf->next_state.fullscreen = false;
6341 shsurf->state_changed = true;
6342 }
6343}
6344
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006345void
6346shell_for_each_layer(struct desktop_shell *shell,
6347 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006348{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006349 struct workspace **ws;
6350
6351 func(shell, &shell->fullscreen_layer, data);
6352 func(shell, &shell->panel_layer, data);
6353 func(shell, &shell->background_layer, data);
6354 func(shell, &shell->lock_layer, data);
6355 func(shell, &shell->input_panel_layer, data);
6356
6357 wl_array_for_each(ws, &shell->workspaces.array)
6358 func(shell, &(*ws)->layer, data);
6359}
6360
6361static void
6362shell_output_destroy_move_layer(struct desktop_shell *shell,
6363 struct weston_layer *layer,
6364 void *data)
6365{
6366 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006367 struct weston_view *view;
6368
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006369 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006370 if (view->output != output)
6371 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006372
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006373 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006374 }
6375}
6376
6377static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006378handle_output_destroy(struct wl_listener *listener, void *data)
6379{
6380 struct shell_output *output_listener =
6381 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006382 struct weston_output *output = output_listener->output;
6383 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006384
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006385 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006386
Xiong Zhang6b481422013-10-23 13:58:32 +08006387 wl_list_remove(&output_listener->destroy_listener.link);
6388 wl_list_remove(&output_listener->link);
6389 free(output_listener);
6390}
6391
6392static void
6393create_shell_output(struct desktop_shell *shell,
6394 struct weston_output *output)
6395{
6396 struct shell_output *shell_output;
6397
6398 shell_output = zalloc(sizeof *shell_output);
6399 if (shell_output == NULL)
6400 return;
6401
6402 shell_output->output = output;
6403 shell_output->shell = shell;
6404 shell_output->destroy_listener.notify = handle_output_destroy;
6405 wl_signal_add(&output->destroy_signal,
6406 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006407 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006408}
6409
6410static void
6411handle_output_create(struct wl_listener *listener, void *data)
6412{
6413 struct desktop_shell *shell =
6414 container_of(listener, struct desktop_shell, output_create_listener);
6415 struct weston_output *output = (struct weston_output *)data;
6416
6417 create_shell_output(shell, output);
6418}
6419
6420static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006421handle_output_move_layer(struct desktop_shell *shell,
6422 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006423{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006424 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006425 struct weston_view *view;
6426 float x, y;
6427
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006428 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006429 if (view->output != output)
6430 continue;
6431
6432 x = view->geometry.x + output->move_x;
6433 y = view->geometry.y + output->move_y;
6434 weston_view_set_position(view, x, y);
6435 }
6436}
6437
6438static void
6439handle_output_move(struct wl_listener *listener, void *data)
6440{
6441 struct desktop_shell *shell;
6442
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006443 shell = container_of(listener, struct desktop_shell,
6444 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006445
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006446 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006447}
6448
6449static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006450setup_output_destroy_handler(struct weston_compositor *ec,
6451 struct desktop_shell *shell)
6452{
6453 struct weston_output *output;
6454
6455 wl_list_init(&shell->output_list);
6456 wl_list_for_each(output, &ec->output_list, link)
6457 create_shell_output(shell, output);
6458
6459 shell->output_create_listener.notify = handle_output_create;
6460 wl_signal_add(&ec->output_created_signal,
6461 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006462
6463 shell->output_move_listener.notify = handle_output_move;
6464 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006465}
6466
6467static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006468shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006469{
Tiago Vignattibe143262012-04-16 17:31:41 +03006470 struct desktop_shell *shell =
6471 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006472 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006473 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006474
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006475 /* Force state to unlocked so we don't try to fade */
6476 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006477
6478 if (shell->child.client) {
6479 /* disable respawn */
6480 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006481 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006482 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006483
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09006484 wl_event_source_remove(shell->screensaver.timer);
6485
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006486 wl_list_remove(&shell->idle_listener.link);
6487 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006488
6489 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006490
Xiong Zhang6b481422013-10-23 13:58:32 +08006491 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6492 wl_list_remove(&shell_output->destroy_listener.link);
6493 wl_list_remove(&shell_output->link);
6494 free(shell_output);
6495 }
6496
6497 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006498 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006499
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006500 wl_array_for_each(ws, &shell->workspaces.array)
6501 workspace_destroy(*ws);
6502 wl_array_release(&shell->workspaces.array);
6503
Pekka Paalanen3c647232011-12-22 13:43:43 +02006504 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006505 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006506 free(shell);
6507}
6508
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006509static void
6510shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6511{
6512 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006513 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006514
6515 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006516 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6517 MODIFIER_CTRL | MODIFIER_ALT,
6518 terminate_binding, ec);
6519 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6520 click_to_activate_binding,
6521 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006522 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6523 click_to_activate_binding,
6524 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006525 weston_compositor_add_touch_binding(ec, 0,
6526 touch_to_activate_binding,
6527 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006528 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6529 MODIFIER_SUPER | MODIFIER_ALT,
6530 surface_opacity_binding, NULL);
6531 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6532 MODIFIER_SUPER, zoom_axis_binding,
6533 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006534
6535 /* configurable bindings */
6536 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006537 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6538 zoom_key_binding, NULL);
6539 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6540 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006541 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6542 maximize_binding, NULL);
6543 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6544 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006545 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6546 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006547 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006548 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6549 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006550 weston_compositor_add_button_binding(ec, BTN_LEFT,
6551 mod | MODIFIER_SHIFT,
6552 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006553
6554 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6555 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6556 rotate_binding, NULL);
6557
Daniel Stone325fc2d2012-05-30 16:31:58 +01006558 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6559 shell);
6560 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6561 ec);
6562 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6563 backlight_binding, ec);
6564 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6565 ec);
6566 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6567 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006568 weston_compositor_add_key_binding(ec, KEY_K, mod,
6569 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006570 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6571 workspace_up_binding, shell);
6572 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6573 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006574 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6575 workspace_move_surface_up_binding,
6576 shell);
6577 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6578 workspace_move_surface_down_binding,
6579 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006580
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006581 if (shell->exposay_modifier)
6582 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6583 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006584
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006585 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6586 if (shell->workspaces.num > 1) {
6587 num_workspace_bindings = shell->workspaces.num;
6588 if (num_workspace_bindings > 6)
6589 num_workspace_bindings = 6;
6590 for (i = 0; i < num_workspace_bindings; i++)
6591 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6592 workspace_f_binding,
6593 shell);
6594 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006595
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006596 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006597}
6598
Jason Ekstrand024177c2014-04-21 19:42:58 -05006599static void
6600handle_seat_created(struct wl_listener *listener, void *data)
6601{
6602 struct weston_seat *seat = data;
6603
6604 create_shell_seat(seat);
6605}
6606
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006607WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006608module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006609 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006610{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006611 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006612 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006613 struct workspace **pws;
6614 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006615 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006616
Peter Huttererf3d62272013-08-08 11:57:05 +10006617 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006618 if (shell == NULL)
6619 return -1;
6620
Kristian Høgsberg75840622011-09-06 13:48:16 -04006621 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006622
6623 shell->destroy_listener.notify = shell_destroy;
6624 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006625 shell->idle_listener.notify = idle_handler;
6626 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6627 shell->wake_listener.notify = wake_handler;
6628 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006629
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006630 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006631 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006632 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006633 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006634 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006635 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006636 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006637 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006638 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006639 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006640 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006641 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006642 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006643
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006644 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6645 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006646 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6647 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006648 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006649
6650 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006651 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006652
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006653 if (input_panel_setup(shell) < 0)
6654 return -1;
6655
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006656 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006657
Daniel Stonedf8133b2013-11-19 11:37:14 +01006658 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6659 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6660
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006661 for (i = 0; i < shell->workspaces.num; i++) {
6662 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6663 if (pws == NULL)
6664 return -1;
6665
6666 *pws = workspace_create();
6667 if (*pws == NULL)
6668 return -1;
6669 }
6670 activate_workspace(shell, 0);
6671
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006672 weston_layer_init(&shell->minimized_layer, NULL);
6673
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006674 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006675 wl_list_init(&shell->workspaces.animation.link);
6676 shell->workspaces.animation.frame = animate_workspace_change_frame;
6677
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006678 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006679 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006680 return -1;
6681
Rafael Antognollie2a34552013-12-03 15:35:45 -02006682 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6683 shell, bind_xdg_shell) == NULL)
6684 return -1;
6685
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006686 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006687 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006688 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006689 return -1;
6690
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006691 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6692 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006693 return -1;
6694
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006695 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6696 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006697 return -1;
6698
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006699 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006700
Jonny Lamb765760d2014-08-20 15:53:19 +02006701 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6702
Xiong Zhang6b481422013-10-23 13:58:32 +08006703 setup_output_destroy_handler(ec, shell);
6704
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006705 loop = wl_display_get_event_loop(ec->wl_display);
6706 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006707
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006708 shell->screensaver.timer =
6709 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6710
Jason Ekstrand024177c2014-04-21 19:42:58 -05006711 wl_list_for_each(seat, &ec->seat_list, link)
6712 handle_seat_created(NULL, seat);
6713 shell->seat_create_listener.notify = handle_seat_created;
6714 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006715
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006716 screenshooter_create(ec);
6717
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006718 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006719
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006720 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006721
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006722 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6723
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006724 return 0;
6725}