blob: e7e31b0ae51c6248645a4a9e764095f00e22cb37 [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
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Scott Moreauff1db4a2012-04-17 19:06:18 -060073struct ping_timer {
74 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060075 uint32_t serial;
76};
77
Philip Withnall648a4dd2013-11-25 18:01:44 +000078/*
79 * Surface stacking and ordering.
80 *
81 * This is handled using several linked lists of surfaces, organised into
82 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
83 * above all of the surfaces in the layer below. The set of layers is static and
84 * in the following order (top-most first):
85 * • Lock layer (only ever displayed on its own)
86 * • Cursor layer
87 * • Fullscreen layer
88 * • Panel layer
89 * • Input panel layer
90 * • Workspace layers
91 * • Background layer
92 *
93 * The list of layers may be manipulated to remove whole layers of surfaces from
94 * display. For example, when locking the screen, all layers except the lock
95 * layer are removed.
96 *
97 * A surface’s layer is modified on configuring the surface, in
98 * set_surface_type() (which is only called when the surface’s type change is
99 * _committed_). If a surface’s type changes (e.g. when making a window
100 * fullscreen) its layer changes too.
101 *
102 * In order to allow popup and transient surfaces to be correctly stacked above
103 * their parent surfaces, each surface tracks both its parent surface, and a
104 * linked list of its children. When a surface’s layer is updated, so are the
105 * layers of its children. Note that child surfaces are *not* the same as
106 * subsurfaces — child/parent surfaces are purely for maintaining stacking
107 * order.
108 *
109 * The children_link list of siblings of a surface (i.e. those surfaces which
110 * have the same parent) only contains weston_surfaces which have a
111 * shell_surface. Stacking is not implemented for non-shell_surface
112 * weston_surfaces. This means that the following implication does *not* hold:
113 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
114 */
115
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200116struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500117 struct wl_resource *resource;
118 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600122 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200123 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400124 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000125 struct wl_list children_list; /* child surfaces of this one */
126 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300127 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800129 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400130 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200132 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800133 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200134 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800135 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700136 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100137
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500140 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 } rotation;
142
143 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100144 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100146 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400147 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 } popup;
149
Alex Wu4539b082012-03-01 12:57:46 +0800150 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300151 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400152 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300153 } transient;
154
155 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800156 enum wl_shell_surface_fullscreen_method type;
157 struct weston_transform transform; /* matrix from x, y */
158 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800160 } fullscreen;
161
Scott Moreauff1db4a2012-04-17 19:06:18 -0600162 struct ping_timer *ping_timer;
163
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200164 struct weston_transform workspace_transform;
165
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500166 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200168 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100169 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400170
171 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172
173 struct {
174 bool maximized;
175 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200176 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200177 } state, next_state; /* surface states */
178 bool state_changed;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179
180 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200181};
182
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400184 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300185 struct shell_surface *shsurf;
186 struct wl_listener shsurf_destroy_listener;
187};
188
Rusty Lynch1084da52013-08-15 09:10:08 -0700189struct shell_touch_grab {
190 struct weston_touch_grab grab;
191 struct shell_surface *shsurf;
192 struct wl_listener shsurf_destroy_listener;
193 struct weston_touch *touch;
194};
195
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300196struct weston_move_grab {
197 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100198 wl_fixed_t dx, dy;
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
221 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400222 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100223 struct wl_list surfaces_list;
224 struct wl_client *client;
225 int32_t initial_up;
226 } popup_grab;
227};
228
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100229void
230set_alpha_if_fullscreen(struct shell_surface *shsurf)
231{
232 if (shsurf && shsurf->state.fullscreen)
233 shsurf->fullscreen.black_view->alpha = 0.25;
234}
235
Alex Wubd3354b2012-04-17 17:20:49 +0800236static struct desktop_shell *
237shell_surface_get_shell(struct shell_surface *shsurf);
238
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500239static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400240surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500241
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300242static void
243shell_fade_startup(struct desktop_shell *shell);
244
Philip Withnallbecb77e2013-11-25 18:01:30 +0000245static struct shell_seat *
246get_shell_seat(struct weston_seat *seat);
247
Philip Withnall648a4dd2013-11-25 18:01:44 +0000248static void
249shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
250
Alex Wubd3354b2012-04-17 17:20:49 +0800251static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200252shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
253
254static bool
255shell_surface_is_xdg_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_popup(struct shell_surface *shsurf);
259
260static void
261shell_surface_set_parent(struct shell_surface *shsurf,
262 struct weston_surface *parent);
263
264static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800265shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
266{
267 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800269
270 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100271
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800273 return false;
274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
276 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800277 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800279}
280
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500281static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400282destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283{
284 struct shell_grab *grab;
285
286 grab = container_of(listener, struct shell_grab,
287 shsurf_destroy_listener);
288
289 grab->shsurf = NULL;
290}
291
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800292struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293get_default_view(struct weston_surface *surface)
294{
295 struct shell_surface *shsurf;
296 struct weston_view *view;
297
298 if (!surface || wl_list_empty(&surface->views))
299 return NULL;
300
301 shsurf = get_shell_surface(surface);
302 if (shsurf)
303 return shsurf->view;
304
305 wl_list_for_each(view, &surface->views, surface_link)
306 if (weston_view_is_mapped(view))
307 return view;
308
309 return container_of(surface->views.next, struct weston_view, surface_link);
310}
311
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300312static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400314
315static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400317 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400319 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300321{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300322 struct desktop_shell *shell = shsurf->shell;
323
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324 popup_grab_end(pointer);
325
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300326 grab->grab.interface = interface;
327 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400328 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500329 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400330 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700332 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400333 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400334 if (shell->child.desktop_shell) {
335 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
336 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337 weston_pointer_set_focus(pointer,
338 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400339 wl_fixed_from_int(0),
340 wl_fixed_from_int(0));
341 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342}
343
344static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300345shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300346{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700347 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400348 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700349 grab->shsurf->grabbed = 0;
350 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300351
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700352 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353}
354
355static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700356shell_touch_grab_start(struct shell_touch_grab *grab,
357 const struct weston_touch_grab_interface *interface,
358 struct shell_surface *shsurf,
359 struct weston_touch *touch)
360{
361 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800362
Rusty Lynch1084da52013-08-15 09:10:08 -0700363 grab->grab.interface = interface;
364 grab->shsurf = shsurf;
365 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
366 wl_signal_add(&shsurf->destroy_signal,
367 &grab->shsurf_destroy_listener);
368
369 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700371
372 weston_touch_start_grab(touch, &grab->grab);
373 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500374 weston_touch_set_focus(touch->seat,
375 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700376}
377
378static void
379shell_touch_grab_end(struct shell_touch_grab *grab)
380{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700381 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700382 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700383 grab->shsurf->grabbed = 0;
384 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700385
386 weston_touch_end_grab(grab->touch);
387}
388
389static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500390center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800391 struct weston_output *output);
392
Daniel Stone496ca172012-05-30 16:31:42 +0100393static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300394get_modifier(char *modifier)
395{
396 if (!modifier)
397 return MODIFIER_SUPER;
398
399 if (!strcmp("ctrl", modifier))
400 return MODIFIER_CTRL;
401 else if (!strcmp("alt", modifier))
402 return MODIFIER_ALT;
403 else if (!strcmp("super", modifier))
404 return MODIFIER_SUPER;
405 else
406 return MODIFIER_SUPER;
407}
408
Juan Zhaoe10d2792012-04-25 19:09:52 +0800409static enum animation_type
410get_animation_type(char *animation)
411{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800412 if (!animation)
413 return ANIMATION_NONE;
414
Juan Zhaoe10d2792012-04-25 19:09:52 +0800415 if (!strcmp("zoom", animation))
416 return ANIMATION_ZOOM;
417 else if (!strcmp("fade", animation))
418 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100419 else if (!strcmp("dim-layer", animation))
420 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800421 else
422 return ANIMATION_NONE;
423}
424
Alex Wu4539b082012-03-01 12:57:46 +0800425static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400426shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200427{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400428 struct weston_config_section *section;
429 int duration;
430 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 section = weston_config_get_section(shell->compositor->config,
433 "screensaver", NULL, NULL);
434 weston_config_section_get_string(section,
435 "path", &shell->screensaver.path, NULL);
436 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200437 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400438
439 section = weston_config_get_section(shell->compositor->config,
440 "shell", NULL, NULL);
441 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100442 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100443 shell->client = s;
444 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400445 "binding-modifier", &s, "super");
446 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200447 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800448
449 weston_config_section_get_string(section,
450 "exposay-modifier", &s, "none");
451 if (strcmp(s, "none") == 0)
452 shell->exposay_modifier = 0;
453 else
454 shell->exposay_modifier = get_modifier(s);
455 free(s);
456
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400457 weston_config_section_get_string(section, "animation", &s, "none");
458 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200459 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700460 weston_config_section_get_string(section,
461 "startup-animation", &s, "fade");
462 shell->startup_animation_type = get_animation_type(s);
463 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700464 if (shell->startup_animation_type == ANIMATION_ZOOM)
465 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100466 weston_config_section_get_string(section, "focus-animation", &s, "none");
467 shell->focus_animation_type = get_animation_type(s);
468 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 weston_config_section_get_uint(section, "num-workspaces",
470 &shell->workspaces.num,
471 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200472}
473
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800474struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100475get_default_output(struct weston_compositor *compositor)
476{
477 return container_of(compositor->output_list.next,
478 struct weston_output, link);
479}
480
481
482/* no-op func for checking focus surface */
483static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600484focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100485{
486}
487
488static struct focus_surface *
489get_focus_surface(struct weston_surface *surface)
490{
491 if (surface->configure == focus_surface_configure)
492 return surface->configure_private;
493 else
494 return NULL;
495}
496
497static bool
498is_focus_surface (struct weston_surface *es)
499{
500 return (es->configure == focus_surface_configure);
501}
502
503static bool
504is_focus_view (struct weston_view *view)
505{
506 return is_focus_surface (view->surface);
507}
508
509static struct focus_surface *
510create_focus_surface(struct weston_compositor *ec,
511 struct weston_output *output)
512{
513 struct focus_surface *fsurf = NULL;
514 struct weston_surface *surface = NULL;
515
516 fsurf = malloc(sizeof *fsurf);
517 if (!fsurf)
518 return NULL;
519
520 fsurf->surface = weston_surface_create(ec);
521 surface = fsurf->surface;
522 if (surface == NULL) {
523 free(fsurf);
524 return NULL;
525 }
526
527 surface->configure = focus_surface_configure;
528 surface->output = output;
529 surface->configure_private = fsurf;
530
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800531 fsurf->view = weston_view_create(surface);
532 if (fsurf->view == NULL) {
533 weston_surface_destroy(surface);
534 free(fsurf);
535 return NULL;
536 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100537 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600539 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600540 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100541 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
542 pixman_region32_fini(&surface->opaque);
543 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
544 output->width, output->height);
545 pixman_region32_fini(&surface->input);
546 pixman_region32_init(&surface->input);
547
548 wl_list_init(&fsurf->workspace_transform.link);
549
550 return fsurf;
551}
552
553static void
554focus_surface_destroy(struct focus_surface *fsurf)
555{
556 weston_surface_destroy(fsurf->surface);
557 free(fsurf);
558}
559
560static void
561focus_animation_done(struct weston_view_animation *animation, void *data)
562{
563 struct workspace *ws = data;
564
565 ws->focus_animation = NULL;
566}
567
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200568static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200569focus_state_destroy(struct focus_state *state)
570{
571 wl_list_remove(&state->seat_destroy_listener.link);
572 wl_list_remove(&state->surface_destroy_listener.link);
573 free(state);
574}
575
576static void
577focus_state_seat_destroy(struct wl_listener *listener, void *data)
578{
579 struct focus_state *state = container_of(listener,
580 struct focus_state,
581 seat_destroy_listener);
582
583 wl_list_remove(&state->link);
584 focus_state_destroy(state);
585}
586
587static void
588focus_state_surface_destroy(struct wl_listener *listener, void *data)
589{
590 struct focus_state *state = container_of(listener,
591 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400592 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400593 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594 struct weston_surface *main_surface, *next;
595 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200596
Pekka Paalanen01388e22013-04-25 13:57:44 +0300597 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
598
Kristian Høgsberge3778222012-07-31 17:29:30 -0400599 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
601 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400602 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100603 if (is_focus_view(view))
604 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400605
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400607 break;
608 }
609
Pekka Paalanen01388e22013-04-25 13:57:44 +0300610 /* if the focus was a sub-surface, activate its main surface */
611 if (main_surface != state->keyboard_focus)
612 next = main_surface;
613
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100614 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400615 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400617 activate(shell, next, state->seat);
618 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
620 if (state->ws->focus_animation)
621 weston_view_animation_destroy(state->ws->focus_animation);
622
623 state->ws->focus_animation = weston_fade_run(
624 state->ws->fsurf_front->view,
625 state->ws->fsurf_front->view->alpha, 0.0, 300,
626 focus_animation_done, state->ws);
627 }
628
Kristian Høgsberge3778222012-07-31 17:29:30 -0400629 wl_list_remove(&state->link);
630 focus_state_destroy(state);
631 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200632}
633
634static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400635focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200636{
Jonas Ådahl04769742012-06-13 00:01:24 +0200637 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200638
639 state = malloc(sizeof *state);
640 if (state == NULL)
641 return NULL;
642
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100643 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400644 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200645 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400646 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200647
648 state->seat_destroy_listener.notify = focus_state_seat_destroy;
649 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400650 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200651 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400652 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200653
654 return state;
655}
656
Jonas Ådahl8538b222012-08-29 22:13:03 +0200657static struct focus_state *
658ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
659{
660 struct workspace *ws = get_current_workspace(shell);
661 struct focus_state *state;
662
663 wl_list_for_each(state, &ws->focus_list, link)
664 if (state->seat == seat)
665 break;
666
667 if (&state->link == &ws->focus_list)
668 state = focus_state_create(seat, ws);
669
670 return state;
671}
672
Jonas Ådahl04769742012-06-13 00:01:24 +0200673static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800674focus_state_set_focus(struct focus_state *state,
675 struct weston_surface *surface)
676{
677 if (state->keyboard_focus) {
678 wl_list_remove(&state->surface_destroy_listener.link);
679 wl_list_init(&state->surface_destroy_listener.link);
680 }
681
682 state->keyboard_focus = surface;
683 if (surface)
684 wl_signal_add(&surface->destroy_signal,
685 &state->surface_destroy_listener);
686}
687
688static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400689restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200690{
691 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400692 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200693
694 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800695 if (state->seat->keyboard == NULL)
696 continue;
697
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400698 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200699
Kristian Høgsberge3148752013-05-06 23:19:49 -0400700 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200701 }
702}
703
704static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200705replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
706 struct weston_seat *seat)
707{
708 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200709
710 wl_list_for_each(state, &ws->focus_list, link) {
711 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800712 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713 return;
714 }
715 }
716}
717
718static void
719drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
720 struct weston_surface *surface)
721{
722 struct focus_state *state;
723
724 wl_list_for_each(state, &ws->focus_list, link)
725 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800726 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200727}
728
729static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100730animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
731 struct weston_view *from, struct weston_view *to)
732{
733 struct weston_output *output;
734 bool focus_surface_created = false;
735
736 /* FIXME: Only support dim animation using two layers */
737 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
738 return;
739
740 output = get_default_output(shell->compositor);
741 if (ws->fsurf_front == NULL && (from || to)) {
742 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800743 if (ws->fsurf_front == NULL)
744 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100745 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800746
747 ws->fsurf_back = create_focus_surface(shell->compositor, output);
748 if (ws->fsurf_back == NULL) {
749 focus_surface_destroy(ws->fsurf_front);
750 return;
751 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100752 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800753
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754 focus_surface_created = true;
755 } else {
756 wl_list_remove(&ws->fsurf_front->view->layer_link);
757 wl_list_remove(&ws->fsurf_back->view->layer_link);
758 }
759
760 if (ws->focus_animation) {
761 weston_view_animation_destroy(ws->focus_animation);
762 ws->focus_animation = NULL;
763 }
764
765 if (to)
766 wl_list_insert(&to->layer_link,
767 &ws->fsurf_front->view->layer_link);
768 else if (from)
769 wl_list_insert(&ws->layer.view_list,
770 &ws->fsurf_front->view->layer_link);
771
772 if (focus_surface_created) {
773 ws->focus_animation = weston_fade_run(
774 ws->fsurf_front->view,
775 ws->fsurf_front->view->alpha, 0.6, 300,
776 focus_animation_done, ws);
777 } else if (from) {
778 wl_list_insert(&from->layer_link,
779 &ws->fsurf_back->view->layer_link);
780 ws->focus_animation = weston_stable_fade_run(
781 ws->fsurf_front->view, 0.0,
782 ws->fsurf_back->view, 0.6,
783 focus_animation_done, ws);
784 } else if (to) {
785 wl_list_insert(&ws->layer.view_list,
786 &ws->fsurf_back->view->layer_link);
787 ws->focus_animation = weston_stable_fade_run(
788 ws->fsurf_front->view, 0.0,
789 ws->fsurf_back->view, 0.6,
790 focus_animation_done, ws);
791 }
792}
793
794static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795workspace_destroy(struct workspace *ws)
796{
Jonas Ådahl04769742012-06-13 00:01:24 +0200797 struct focus_state *state, *next;
798
799 wl_list_for_each_safe(state, next, &ws->focus_list, link)
800 focus_state_destroy(state);
801
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100802 if (ws->fsurf_front)
803 focus_surface_destroy(ws->fsurf_front);
804 if (ws->fsurf_back)
805 focus_surface_destroy(ws->fsurf_back);
806
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200807 free(ws);
808}
809
Jonas Ådahl04769742012-06-13 00:01:24 +0200810static void
811seat_destroyed(struct wl_listener *listener, void *data)
812{
813 struct weston_seat *seat = data;
814 struct focus_state *state, *next;
815 struct workspace *ws = container_of(listener,
816 struct workspace,
817 seat_destroyed_listener);
818
819 wl_list_for_each_safe(state, next, &ws->focus_list, link)
820 if (state->seat == seat)
821 wl_list_remove(&state->link);
822}
823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824static struct workspace *
825workspace_create(void)
826{
827 struct workspace *ws = malloc(sizeof *ws);
828 if (ws == NULL)
829 return NULL;
830
831 weston_layer_init(&ws->layer, NULL);
832
Jonas Ådahl04769742012-06-13 00:01:24 +0200833 wl_list_init(&ws->focus_list);
834 wl_list_init(&ws->seat_destroyed_listener.link);
835 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 ws->fsurf_front = NULL;
837 ws->fsurf_back = NULL;
838 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200839
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200840 return ws;
841}
842
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843static int
844workspace_is_empty(struct workspace *ws)
845{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847}
848
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200849static struct workspace *
850get_workspace(struct desktop_shell *shell, unsigned int index)
851{
852 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200853 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200854 pws += index;
855 return *pws;
856}
857
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800858struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200859get_current_workspace(struct desktop_shell *shell)
860{
861 return get_workspace(shell, shell->workspaces.current);
862}
863
864static void
865activate_workspace(struct desktop_shell *shell, unsigned int index)
866{
867 struct workspace *ws;
868
869 ws = get_workspace(shell, index);
870 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
871
872 shell->workspaces.current = index;
873}
874
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200875static unsigned int
876get_output_height(struct weston_output *output)
877{
878 return abs(output->region.extents.y1 - output->region.extents.y2);
879}
880
881static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200883{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 struct weston_transform *transform;
885
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100886 if (is_focus_view(view)) {
887 struct focus_surface *fsurf = get_focus_surface(view->surface);
888 transform = &fsurf->workspace_transform;
889 } else {
890 struct shell_surface *shsurf = get_shell_surface(view->surface);
891 transform = &shsurf->workspace_transform;
892 }
893
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100896 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100898 weston_matrix_init(&transform->matrix);
899 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200900 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500901 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200902}
903
904static void
905workspace_translate_out(struct workspace *ws, double fraction)
906{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908 unsigned int height;
909 double d;
910
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
912 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913 d = height * fraction;
914
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100915 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200916 }
917}
918
919static void
920workspace_translate_in(struct workspace *ws, double fraction)
921{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200923 unsigned int height;
924 double d;
925
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
927 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928
929 if (fraction > 0)
930 d = -(height - height * fraction);
931 else
932 d = height + height * fraction;
933
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100934 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 }
936}
937
938static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939broadcast_current_workspace_state(struct desktop_shell *shell)
940{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700941 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700943 wl_resource_for_each(resource, &shell->workspaces.client_list)
944 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200945 shell->workspaces.current,
946 shell->workspaces.num);
947}
948
949static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950reverse_workspace_change_animation(struct desktop_shell *shell,
951 unsigned int index,
952 struct workspace *from,
953 struct workspace *to)
954{
955 shell->workspaces.current = index;
956
957 shell->workspaces.anim_to = to;
958 shell->workspaces.anim_from = from;
959 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
960 shell->workspaces.anim_timestamp = 0;
961
Scott Moreau4272e632012-08-13 09:58:41 -0600962 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200963}
964
965static void
966workspace_deactivate_transforms(struct workspace *ws)
967{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 if (is_focus_view(view)) {
973 struct focus_surface *fsurf = get_focus_surface(view->surface);
974 transform = &fsurf->workspace_transform;
975 } else {
976 struct shell_surface *shsurf = get_shell_surface(view->surface);
977 transform = &shsurf->workspace_transform;
978 }
979
980 if (!wl_list_empty(&transform->link)) {
981 wl_list_remove(&transform->link);
982 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200983 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985 }
986}
987
988static void
989finish_workspace_change_animation(struct desktop_shell *shell,
990 struct workspace *from,
991 struct workspace *to)
992{
Scott Moreau4272e632012-08-13 09:58:41 -0600993 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994
995 wl_list_remove(&shell->workspaces.animation.link);
996 workspace_deactivate_transforms(from);
997 workspace_deactivate_transforms(to);
998 shell->workspaces.anim_to = NULL;
999
1000 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1001}
1002
1003static void
1004animate_workspace_change_frame(struct weston_animation *animation,
1005 struct weston_output *output, uint32_t msecs)
1006{
1007 struct desktop_shell *shell =
1008 container_of(animation, struct desktop_shell,
1009 workspaces.animation);
1010 struct workspace *from = shell->workspaces.anim_from;
1011 struct workspace *to = shell->workspaces.anim_to;
1012 uint32_t t;
1013 double x, y;
1014
1015 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1016 finish_workspace_change_animation(shell, from, to);
1017 return;
1018 }
1019
1020 if (shell->workspaces.anim_timestamp == 0) {
1021 if (shell->workspaces.anim_current == 0.0)
1022 shell->workspaces.anim_timestamp = msecs;
1023 else
1024 shell->workspaces.anim_timestamp =
1025 msecs -
1026 /* Invers of movement function 'y' below. */
1027 (asin(1.0 - shell->workspaces.anim_current) *
1028 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1029 M_2_PI);
1030 }
1031
1032 t = msecs - shell->workspaces.anim_timestamp;
1033
1034 /*
1035 * x = [0, π/2]
1036 * y(x) = sin(x)
1037 */
1038 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1039 y = sin(x);
1040
1041 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001042 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001043
1044 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1045 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1046 shell->workspaces.anim_current = y;
1047
Scott Moreau4272e632012-08-13 09:58:41 -06001048 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001049 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001050 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001052}
1053
1054static void
1055animate_workspace_change(struct desktop_shell *shell,
1056 unsigned int index,
1057 struct workspace *from,
1058 struct workspace *to)
1059{
1060 struct weston_output *output;
1061
1062 int dir;
1063
1064 if (index > shell->workspaces.current)
1065 dir = -1;
1066 else
1067 dir = 1;
1068
1069 shell->workspaces.current = index;
1070
1071 shell->workspaces.anim_dir = dir;
1072 shell->workspaces.anim_from = from;
1073 shell->workspaces.anim_to = to;
1074 shell->workspaces.anim_current = 0.0;
1075 shell->workspaces.anim_timestamp = 0;
1076
1077 output = container_of(shell->compositor->output_list.next,
1078 struct weston_output, link);
1079 wl_list_insert(&output->animation_list,
1080 &shell->workspaces.animation.link);
1081
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001082 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083
1084 workspace_translate_in(to, 0);
1085
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001086 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001087
Scott Moreau4272e632012-08-13 09:58:41 -06001088 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089}
1090
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001091static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001092update_workspace(struct desktop_shell *shell, unsigned int index,
1093 struct workspace *from, struct workspace *to)
1094{
1095 shell->workspaces.current = index;
1096 wl_list_insert(&from->layer.link, &to->layer.link);
1097 wl_list_remove(&from->layer.link);
1098}
1099
1100static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001101change_workspace(struct desktop_shell *shell, unsigned int index)
1102{
1103 struct workspace *from;
1104 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001105 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001106
1107 if (index == shell->workspaces.current)
1108 return;
1109
1110 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001112 return;
1113
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001114 from = get_current_workspace(shell);
1115 to = get_workspace(shell, index);
1116
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 if (shell->workspaces.anim_from == to &&
1118 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001121 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 return;
1123 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001124
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 if (shell->workspaces.anim_to != NULL)
1126 finish_workspace_change_animation(shell,
1127 shell->workspaces.anim_from,
1128 shell->workspaces.anim_to);
1129
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001130 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001131
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001132 if (shell->focus_animation_type != ANIMATION_NONE) {
1133 wl_list_for_each(state, &from->focus_list, link)
1134 if (state->keyboard_focus)
1135 animate_focus_change(shell, from,
1136 get_default_view(state->keyboard_focus), NULL);
1137
1138 wl_list_for_each(state, &to->focus_list, link)
1139 if (state->keyboard_focus)
1140 animate_focus_change(shell, to,
1141 NULL, get_default_view(state->keyboard_focus));
1142 }
1143
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001144 if (workspace_is_empty(to) && workspace_is_empty(from))
1145 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146 else
1147 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001148
1149 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001150}
1151
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001152static bool
1153workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1154{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001156 struct wl_list *e;
1157
1158 if (wl_list_empty(list))
1159 return false;
1160
1161 e = list->next;
1162
1163 if (e->next != list)
1164 return false;
1165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001167}
1168
1169static void
Philip Withnall659163d2013-11-25 18:01:36 +00001170move_surface_to_workspace(struct desktop_shell *shell,
1171 struct shell_surface *shsurf,
1172 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001173{
1174 struct workspace *from;
1175 struct workspace *to;
1176 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001177 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001178 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001179
1180 if (workspace == shell->workspaces.current)
1181 return;
1182
Philip Withnall659163d2013-11-25 18:01:36 +00001183 view = get_default_view(shsurf->surface);
1184 if (!view)
1185 return;
1186
1187 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1188
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001189 if (workspace >= shell->workspaces.num)
1190 workspace = shell->workspaces.num - 1;
1191
Jonas Ådahle9d22502012-08-29 22:13:01 +02001192 from = get_current_workspace(shell);
1193 to = get_workspace(shell, workspace);
1194
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 wl_list_remove(&view->layer_link);
1196 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001197
Philip Withnall648a4dd2013-11-25 18:01:44 +00001198 shell_surface_update_child_surface_layers(shsurf);
1199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001201 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1202 if (!seat->keyboard)
1203 continue;
1204
1205 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001207 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001208 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001209
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001211}
1212
1213static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001215 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001216 unsigned int index)
1217{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001218 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 struct shell_surface *shsurf;
1221 struct workspace *from;
1222 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001223 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224
Pekka Paalanen01388e22013-04-25 13:57:44 +03001225 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view = get_default_view(surface);
1227 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001228 index == shell->workspaces.current ||
1229 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 return;
1231
1232 from = get_current_workspace(shell);
1233 to = get_workspace(shell, index);
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 wl_list_remove(&view->layer_link);
1236 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001237
Philip Withnall659163d2013-11-25 18:01:36 +00001238 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001239 if (shsurf != NULL)
1240 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001241
Jonas Ådahle9d22502012-08-29 22:13:01 +02001242 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001243 drop_focus_state(shell, from, surface);
1244
1245 if (shell->workspaces.anim_from == to &&
1246 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001247 wl_list_remove(&to->layer.link);
1248 wl_list_insert(from->layer.link.prev, &to->layer.link);
1249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001251 broadcast_current_workspace_state(shell);
1252
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 return;
1254 }
1255
1256 if (shell->workspaces.anim_to != NULL)
1257 finish_workspace_change_animation(shell,
1258 shell->workspaces.anim_from,
1259 shell->workspaces.anim_to);
1260
1261 if (workspace_is_empty(from) &&
1262 workspace_has_only(to, surface))
1263 update_workspace(shell, index, from, to);
1264 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001265 if (shsurf != NULL &&
1266 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 wl_list_insert(&shell->workspaces.anim_sticky_list,
1268 &shsurf->workspace_transform.link);
1269
1270 animate_workspace_change(shell, index, from, to);
1271 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001272
1273 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001274
1275 state = ensure_focus_state(shell, seat);
1276 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001277 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001278}
1279
1280static void
1281workspace_manager_move_surface(struct wl_client *client,
1282 struct wl_resource *resource,
1283 struct wl_resource *surface_resource,
1284 uint32_t workspace)
1285{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001286 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001287 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001288 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001289 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001290 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291
Pekka Paalanen01388e22013-04-25 13:57:44 +03001292 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001293 shell_surface = get_shell_surface(main_surface);
1294 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001296
1297 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001298}
1299
1300static const struct workspace_manager_interface workspace_manager_implementation = {
1301 workspace_manager_move_surface,
1302};
1303
1304static void
1305unbind_resource(struct wl_resource *resource)
1306{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001307 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308}
1309
1310static void
1311bind_workspace_manager(struct wl_client *client,
1312 void *data, uint32_t version, uint32_t id)
1313{
1314 struct desktop_shell *shell = data;
1315 struct wl_resource *resource;
1316
Jason Ekstranda85118c2013-06-27 20:17:02 -05001317 resource = wl_resource_create(client,
1318 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001319
1320 if (resource == NULL) {
1321 weston_log("couldn't add workspace manager object");
1322 return;
1323 }
1324
Jason Ekstranda85118c2013-06-27 20:17:02 -05001325 wl_resource_set_implementation(resource,
1326 &workspace_manager_implementation,
1327 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001328 wl_list_insert(&shell->workspaces.client_list,
1329 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001330
1331 workspace_manager_send_state(resource,
1332 shell->workspaces.current,
1333 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001334}
1335
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001336static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001337touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1338 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1339{
1340}
1341
1342static void
1343touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1344{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001345 struct weston_touch_move_grab *move =
1346 (struct weston_touch_move_grab *) container_of(
1347 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001348
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001349 if (touch_id == 0)
1350 move->active = 0;
1351
Jonas Ådahl9484b692013-12-02 22:05:03 +01001352 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001353 shell_touch_grab_end(&move->base);
1354 free(move);
1355 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001356}
1357
1358static void
1359touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1360 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1361{
1362 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1363 struct shell_surface *shsurf = move->base.shsurf;
1364 struct weston_surface *es;
1365 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1366 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1367
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001368 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001369 return;
1370
1371 es = shsurf->surface;
1372
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001373 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001374
1375 weston_compositor_schedule_repaint(es->compositor);
1376}
1377
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001378static void
1379touch_move_grab_cancel(struct weston_touch_grab *grab)
1380{
1381 struct weston_touch_move_grab *move =
1382 (struct weston_touch_move_grab *) container_of(
1383 grab, struct shell_touch_grab, grab);
1384
1385 shell_touch_grab_end(&move->base);
1386 free(move);
1387}
1388
Rusty Lynch1084da52013-08-15 09:10:08 -07001389static const struct weston_touch_grab_interface touch_move_grab_interface = {
1390 touch_move_grab_down,
1391 touch_move_grab_up,
1392 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001393 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001394};
1395
1396static int
1397surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1398{
1399 struct weston_touch_move_grab *move;
1400
1401 if (!shsurf)
1402 return -1;
1403
Rafael Antognolli03b16592013-12-03 15:35:42 -02001404 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001405 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001406 if (shsurf->grabbed)
1407 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001408
1409 move = malloc(sizeof *move);
1410 if (!move)
1411 return -1;
1412
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001413 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001415 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001417 seat->touch->grab_y;
1418
1419 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1420 seat->touch);
1421
1422 return 0;
1423}
1424
1425static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001426noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001427{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001428}
1429
1430static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001431move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1432 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001434 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001435 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001436 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001437 int dx, dy;
1438
1439 weston_pointer_move(pointer, x, y);
1440 dx = wl_fixed_to_int(pointer->x + move->dx);
1441 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001442
1443 if (!shsurf)
1444 return;
1445
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001446 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001449}
1450
1451static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001452move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001453 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001454{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001455 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1456 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001457 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001458 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001459
Daniel Stone4dbadb12012-05-30 16:31:51 +01001460 if (pointer->button_count == 0 &&
1461 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001462 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001463 free(grab);
1464 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001465}
1466
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001467static void
1468move_grab_cancel(struct weston_pointer_grab *grab)
1469{
1470 struct shell_grab *shell_grab =
1471 container_of(grab, struct shell_grab, grab);
1472
1473 shell_grab_end(shell_grab);
1474 free(grab);
1475}
1476
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001477static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001478 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001479 move_grab_motion,
1480 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001481 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001482};
1483
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001484static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001485surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001486{
1487 struct weston_move_grab *move;
1488
1489 if (!shsurf)
1490 return -1;
1491
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001492 if (shsurf->grabbed)
1493 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001494 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001495 return 0;
1496
1497 move = malloc(sizeof *move);
1498 if (!move)
1499 return -1;
1500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001502 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001504 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001505
1506 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001507 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001508
1509 return 0;
1510}
1511
1512static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001513common_surface_move(struct wl_resource *resource,
1514 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001515{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001516 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001517 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001518 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001520 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001521 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001522 seat->pointer->button_count > 0 &&
1523 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001525 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001526 (surface_move(shsurf, seat) < 0))
1527 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001528 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001529 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001530 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001532 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001533 (surface_touch_move(shsurf, seat) < 0))
1534 wl_resource_post_no_memory(resource);
1535 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001536}
1537
Rafael Antognollie2a34552013-12-03 15:35:45 -02001538static void
1539shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1540 struct wl_resource *seat_resource, uint32_t serial)
1541{
1542 common_surface_move(resource, seat_resource, serial);
1543}
1544
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001545struct weston_resize_grab {
1546 struct shell_grab base;
1547 uint32_t edges;
1548 int32_t width, height;
1549};
1550
1551static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001552resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1553 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001554{
1555 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001556 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001557 struct shell_surface *shsurf = resize->base.shsurf;
1558 int32_t width, height;
1559 wl_fixed_t from_x, from_y;
1560 wl_fixed_t to_x, to_y;
1561
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001562 weston_pointer_move(pointer, x, y);
1563
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564 if (!shsurf)
1565 return;
1566
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 weston_view_from_global_fixed(shsurf->view,
1568 pointer->grab_x, pointer->grab_y,
1569 &from_x, &from_y);
1570 weston_view_from_global_fixed(shsurf->view,
1571 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572
1573 width = resize->width;
1574 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1575 width += wl_fixed_to_int(from_x - to_x);
1576 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1577 width += wl_fixed_to_int(to_x - from_x);
1578 }
1579
1580 height = resize->height;
1581 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1582 height += wl_fixed_to_int(from_y - to_y);
1583 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1584 height += wl_fixed_to_int(to_y - from_y);
1585 }
1586
1587 shsurf->client->send_configure(shsurf->surface,
1588 resize->edges, width, height);
1589}
1590
1591static void
1592send_configure(struct weston_surface *surface,
1593 uint32_t edges, int32_t width, int32_t height)
1594{
1595 struct shell_surface *shsurf = get_shell_surface(surface);
1596
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001597 assert(shsurf);
1598
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001599 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001600 edges, width, height);
1601}
1602
1603static const struct weston_shell_client shell_client = {
1604 send_configure
1605};
1606
1607static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001608resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001609 uint32_t time, uint32_t button, uint32_t state_w)
1610{
1611 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001612 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613 enum wl_pointer_button_state state = state_w;
1614
1615 if (pointer->button_count == 0 &&
1616 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1617 shell_grab_end(&resize->base);
1618 free(grab);
1619 }
1620}
1621
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001622static void
1623resize_grab_cancel(struct weston_pointer_grab *grab)
1624{
1625 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1626
1627 shell_grab_end(&resize->base);
1628 free(grab);
1629}
1630
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001631static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 noop_grab_focus,
1633 resize_grab_motion,
1634 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001635 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636};
1637
Giulio Camuffob8366642013-04-25 13:57:46 +03001638/*
1639 * Returns the bounding box of a surface and all its sub-surfaces,
1640 * in the surface coordinates system. */
1641static void
1642surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1643 int32_t *y, int32_t *w, int32_t *h) {
1644 pixman_region32_t region;
1645 pixman_box32_t *box;
1646 struct weston_subsurface *subsurface;
1647
1648 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 surface->width,
1650 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001651
1652 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1653 pixman_region32_union_rect(&region, &region,
1654 subsurface->position.x,
1655 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 subsurface->surface->width,
1657 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001658 }
1659
1660 box = pixman_region32_extents(&region);
1661 if (x)
1662 *x = box->x1;
1663 if (y)
1664 *y = box->y1;
1665 if (w)
1666 *w = box->x2 - box->x1;
1667 if (h)
1668 *h = box->y2 - box->y1;
1669
1670 pixman_region32_fini(&region);
1671}
1672
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673static int
1674surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001675 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001676{
1677 struct weston_resize_grab *resize;
1678
Rafael Antognolli03b16592013-12-03 15:35:42 -02001679 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680 return 0;
1681
1682 if (edges == 0 || edges > 15 ||
1683 (edges & 3) == 3 || (edges & 12) == 12)
1684 return 0;
1685
1686 resize = malloc(sizeof *resize);
1687 if (!resize)
1688 return -1;
1689
1690 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001691 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1692 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693
1694 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001695 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696
1697 return 0;
1698}
1699
1700static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001701common_surface_resize(struct wl_resource *resource,
1702 struct wl_resource *seat_resource, uint32_t serial,
1703 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001705 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001706 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001707 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708
Rafael Antognolli03b16592013-12-03 15:35:42 -02001709 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710 return;
1711
Kristian Høgsberge3148752013-05-06 23:19:49 -04001712 if (seat->pointer->button_count == 0 ||
1713 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001714 seat->pointer->focus == NULL)
1715 return;
1716
1717 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1718 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 return;
1720
Kristian Høgsberge3148752013-05-06 23:19:49 -04001721 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001722 wl_resource_post_no_memory(resource);
1723}
1724
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001725static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001726shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1727 struct wl_resource *seat_resource, uint32_t serial,
1728 uint32_t edges)
1729{
1730 common_surface_resize(resource, seat_resource, serial, edges);
1731}
1732
1733static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001734end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1735
1736static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001737busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001738{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001740 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 wl_fixed_t sx, sy;
1743
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 view = weston_compositor_pick_view(pointer->seat->compositor,
1745 pointer->x, pointer->y,
1746 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001747
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001749 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001750}
1751
1752static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001753busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1754 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001756 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001759static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001760busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001761 uint32_t time, uint32_t button, uint32_t state)
1762{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001763 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001764 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001765 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001766
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 if (shsurf && button == BTN_LEFT && state) {
1768 activate(shsurf->shell, shsurf->surface, seat);
1769 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001770 } else if (shsurf && button == BTN_RIGHT && state) {
1771 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001772 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001773 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774}
1775
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001776static void
1777busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1778{
1779 struct shell_grab *grab = (struct shell_grab *) base;
1780
1781 shell_grab_end(grab);
1782 free(grab);
1783}
1784
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001785static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786 busy_cursor_grab_focus,
1787 busy_cursor_grab_motion,
1788 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001789 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790};
1791
1792static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001793set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794{
1795 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796
1797 grab = malloc(sizeof *grab);
1798 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001799 return;
1800
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001801 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1802 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001804
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001806end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001807{
1808 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1809
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001810 if (grab->grab.interface == &busy_cursor_grab_interface &&
1811 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001812 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001814 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001815}
1816
Scott Moreau9521d5e2012-04-19 13:06:17 -06001817static void
1818ping_timer_destroy(struct shell_surface *shsurf)
1819{
1820 if (!shsurf || !shsurf->ping_timer)
1821 return;
1822
1823 if (shsurf->ping_timer->source)
1824 wl_event_source_remove(shsurf->ping_timer->source);
1825
1826 free(shsurf->ping_timer);
1827 shsurf->ping_timer = NULL;
1828}
1829
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001830static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001831ping_timeout_handler(void *data)
1832{
1833 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001835
Scott Moreau9521d5e2012-04-19 13:06:17 -06001836 /* Client is not responding */
1837 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001838
1839 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001840 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001841 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001842 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001843
1844 return 1;
1845}
1846
1847static void
1848ping_handler(struct weston_surface *surface, uint32_t serial)
1849{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001850 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001851 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001852 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001853
1854 if (!shsurf)
1855 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001856 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001857 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001858
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001859 if (shsurf->surface == shsurf->shell->grab_surface)
1860 return;
1861
Scott Moreauff1db4a2012-04-17 19:06:18 -06001862 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001863 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001864 if (!shsurf->ping_timer)
1865 return;
1866
1867 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001868 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1869 shsurf->ping_timer->source =
1870 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1871 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1872
Rafael Antognollie2a34552013-12-03 15:35:45 -02001873 if (shell_surface_is_wl_shell_surface(shsurf))
1874 wl_shell_surface_send_ping(shsurf->resource, serial);
1875 else if (shell_surface_is_xdg_surface(shsurf))
1876 xdg_surface_send_ping(shsurf->resource, serial);
1877 else if (shell_surface_is_xdg_popup(shsurf))
1878 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001879 }
1880}
1881
1882static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883handle_pointer_focus(struct wl_listener *listener, void *data)
1884{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001885 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001887 struct weston_compositor *compositor;
1888 struct shell_surface *shsurf;
1889 uint32_t serial;
1890
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001892 return;
1893
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894 compositor = view->surface->compositor;
1895 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001896
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001897 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001898 set_busy_cursor(shsurf, pointer);
1899 } else {
1900 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001902 }
1903}
1904
1905static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001906create_pointer_focus_listener(struct weston_seat *seat)
1907{
1908 struct wl_listener *listener;
1909
Kristian Høgsberge3148752013-05-06 23:19:49 -04001910 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001911 return;
1912
1913 listener = malloc(sizeof *listener);
1914 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001915 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001916}
1917
1918static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001919shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1920{
1921 if (--shsurf->focus_count == 0)
1922 if (shell_surface_is_xdg_surface(shsurf))
1923 xdg_surface_send_focused_unset(shsurf->resource);
1924}
1925
1926static void
1927shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1928{
1929 if (shsurf->focus_count++ == 0)
1930 if (shell_surface_is_xdg_surface(shsurf))
1931 xdg_surface_send_focused_set(shsurf->resource);
1932}
1933
1934static void
1935handle_keyboard_focus(struct wl_listener *listener, void *data)
1936{
1937 struct weston_keyboard *keyboard = data;
1938 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1939
1940 if (seat->focused_surface) {
1941 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1942 if (shsurf)
1943 shell_surface_lose_keyboard_focus(shsurf);
1944 }
1945
1946 seat->focused_surface = keyboard->focus;
1947
1948 if (seat->focused_surface) {
1949 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1950 if (shsurf)
1951 shell_surface_gain_keyboard_focus(shsurf);
1952 }
1953}
1954
1955static void
1956create_keyboard_focus_listener(struct weston_seat *seat)
1957{
1958 struct wl_listener *listener;
1959
1960 if (!seat->keyboard)
1961 return;
1962
1963 listener = malloc(sizeof *listener);
1964 listener->notify = handle_keyboard_focus;
1965 wl_signal_add(&seat->keyboard->focus_signal, listener);
1966}
1967
1968static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001969xdg_surface_set_transient_for(struct wl_client *client,
1970 struct wl_resource *resource,
1971 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001972{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001973 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001974 struct weston_surface *parent;
1975
1976 if (parent_resource)
1977 parent = wl_resource_get_user_data(parent_resource);
1978 else
1979 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001980
1981 shell_surface_set_parent(shsurf, parent);
1982}
1983
1984static void
1985surface_pong(struct shell_surface *shsurf, uint32_t serial)
1986{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001988 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001989
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001990 if (shsurf->ping_timer == NULL)
1991 /* Just ignore unsolicited pong. */
1992 return;
1993
Scott Moreauff1db4a2012-04-17 19:06:18 -06001994 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001995 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001996 wl_list_for_each(seat, &ec->seat_list, link) {
1997 if(seat->pointer)
1998 end_busy_cursor(shsurf, seat->pointer);
1999 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06002000 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002001 }
2002}
2003
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002004static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002005shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2006 uint32_t serial)
2007{
2008 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2009
2010 surface_pong(shsurf, serial);
2011
2012}
2013
2014static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002015set_title(struct shell_surface *shsurf, const char *title)
2016{
2017 free(shsurf->title);
2018 shsurf->title = strdup(title);
2019}
2020
2021static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002022shell_surface_set_title(struct wl_client *client,
2023 struct wl_resource *resource, const char *title)
2024{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002025 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002026
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002027 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002028}
2029
2030static void
2031shell_surface_set_class(struct wl_client *client,
2032 struct wl_resource *resource, const char *class)
2033{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002034 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002035
2036 free(shsurf->class);
2037 shsurf->class = strdup(class);
2038}
2039
Alex Wu4539b082012-03-01 12:57:46 +08002040static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002041restore_output_mode(struct weston_output *output)
2042{
Hardening57388e42013-09-18 23:56:36 +02002043 if (output->current_mode != output->original_mode ||
2044 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002045 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002046 output->original_mode,
2047 output->original_scale,
2048 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002049}
2050
2051static void
2052restore_all_output_modes(struct weston_compositor *compositor)
2053{
2054 struct weston_output *output;
2055
2056 wl_list_for_each(output, &compositor->output_list, link)
2057 restore_output_mode(output);
2058}
2059
Philip Withnallbecb77e2013-11-25 18:01:30 +00002060static int
2061get_output_panel_height(struct desktop_shell *shell,
2062 struct weston_output *output)
2063{
2064 struct weston_view *view;
2065 int panel_height = 0;
2066
2067 if (!output)
2068 return 0;
2069
2070 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2071 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002072 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002073 break;
2074 }
2075 }
2076
2077 return panel_height;
2078}
2079
Philip Withnall07926d92013-11-25 18:01:40 +00002080/* The surface will be inserted into the list immediately after the link
2081 * returned by this function (i.e. will be stacked immediately above the
2082 * returned link). */
2083static struct wl_list *
2084shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2085{
2086 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002087 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002088
2089 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002090 case SHELL_SURFACE_POPUP:
2091 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002092 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002093 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002094 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002095 /* Move the surface to its parent layer so
2096 * that surfaces which are transient for
2097 * fullscreen surfaces don't get hidden by the
2098 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002099
2100 /* TODO: Handle a parent with multiple views */
2101 parent = get_default_view(shsurf->parent);
2102 if (parent)
2103 return parent->layer_link.prev;
2104 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002105 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002106
2107 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002108 return &shsurf->shell->fullscreen_layer.view_list;
2109
Philip Withnall07926d92013-11-25 18:01:40 +00002110 case SHELL_SURFACE_NONE:
2111 default:
2112 /* Go to the fallback, below. */
2113 break;
2114 }
2115
2116 /* Move the surface to a normal workspace layer so that surfaces
2117 * which were previously fullscreen or transient are no longer
2118 * rendered on top. */
2119 ws = get_current_workspace(shsurf->shell);
2120 return &ws->layer.view_list;
2121}
2122
Philip Withnall648a4dd2013-11-25 18:01:44 +00002123static void
2124shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2125{
2126 struct shell_surface *child;
2127
2128 /* Move the child layers to the same workspace as shsurf. They will be
2129 * stacked above shsurf. */
2130 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2131 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2132 weston_view_geometry_dirty(child->view);
2133 wl_list_remove(&child->view->layer_link);
2134 wl_list_insert(shsurf->view->layer_link.prev,
2135 &child->view->layer_link);
2136 weston_view_geometry_dirty(child->view);
2137 weston_surface_damage(child->surface);
2138
2139 /* Recurse. We don’t expect this to recurse very far (if
2140 * at all) because that would imply we have transient
2141 * (or popup) children of transient surfaces, which
2142 * would be unusual. */
2143 shell_surface_update_child_surface_layers(child);
2144 }
2145 }
2146}
2147
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002148/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002149 * geometry to ensure the changes are redrawn.
2150 *
2151 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2152 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002153static void
2154shell_surface_update_layer(struct shell_surface *shsurf)
2155{
2156 struct wl_list *new_layer_link;
2157
2158 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2159
2160 if (new_layer_link == &shsurf->view->layer_link)
2161 return;
2162
2163 weston_view_geometry_dirty(shsurf->view);
2164 wl_list_remove(&shsurf->view->layer_link);
2165 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2166 weston_view_geometry_dirty(shsurf->view);
2167 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002168
2169 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002170}
2171
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002172static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002173shell_surface_set_parent(struct shell_surface *shsurf,
2174 struct weston_surface *parent)
2175{
2176 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002177
2178 wl_list_remove(&shsurf->children_link);
2179 wl_list_init(&shsurf->children_link);
2180
2181 /* Insert into the parent surface’s child list. */
2182 if (parent != NULL) {
2183 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2184 if (parent_shsurf != NULL)
2185 wl_list_insert(&parent_shsurf->children_list,
2186 &shsurf->children_link);
2187 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002188}
2189
2190static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002191shell_surface_set_output(struct shell_surface *shsurf,
2192 struct weston_output *output)
2193{
2194 struct weston_surface *es = shsurf->surface;
2195
2196 /* get the default output, if the client set it as NULL
2197 check whether the ouput is available */
2198 if (output)
2199 shsurf->output = output;
2200 else if (es->output)
2201 shsurf->output = es->output;
2202 else
2203 shsurf->output = get_default_output(es->compositor);
2204}
2205
2206static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002207surface_clear_next_states(struct shell_surface *shsurf)
2208{
2209 shsurf->next_state.maximized = false;
2210 shsurf->next_state.fullscreen = false;
2211
2212 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2213 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2214 shsurf->state_changed = true;
2215}
2216
2217static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002218set_toplevel(struct shell_surface *shsurf)
2219{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002220 shell_surface_set_parent(shsurf, NULL);
2221 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002222 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002223
2224 /* The layer_link is updated in set_surface_type(),
2225 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002226}
2227
2228static void
2229shell_surface_set_toplevel(struct wl_client *client,
2230 struct wl_resource *resource)
2231{
2232 struct shell_surface *surface = wl_resource_get_user_data(resource);
2233
2234 set_toplevel(surface);
2235}
2236
2237static void
2238set_transient(struct shell_surface *shsurf,
2239 struct weston_surface *parent, int x, int y, uint32_t flags)
2240{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002241 assert(parent != NULL);
2242
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002243 shell_surface_set_parent(shsurf, parent);
2244
2245 surface_clear_next_states(shsurf);
2246
Philip Withnallbecb77e2013-11-25 18:01:30 +00002247 shsurf->transient.x = x;
2248 shsurf->transient.y = y;
2249 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002250
Rafael Antognollied207b42013-12-03 15:35:43 -02002251 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002252 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002253 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002254
2255 /* The layer_link is updated in set_surface_type(),
2256 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257}
2258
2259static void
2260shell_surface_set_transient(struct wl_client *client,
2261 struct wl_resource *resource,
2262 struct wl_resource *parent_resource,
2263 int x, int y, uint32_t flags)
2264{
2265 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2266 struct weston_surface *parent =
2267 wl_resource_get_user_data(parent_resource);
2268
2269 set_transient(shsurf, parent, x, y, flags);
2270}
2271
2272static void
2273set_fullscreen(struct shell_surface *shsurf,
2274 uint32_t method,
2275 uint32_t framerate,
2276 struct weston_output *output)
2277{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002278 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002279
2280 shsurf->fullscreen_output = shsurf->output;
2281 shsurf->fullscreen.type = method;
2282 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002283
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002284 shsurf->next_state.fullscreen = true;
2285 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002286 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287
2288 shsurf->client->send_configure(shsurf->surface, 0,
2289 shsurf->output->width,
2290 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002291
2292 /* The layer_link is updated in set_surface_type(),
2293 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002294}
2295
2296static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002297weston_view_set_initial_position(struct weston_view *view,
2298 struct desktop_shell *shell);
2299
2300static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002301unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002302{
Philip Withnallf85fe842013-11-25 18:01:37 +00002303 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002304 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2305 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002306 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002307 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002308 shsurf->fullscreen_output = NULL;
2309
Alex Wu4539b082012-03-01 12:57:46 +08002310 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2311 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002312
Alex Wu4539b082012-03-01 12:57:46 +08002313 wl_list_remove(&shsurf->fullscreen.transform.link);
2314 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002315
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 if (shsurf->fullscreen.black_view)
2317 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2318 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002319
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002320 if (shsurf->saved_position_valid)
2321 weston_view_set_position(shsurf->view,
2322 shsurf->saved_x, shsurf->saved_y);
2323 else
2324 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2325
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002326 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002328 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002329 shsurf->saved_rotation_valid = false;
2330 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002331
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002332 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002333}
2334
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002335static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336shell_surface_set_fullscreen(struct wl_client *client,
2337 struct wl_resource *resource,
2338 uint32_t method,
2339 uint32_t framerate,
2340 struct wl_resource *output_resource)
2341{
2342 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2343 struct weston_output *output;
2344
2345 if (output_resource)
2346 output = wl_resource_get_user_data(output_resource);
2347 else
2348 output = NULL;
2349
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002350 shell_surface_set_parent(shsurf, NULL);
2351
Rafael Antognolli03b16592013-12-03 15:35:42 -02002352 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 set_fullscreen(shsurf, method, framerate, output);
2354}
2355
2356static void
2357set_popup(struct shell_surface *shsurf,
2358 struct weston_surface *parent,
2359 struct weston_seat *seat,
2360 uint32_t serial,
2361 int32_t x,
2362 int32_t y)
2363{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002364 assert(parent != NULL);
2365
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366 shsurf->popup.shseat = get_shell_seat(seat);
2367 shsurf->popup.serial = serial;
2368 shsurf->popup.x = x;
2369 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002370
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002371 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372}
2373
2374static void
2375shell_surface_set_popup(struct wl_client *client,
2376 struct wl_resource *resource,
2377 struct wl_resource *seat_resource,
2378 uint32_t serial,
2379 struct wl_resource *parent_resource,
2380 int32_t x, int32_t y, uint32_t flags)
2381{
2382 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002383 struct weston_surface *parent =
2384 wl_resource_get_user_data(parent_resource);
2385
2386 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002387
Rafael Antognolli03b16592013-12-03 15:35:42 -02002388 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002389 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002390 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002391 wl_resource_get_user_data(seat_resource),
2392 serial, x, y);
2393}
2394
2395static void
2396set_maximized(struct shell_surface *shsurf,
2397 struct weston_output *output)
2398{
2399 struct desktop_shell *shell;
2400 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401
Philip Withnall352e7ed2013-11-25 18:01:35 +00002402 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002403
2404 shell = shell_surface_get_shell(shsurf);
2405 panel_height = get_output_panel_height(shell, shsurf->output);
2406 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2407
2408 shsurf->client->send_configure(shsurf->surface, edges,
2409 shsurf->output->width,
2410 shsurf->output->height - panel_height);
2411
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002412 shsurf->next_state.maximized = true;
2413 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002414 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002415}
2416
2417static void
2418unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002419{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002420 /* undo all maximized things here */
2421 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002422
2423 if (shsurf->saved_position_valid)
2424 weston_view_set_position(shsurf->view,
2425 shsurf->saved_x, shsurf->saved_y);
2426 else
2427 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002428
2429 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430 wl_list_insert(&shsurf->view->geometry.transformation_list,
2431 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002432 shsurf->saved_rotation_valid = false;
2433 }
2434
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002435 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002436}
2437
Philip Withnallbecb77e2013-11-25 18:01:30 +00002438static void
2439shell_surface_set_maximized(struct wl_client *client,
2440 struct wl_resource *resource,
2441 struct wl_resource *output_resource)
2442{
2443 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2444 struct weston_output *output;
2445
2446 if (output_resource)
2447 output = wl_resource_get_user_data(output_resource);
2448 else
2449 output = NULL;
2450
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002451 shell_surface_set_parent(shsurf, NULL);
2452
Rafael Antognolli03b16592013-12-03 15:35:42 -02002453 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454 set_maximized(shsurf, output);
2455}
2456
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002457/* This is only ever called from set_surface_type(), so there’s no need to
2458 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002459static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002461{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002462 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002464 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002465 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002466
Pekka Paalanen98262232011-12-01 10:42:22 +02002467 return 0;
2468}
2469
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002470static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002471set_full_output(struct shell_surface *shsurf)
2472{
2473 shsurf->saved_x = shsurf->view->geometry.x;
2474 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002475 shsurf->saved_width = shsurf->surface->width;
2476 shsurf->saved_height = shsurf->surface->height;
2477 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002478 shsurf->saved_position_valid = true;
2479
2480 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2481 wl_list_remove(&shsurf->rotation.transform.link);
2482 wl_list_init(&shsurf->rotation.transform.link);
2483 weston_view_geometry_dirty(shsurf->view);
2484 shsurf->saved_rotation_valid = true;
2485 }
2486}
2487
2488static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002489set_surface_type(struct shell_surface *shsurf)
2490{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002491 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002492 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002493
Philip Withnallbecb77e2013-11-25 18:01:30 +00002494 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002495
Rafael Antognolli03b16592013-12-03 15:35:42 -02002496 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002497 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002498
2499 switch (shsurf->type) {
2500 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002501 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002502 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002503 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 weston_view_set_position(shsurf->view,
2505 pev->geometry.x + shsurf->transient.x,
2506 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002507 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002508 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002509
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002510 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511 weston_view_set_position(shsurf->view, shsurf->transient.x,
2512 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002513 break;
2514
Philip Withnall0f640e22013-11-25 18:01:31 +00002515 case SHELL_SURFACE_POPUP:
2516 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002517 default:
2518 break;
2519 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002520
2521 /* Update the surface’s layer. */
2522 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002523}
2524
Tiago Vignattibe143262012-04-16 17:31:41 +03002525static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002526shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002527{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002528 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002529}
2530
Alex Wu21858432012-04-01 20:13:08 +08002531static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002532black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002533
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002535create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002536 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002537 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002538{
2539 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002541
2542 surface = weston_surface_create(ec);
2543 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002544 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002545 return NULL;
2546 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 view = weston_view_create(surface);
2548 if (surface == NULL) {
2549 weston_log("no memory\n");
2550 weston_surface_destroy(surface);
2551 return NULL;
2552 }
Alex Wu4539b082012-03-01 12:57:46 +08002553
Alex Wu21858432012-04-01 20:13:08 +08002554 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002555 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002556 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002557 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002558 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002559 pixman_region32_fini(&surface->input);
2560 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002561
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002562 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002563 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002564
2565 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002566}
2567
Philip Withnalled8f1a92013-11-25 18:01:43 +00002568static void
2569shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2570{
2571 struct weston_output *output = shsurf->fullscreen_output;
2572
Rafael Antognolli03b16592013-12-03 15:35:42 -02002573 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002574
2575 if (!shsurf->fullscreen.black_view)
2576 shsurf->fullscreen.black_view =
2577 create_black_surface(shsurf->surface->compositor,
2578 shsurf->surface,
2579 output->x, output->y,
2580 output->width,
2581 output->height);
2582
2583 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2584 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2585 wl_list_insert(&shsurf->view->layer_link,
2586 &shsurf->fullscreen.black_view->layer_link);
2587 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2588 weston_surface_damage(shsurf->surface);
2589}
2590
Alex Wu4539b082012-03-01 12:57:46 +08002591/* Create black surface and append it to the associated fullscreen surface.
2592 * Handle size dismatch and positioning according to the method. */
2593static void
2594shell_configure_fullscreen(struct shell_surface *shsurf)
2595{
2596 struct weston_output *output = shsurf->fullscreen_output;
2597 struct weston_surface *surface = shsurf->surface;
2598 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002599 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002600 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002601
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002602 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2603 restore_output_mode(output);
2604
Philip Withnalled8f1a92013-11-25 18:01:43 +00002605 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002606
Jason Ekstranda7af7042013-10-12 22:38:11 -05002607 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002608 &surf_width, &surf_height);
2609
Alex Wu4539b082012-03-01 12:57:46 +08002610 switch (shsurf->fullscreen.type) {
2611 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002612 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002614 break;
2615 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002616 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002617 if (output->width == surf_width &&
2618 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 weston_view_set_position(shsurf->view,
2620 output->x - surf_x,
2621 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002622 break;
2623 }
2624
Alex Wu4539b082012-03-01 12:57:46 +08002625 matrix = &shsurf->fullscreen.transform.matrix;
2626 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002627
Scott Moreau1bad5db2012-08-18 01:04:05 -06002628 output_aspect = (float) output->width /
2629 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630 /* XXX: Use surf_width and surf_height here? */
2631 surface_aspect = (float) surface->width /
2632 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002633 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002634 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002635 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002636 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002637 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002638 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002639
Alex Wu4539b082012-03-01 12:57:46 +08002640 weston_matrix_scale(matrix, scale, scale, 1);
2641 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002643 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002644 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2645 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002647
Alex Wu4539b082012-03-01 12:57:46 +08002648 break;
2649 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002650 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002651 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002652 surf_width * surface->buffer_viewport.scale,
2653 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002654 shsurf->fullscreen.framerate};
2655
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002656 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002657 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658 weston_view_set_position(shsurf->view,
2659 output->x - surf_x,
2660 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002661 shsurf->fullscreen.black_view->surface->width = output->width;
2662 shsurf->fullscreen.black_view->surface->height = output->height;
2663 weston_view_set_position(shsurf->fullscreen.black_view,
2664 output->x - surf_x,
2665 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002666 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002667 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002668 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002670 }
Alex Wubd3354b2012-04-17 17:20:49 +08002671 }
Alex Wu4539b082012-03-01 12:57:46 +08002672 break;
2673 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002675 break;
2676 default:
2677 break;
2678 }
2679}
2680
Alex Wu4539b082012-03-01 12:57:46 +08002681static void
2682shell_map_fullscreen(struct shell_surface *shsurf)
2683{
Alex Wubd3354b2012-04-17 17:20:49 +08002684 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002685}
2686
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002687static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002688set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2689{
2690 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002691 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002692 shsurf->transient.x = x;
2693 shsurf->transient.y = y;
2694 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002695
2696 shell_surface_set_parent(shsurf, NULL);
2697
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002698 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002699 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002700}
2701
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002702static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002703
2704static void
2705destroy_shell_seat(struct wl_listener *listener, void *data)
2706{
2707 struct shell_seat *shseat =
2708 container_of(listener,
2709 struct shell_seat, seat_destroy_listener);
2710 struct shell_surface *shsurf, *prev = NULL;
2711
2712 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002713 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002714 shseat->popup_grab.client = NULL;
2715
2716 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2717 shsurf->popup.shseat = NULL;
2718 if (prev) {
2719 wl_list_init(&prev->popup.grab_link);
2720 }
2721 prev = shsurf;
2722 }
2723 wl_list_init(&prev->popup.grab_link);
2724 }
2725
2726 wl_list_remove(&shseat->seat_destroy_listener.link);
2727 free(shseat);
2728}
2729
2730static struct shell_seat *
2731create_shell_seat(struct weston_seat *seat)
2732{
2733 struct shell_seat *shseat;
2734
2735 shseat = calloc(1, sizeof *shseat);
2736 if (!shseat) {
2737 weston_log("no memory to allocate shell seat\n");
2738 return NULL;
2739 }
2740
2741 shseat->seat = seat;
2742 wl_list_init(&shseat->popup_grab.surfaces_list);
2743
2744 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2745 wl_signal_add(&seat->destroy_signal,
2746 &shseat->seat_destroy_listener);
2747
2748 return shseat;
2749}
2750
2751static struct shell_seat *
2752get_shell_seat(struct weston_seat *seat)
2753{
2754 struct wl_listener *listener;
2755
2756 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2757 if (listener == NULL)
2758 return create_shell_seat(seat);
2759
2760 return container_of(listener,
2761 struct shell_seat, seat_destroy_listener);
2762}
2763
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002764static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002765popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002766{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002767 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002769 struct shell_seat *shseat =
2770 container_of(grab, struct shell_seat, popup_grab.grab);
2771 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002772 wl_fixed_t sx, sy;
2773
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774 view = weston_compositor_pick_view(pointer->seat->compositor,
2775 pointer->x, pointer->y,
2776 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002777
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778 if (view && view->surface->resource &&
2779 wl_resource_get_client(view->surface->resource) == client) {
2780 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002781 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002782 weston_pointer_set_focus(pointer, NULL,
2783 wl_fixed_from_int(0),
2784 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002785 }
2786}
2787
2788static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002789popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2790 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002791{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002792 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002793 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002794 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002795
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002796 weston_pointer_move(pointer, x, y);
2797
Neil Roberts96d790e2013-09-19 17:32:00 +01002798 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 weston_view_from_global_fixed(pointer->focus,
2800 pointer->x, pointer->y,
2801 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002802 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002803 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002804}
2805
2806static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002807popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002808 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002809{
2810 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002811 struct shell_seat *shseat =
2812 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002813 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002814 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002815 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002816 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002817
Neil Roberts96d790e2013-09-19 17:32:00 +01002818 resource_list = &grab->pointer->focus_resource_list;
2819 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002820 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002821 wl_resource_for_each(resource, resource_list) {
2822 wl_pointer_send_button(resource, serial,
2823 time, button, state);
2824 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002825 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002826 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002827 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002828 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002829 }
2830
Daniel Stone4dbadb12012-05-30 16:31:51 +01002831 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002832 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002833}
2834
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002835static void
2836popup_grab_cancel(struct weston_pointer_grab *grab)
2837{
2838 popup_grab_end(grab->pointer);
2839}
2840
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002841static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002842 popup_grab_focus,
2843 popup_grab_motion,
2844 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002845 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846};
2847
2848static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002849shell_surface_send_popup_done(struct shell_surface *shsurf)
2850{
2851 if (shell_surface_is_wl_shell_surface(shsurf))
2852 wl_shell_surface_send_popup_done(shsurf->resource);
2853 else if (shell_surface_is_xdg_popup(shsurf))
2854 xdg_popup_send_popup_done(shsurf->resource,
2855 shsurf->popup.serial);
2856}
2857
2858static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002859popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002860{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002861 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002862 struct shell_seat *shseat =
2863 container_of(grab, struct shell_seat, popup_grab.grab);
2864 struct shell_surface *shsurf;
2865 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002866
2867 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002868 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002869 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002870 shseat->popup_grab.grab.interface = NULL;
2871 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002872 /* Send the popup_done event to all the popups open */
2873 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002874 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002875 shsurf->popup.shseat = NULL;
2876 if (prev) {
2877 wl_list_init(&prev->popup.grab_link);
2878 }
2879 prev = shsurf;
2880 }
2881 wl_list_init(&prev->popup.grab_link);
2882 wl_list_init(&shseat->popup_grab.surfaces_list);
2883 }
2884}
2885
2886static void
2887add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2888{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002889 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002890
2891 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002892 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002893 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002894 /* We must make sure here that this popup was opened after
2895 * a mouse press, and not just by moving around with other
2896 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002897 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002898 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899
Rob Bradforddfe31052013-06-26 19:49:11 +01002900 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002901 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002902 } else {
2903 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002904 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002905}
2906
2907static void
2908remove_popup_grab(struct shell_surface *shsurf)
2909{
2910 struct shell_seat *shseat = shsurf->popup.shseat;
2911
2912 wl_list_remove(&shsurf->popup.grab_link);
2913 wl_list_init(&shsurf->popup.grab_link);
2914 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002915 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002916 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002917 }
2918}
2919
2920static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002921shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002923 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002924 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926 shsurf->surface->output = parent_view->output;
2927 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002928
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 weston_view_set_transform_parent(shsurf->view, parent_view);
2930 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2931 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002932
Kristian Høgsberge3148752013-05-06 23:19:49 -04002933 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002935 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002936 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002937 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002938 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002939}
2940
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002941static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002942 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002943 shell_surface_move,
2944 shell_surface_resize,
2945 shell_surface_set_toplevel,
2946 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002947 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002948 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002949 shell_surface_set_maximized,
2950 shell_surface_set_title,
2951 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002952};
2953
2954static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002955destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002956{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002957 struct shell_surface *child, *next;
2958
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002959 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2960
Giulio Camuffo5085a752013-03-25 21:42:45 +01002961 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2962 remove_popup_grab(shsurf);
2963 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964
Alex Wubd3354b2012-04-17 17:20:49 +08002965 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002966 shell_surface_is_top_fullscreen(shsurf))
2967 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002968
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 if (shsurf->fullscreen.black_view)
2970 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002971
Alex Wubd3354b2012-04-17 17:20:49 +08002972 /* As destroy_resource() use wl_list_for_each_safe(),
2973 * we can always remove the listener.
2974 */
2975 wl_list_remove(&shsurf->surface_destroy_listener.link);
2976 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002977 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002978 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002979
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 weston_view_destroy(shsurf->view);
2981
Philip Withnall648a4dd2013-11-25 18:01:44 +00002982 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01002983 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
2984 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002985
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002986 wl_list_remove(&shsurf->link);
2987 free(shsurf);
2988}
2989
2990static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002991shell_destroy_shell_surface(struct wl_resource *resource)
2992{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002993 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002994
2995 destroy_shell_surface(shsurf);
2996}
2997
2998static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002999shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003000{
3001 struct shell_surface *shsurf = container_of(listener,
3002 struct shell_surface,
3003 surface_destroy_listener);
3004
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003005 if (shsurf->resource)
3006 wl_resource_destroy(shsurf->resource);
3007 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003008 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003009}
3010
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003011static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003012shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003013
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003014struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003015get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003016{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003017 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003018 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003019 else
3020 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003021}
3022
Rafael Antognollie2a34552013-12-03 15:35:45 -02003023static struct shell_surface *
3024create_common_surface(void *shell, struct weston_surface *surface,
3025 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003026{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003027 struct shell_surface *shsurf;
3028
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003029 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003030 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003031 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003032 }
3033
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003034 shsurf = calloc(1, sizeof *shsurf);
3035 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003036 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003037 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003038 }
3039
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 shsurf->view = weston_view_create(surface);
3041 if (!shsurf->view) {
3042 weston_log("no memory to allocate shell surface\n");
3043 free(shsurf);
3044 return NULL;
3045 }
3046
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003047 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003048 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003049
Tiago Vignattibc052c92012-04-19 16:18:18 +03003050 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003051 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003052 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003053 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003054 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003055 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003056 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3057 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003058 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003059 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003060 wl_list_init(&shsurf->fullscreen.transform.link);
3061
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003062 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003063 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003064 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003065 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003066
3067 /* init link so its safe to always remove it in destroy_shell_surface */
3068 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003070
Pekka Paalanen460099f2012-01-20 16:48:25 +02003071 /* empty when not in use */
3072 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003073 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003074
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003075 wl_list_init(&shsurf->workspace_transform.link);
3076
Philip Withnall648a4dd2013-11-25 18:01:44 +00003077 wl_list_init(&shsurf->children_link);
3078 wl_list_init(&shsurf->children_list);
3079 shsurf->parent = NULL;
3080
Pekka Paalanen98262232011-12-01 10:42:22 +02003081 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003082
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003083 shsurf->client = client;
3084
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003085 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003086}
3087
Rafael Antognollie2a34552013-12-03 15:35:45 -02003088static struct shell_surface *
3089create_shell_surface(void *shell, struct weston_surface *surface,
3090 const struct weston_shell_client *client)
3091{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003092 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003093}
3094
Jason Ekstranda7af7042013-10-12 22:38:11 -05003095static struct weston_view *
3096get_primary_view(void *shell, struct shell_surface *shsurf)
3097{
3098 return shsurf->view;
3099}
3100
Tiago Vignattibc052c92012-04-19 16:18:18 +03003101static void
3102shell_get_shell_surface(struct wl_client *client,
3103 struct wl_resource *resource,
3104 uint32_t id,
3105 struct wl_resource *surface_resource)
3106{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003107 struct weston_surface *surface =
3108 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003109 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003110 struct shell_surface *shsurf;
3111
3112 if (get_shell_surface(surface)) {
3113 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003114 WL_DISPLAY_ERROR_INVALID_OBJECT,
3115 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003116 return;
3117 }
3118
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003119 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003120 if (!shsurf) {
3121 wl_resource_post_error(surface_resource,
3122 WL_DISPLAY_ERROR_INVALID_OBJECT,
3123 "surface->configure already set");
3124 return;
3125 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003126
Jason Ekstranda85118c2013-06-27 20:17:02 -05003127 shsurf->resource =
3128 wl_resource_create(client,
3129 &wl_shell_surface_interface, 1, id);
3130 wl_resource_set_implementation(shsurf->resource,
3131 &shell_surface_implementation,
3132 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003133}
3134
Rafael Antognollie2a34552013-12-03 15:35:45 -02003135static bool
3136shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3137{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003138 /* A shell surface without a resource is created from xwayland
3139 * and is considered a wl_shell surface for now. */
3140
3141 return shsurf->resource == NULL ||
3142 wl_resource_instance_of(shsurf->resource,
3143 &wl_shell_surface_interface,
3144 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003145}
3146
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003147static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003148 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003149};
3150
Rafael Antognollie2a34552013-12-03 15:35:45 -02003151/****************************
3152 * xdg-shell implementation */
3153
3154static void
3155xdg_surface_destroy(struct wl_client *client,
3156 struct wl_resource *resource)
3157{
3158 wl_resource_destroy(resource);
3159}
3160
3161static void
3162xdg_surface_pong(struct wl_client *client,
3163 struct wl_resource *resource,
3164 uint32_t serial)
3165{
3166 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3167
3168 surface_pong(shsurf, serial);
3169}
3170
3171static void
3172xdg_surface_set_app_id(struct wl_client *client,
3173 struct wl_resource *resource,
3174 const char *app_id)
3175{
3176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3177
3178 free(shsurf->class);
3179 shsurf->class = strdup(app_id);
3180}
3181
3182static void
3183xdg_surface_set_title(struct wl_client *client,
3184 struct wl_resource *resource, const char *title)
3185{
3186 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3187
3188 set_title(shsurf, title);
3189}
3190
3191static void
3192xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3193 struct wl_resource *seat_resource, uint32_t serial)
3194{
3195 common_surface_move(resource, seat_resource, serial);
3196}
3197
3198static void
3199xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3200 struct wl_resource *seat_resource, uint32_t serial,
3201 uint32_t edges)
3202{
3203 common_surface_resize(resource, seat_resource, serial, edges);
3204}
3205
3206static void
3207xdg_surface_set_output(struct wl_client *client,
3208 struct wl_resource *resource,
3209 struct wl_resource *output_resource)
3210{
3211 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3212 struct weston_output *output;
3213
3214 if (output_resource)
3215 output = wl_resource_get_user_data(output_resource);
3216 else
3217 output = NULL;
3218
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003219 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003220}
3221
3222static void
3223xdg_surface_set_fullscreen(struct wl_client *client,
3224 struct wl_resource *resource)
3225{
3226 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3227
3228 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3229 return;
3230
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003231 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003232 set_fullscreen(shsurf,
3233 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003234 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003235}
3236
3237static void
3238xdg_surface_unset_fullscreen(struct wl_client *client,
3239 struct wl_resource *resource)
3240{
3241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003242 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003243
3244 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3245 return;
3246
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003247 if (!shsurf->next_state.fullscreen)
3248 return;
3249
Rafael Antognollie2a34552013-12-03 15:35:45 -02003250 shsurf->next_state.fullscreen = false;
3251 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003252
3253 if (shsurf->saved_size_valid) {
3254 width = shsurf->saved_width;
3255 height = shsurf->saved_height;
3256 shsurf->saved_size_valid = false;
3257 } else {
3258 width = shsurf->surface->width;
3259 height = shsurf->surface->height;
3260 }
3261
3262 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003263}
3264
3265static void
3266xdg_surface_set_maximized(struct wl_client *client,
3267 struct wl_resource *resource)
3268{
3269 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3270
3271 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3272 return;
3273
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003274 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003275 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003276}
3277
3278static void
3279xdg_surface_unset_maximized(struct wl_client *client,
3280 struct wl_resource *resource)
3281{
3282 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003283 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003284
3285 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3286 return;
3287
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003288 if (!shsurf->next_state.maximized)
3289 return;
3290
Rafael Antognollie2a34552013-12-03 15:35:45 -02003291 shsurf->next_state.maximized = false;
3292 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003293
3294 if (shsurf->saved_size_valid) {
3295 width = shsurf->saved_width;
3296 height = shsurf->saved_height;
3297 shsurf->saved_size_valid = false;
3298 } else {
3299 width = shsurf->surface->width;
3300 height = shsurf->surface->height;
3301 }
3302
3303 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003304}
3305
3306static const struct xdg_surface_interface xdg_surface_implementation = {
3307 xdg_surface_destroy,
3308 xdg_surface_set_transient_for,
3309 xdg_surface_set_title,
3310 xdg_surface_set_app_id,
3311 xdg_surface_pong,
3312 xdg_surface_move,
3313 xdg_surface_resize,
3314 xdg_surface_set_output,
3315 xdg_surface_set_fullscreen,
3316 xdg_surface_unset_fullscreen,
3317 xdg_surface_set_maximized,
3318 xdg_surface_unset_maximized,
3319 NULL /* set_minimized */
3320};
3321
3322static void
3323xdg_send_configure(struct weston_surface *surface,
3324 uint32_t edges, int32_t width, int32_t height)
3325{
3326 struct shell_surface *shsurf = get_shell_surface(surface);
3327
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003328 assert(shsurf);
3329
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3331}
3332
3333static const struct weston_shell_client xdg_client = {
3334 xdg_send_configure
3335};
3336
3337static void
3338xdg_use_unstable_version(struct wl_client *client,
3339 struct wl_resource *resource,
3340 int32_t version)
3341{
3342 if (version > 1) {
3343 wl_resource_post_error(resource,
3344 1,
3345 "xdg-shell:: version not implemented yet.");
3346 return;
3347 }
3348}
3349
3350static struct shell_surface *
3351create_xdg_surface(void *shell, struct weston_surface *surface,
3352 const struct weston_shell_client *client)
3353{
3354 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003355
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003356 shsurf = create_common_surface(shell, surface, client);
3357 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358
3359 return shsurf;
3360}
3361
3362static void
3363xdg_get_xdg_surface(struct wl_client *client,
3364 struct wl_resource *resource,
3365 uint32_t id,
3366 struct wl_resource *surface_resource)
3367{
3368 struct weston_surface *surface =
3369 wl_resource_get_user_data(surface_resource);
3370 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3371 struct shell_surface *shsurf;
3372
3373 if (get_shell_surface(surface)) {
3374 wl_resource_post_error(surface_resource,
3375 WL_DISPLAY_ERROR_INVALID_OBJECT,
3376 "desktop_shell::get_shell_surface already requested");
3377 return;
3378 }
3379
3380 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3381 if (!shsurf) {
3382 wl_resource_post_error(surface_resource,
3383 WL_DISPLAY_ERROR_INVALID_OBJECT,
3384 "surface->configure already set");
3385 return;
3386 }
3387
3388 shsurf->resource =
3389 wl_resource_create(client,
3390 &xdg_surface_interface, 1, id);
3391 wl_resource_set_implementation(shsurf->resource,
3392 &xdg_surface_implementation,
3393 shsurf, shell_destroy_shell_surface);
3394}
3395
3396static bool
3397shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3398{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003399 return shsurf->resource &&
3400 wl_resource_instance_of(shsurf->resource,
3401 &xdg_surface_interface,
3402 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003403}
3404
3405/* xdg-popup implementation */
3406
3407static void
3408xdg_popup_destroy(struct wl_client *client,
3409 struct wl_resource *resource)
3410{
3411 wl_resource_destroy(resource);
3412}
3413
3414static void
3415xdg_popup_pong(struct wl_client *client,
3416 struct wl_resource *resource,
3417 uint32_t serial)
3418{
3419 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3420
3421 surface_pong(shsurf, serial);
3422}
3423
3424static const struct xdg_popup_interface xdg_popup_implementation = {
3425 xdg_popup_destroy,
3426 xdg_popup_pong
3427};
3428
3429static void
3430xdg_popup_send_configure(struct weston_surface *surface,
3431 uint32_t edges, int32_t width, int32_t height)
3432{
3433}
3434
3435static const struct weston_shell_client xdg_popup_client = {
3436 xdg_popup_send_configure
3437};
3438
3439static struct shell_surface *
3440create_xdg_popup(void *shell, struct weston_surface *surface,
3441 const struct weston_shell_client *client,
3442 struct weston_surface *parent,
3443 struct shell_seat *seat,
3444 uint32_t serial,
3445 int32_t x, int32_t y)
3446{
3447 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003448
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003449 shsurf = create_common_surface(shell, surface, client);
3450 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003451 shsurf->popup.shseat = seat;
3452 shsurf->popup.serial = serial;
3453 shsurf->popup.x = x;
3454 shsurf->popup.y = y;
3455 shell_surface_set_parent(shsurf, parent);
3456
3457 return shsurf;
3458}
3459
3460static void
3461xdg_get_xdg_popup(struct wl_client *client,
3462 struct wl_resource *resource,
3463 uint32_t id,
3464 struct wl_resource *surface_resource,
3465 struct wl_resource *parent_resource,
3466 struct wl_resource *seat_resource,
3467 uint32_t serial,
3468 int32_t x, int32_t y, uint32_t flags)
3469{
3470 struct weston_surface *surface =
3471 wl_resource_get_user_data(surface_resource);
3472 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3473 struct shell_surface *shsurf;
3474 struct weston_surface *parent;
3475 struct shell_seat *seat;
3476
3477 if (get_shell_surface(surface)) {
3478 wl_resource_post_error(surface_resource,
3479 WL_DISPLAY_ERROR_INVALID_OBJECT,
3480 "desktop_shell::get_shell_surface already requested");
3481 return;
3482 }
3483
3484 if (!parent_resource) {
3485 wl_resource_post_error(surface_resource,
3486 WL_DISPLAY_ERROR_INVALID_OBJECT,
3487 "xdg_shell::get_xdg_popup requires a parent shell surface");
3488 }
3489
3490 parent = wl_resource_get_user_data(parent_resource);
3491 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3492
3493 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3494 parent, seat, serial, x, y);
3495 if (!shsurf) {
3496 wl_resource_post_error(surface_resource,
3497 WL_DISPLAY_ERROR_INVALID_OBJECT,
3498 "surface->configure already set");
3499 return;
3500 }
3501
3502 shsurf->resource =
3503 wl_resource_create(client,
3504 &xdg_popup_interface, 1, id);
3505 wl_resource_set_implementation(shsurf->resource,
3506 &xdg_popup_implementation,
3507 shsurf, shell_destroy_shell_surface);
3508}
3509
3510static bool
3511shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3512{
3513 return wl_resource_instance_of(shsurf->resource,
3514 &xdg_popup_interface,
3515 &xdg_popup_implementation);
3516}
3517
3518static const struct xdg_shell_interface xdg_implementation = {
3519 xdg_use_unstable_version,
3520 xdg_get_xdg_surface,
3521 xdg_get_xdg_popup
3522};
3523
3524static int
3525xdg_shell_unversioned_dispatch(const void *implementation,
3526 void *_target, uint32_t opcode,
3527 const struct wl_message *message,
3528 union wl_argument *args)
3529{
3530 struct wl_resource *resource = _target;
3531 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3532
3533 if (opcode != 0) {
3534 wl_resource_post_error(resource,
3535 WL_DISPLAY_ERROR_INVALID_OBJECT,
3536 "must call use_unstable_version first");
3537 return 0;
3538 }
3539
3540#define XDG_SERVER_VERSION 1
3541
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003542 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3543 "shell implementation doesn't match protocol version");
3544
Rafael Antognollie2a34552013-12-03 15:35:45 -02003545 if (args[0].i != XDG_SERVER_VERSION) {
3546 wl_resource_post_error(resource,
3547 WL_DISPLAY_ERROR_INVALID_OBJECT,
3548 "incompatible version, server is %d "
3549 "client wants %d",
3550 XDG_SERVER_VERSION, args[0].i);
3551 return 0;
3552 }
3553
3554 wl_resource_set_implementation(resource, &xdg_implementation,
3555 shell, NULL);
3556
3557 return 1;
3558}
3559
3560/* end of xdg-shell implementation */
3561/***********************************/
3562
Kristian Høgsberg07937562011-04-12 17:25:42 -04003563static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003564shell_fade(struct desktop_shell *shell, enum fade_type type);
3565
3566static int
3567screensaver_timeout(void *data)
3568{
3569 struct desktop_shell *shell = data;
3570
3571 shell_fade(shell, FADE_OUT);
3572
3573 return 1;
3574}
3575
3576static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003577handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003578{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003579 struct desktop_shell *shell =
3580 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003581
Pekka Paalanen18027e52011-12-02 16:31:49 +02003582 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003583
3584 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003585 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003586}
3587
3588static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003589launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003590{
3591 if (shell->screensaver.binding)
3592 return;
3593
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003594 if (!shell->screensaver.path) {
3595 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003596 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003597 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003598
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003599 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003600 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003601 return;
3602 }
3603
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003604 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003605 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003606 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003607 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003608}
3609
3610static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003611terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003612{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003613 if (shell->screensaver.process.pid == 0)
3614 return;
3615
3616 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003617}
3618
3619static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003620configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003621{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003623
Jason Ekstranda7af7042013-10-12 22:38:11 -05003624 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3625 if (v->output == ev->output && v != ev) {
3626 weston_view_unmap(v);
3627 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003628 }
3629 }
3630
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003631 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003632
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 if (wl_list_empty(&ev->layer_link)) {
3634 wl_list_insert(&layer->view_list, &ev->layer_link);
3635 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003636 }
3637}
3638
3639static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003640background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003641{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003642 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003643 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003644
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 view = container_of(es->views.next, struct weston_view, surface_link);
3646
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003647 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003648}
3649
3650static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003651desktop_shell_set_background(struct wl_client *client,
3652 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003653 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003654 struct wl_resource *surface_resource)
3655{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003656 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003657 struct weston_surface *surface =
3658 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003659 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003660
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003661 if (surface->configure) {
3662 wl_resource_post_error(surface_resource,
3663 WL_DISPLAY_ERROR_INVALID_OBJECT,
3664 "surface role already assigned");
3665 return;
3666 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003667
Jason Ekstranda7af7042013-10-12 22:38:11 -05003668 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3669 weston_view_destroy(view);
3670 view = weston_view_create(surface);
3671
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003672 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003673 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003674 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003675 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003676 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003677 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003678 surface->output->width,
3679 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003680}
3681
3682static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003683panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003684{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003685 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003686 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003687
Jason Ekstranda7af7042013-10-12 22:38:11 -05003688 view = container_of(es->views.next, struct weston_view, surface_link);
3689
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003690 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003691}
3692
3693static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003694desktop_shell_set_panel(struct wl_client *client,
3695 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003696 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003697 struct wl_resource *surface_resource)
3698{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003699 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003700 struct weston_surface *surface =
3701 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003703
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003704 if (surface->configure) {
3705 wl_resource_post_error(surface_resource,
3706 WL_DISPLAY_ERROR_INVALID_OBJECT,
3707 "surface role already assigned");
3708 return;
3709 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003710
Jason Ekstranda7af7042013-10-12 22:38:11 -05003711 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3712 weston_view_destroy(view);
3713 view = weston_view_create(surface);
3714
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003715 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003716 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003717 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003718 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003719 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003720 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003721 surface->output->width,
3722 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003723}
3724
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003725static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003726lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003727{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003728 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003729 struct weston_view *view;
3730
3731 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003732
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003733 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003734 return;
3735
Jason Ekstranda7af7042013-10-12 22:38:11 -05003736 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003737
3738 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003739 wl_list_insert(&shell->lock_layer.view_list,
3740 &view->layer_link);
3741 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003742 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003743 }
3744}
3745
3746static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003747handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003748{
Tiago Vignattibe143262012-04-16 17:31:41 +03003749 struct desktop_shell *shell =
3750 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003751
Martin Minarik6d118362012-06-07 18:01:59 +02003752 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003753 shell->lock_surface = NULL;
3754}
3755
3756static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003757desktop_shell_set_lock_surface(struct wl_client *client,
3758 struct wl_resource *resource,
3759 struct wl_resource *surface_resource)
3760{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003761 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003762 struct weston_surface *surface =
3763 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003764
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003765 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003766
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003767 if (!shell->locked)
3768 return;
3769
Pekka Paalanen98262232011-12-01 10:42:22 +02003770 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003771
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003772 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003773 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003774 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003775
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003776 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003777 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003778 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003779}
3780
3781static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003782resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003783{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003784 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003785
Pekka Paalanen77346a62011-11-30 16:26:35 +02003786 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003788 wl_list_remove(&shell->lock_layer.link);
3789 wl_list_insert(&shell->compositor->cursor_layer.link,
3790 &shell->fullscreen_layer.link);
3791 wl_list_insert(&shell->fullscreen_layer.link,
3792 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003793 if (shell->showing_input_panels) {
3794 wl_list_insert(&shell->panel_layer.link,
3795 &shell->input_panel_layer.link);
3796 wl_list_insert(&shell->input_panel_layer.link,
3797 &ws->layer.link);
3798 } else {
3799 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3800 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003801
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003802 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003803
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003804 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003805 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003806 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003807}
3808
3809static void
3810desktop_shell_unlock(struct wl_client *client,
3811 struct wl_resource *resource)
3812{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003813 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003814
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003815 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003816
3817 if (shell->locked)
3818 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003819}
3820
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003821static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003822desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003823 struct wl_resource *resource,
3824 struct wl_resource *surface_resource)
3825{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003826 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003827
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003828 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003829 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003830}
3831
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003832static void
3833desktop_shell_desktop_ready(struct wl_client *client,
3834 struct wl_resource *resource)
3835{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003836 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003837
3838 shell_fade_startup(shell);
3839}
3840
Kristian Høgsberg75840622011-09-06 13:48:16 -04003841static const struct desktop_shell_interface desktop_shell_implementation = {
3842 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003843 desktop_shell_set_panel,
3844 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003845 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003846 desktop_shell_set_grab_surface,
3847 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003848};
3849
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003850static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003851get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003852{
3853 struct shell_surface *shsurf;
3854
3855 shsurf = get_shell_surface(surface);
3856 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003857 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003858 return shsurf->type;
3859}
3860
Kristian Høgsberg75840622011-09-06 13:48:16 -04003861static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003862move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003863{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003864 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003865 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003866 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003867
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003868 if (seat->pointer->focus == NULL)
3869 return;
3870
3871 focus = seat->pointer->focus->surface;
3872
Pekka Paalanen01388e22013-04-25 13:57:44 +03003873 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003874 if (surface == NULL)
3875 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003876
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003877 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003878 if (shsurf == NULL || shsurf->state.fullscreen ||
3879 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003880 return;
3881
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003882 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003883}
3884
3885static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003886maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3887{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003888 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003889 struct weston_surface *surface;
3890 struct shell_surface *shsurf;
3891
3892 surface = weston_surface_get_main_surface(focus);
3893 if (surface == NULL)
3894 return;
3895
3896 shsurf = get_shell_surface(surface);
3897 if (shsurf == NULL)
3898 return;
3899
3900 if (!shell_surface_is_xdg_surface(shsurf))
3901 return;
3902
3903 if (shsurf->state.maximized)
3904 xdg_surface_send_request_unset_maximized(shsurf->resource);
3905 else
3906 xdg_surface_send_request_set_maximized(shsurf->resource);
3907}
3908
3909static void
3910fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3911{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003912 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003913 struct weston_surface *surface;
3914 struct shell_surface *shsurf;
3915
3916 surface = weston_surface_get_main_surface(focus);
3917 if (surface == NULL)
3918 return;
3919
3920 shsurf = get_shell_surface(surface);
3921 if (shsurf == NULL)
3922 return;
3923
3924 if (!shell_surface_is_xdg_surface(shsurf))
3925 return;
3926
3927 if (shsurf->state.fullscreen)
3928 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3929 else
3930 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3931}
3932
3933static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003934touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3935{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003936 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003937 struct weston_surface *surface;
3938 struct shell_surface *shsurf;
3939
3940 surface = weston_surface_get_main_surface(focus);
3941 if (surface == NULL)
3942 return;
3943
3944 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003945 if (shsurf == NULL || shsurf->state.fullscreen ||
3946 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003947 return;
3948
3949 surface_touch_move(shsurf, (struct weston_seat *) seat);
3950}
3951
3952static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003953resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003954{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003955 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003956 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003957 uint32_t edges = 0;
3958 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003959 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003960
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003961 if (seat->pointer->focus == NULL)
3962 return;
3963
3964 focus = seat->pointer->focus->surface;
3965
Pekka Paalanen01388e22013-04-25 13:57:44 +03003966 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003967 if (surface == NULL)
3968 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003969
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003970 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003971 if (shsurf == NULL || shsurf->state.fullscreen ||
3972 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003973 return;
3974
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975 weston_view_from_global(shsurf->view,
3976 wl_fixed_to_int(seat->pointer->grab_x),
3977 wl_fixed_to_int(seat->pointer->grab_y),
3978 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003979
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003980 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003981 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003982 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003983 edges |= 0;
3984 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003985 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003986
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003987 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003988 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003989 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003990 edges |= 0;
3991 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003992 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003993
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003994 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003995}
3996
3997static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003998surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003999 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004000{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004001 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004002 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004003 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004004 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004005
Pekka Paalanen01388e22013-04-25 13:57:44 +03004006 /* XXX: broken for windows containing sub-surfaces */
4007 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004008 if (surface == NULL)
4009 return;
4010
4011 shsurf = get_shell_surface(surface);
4012 if (!shsurf)
4013 return;
4014
Jason Ekstranda7af7042013-10-12 22:38:11 -05004015 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004016
Jason Ekstranda7af7042013-10-12 22:38:11 -05004017 if (shsurf->view->alpha > 1.0)
4018 shsurf->view->alpha = 1.0;
4019 if (shsurf->view->alpha < step)
4020 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004021
Jason Ekstranda7af7042013-10-12 22:38:11 -05004022 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004023 weston_surface_damage(surface);
4024}
4025
4026static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004027do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004028 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004029{
Daniel Stone37816df2012-05-16 18:45:18 +01004030 struct weston_seat *ws = (struct weston_seat *) seat;
4031 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004032 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004033 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004034
4035 wl_list_for_each(output, &compositor->output_list, link) {
4036 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004037 wl_fixed_to_double(seat->pointer->x),
4038 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004039 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004040 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004041 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004042 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004043 increment = -output->zoom.increment;
4044 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004045 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004046 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004047 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004048 else
4049 increment = 0;
4050
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004051 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004052
Scott Moreaue6603982012-06-11 13:07:51 -06004053 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004054 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004055 else if (output->zoom.level > output->zoom.max_level)
4056 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004057 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004058 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004059 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004060
Scott Moreaue6603982012-06-11 13:07:51 -06004061 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004062
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004064 }
4065 }
4066}
4067
Scott Moreauccbf29d2012-02-22 14:21:41 -07004068static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004069zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004070 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004071{
4072 do_zoom(seat, time, 0, axis, value);
4073}
4074
4075static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004076zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004077 void *data)
4078{
4079 do_zoom(seat, time, key, 0, 0);
4080}
4081
4082static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004083terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004084 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004085{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004086 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004087
Daniel Stone325fc2d2012-05-30 16:31:58 +01004088 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004089}
4090
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004091static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004092rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4093 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004094{
4095 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004096 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004097 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004098 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004099 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004100
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004101 weston_pointer_move(pointer, x, y);
4102
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004103 if (!shsurf)
4104 return;
4105
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004106 cx = 0.5f * shsurf->surface->width;
4107 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004108
Daniel Stone37816df2012-05-16 18:45:18 +01004109 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4110 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004111 r = sqrtf(dx * dx + dy * dy);
4112
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004113 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004114 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004115
4116 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004117 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004118 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004119
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004120 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004121 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004122
4123 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004124 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004125 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004126 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004127 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004128
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004129 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004130 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004131 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004132 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004133 wl_list_init(&shsurf->rotation.transform.link);
4134 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004135 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004136 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004137
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004138 /* We need to adjust the position of the surface
4139 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004140 cposx = shsurf->view->geometry.x + cx;
4141 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004142 dposx = rotate->center.x - cposx;
4143 dposy = rotate->center.y - cposy;
4144 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004145 weston_view_set_position(shsurf->view,
4146 shsurf->view->geometry.x + dposx,
4147 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004148 }
4149
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004150 /* Repaint implies weston_surface_update_transform(), which
4151 * lazily applies the damage due to rotation update.
4152 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004153 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004154}
4155
4156static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004157rotate_grab_button(struct weston_pointer_grab *grab,
4158 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004159{
4160 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004161 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004162 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004163 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004164 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004165
Daniel Stone4dbadb12012-05-30 16:31:51 +01004166 if (pointer->button_count == 0 &&
4167 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004168 if (shsurf)
4169 weston_matrix_multiply(&shsurf->rotation.rotation,
4170 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004171 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004172 free(rotate);
4173 }
4174}
4175
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004176static void
4177rotate_grab_cancel(struct weston_pointer_grab *grab)
4178{
4179 struct rotate_grab *rotate =
4180 container_of(grab, struct rotate_grab, base.grab);
4181
4182 shell_grab_end(&rotate->base);
4183 free(rotate);
4184}
4185
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004186static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004187 noop_grab_focus,
4188 rotate_grab_motion,
4189 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004190 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004191};
4192
4193static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004194surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004195{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004196 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004197 float dx, dy;
4198 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004199
Pekka Paalanen460099f2012-01-20 16:48:25 +02004200 rotate = malloc(sizeof *rotate);
4201 if (!rotate)
4202 return;
4203
Jason Ekstranda7af7042013-10-12 22:38:11 -05004204 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004205 surface->surface->width * 0.5f,
4206 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004208
Daniel Stone37816df2012-05-16 18:45:18 +01004209 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4210 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004211 r = sqrtf(dx * dx + dy * dy);
4212 if (r > 20.0f) {
4213 struct weston_matrix inverse;
4214
4215 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004216 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004217 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004218
4219 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004220 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004221 } else {
4222 weston_matrix_init(&surface->rotation.rotation);
4223 weston_matrix_init(&rotate->rotation);
4224 }
4225
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004226 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4227 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004228}
4229
4230static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004231rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004232 void *data)
4233{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004234 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004235 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004236 struct shell_surface *surface;
4237
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004238 if (seat->pointer->focus == NULL)
4239 return;
4240
4241 focus = seat->pointer->focus->surface;
4242
Pekka Paalanen01388e22013-04-25 13:57:44 +03004243 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004244 if (base_surface == NULL)
4245 return;
4246
4247 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004248 if (surface == NULL || surface->state.fullscreen ||
4249 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004250 return;
4251
4252 surface_rotate(surface, seat);
4253}
4254
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004255/* Move all fullscreen layers down to the current workspace in a non-reversible
4256 * manner. This should be used when implementing shell-wide overlays, such as
4257 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004258void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004259lower_fullscreen_layer(struct desktop_shell *shell)
4260{
4261 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004262 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004263
4264 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 wl_list_for_each_reverse_safe(view, prev,
4266 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004267 layer_link) {
4268 wl_list_remove(&view->layer_link);
4269 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4270 weston_view_damage_below(view);
4271 weston_surface_damage(view->surface);
4272 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004273}
4274
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004275void
Tiago Vignattibe143262012-04-16 17:31:41 +03004276activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004277 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004278{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004279 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004280 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004281 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004282 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004283 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004284
Pekka Paalanen01388e22013-04-25 13:57:44 +03004285 main_surface = weston_surface_get_main_surface(es);
4286
Daniel Stone37816df2012-05-16 18:45:18 +01004287 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004288
Jonas Ådahl8538b222012-08-29 22:13:03 +02004289 state = ensure_focus_state(shell, seat);
4290 if (state == NULL)
4291 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004292
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004293 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004294 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004295
Rafael Antognolli03b16592013-12-03 15:35:42 -02004296 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004297 assert(shsurf);
4298
Rafael Antognolli03b16592013-12-03 15:35:42 -02004299 if (shsurf->state.fullscreen)
4300 shell_configure_fullscreen(shsurf);
4301 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004302 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004303
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004304 /* Update the surface’s layer. This brings it to the top of the stacking
4305 * order as appropriate. */
4306 shell_surface_update_layer(shsurf);
4307
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004308 if (shell->focus_animation_type != ANIMATION_NONE) {
4309 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004310 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004311 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004312}
4313
Alex Wu21858432012-04-01 20:13:08 +08004314/* no-op func for checking black surface */
4315static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004316black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004317{
4318}
4319
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004320static bool
Alex Wu21858432012-04-01 20:13:08 +08004321is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4322{
4323 if (es->configure == black_surface_configure) {
4324 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004325 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004326 return true;
4327 }
4328 return false;
4329}
4330
Kristian Høgsberg75840622011-09-06 13:48:16 -04004331static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004332activate_binding(struct weston_seat *seat,
4333 struct desktop_shell *shell,
4334 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004335{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004336 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004337
Alex Wu9c35e6b2012-03-05 14:13:13 +08004338 if (!focus)
4339 return;
4340
Pekka Paalanen01388e22013-04-25 13:57:44 +03004341 if (is_black_surface(focus, &main_surface))
4342 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004343
Pekka Paalanen01388e22013-04-25 13:57:44 +03004344 main_surface = weston_surface_get_main_surface(focus);
4345 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004346 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004347
Neil Robertsa28c6932013-10-03 16:43:04 +01004348 activate(shell, focus, seat);
4349}
4350
4351static void
4352click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4353 void *data)
4354{
4355 if (seat->pointer->grab != &seat->pointer->default_grab)
4356 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004357 if (seat->pointer->focus == NULL)
4358 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004359
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004360 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004361}
4362
4363static void
4364touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4365{
4366 if (seat->touch->grab != &seat->touch->default_grab)
4367 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004368 if (seat->touch->focus == NULL)
4369 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004370
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004371 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004372}
4373
4374static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004375lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004376{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004377 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004378
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004379 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004380 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004381 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004382 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004383
4384 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004385
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004386 /* Hide all surfaces by removing the fullscreen, panel and
4387 * toplevel layers. This way nothing else can show or receive
4388 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004389
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004390 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004391 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004392 if (shell->showing_input_panels)
4393 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004394 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004395 wl_list_insert(&shell->compositor->cursor_layer.link,
4396 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004397
Pekka Paalanen77346a62011-11-30 16:26:35 +02004398 launch_screensaver(shell);
4399
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004400 /* TODO: disable bindings that should not work while locked. */
4401
4402 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004403}
4404
4405static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004406unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004407{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004408 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004409 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004410 return;
4411 }
4412
4413 /* If desktop-shell client has gone away, unlock immediately. */
4414 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004415 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004416 return;
4417 }
4418
4419 if (shell->prepare_event_sent)
4420 return;
4421
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004422 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004423 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004424}
4425
4426static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004427shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004428{
4429 struct desktop_shell *shell = data;
4430
4431 shell->fade.animation = NULL;
4432
4433 switch (shell->fade.type) {
4434 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435 weston_surface_destroy(shell->fade.view->surface);
4436 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004437 break;
4438 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004439 lock(shell);
4440 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004441 default:
4442 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004443 }
4444}
4445
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004447shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004448{
4449 struct weston_compositor *compositor = shell->compositor;
4450 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004452
4453 surface = weston_surface_create(compositor);
4454 if (!surface)
4455 return NULL;
4456
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 view = weston_view_create(surface);
4458 if (!view) {
4459 weston_surface_destroy(surface);
4460 return NULL;
4461 }
4462
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004463 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004464 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004465 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 wl_list_insert(&compositor->fade_layer.view_list,
4467 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004468 pixman_region32_init(&surface->input);
4469
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004471}
4472
4473static void
4474shell_fade(struct desktop_shell *shell, enum fade_type type)
4475{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004476 float tint;
4477
4478 switch (type) {
4479 case FADE_IN:
4480 tint = 0.0;
4481 break;
4482 case FADE_OUT:
4483 tint = 1.0;
4484 break;
4485 default:
4486 weston_log("shell: invalid fade type\n");
4487 return;
4488 }
4489
4490 shell->fade.type = type;
4491
Jason Ekstranda7af7042013-10-12 22:38:11 -05004492 if (shell->fade.view == NULL) {
4493 shell->fade.view = shell_fade_create_surface(shell);
4494 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004495 return;
4496
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 shell->fade.view->alpha = 1.0 - tint;
4498 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004499 }
4500
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004501 if (shell->fade.view->output == NULL) {
4502 /* If the black view gets a NULL output, we lost the
4503 * last output and we'll just cancel the fade. This
4504 * happens when you close the last window under the
4505 * X11 or Wayland backends. */
4506 shell->locked = false;
4507 weston_surface_destroy(shell->fade.view->surface);
4508 shell->fade.view = NULL;
4509 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004510 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004511 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004512 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004514 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004515 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004516 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004517}
4518
4519static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004520do_shell_fade_startup(void *data)
4521{
4522 struct desktop_shell *shell = data;
4523
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004524 if (shell->startup_animation_type == ANIMATION_FADE)
4525 shell_fade(shell, FADE_IN);
4526 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004527 weston_surface_destroy(shell->fade.view->surface);
4528 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004529 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004530}
4531
4532static void
4533shell_fade_startup(struct desktop_shell *shell)
4534{
4535 struct wl_event_loop *loop;
4536
4537 if (!shell->fade.startup_timer)
4538 return;
4539
4540 wl_event_source_remove(shell->fade.startup_timer);
4541 shell->fade.startup_timer = NULL;
4542
4543 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4544 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4545}
4546
4547static int
4548fade_startup_timeout(void *data)
4549{
4550 struct desktop_shell *shell = data;
4551
4552 shell_fade_startup(shell);
4553 return 0;
4554}
4555
4556static void
4557shell_fade_init(struct desktop_shell *shell)
4558{
4559 /* Make compositor output all black, and wait for the desktop-shell
4560 * client to signal it is ready, then fade in. The timer triggers a
4561 * fade-in, in case the desktop-shell client takes too long.
4562 */
4563
4564 struct wl_event_loop *loop;
4565
Jason Ekstranda7af7042013-10-12 22:38:11 -05004566 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004567 weston_log("%s: warning: fade surface already exists\n",
4568 __func__);
4569 return;
4570 }
4571
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 shell->fade.view = shell_fade_create_surface(shell);
4573 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574 return;
4575
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 weston_view_update_transform(shell->fade.view);
4577 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004578
4579 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4580 shell->fade.startup_timer =
4581 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4582 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4583}
4584
4585static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004586idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004587{
4588 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004589 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004590 struct weston_seat *seat;
4591
4592 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4593 if (seat->pointer)
4594 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004595
4596 shell_fade(shell, FADE_OUT);
4597 /* lock() is called from shell_fade_done() */
4598}
4599
4600static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004601wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004602{
4603 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004604 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004605
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004606 unlock(shell);
4607}
4608
4609static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004610center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004611{
Giulio Camuffob8366642013-04-25 13:57:46 +03004612 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004613 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004614
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004616
4617 x = output->x + (output->width - width) / 2 - surf_x / 2;
4618 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004619
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004620 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004621}
4622
4623static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624weston_view_set_initial_position(struct weston_view *view,
4625 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004626{
4627 struct weston_compositor *compositor = shell->compositor;
4628 int ix = 0, iy = 0;
4629 int range_x, range_y;
4630 int dx, dy, x, y, panel_height;
4631 struct weston_output *output, *target_output = NULL;
4632 struct weston_seat *seat;
4633
4634 /* As a heuristic place the new window on the same output as the
4635 * pointer. Falling back to the output containing 0, 0.
4636 *
4637 * TODO: Do something clever for touch too?
4638 */
4639 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004640 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004641 ix = wl_fixed_to_int(seat->pointer->x);
4642 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004643 break;
4644 }
4645 }
4646
4647 wl_list_for_each(output, &compositor->output_list, link) {
4648 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4649 target_output = output;
4650 break;
4651 }
4652 }
4653
4654 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655 weston_view_set_position(view, 10 + random() % 400,
4656 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004657 return;
4658 }
4659
4660 /* Valid range within output where the surface will still be onscreen.
4661 * If this is negative it means that the surface is bigger than
4662 * output.
4663 */
4664 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004665 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004666 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004667 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004668
Rob Bradford4cb88c72012-08-13 15:18:44 +01004669 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004670 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004671 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004672 dx = 0;
4673
4674 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004675 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004676 else
4677 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004678
4679 x = target_output->x + dx;
4680 y = target_output->y + dy;
4681
Jason Ekstranda7af7042013-10-12 22:38:11 -05004682 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004683}
4684
4685static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004687 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004688{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004689 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004690 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004691 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004692 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004693
Pekka Paalanen77346a62011-11-30 16:26:35 +02004694 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004695 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004696 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004697 if (shsurf->state.fullscreen) {
4698 center_on_output(shsurf->view, shsurf->fullscreen_output);
4699 shell_map_fullscreen(shsurf);
4700 } else if (shsurf->state.maximized) {
4701 /* use surface configure to set the geometry */
4702 panel_height = get_output_panel_height(shell, shsurf->output);
4703 surface_subsurfaces_boundingbox(shsurf->surface,
4704 &surf_x, &surf_y, NULL, NULL);
4705 weston_view_set_position(shsurf->view,
4706 shsurf->output->x - surf_x,
4707 shsurf->output->y +
4708 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004709 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004710 weston_view_set_initial_position(shsurf->view, shell);
4711 }
Juan Zhao96879df2012-02-07 08:45:41 +08004712 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004713 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004714 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004715 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004716 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004717 weston_view_set_position(shsurf->view,
4718 shsurf->view->geometry.x + sx,
4719 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004720 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004721 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004722 default:
4723 ;
4724 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004725
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004726 /* Surface stacking order, see also activate(). */
4727 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004728
Jason Ekstranda7af7042013-10-12 22:38:11 -05004729 if (shsurf->type != SHELL_SURFACE_NONE) {
4730 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004731 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004732 shsurf->surface->output = shsurf->output;
4733 shsurf->view->output = shsurf->output;
4734 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004735 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004736
Jason Ekstranda7af7042013-10-12 22:38:11 -05004737 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004738 /* XXX: xwayland's using the same fields for transient type */
4739 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004740 if (shsurf->transient.flags ==
4741 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4742 break;
4743 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004744 if (shsurf->state.relative &&
4745 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4746 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004747 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004748 break;
4749 wl_list_for_each(seat, &compositor->seat_list, link)
4750 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004751 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004752 case SHELL_SURFACE_POPUP:
4753 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004754 default:
4755 break;
4756 }
4757
Rafael Antognolli03b16592013-12-03 15:35:42 -02004758 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4759 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004760 {
4761 switch (shell->win_animation_type) {
4762 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004764 break;
4765 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004767 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004768 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004769 default:
4770 break;
4771 }
4772 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004773}
4774
4775static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004776configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004777 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004778{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004779 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004780 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004781 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004782
Pekka Paalanen77346a62011-11-30 16:26:35 +02004783 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004784
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004785 assert(shsurf);
4786
Rafael Antognolli03b16592013-12-03 15:35:42 -02004787 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004788 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004789 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004790 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004791 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4792 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004793 mx = shsurf->output->x - surf_x;
4794 my = shsurf->output->y +
4795 get_output_panel_height(shell,shsurf->output) - surf_y;
4796 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004797 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004798 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004799 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004800
Alex Wu4539b082012-03-01 12:57:46 +08004801 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004802 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 wl_list_for_each(view, &surface->views, surface_link)
4804 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004805
Rafael Antognolli03b16592013-12-03 15:35:42 -02004806 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004807 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004808 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004809}
4810
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004811static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004812shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004813{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004814 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004815 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004816 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004817
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004818 assert(shsurf);
4819
4820 shell = shsurf->shell;
4821
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004822 if (!weston_surface_is_mapped(es) &&
4823 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004824 remove_popup_grab(shsurf);
4825 }
4826
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004827 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004828 return;
4829
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004830 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004831 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004832 type_changed = 1;
4833 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004834
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004835 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004836 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004837 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004838 shsurf->last_width != es->width ||
4839 shsurf->last_height != es->height) {
4840 shsurf->last_width = es->width;
4841 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004842 float from_x, from_y;
4843 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4846 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004847 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004849 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004850 }
4851}
4852
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004853static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004854
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004855static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004856desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004857{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004858 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004859 struct desktop_shell *shell =
4860 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004861
4862 shell->child.process.pid = 0;
4863 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004864
4865 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4866 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004867 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004868 shell->child.deathstamp = time;
4869 shell->child.deathcount = 0;
4870 }
4871
4872 shell->child.deathcount++;
4873 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004874 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004875 return;
4876 }
4877
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004878 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004879 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004880 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004881}
4882
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004883static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004884desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4885{
4886 struct desktop_shell *shell;
4887
4888 shell = container_of(listener, struct desktop_shell,
4889 child.client_destroy_listener);
4890
4891 shell->child.client = NULL;
4892}
4893
4894static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004895launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004896{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004897 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004898
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004899 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004900 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004901 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004902 desktop_shell_sigchld);
4903
4904 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004905 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004906
4907 shell->child.client_destroy_listener.notify =
4908 desktop_shell_client_destroy;
4909 wl_client_add_destroy_listener(shell->child.client,
4910 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004911}
4912
4913static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004914bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4915{
Tiago Vignattibe143262012-04-16 17:31:41 +03004916 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004917 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004918
Jason Ekstranda85118c2013-06-27 20:17:02 -05004919 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4920 if (resource)
4921 wl_resource_set_implementation(resource, &shell_implementation,
4922 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004923}
4924
Kristian Høgsberg75840622011-09-06 13:48:16 -04004925static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004926bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4927{
4928 struct desktop_shell *shell = data;
4929 struct wl_resource *resource;
4930
4931 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4932 if (resource)
4933 wl_resource_set_dispatcher(resource,
4934 xdg_shell_unversioned_dispatch,
4935 NULL, shell, NULL);
4936}
4937
4938static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004939unbind_desktop_shell(struct wl_resource *resource)
4940{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004941 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004942
4943 if (shell->locked)
4944 resume_desktop(shell);
4945
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004946 shell->child.desktop_shell = NULL;
4947 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004948}
4949
4950static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004951bind_desktop_shell(struct wl_client *client,
4952 void *data, uint32_t version, uint32_t id)
4953{
Tiago Vignattibe143262012-04-16 17:31:41 +03004954 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004955 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004956
Jason Ekstranda85118c2013-06-27 20:17:02 -05004957 resource = wl_resource_create(client, &desktop_shell_interface,
4958 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004959
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004960 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004961 wl_resource_set_implementation(resource,
4962 &desktop_shell_implementation,
4963 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004964 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004965
4966 if (version < 2)
4967 shell_fade_startup(shell);
4968
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004969 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004970 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004971
4972 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4973 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004974 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004975}
4976
Pekka Paalanen6e168112011-11-24 11:34:05 +02004977static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004978screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004979{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004980 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004981 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004982
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004983 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004984 return;
4985
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004986 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004987 if (!shell->locked)
4988 return;
4989
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 view = container_of(surface->views.next, struct weston_view, surface_link);
4991 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004992
Jason Ekstranda7af7042013-10-12 22:38:11 -05004993 if (wl_list_empty(&view->layer_link)) {
4994 wl_list_insert(shell->lock_layer.view_list.prev,
4995 &view->layer_link);
4996 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004997 wl_event_source_timer_update(shell->screensaver.timer,
4998 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004999 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005000 }
5001}
5002
5003static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005004screensaver_set_surface(struct wl_client *client,
5005 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005006 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005007 struct wl_resource *output_resource)
5008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005010 struct weston_surface *surface =
5011 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005012 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013 struct weston_view *view, *next;
5014
5015 /* Make sure we only have one view */
5016 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5017 weston_view_destroy(view);
5018 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005019
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005020 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005021 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005022 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005023}
5024
5025static const struct screensaver_interface screensaver_implementation = {
5026 screensaver_set_surface
5027};
5028
5029static void
5030unbind_screensaver(struct wl_resource *resource)
5031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005032 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005033
Pekka Paalanen77346a62011-11-30 16:26:35 +02005034 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005035}
5036
5037static void
5038bind_screensaver(struct wl_client *client,
5039 void *data, uint32_t version, uint32_t id)
5040{
Tiago Vignattibe143262012-04-16 17:31:41 +03005041 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005042 struct wl_resource *resource;
5043
Jason Ekstranda85118c2013-06-27 20:17:02 -05005044 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005045
Pekka Paalanen77346a62011-11-30 16:26:35 +02005046 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005047 wl_resource_set_implementation(resource,
5048 &screensaver_implementation,
5049 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005050 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005051 return;
5052 }
5053
5054 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5055 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005056 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005057}
5058
Kristian Høgsberg07045392012-02-19 18:52:44 -05005059struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005060 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005061 struct weston_surface *current;
5062 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005063 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005064};
5065
5066static void
5067switcher_next(struct switcher *switcher)
5068{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005069 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005070 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005071 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005072 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005073
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005075 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005076 if (shsurf &&
5077 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5078 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005079 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005082 next = view->surface;
5083 prev = view->surface;
5084 view->alpha = 0.25;
5085 weston_view_geometry_dirty(view);
5086 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005087 }
Alex Wu1659daa2012-04-01 20:13:09 +08005088
Jason Ekstranda7af7042013-10-12 22:38:11 -05005089 if (is_black_surface(view->surface, NULL)) {
5090 view->alpha = 0.25;
5091 weston_view_geometry_dirty(view);
5092 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005093 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005094 }
5095
5096 if (next == NULL)
5097 next = first;
5098
Alex Wu07b26062012-03-12 16:06:01 +08005099 if (next == NULL)
5100 return;
5101
Kristian Høgsberg07045392012-02-19 18:52:44 -05005102 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005103 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005104
5105 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005106 wl_list_for_each(view, &next->views, surface_link)
5107 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005108
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005109 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005110 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005111 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005112}
5113
5114static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005115switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005116{
5117 struct switcher *switcher =
5118 container_of(listener, struct switcher, listener);
5119
5120 switcher_next(switcher);
5121}
5122
5123static void
Daniel Stone351eb612012-05-31 15:27:47 -04005124switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005125{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005126 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005127 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005128 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005129
Jason Ekstranda7af7042013-10-12 22:38:11 -05005130 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005131 if (is_focus_view(view))
5132 continue;
5133
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 view->alpha = 1.0;
5135 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005136 }
5137
Alex Wu07b26062012-03-12 16:06:01 +08005138 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005139 activate(switcher->shell, switcher->current,
5140 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005141 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005142 weston_keyboard_end_grab(keyboard);
5143 if (keyboard->input_method_resource)
5144 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005145 free(switcher);
5146}
5147
5148static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005149switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005150 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005151{
5152 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005153 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005154
Daniel Stonec9785ea2012-05-30 16:31:52 +01005155 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005156 switcher_next(switcher);
5157}
5158
5159static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005160switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005161 uint32_t mods_depressed, uint32_t mods_latched,
5162 uint32_t mods_locked, uint32_t group)
5163{
5164 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005165 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005166
Daniel Stone351eb612012-05-31 15:27:47 -04005167 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5168 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005169}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005170
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005171static void
5172switcher_cancel(struct weston_keyboard_grab *grab)
5173{
5174 struct switcher *switcher = container_of(grab, struct switcher, grab);
5175
5176 switcher_destroy(switcher);
5177}
5178
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005179static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005180 switcher_key,
5181 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005182 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005183};
5184
5185static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005186switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005187 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005188{
Tiago Vignattibe143262012-04-16 17:31:41 +03005189 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005190 struct switcher *switcher;
5191
5192 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005193 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005194 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005195 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005196 wl_list_init(&switcher->listener.link);
5197
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005198 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005199 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005200 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005201 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5202 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005203 switcher_next(switcher);
5204}
5205
Pekka Paalanen3c647232011-12-22 13:43:43 +02005206static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005207backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005208 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005209{
5210 struct weston_compositor *compositor = data;
5211 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005212 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005213
5214 /* TODO: we're limiting to simple use cases, where we assume just
5215 * control on the primary display. We'd have to extend later if we
5216 * ever get support for setting backlights on random desktop LCD
5217 * panels though */
5218 output = get_default_output(compositor);
5219 if (!output)
5220 return;
5221
5222 if (!output->set_backlight)
5223 return;
5224
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005225 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5226 backlight_new = output->backlight_current - 25;
5227 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5228 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005229
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005230 if (backlight_new < 5)
5231 backlight_new = 5;
5232 if (backlight_new > 255)
5233 backlight_new = 255;
5234
5235 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005236 output->set_backlight(output, output->backlight_current);
5237}
5238
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005239struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005240 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005241 struct weston_seat *seat;
5242 uint32_t key[2];
5243 int key_released[2];
5244};
5245
5246static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005247debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005248 uint32_t key, uint32_t state)
5249{
5250 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005251 struct weston_compositor *ec = db->seat->compositor;
5252 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005253 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005254 uint32_t serial;
5255 int send = 0, terminate = 0;
5256 int check_binding = 1;
5257 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005258 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005259
5260 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5261 /* Do not run bindings on key releases */
5262 check_binding = 0;
5263
5264 for (i = 0; i < 2; i++)
5265 if (key == db->key[i])
5266 db->key_released[i] = 1;
5267
5268 if (db->key_released[0] && db->key_released[1]) {
5269 /* All key releases been swalled so end the grab */
5270 terminate = 1;
5271 } else if (key != db->key[0] && key != db->key[1]) {
5272 /* Should not swallow release of other keys */
5273 send = 1;
5274 }
5275 } else if (key == db->key[0] && !db->key_released[0]) {
5276 /* Do not check bindings for the first press of the binding
5277 * key. This allows it to be used as a debug shortcut.
5278 * We still need to swallow this event. */
5279 check_binding = 0;
5280 } else if (db->key[1]) {
5281 /* If we already ran a binding don't process another one since
5282 * we can't keep track of all the binding keys that were
5283 * pressed in order to swallow the release events. */
5284 send = 1;
5285 check_binding = 0;
5286 }
5287
5288 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005289 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5290 key, state)) {
5291 /* We ran a binding so swallow the press and keep the
5292 * grab to swallow the released too. */
5293 send = 0;
5294 terminate = 0;
5295 db->key[1] = key;
5296 } else {
5297 /* Terminate the grab since the key pressed is not a
5298 * debug binding key. */
5299 send = 1;
5300 terminate = 1;
5301 }
5302 }
5303
5304 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005305 serial = wl_display_next_serial(display);
5306 resource_list = &grab->keyboard->focus_resource_list;
5307 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005308 wl_keyboard_send_key(resource, serial, time, key, state);
5309 }
5310 }
5311
5312 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005313 weston_keyboard_end_grab(grab->keyboard);
5314 if (grab->keyboard->input_method_resource)
5315 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005316 free(db);
5317 }
5318}
5319
5320static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005321debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005322 uint32_t mods_depressed, uint32_t mods_latched,
5323 uint32_t mods_locked, uint32_t group)
5324{
5325 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005326 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005327
Neil Roberts96d790e2013-09-19 17:32:00 +01005328 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005329
Neil Roberts96d790e2013-09-19 17:32:00 +01005330 wl_resource_for_each(resource, resource_list) {
5331 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5332 mods_latched, mods_locked, group);
5333 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005334}
5335
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005336static void
5337debug_binding_cancel(struct weston_keyboard_grab *grab)
5338{
5339 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5340
5341 weston_keyboard_end_grab(grab->keyboard);
5342 free(db);
5343}
5344
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005345struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005346 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005347 debug_binding_modifiers,
5348 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005349};
5350
5351static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005352debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005353{
5354 struct debug_binding_grab *grab;
5355
5356 grab = calloc(1, sizeof *grab);
5357 if (!grab)
5358 return;
5359
5360 grab->seat = (struct weston_seat *) seat;
5361 grab->key[0] = key;
5362 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005363 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005364}
5365
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005366static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005367force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005368 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005369{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005370 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005371 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005372 struct desktop_shell *shell = data;
5373 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005374 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005375
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005376 focus_surface = seat->keyboard->focus;
5377 if (!focus_surface)
5378 return;
5379
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005380 wl_signal_emit(&compositor->kill_signal, focus_surface);
5381
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005382 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005383 wl_client_get_credentials(client, &pid, NULL, NULL);
5384
5385 /* Skip clients that we launched ourselves (the credentials of
5386 * the socketpair is ours) */
5387 if (pid == getpid())
5388 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005389
Daniel Stone325fc2d2012-05-30 16:31:58 +01005390 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005391}
5392
5393static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005394workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005395 uint32_t key, void *data)
5396{
5397 struct desktop_shell *shell = data;
5398 unsigned int new_index = shell->workspaces.current;
5399
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005400 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005401 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005402 if (new_index != 0)
5403 new_index--;
5404
5405 change_workspace(shell, new_index);
5406}
5407
5408static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005409workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005410 uint32_t key, void *data)
5411{
5412 struct desktop_shell *shell = data;
5413 unsigned int new_index = shell->workspaces.current;
5414
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005415 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005416 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005417 if (new_index < shell->workspaces.num - 1)
5418 new_index++;
5419
5420 change_workspace(shell, new_index);
5421}
5422
5423static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005424workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005425 uint32_t key, void *data)
5426{
5427 struct desktop_shell *shell = data;
5428 unsigned int new_index;
5429
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005430 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005431 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005432 new_index = key - KEY_F1;
5433 if (new_index >= shell->workspaces.num)
5434 new_index = shell->workspaces.num - 1;
5435
5436 change_workspace(shell, new_index);
5437}
5438
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005439static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005440workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005441 uint32_t key, void *data)
5442{
5443 struct desktop_shell *shell = data;
5444 unsigned int new_index = shell->workspaces.current;
5445
5446 if (shell->locked)
5447 return;
5448
5449 if (new_index != 0)
5450 new_index--;
5451
5452 take_surface_to_workspace_by_seat(shell, seat, new_index);
5453}
5454
5455static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005456workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005457 uint32_t key, void *data)
5458{
5459 struct desktop_shell *shell = data;
5460 unsigned int new_index = shell->workspaces.current;
5461
5462 if (shell->locked)
5463 return;
5464
5465 if (new_index < shell->workspaces.num - 1)
5466 new_index++;
5467
5468 take_surface_to_workspace_by_seat(shell, seat, new_index);
5469}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005470
5471static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005472shell_reposition_view_on_output_destroy(struct weston_view *view)
5473{
5474 struct weston_output *output, *first_output;
5475 struct weston_compositor *ec = view->surface->compositor;
5476 struct shell_surface *shsurf;
5477 float x, y;
5478 int visible;
5479
5480 x = view->geometry.x;
5481 y = view->geometry.y;
5482
5483 /* At this point the destroyed output is not in the list anymore.
5484 * If the view is still visible somewhere, we leave where it is,
5485 * otherwise, move it to the first output. */
5486 visible = 0;
5487 wl_list_for_each(output, &ec->output_list, link) {
5488 if (pixman_region32_contains_point(&output->region,
5489 x, y, NULL)) {
5490 visible = 1;
5491 break;
5492 }
5493 }
5494
5495 if (!visible) {
5496 first_output = container_of(ec->output_list.next,
5497 struct weston_output, link);
5498
5499 x = first_output->x + first_output->width / 4;
5500 y = first_output->y + first_output->height / 4;
5501 }
5502
5503 weston_view_set_position(view, x, y);
5504
5505 shsurf = get_shell_surface(view->surface);
5506
5507 if (shsurf) {
5508 shsurf->saved_position_valid = false;
5509 shsurf->next_state.maximized = false;
5510 shsurf->next_state.fullscreen = false;
5511 shsurf->state_changed = true;
5512 }
5513}
5514
5515static void
5516shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5517{
5518 struct desktop_shell *shell = shell_output->shell;
5519 struct weston_output *output = shell_output->output;
5520 struct weston_layer *layer;
5521 struct weston_view *view;
5522
5523 /* Move all views in the layers owned by the shell */
5524 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5525 wl_list_for_each(view, &layer->view_list, layer_link) {
5526 if (view->output != output)
5527 continue;
5528
5529 shell_reposition_view_on_output_destroy(view);
5530 }
5531
5532 /* We don't start from the beggining of the layer list, so
5533 * make sure we don't wrap around it. */
5534 if (layer == &shell->background_layer)
5535 break;
5536 }
5537}
5538
5539static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005540handle_output_destroy(struct wl_listener *listener, void *data)
5541{
5542 struct shell_output *output_listener =
5543 container_of(listener, struct shell_output, destroy_listener);
5544
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005545 shell_reposition_views_on_output_destroy(output_listener);
5546
Xiong Zhang6b481422013-10-23 13:58:32 +08005547 wl_list_remove(&output_listener->destroy_listener.link);
5548 wl_list_remove(&output_listener->link);
5549 free(output_listener);
5550}
5551
5552static void
5553create_shell_output(struct desktop_shell *shell,
5554 struct weston_output *output)
5555{
5556 struct shell_output *shell_output;
5557
5558 shell_output = zalloc(sizeof *shell_output);
5559 if (shell_output == NULL)
5560 return;
5561
5562 shell_output->output = output;
5563 shell_output->shell = shell;
5564 shell_output->destroy_listener.notify = handle_output_destroy;
5565 wl_signal_add(&output->destroy_signal,
5566 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005567 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005568}
5569
5570static void
5571handle_output_create(struct wl_listener *listener, void *data)
5572{
5573 struct desktop_shell *shell =
5574 container_of(listener, struct desktop_shell, output_create_listener);
5575 struct weston_output *output = (struct weston_output *)data;
5576
5577 create_shell_output(shell, output);
5578}
5579
5580static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005581handle_output_move(struct wl_listener *listener, void *data)
5582{
5583 struct desktop_shell *shell;
5584 struct weston_output *output;
5585 struct weston_layer *layer;
5586 struct weston_view *view;
5587 float x, y;
5588
5589 shell = container_of(listener, struct desktop_shell,
5590 output_move_listener);
5591 output = data;
5592
5593 /* Move all views in the layers owned by the shell */
5594 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5595 wl_list_for_each(view, &layer->view_list, layer_link) {
5596 if (view->output != output)
5597 continue;
5598
5599 x = view->geometry.x + output->move_x;
5600 y = view->geometry.y + output->move_y;
5601 weston_view_set_position(view, x, y);
5602 }
5603
5604 /* We don't start from the beggining of the layer list, so
5605 * make sure we don't wrap around it. */
5606 if (layer == &shell->background_layer)
5607 break;
5608 }
5609}
5610
5611static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005612setup_output_destroy_handler(struct weston_compositor *ec,
5613 struct desktop_shell *shell)
5614{
5615 struct weston_output *output;
5616
5617 wl_list_init(&shell->output_list);
5618 wl_list_for_each(output, &ec->output_list, link)
5619 create_shell_output(shell, output);
5620
5621 shell->output_create_listener.notify = handle_output_create;
5622 wl_signal_add(&ec->output_created_signal,
5623 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005624
5625 shell->output_move_listener.notify = handle_output_move;
5626 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005627}
5628
5629static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005630shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005631{
Tiago Vignattibe143262012-04-16 17:31:41 +03005632 struct desktop_shell *shell =
5633 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005634 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005635 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005636
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005637 /* Force state to unlocked so we don't try to fade */
5638 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005639 if (shell->child.client)
5640 wl_client_destroy(shell->child.client);
5641
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005642 wl_list_remove(&shell->idle_listener.link);
5643 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005644
5645 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005646
Xiong Zhang6b481422013-10-23 13:58:32 +08005647 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5648 wl_list_remove(&shell_output->destroy_listener.link);
5649 wl_list_remove(&shell_output->link);
5650 free(shell_output);
5651 }
5652
5653 wl_list_remove(&shell->output_create_listener.link);
5654
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005655 wl_array_for_each(ws, &shell->workspaces.array)
5656 workspace_destroy(*ws);
5657 wl_array_release(&shell->workspaces.array);
5658
Pekka Paalanen3c647232011-12-22 13:43:43 +02005659 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005660 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005661 free(shell);
5662}
5663
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005664static void
5665shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5666{
5667 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005668 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005669
5670 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005671 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5672 MODIFIER_CTRL | MODIFIER_ALT,
5673 terminate_binding, ec);
5674 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5675 click_to_activate_binding,
5676 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005677 weston_compositor_add_touch_binding(ec, 0,
5678 touch_to_activate_binding,
5679 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005680 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5681 MODIFIER_SUPER | MODIFIER_ALT,
5682 surface_opacity_binding, NULL);
5683 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5684 MODIFIER_SUPER, zoom_axis_binding,
5685 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005686
5687 /* configurable bindings */
5688 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005689 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5690 zoom_key_binding, NULL);
5691 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5692 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005693 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5694 maximize_binding, NULL);
5695 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5696 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005697 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5698 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005699 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005700 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5701 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005702 weston_compositor_add_button_binding(ec, BTN_LEFT,
5703 mod | MODIFIER_SHIFT,
5704 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005705
5706 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5707 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5708 rotate_binding, NULL);
5709
Daniel Stone325fc2d2012-05-30 16:31:58 +01005710 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5711 shell);
5712 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5713 ec);
5714 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5715 backlight_binding, ec);
5716 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5717 ec);
5718 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5719 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005720 weston_compositor_add_key_binding(ec, KEY_K, mod,
5721 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005722 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5723 workspace_up_binding, shell);
5724 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5725 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005726 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5727 workspace_move_surface_up_binding,
5728 shell);
5729 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5730 workspace_move_surface_down_binding,
5731 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005732
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005733 if (shell->exposay_modifier)
5734 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5735 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005736
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005737 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5738 if (shell->workspaces.num > 1) {
5739 num_workspace_bindings = shell->workspaces.num;
5740 if (num_workspace_bindings > 6)
5741 num_workspace_bindings = 6;
5742 for (i = 0; i < num_workspace_bindings; i++)
5743 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5744 workspace_f_binding,
5745 shell);
5746 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005747
5748 /* Debug bindings */
5749 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5750 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005751}
5752
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005753WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005754module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005755 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005756{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005757 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005758 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005759 struct workspace **pws;
5760 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005761 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005762
Peter Huttererf3d62272013-08-08 11:57:05 +10005763 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005764 if (shell == NULL)
5765 return -1;
5766
Kristian Høgsberg75840622011-09-06 13:48:16 -04005767 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005768
5769 shell->destroy_listener.notify = shell_destroy;
5770 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005771 shell->idle_listener.notify = idle_handler;
5772 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5773 shell->wake_listener.notify = wake_handler;
5774 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005775
Scott Moreauff1db4a2012-04-17 19:06:18 -06005776 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005777 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005778 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005779 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005780 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005781 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005782 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005783 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005784 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005785 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005786 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005788 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5789 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005790 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5791 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005792 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005793
5794 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005795 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005796
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005797 if (input_panel_setup(shell) < 0)
5798 return -1;
5799
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005800 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005801
Daniel Stonedf8133b2013-11-19 11:37:14 +01005802 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5803 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5804
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005805 for (i = 0; i < shell->workspaces.num; i++) {
5806 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5807 if (pws == NULL)
5808 return -1;
5809
5810 *pws = workspace_create();
5811 if (*pws == NULL)
5812 return -1;
5813 }
5814 activate_workspace(shell, 0);
5815
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005816 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005817 wl_list_init(&shell->workspaces.animation.link);
5818 shell->workspaces.animation.frame = animate_workspace_change_frame;
5819
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005820 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005821 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005822 return -1;
5823
Rafael Antognollie2a34552013-12-03 15:35:45 -02005824 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5825 shell, bind_xdg_shell) == NULL)
5826 return -1;
5827
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005828 if (wl_global_create(ec->wl_display,
5829 &desktop_shell_interface, 2,
5830 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005831 return -1;
5832
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005833 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5834 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005835 return -1;
5836
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005837 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5838 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005839 return -1;
5840
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005841 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005842
Xiong Zhang6b481422013-10-23 13:58:32 +08005843 setup_output_destroy_handler(ec, shell);
5844
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005845 loop = wl_display_get_event_loop(ec->wl_display);
5846 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005847
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005848 shell->screensaver.timer =
5849 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5850
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005851 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005852 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005853 create_keyboard_focus_listener(seat);
5854 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005855
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005856 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005857
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005858 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005859
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005860 return 0;
5861}