blob: 2f8e61062f774f537278d67df38a154867023c26 [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;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct weston_touch_move_grab {
200 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800201 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700202 wl_fixed_t dx, dy;
203};
204
Pekka Paalanen460099f2012-01-20 16:48:25 +0200205struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500207 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200209 float x;
210 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 } center;
212};
213
Giulio Camuffo5085a752013-03-25 21:42:45 +0100214struct shell_seat {
215 struct weston_seat *seat;
216 struct wl_listener seat_destroy_listener;
217
218 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400219 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220 struct wl_list surfaces_list;
221 struct wl_client *client;
222 int32_t initial_up;
223 } popup_grab;
224};
225
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100226void
227set_alpha_if_fullscreen(struct shell_surface *shsurf)
228{
229 if (shsurf && shsurf->state.fullscreen)
230 shsurf->fullscreen.black_view->alpha = 0.25;
231}
232
Alex Wubd3354b2012-04-17 17:20:49 +0800233static struct desktop_shell *
234shell_surface_get_shell(struct shell_surface *shsurf);
235
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500236static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400237surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500238
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300239static void
240shell_fade_startup(struct desktop_shell *shell);
241
Philip Withnallbecb77e2013-11-25 18:01:30 +0000242static struct shell_seat *
243get_shell_seat(struct weston_seat *seat);
244
Philip Withnall648a4dd2013-11-25 18:01:44 +0000245static void
246shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
247
Alex Wubd3354b2012-04-17 17:20:49 +0800248static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200249shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
250
251static bool
252shell_surface_is_xdg_surface(struct shell_surface *shsurf);
253
254static bool
255shell_surface_is_xdg_popup(struct shell_surface *shsurf);
256
257static void
258shell_surface_set_parent(struct shell_surface *shsurf,
259 struct weston_surface *parent);
260
261static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800262shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
263{
264 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800266
267 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100268
Jason Ekstranda7af7042013-10-12 22:38:11 -0500269 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800270 return false;
271
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
273 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800274 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800276}
277
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500278static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280{
281 struct shell_grab *grab;
282
283 grab = container_of(listener, struct shell_grab,
284 shsurf_destroy_listener);
285
286 grab->shsurf = NULL;
287}
288
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800289struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500290get_default_view(struct weston_surface *surface)
291{
292 struct shell_surface *shsurf;
293 struct weston_view *view;
294
295 if (!surface || wl_list_empty(&surface->views))
296 return NULL;
297
298 shsurf = get_shell_surface(surface);
299 if (shsurf)
300 return shsurf->view;
301
302 wl_list_for_each(view, &surface->views, surface_link)
303 if (weston_view_is_mapped(view))
304 return view;
305
306 return container_of(surface->views.next, struct weston_view, surface_link);
307}
308
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300309static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400310popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400311
312static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300313shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300315 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct desktop_shell *shell = shsurf->shell;
320
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400321 popup_grab_end(pointer);
322
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323 grab->grab.interface = interface;
324 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400325 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500326 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400327 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700329 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400330 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400331 if (shell->child.desktop_shell) {
332 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
333 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 weston_pointer_set_focus(pointer,
335 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400336 wl_fixed_from_int(0),
337 wl_fixed_from_int(0));
338 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339}
340
341static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300342shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700344 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400345 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700346 grab->shsurf->grabbed = 0;
347 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700349 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350}
351
352static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700353shell_touch_grab_start(struct shell_touch_grab *grab,
354 const struct weston_touch_grab_interface *interface,
355 struct shell_surface *shsurf,
356 struct weston_touch *touch)
357{
358 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800359
Rusty Lynch1084da52013-08-15 09:10:08 -0700360 grab->grab.interface = interface;
361 grab->shsurf = shsurf;
362 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
363 wl_signal_add(&shsurf->destroy_signal,
364 &grab->shsurf_destroy_listener);
365
366 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700367 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700368
369 weston_touch_start_grab(touch, &grab->grab);
370 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371 weston_touch_set_focus(touch->seat,
372 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700373}
374
375static void
376shell_touch_grab_end(struct shell_touch_grab *grab)
377{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700379 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700380 grab->shsurf->grabbed = 0;
381 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700382
383 weston_touch_end_grab(grab->touch);
384}
385
386static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500387center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800388 struct weston_output *output);
389
Daniel Stone496ca172012-05-30 16:31:42 +0100390static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300391get_modifier(char *modifier)
392{
393 if (!modifier)
394 return MODIFIER_SUPER;
395
396 if (!strcmp("ctrl", modifier))
397 return MODIFIER_CTRL;
398 else if (!strcmp("alt", modifier))
399 return MODIFIER_ALT;
400 else if (!strcmp("super", modifier))
401 return MODIFIER_SUPER;
402 else
403 return MODIFIER_SUPER;
404}
405
Juan Zhaoe10d2792012-04-25 19:09:52 +0800406static enum animation_type
407get_animation_type(char *animation)
408{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800409 if (!animation)
410 return ANIMATION_NONE;
411
Juan Zhaoe10d2792012-04-25 19:09:52 +0800412 if (!strcmp("zoom", animation))
413 return ANIMATION_ZOOM;
414 else if (!strcmp("fade", animation))
415 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100416 else if (!strcmp("dim-layer", animation))
417 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800418 else
419 return ANIMATION_NONE;
420}
421
Alex Wu4539b082012-03-01 12:57:46 +0800422static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400423shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200424{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400425 struct weston_config_section *section;
426 int duration;
427 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200428
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400429 section = weston_config_get_section(shell->compositor->config,
430 "screensaver", NULL, NULL);
431 weston_config_section_get_string(section,
432 "path", &shell->screensaver.path, NULL);
433 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200434 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400435
436 section = weston_config_get_section(shell->compositor->config,
437 "shell", NULL, NULL);
438 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100439 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100440 shell->client = s;
441 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 "binding-modifier", &s, "super");
443 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200444 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800445
446 weston_config_section_get_string(section,
447 "exposay-modifier", &s, "none");
448 if (strcmp(s, "none") == 0)
449 shell->exposay_modifier = 0;
450 else
451 shell->exposay_modifier = get_modifier(s);
452 free(s);
453
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400454 weston_config_section_get_string(section, "animation", &s, "none");
455 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200456 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700457 weston_config_section_get_string(section,
458 "startup-animation", &s, "fade");
459 shell->startup_animation_type = get_animation_type(s);
460 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700461 if (shell->startup_animation_type == ANIMATION_ZOOM)
462 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100463 weston_config_section_get_string(section, "focus-animation", &s, "none");
464 shell->focus_animation_type = get_animation_type(s);
465 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466 weston_config_section_get_uint(section, "num-workspaces",
467 &shell->workspaces.num,
468 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200469}
470
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800471struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100472get_default_output(struct weston_compositor *compositor)
473{
474 return container_of(compositor->output_list.next,
475 struct weston_output, link);
476}
477
478
479/* no-op func for checking focus surface */
480static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600481focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100482{
483}
484
485static struct focus_surface *
486get_focus_surface(struct weston_surface *surface)
487{
488 if (surface->configure == focus_surface_configure)
489 return surface->configure_private;
490 else
491 return NULL;
492}
493
494static bool
495is_focus_surface (struct weston_surface *es)
496{
497 return (es->configure == focus_surface_configure);
498}
499
500static bool
501is_focus_view (struct weston_view *view)
502{
503 return is_focus_surface (view->surface);
504}
505
506static struct focus_surface *
507create_focus_surface(struct weston_compositor *ec,
508 struct weston_output *output)
509{
510 struct focus_surface *fsurf = NULL;
511 struct weston_surface *surface = NULL;
512
513 fsurf = malloc(sizeof *fsurf);
514 if (!fsurf)
515 return NULL;
516
517 fsurf->surface = weston_surface_create(ec);
518 surface = fsurf->surface;
519 if (surface == NULL) {
520 free(fsurf);
521 return NULL;
522 }
523
524 surface->configure = focus_surface_configure;
525 surface->output = output;
526 surface->configure_private = fsurf;
527
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800528 fsurf->view = weston_view_create(surface);
529 if (fsurf->view == NULL) {
530 weston_surface_destroy(surface);
531 free(fsurf);
532 return NULL;
533 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100534 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600536 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600537 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
539 pixman_region32_fini(&surface->opaque);
540 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
541 output->width, output->height);
542 pixman_region32_fini(&surface->input);
543 pixman_region32_init(&surface->input);
544
545 wl_list_init(&fsurf->workspace_transform.link);
546
547 return fsurf;
548}
549
550static void
551focus_surface_destroy(struct focus_surface *fsurf)
552{
553 weston_surface_destroy(fsurf->surface);
554 free(fsurf);
555}
556
557static void
558focus_animation_done(struct weston_view_animation *animation, void *data)
559{
560 struct workspace *ws = data;
561
562 ws->focus_animation = NULL;
563}
564
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200565static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200566focus_state_destroy(struct focus_state *state)
567{
568 wl_list_remove(&state->seat_destroy_listener.link);
569 wl_list_remove(&state->surface_destroy_listener.link);
570 free(state);
571}
572
573static void
574focus_state_seat_destroy(struct wl_listener *listener, void *data)
575{
576 struct focus_state *state = container_of(listener,
577 struct focus_state,
578 seat_destroy_listener);
579
580 wl_list_remove(&state->link);
581 focus_state_destroy(state);
582}
583
584static void
585focus_state_surface_destroy(struct wl_listener *listener, void *data)
586{
587 struct focus_state *state = container_of(listener,
588 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400589 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400590 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 struct weston_surface *main_surface, *next;
592 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200593
Pekka Paalanen01388e22013-04-25 13:57:44 +0300594 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
595
Kristian Høgsberge3778222012-07-31 17:29:30 -0400596 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
598 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400599 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100600 if (is_focus_view(view))
601 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400602
Jason Ekstranda7af7042013-10-12 22:38:11 -0500603 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400604 break;
605 }
606
Pekka Paalanen01388e22013-04-25 13:57:44 +0300607 /* if the focus was a sub-surface, activate its main surface */
608 if (main_surface != state->keyboard_focus)
609 next = main_surface;
610
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100611 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400612 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100613 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400614 activate(shell, next, state->seat);
615 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
617 if (state->ws->focus_animation)
618 weston_view_animation_destroy(state->ws->focus_animation);
619
620 state->ws->focus_animation = weston_fade_run(
621 state->ws->fsurf_front->view,
622 state->ws->fsurf_front->view->alpha, 0.0, 300,
623 focus_animation_done, state->ws);
624 }
625
Kristian Høgsberge3778222012-07-31 17:29:30 -0400626 wl_list_remove(&state->link);
627 focus_state_destroy(state);
628 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200629}
630
631static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400632focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200633{
Jonas Ådahl04769742012-06-13 00:01:24 +0200634 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200635
636 state = malloc(sizeof *state);
637 if (state == NULL)
638 return NULL;
639
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100640 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400641 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400643 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200644
645 state->seat_destroy_listener.notify = focus_state_seat_destroy;
646 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400647 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200648 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400649 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200650
651 return state;
652}
653
Jonas Ådahl8538b222012-08-29 22:13:03 +0200654static struct focus_state *
655ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
656{
657 struct workspace *ws = get_current_workspace(shell);
658 struct focus_state *state;
659
660 wl_list_for_each(state, &ws->focus_list, link)
661 if (state->seat == seat)
662 break;
663
664 if (&state->link == &ws->focus_list)
665 state = focus_state_create(seat, ws);
666
667 return state;
668}
669
Jonas Ådahl04769742012-06-13 00:01:24 +0200670static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400671restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200672{
673 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400674 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200675
676 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800677 if (state->seat->keyboard == NULL)
678 continue;
679
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400680 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200681
Kristian Høgsberge3148752013-05-06 23:19:49 -0400682 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200683 }
684}
685
686static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200687replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
688 struct weston_seat *seat)
689{
690 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400691 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200692
693 wl_list_for_each(state, &ws->focus_list, link) {
694 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400695 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500696 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200697 return;
698 }
699 }
700}
701
702static void
703drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
704 struct weston_surface *surface)
705{
706 struct focus_state *state;
707
708 wl_list_for_each(state, &ws->focus_list, link)
709 if (state->keyboard_focus == surface)
710 state->keyboard_focus = NULL;
711}
712
713static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100714animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
715 struct weston_view *from, struct weston_view *to)
716{
717 struct weston_output *output;
718 bool focus_surface_created = false;
719
720 /* FIXME: Only support dim animation using two layers */
721 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
722 return;
723
724 output = get_default_output(shell->compositor);
725 if (ws->fsurf_front == NULL && (from || to)) {
726 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800727 if (ws->fsurf_front == NULL)
728 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800730
731 ws->fsurf_back = create_focus_surface(shell->compositor, output);
732 if (ws->fsurf_back == NULL) {
733 focus_surface_destroy(ws->fsurf_front);
734 return;
735 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100736 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800737
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738 focus_surface_created = true;
739 } else {
740 wl_list_remove(&ws->fsurf_front->view->layer_link);
741 wl_list_remove(&ws->fsurf_back->view->layer_link);
742 }
743
744 if (ws->focus_animation) {
745 weston_view_animation_destroy(ws->focus_animation);
746 ws->focus_animation = NULL;
747 }
748
749 if (to)
750 wl_list_insert(&to->layer_link,
751 &ws->fsurf_front->view->layer_link);
752 else if (from)
753 wl_list_insert(&ws->layer.view_list,
754 &ws->fsurf_front->view->layer_link);
755
756 if (focus_surface_created) {
757 ws->focus_animation = weston_fade_run(
758 ws->fsurf_front->view,
759 ws->fsurf_front->view->alpha, 0.6, 300,
760 focus_animation_done, ws);
761 } else if (from) {
762 wl_list_insert(&from->layer_link,
763 &ws->fsurf_back->view->layer_link);
764 ws->focus_animation = weston_stable_fade_run(
765 ws->fsurf_front->view, 0.0,
766 ws->fsurf_back->view, 0.6,
767 focus_animation_done, ws);
768 } else if (to) {
769 wl_list_insert(&ws->layer.view_list,
770 &ws->fsurf_back->view->layer_link);
771 ws->focus_animation = weston_stable_fade_run(
772 ws->fsurf_front->view, 0.0,
773 ws->fsurf_back->view, 0.6,
774 focus_animation_done, ws);
775 }
776}
777
778static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200779workspace_destroy(struct workspace *ws)
780{
Jonas Ådahl04769742012-06-13 00:01:24 +0200781 struct focus_state *state, *next;
782
783 wl_list_for_each_safe(state, next, &ws->focus_list, link)
784 focus_state_destroy(state);
785
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100786 if (ws->fsurf_front)
787 focus_surface_destroy(ws->fsurf_front);
788 if (ws->fsurf_back)
789 focus_surface_destroy(ws->fsurf_back);
790
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200791 free(ws);
792}
793
Jonas Ådahl04769742012-06-13 00:01:24 +0200794static void
795seat_destroyed(struct wl_listener *listener, void *data)
796{
797 struct weston_seat *seat = data;
798 struct focus_state *state, *next;
799 struct workspace *ws = container_of(listener,
800 struct workspace,
801 seat_destroyed_listener);
802
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 if (state->seat == seat)
805 wl_list_remove(&state->link);
806}
807
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200808static struct workspace *
809workspace_create(void)
810{
811 struct workspace *ws = malloc(sizeof *ws);
812 if (ws == NULL)
813 return NULL;
814
815 weston_layer_init(&ws->layer, NULL);
816
Jonas Ådahl04769742012-06-13 00:01:24 +0200817 wl_list_init(&ws->focus_list);
818 wl_list_init(&ws->seat_destroyed_listener.link);
819 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100820 ws->fsurf_front = NULL;
821 ws->fsurf_back = NULL;
822 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824 return ws;
825}
826
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200827static int
828workspace_is_empty(struct workspace *ws)
829{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500830 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200831}
832
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200833static struct workspace *
834get_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200837 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838 pws += index;
839 return *pws;
840}
841
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800842struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200843get_current_workspace(struct desktop_shell *shell)
844{
845 return get_workspace(shell, shell->workspaces.current);
846}
847
848static void
849activate_workspace(struct desktop_shell *shell, unsigned int index)
850{
851 struct workspace *ws;
852
853 ws = get_workspace(shell, index);
854 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
855
856 shell->workspaces.current = index;
857}
858
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200859static unsigned int
860get_output_height(struct weston_output *output)
861{
862 return abs(output->region.extents.y1 - output->region.extents.y2);
863}
864
865static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200867{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 struct weston_transform *transform;
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 if (is_focus_view(view)) {
871 struct focus_surface *fsurf = get_focus_surface(view->surface);
872 transform = &fsurf->workspace_transform;
873 } else {
874 struct shell_surface *shsurf = get_shell_surface(view->surface);
875 transform = &shsurf->workspace_transform;
876 }
877
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200878 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100880 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 weston_matrix_init(&transform->matrix);
883 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200886}
887
888static void
889workspace_translate_out(struct workspace *ws, double fraction)
890{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200892 unsigned int height;
893 double d;
894
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
896 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897 d = height * fraction;
898
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100899 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200900 }
901}
902
903static void
904workspace_translate_in(struct workspace *ws, double fraction)
905{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200907 unsigned int height;
908 double d;
909
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
911 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912
913 if (fraction > 0)
914 d = -(height - height * fraction);
915 else
916 d = height + height * fraction;
917
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200919 }
920}
921
922static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200923broadcast_current_workspace_state(struct desktop_shell *shell)
924{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700925 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200926
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700927 wl_resource_for_each(resource, &shell->workspaces.client_list)
928 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200929 shell->workspaces.current,
930 shell->workspaces.num);
931}
932
933static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934reverse_workspace_change_animation(struct desktop_shell *shell,
935 unsigned int index,
936 struct workspace *from,
937 struct workspace *to)
938{
939 shell->workspaces.current = index;
940
941 shell->workspaces.anim_to = to;
942 shell->workspaces.anim_from = from;
943 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
944 shell->workspaces.anim_timestamp = 0;
945
Scott Moreau4272e632012-08-13 09:58:41 -0600946 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947}
948
949static void
950workspace_deactivate_transforms(struct workspace *ws)
951{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100956 if (is_focus_view(view)) {
957 struct focus_surface *fsurf = get_focus_surface(view->surface);
958 transform = &fsurf->workspace_transform;
959 } else {
960 struct shell_surface *shsurf = get_shell_surface(view->surface);
961 transform = &shsurf->workspace_transform;
962 }
963
964 if (!wl_list_empty(&transform->link)) {
965 wl_list_remove(&transform->link);
966 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200967 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200969 }
970}
971
972static void
973finish_workspace_change_animation(struct desktop_shell *shell,
974 struct workspace *from,
975 struct workspace *to)
976{
Scott Moreau4272e632012-08-13 09:58:41 -0600977 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
979 wl_list_remove(&shell->workspaces.animation.link);
980 workspace_deactivate_transforms(from);
981 workspace_deactivate_transforms(to);
982 shell->workspaces.anim_to = NULL;
983
984 wl_list_remove(&shell->workspaces.anim_from->layer.link);
985}
986
987static void
988animate_workspace_change_frame(struct weston_animation *animation,
989 struct weston_output *output, uint32_t msecs)
990{
991 struct desktop_shell *shell =
992 container_of(animation, struct desktop_shell,
993 workspaces.animation);
994 struct workspace *from = shell->workspaces.anim_from;
995 struct workspace *to = shell->workspaces.anim_to;
996 uint32_t t;
997 double x, y;
998
999 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1000 finish_workspace_change_animation(shell, from, to);
1001 return;
1002 }
1003
1004 if (shell->workspaces.anim_timestamp == 0) {
1005 if (shell->workspaces.anim_current == 0.0)
1006 shell->workspaces.anim_timestamp = msecs;
1007 else
1008 shell->workspaces.anim_timestamp =
1009 msecs -
1010 /* Invers of movement function 'y' below. */
1011 (asin(1.0 - shell->workspaces.anim_current) *
1012 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1013 M_2_PI);
1014 }
1015
1016 t = msecs - shell->workspaces.anim_timestamp;
1017
1018 /*
1019 * x = [0, π/2]
1020 * y(x) = sin(x)
1021 */
1022 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1023 y = sin(x);
1024
1025 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001026 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027
1028 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1029 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1030 shell->workspaces.anim_current = y;
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001034 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036}
1037
1038static void
1039animate_workspace_change(struct desktop_shell *shell,
1040 unsigned int index,
1041 struct workspace *from,
1042 struct workspace *to)
1043{
1044 struct weston_output *output;
1045
1046 int dir;
1047
1048 if (index > shell->workspaces.current)
1049 dir = -1;
1050 else
1051 dir = 1;
1052
1053 shell->workspaces.current = index;
1054
1055 shell->workspaces.anim_dir = dir;
1056 shell->workspaces.anim_from = from;
1057 shell->workspaces.anim_to = to;
1058 shell->workspaces.anim_current = 0.0;
1059 shell->workspaces.anim_timestamp = 0;
1060
1061 output = container_of(shell->compositor->output_list.next,
1062 struct weston_output, link);
1063 wl_list_insert(&output->animation_list,
1064 &shell->workspaces.animation.link);
1065
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001066 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067
1068 workspace_translate_in(to, 0);
1069
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001070 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001071
Scott Moreau4272e632012-08-13 09:58:41 -06001072 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073}
1074
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001075static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001076update_workspace(struct desktop_shell *shell, unsigned int index,
1077 struct workspace *from, struct workspace *to)
1078{
1079 shell->workspaces.current = index;
1080 wl_list_insert(&from->layer.link, &to->layer.link);
1081 wl_list_remove(&from->layer.link);
1082}
1083
1084static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001085change_workspace(struct desktop_shell *shell, unsigned int index)
1086{
1087 struct workspace *from;
1088 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001089 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001090
1091 if (index == shell->workspaces.current)
1092 return;
1093
1094 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001096 return;
1097
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001098 from = get_current_workspace(shell);
1099 to = get_workspace(shell, index);
1100
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 if (shell->workspaces.anim_from == to &&
1102 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001103 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001105 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 return;
1107 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001108
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109 if (shell->workspaces.anim_to != NULL)
1110 finish_workspace_change_animation(shell,
1111 shell->workspaces.anim_from,
1112 shell->workspaces.anim_to);
1113
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001114 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001115
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001116 if (shell->focus_animation_type != ANIMATION_NONE) {
1117 wl_list_for_each(state, &from->focus_list, link)
1118 if (state->keyboard_focus)
1119 animate_focus_change(shell, from,
1120 get_default_view(state->keyboard_focus), NULL);
1121
1122 wl_list_for_each(state, &to->focus_list, link)
1123 if (state->keyboard_focus)
1124 animate_focus_change(shell, to,
1125 NULL, get_default_view(state->keyboard_focus));
1126 }
1127
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001128 if (workspace_is_empty(to) && workspace_is_empty(from))
1129 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 else
1131 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001132
1133 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001134}
1135
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001136static bool
1137workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1138{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001140 struct wl_list *e;
1141
1142 if (wl_list_empty(list))
1143 return false;
1144
1145 e = list->next;
1146
1147 if (e->next != list)
1148 return false;
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001151}
1152
1153static void
Philip Withnall659163d2013-11-25 18:01:36 +00001154move_surface_to_workspace(struct desktop_shell *shell,
1155 struct shell_surface *shsurf,
1156 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001157{
1158 struct workspace *from;
1159 struct workspace *to;
1160 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001161 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001162 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001163
1164 if (workspace == shell->workspaces.current)
1165 return;
1166
Philip Withnall659163d2013-11-25 18:01:36 +00001167 view = get_default_view(shsurf->surface);
1168 if (!view)
1169 return;
1170
1171 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1172
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001173 if (workspace >= shell->workspaces.num)
1174 workspace = shell->workspaces.num - 1;
1175
Jonas Ådahle9d22502012-08-29 22:13:01 +02001176 from = get_current_workspace(shell);
1177 to = get_workspace(shell, workspace);
1178
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 wl_list_remove(&view->layer_link);
1180 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001181
Philip Withnall648a4dd2013-11-25 18:01:44 +00001182 shell_surface_update_child_surface_layers(shsurf);
1183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001185 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1186 if (!seat->keyboard)
1187 continue;
1188
1189 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001191 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001192 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001193
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001195}
1196
1197static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001199 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200 unsigned int index)
1201{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001202 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 struct shell_surface *shsurf;
1205 struct workspace *from;
1206 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001207 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208
Pekka Paalanen01388e22013-04-25 13:57:44 +03001209 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 view = get_default_view(surface);
1211 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001212 index == shell->workspaces.current ||
1213 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 return;
1215
1216 from = get_current_workspace(shell);
1217 to = get_workspace(shell, index);
1218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 wl_list_remove(&view->layer_link);
1220 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221
Philip Withnall659163d2013-11-25 18:01:36 +00001222 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001223 if (shsurf != NULL)
1224 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001225
Jonas Ådahle9d22502012-08-29 22:13:01 +02001226 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 drop_focus_state(shell, from, surface);
1228
1229 if (shell->workspaces.anim_from == to &&
1230 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001231 wl_list_remove(&to->layer.link);
1232 wl_list_insert(from->layer.link.prev, &to->layer.link);
1233
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001235 broadcast_current_workspace_state(shell);
1236
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001237 return;
1238 }
1239
1240 if (shell->workspaces.anim_to != NULL)
1241 finish_workspace_change_animation(shell,
1242 shell->workspaces.anim_from,
1243 shell->workspaces.anim_to);
1244
1245 if (workspace_is_empty(from) &&
1246 workspace_has_only(to, surface))
1247 update_workspace(shell, index, from, to);
1248 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001249 if (shsurf != NULL &&
1250 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251 wl_list_insert(&shell->workspaces.anim_sticky_list,
1252 &shsurf->workspace_transform.link);
1253
1254 animate_workspace_change(shell, index, from, to);
1255 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001256
1257 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001258
1259 state = ensure_focus_state(shell, seat);
1260 if (state != NULL)
1261 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001262}
1263
1264static void
1265workspace_manager_move_surface(struct wl_client *client,
1266 struct wl_resource *resource,
1267 struct wl_resource *surface_resource,
1268 uint32_t workspace)
1269{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001270 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001271 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001272 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001273 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001274 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275
Pekka Paalanen01388e22013-04-25 13:57:44 +03001276 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001277 shell_surface = get_shell_surface(main_surface);
1278 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001280
1281 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282}
1283
1284static const struct workspace_manager_interface workspace_manager_implementation = {
1285 workspace_manager_move_surface,
1286};
1287
1288static void
1289unbind_resource(struct wl_resource *resource)
1290{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001291 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001292}
1293
1294static void
1295bind_workspace_manager(struct wl_client *client,
1296 void *data, uint32_t version, uint32_t id)
1297{
1298 struct desktop_shell *shell = data;
1299 struct wl_resource *resource;
1300
Jason Ekstranda85118c2013-06-27 20:17:02 -05001301 resource = wl_resource_create(client,
1302 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303
1304 if (resource == NULL) {
1305 weston_log("couldn't add workspace manager object");
1306 return;
1307 }
1308
Jason Ekstranda85118c2013-06-27 20:17:02 -05001309 wl_resource_set_implementation(resource,
1310 &workspace_manager_implementation,
1311 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001312 wl_list_insert(&shell->workspaces.client_list,
1313 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001314
1315 workspace_manager_send_state(resource,
1316 shell->workspaces.current,
1317 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318}
1319
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001320static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001321touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1322 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1323{
1324}
1325
1326static void
1327touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1328{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001329 struct weston_touch_move_grab *move =
1330 (struct weston_touch_move_grab *) container_of(
1331 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001332
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001333 if (touch_id == 0)
1334 move->active = 0;
1335
Jonas Ådahl9484b692013-12-02 22:05:03 +01001336 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001337 shell_touch_grab_end(&move->base);
1338 free(move);
1339 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001340}
1341
1342static void
1343touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1344 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1345{
1346 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1347 struct shell_surface *shsurf = move->base.shsurf;
1348 struct weston_surface *es;
1349 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1350 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1351
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001352 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353 return;
1354
1355 es = shsurf->surface;
1356
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001357 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001358
1359 weston_compositor_schedule_repaint(es->compositor);
1360}
1361
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001362static void
1363touch_move_grab_cancel(struct weston_touch_grab *grab)
1364{
1365 struct weston_touch_move_grab *move =
1366 (struct weston_touch_move_grab *) container_of(
1367 grab, struct shell_touch_grab, grab);
1368
1369 shell_touch_grab_end(&move->base);
1370 free(move);
1371}
1372
Rusty Lynch1084da52013-08-15 09:10:08 -07001373static const struct weston_touch_grab_interface touch_move_grab_interface = {
1374 touch_move_grab_down,
1375 touch_move_grab_up,
1376 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001377 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001378};
1379
1380static int
1381surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1382{
1383 struct weston_touch_move_grab *move;
1384
1385 if (!shsurf)
1386 return -1;
1387
Rafael Antognolli03b16592013-12-03 15:35:42 -02001388 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001389 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001390 if (shsurf->grabbed)
1391 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001392
1393 move = malloc(sizeof *move);
1394 if (!move)
1395 return -1;
1396
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001397 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001399 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001401 seat->touch->grab_y;
1402
1403 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1404 seat->touch);
1405
1406 return 0;
1407}
1408
1409static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001410noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001411{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001412}
1413
1414static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001415move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1416 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001417{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001418 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001419 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001420 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001421 int dx, dy;
1422
1423 weston_pointer_move(pointer, x, y);
1424 dx = wl_fixed_to_int(pointer->x + move->dx);
1425 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001426
1427 if (!shsurf)
1428 return;
1429
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001430 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433}
1434
1435static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001436move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001437 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001438{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001439 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1440 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001441 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001442 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001443
Daniel Stone4dbadb12012-05-30 16:31:51 +01001444 if (pointer->button_count == 0 &&
1445 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001446 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001447 free(grab);
1448 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001449}
1450
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001451static void
1452move_grab_cancel(struct weston_pointer_grab *grab)
1453{
1454 struct shell_grab *shell_grab =
1455 container_of(grab, struct shell_grab, grab);
1456
1457 shell_grab_end(shell_grab);
1458 free(grab);
1459}
1460
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001461static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001462 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001463 move_grab_motion,
1464 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001465 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001466};
1467
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001468static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001469surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001470{
1471 struct weston_move_grab *move;
1472
1473 if (!shsurf)
1474 return -1;
1475
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001476 if (shsurf->grabbed)
1477 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001478 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001479 return 0;
1480
1481 move = malloc(sizeof *move);
1482 if (!move)
1483 return -1;
1484
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001486 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001488 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001489
1490 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001491 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001492
1493 return 0;
1494}
1495
1496static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001497common_surface_move(struct wl_resource *resource,
1498 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001500 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001501 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001502 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001503
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001504 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001505 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001506 seat->pointer->button_count > 0 &&
1507 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001509 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001510 (surface_move(shsurf, seat) < 0))
1511 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001512 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001513 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001514 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001516 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001517 (surface_touch_move(shsurf, seat) < 0))
1518 wl_resource_post_no_memory(resource);
1519 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001520}
1521
Rafael Antognollie2a34552013-12-03 15:35:45 -02001522static void
1523shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1524 struct wl_resource *seat_resource, uint32_t serial)
1525{
1526 common_surface_move(resource, seat_resource, serial);
1527}
1528
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001529struct weston_resize_grab {
1530 struct shell_grab base;
1531 uint32_t edges;
1532 int32_t width, height;
1533};
1534
1535static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001536resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1537 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538{
1539 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001540 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541 struct shell_surface *shsurf = resize->base.shsurf;
1542 int32_t width, height;
1543 wl_fixed_t from_x, from_y;
1544 wl_fixed_t to_x, to_y;
1545
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001546 weston_pointer_move(pointer, x, y);
1547
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548 if (!shsurf)
1549 return;
1550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 weston_view_from_global_fixed(shsurf->view,
1552 pointer->grab_x, pointer->grab_y,
1553 &from_x, &from_y);
1554 weston_view_from_global_fixed(shsurf->view,
1555 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556
1557 width = resize->width;
1558 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1559 width += wl_fixed_to_int(from_x - to_x);
1560 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1561 width += wl_fixed_to_int(to_x - from_x);
1562 }
1563
1564 height = resize->height;
1565 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1566 height += wl_fixed_to_int(from_y - to_y);
1567 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1568 height += wl_fixed_to_int(to_y - from_y);
1569 }
1570
1571 shsurf->client->send_configure(shsurf->surface,
1572 resize->edges, width, height);
1573}
1574
1575static void
1576send_configure(struct weston_surface *surface,
1577 uint32_t edges, int32_t width, int32_t height)
1578{
1579 struct shell_surface *shsurf = get_shell_surface(surface);
1580
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001581 assert(shsurf);
1582
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001583 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584 edges, width, height);
1585}
1586
1587static const struct weston_shell_client shell_client = {
1588 send_configure
1589};
1590
1591static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001592resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001593 uint32_t time, uint32_t button, uint32_t state_w)
1594{
1595 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001596 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 enum wl_pointer_button_state state = state_w;
1598
1599 if (pointer->button_count == 0 &&
1600 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1601 shell_grab_end(&resize->base);
1602 free(grab);
1603 }
1604}
1605
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001606static void
1607resize_grab_cancel(struct weston_pointer_grab *grab)
1608{
1609 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1610
1611 shell_grab_end(&resize->base);
1612 free(grab);
1613}
1614
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001615static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616 noop_grab_focus,
1617 resize_grab_motion,
1618 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001619 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001620};
1621
Giulio Camuffob8366642013-04-25 13:57:46 +03001622/*
1623 * Returns the bounding box of a surface and all its sub-surfaces,
1624 * in the surface coordinates system. */
1625static void
1626surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1627 int32_t *y, int32_t *w, int32_t *h) {
1628 pixman_region32_t region;
1629 pixman_box32_t *box;
1630 struct weston_subsurface *subsurface;
1631
1632 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 surface->width,
1634 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001635
1636 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1637 pixman_region32_union_rect(&region, &region,
1638 subsurface->position.x,
1639 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 subsurface->surface->width,
1641 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001642 }
1643
1644 box = pixman_region32_extents(&region);
1645 if (x)
1646 *x = box->x1;
1647 if (y)
1648 *y = box->y1;
1649 if (w)
1650 *w = box->x2 - box->x1;
1651 if (h)
1652 *h = box->y2 - box->y1;
1653
1654 pixman_region32_fini(&region);
1655}
1656
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657static int
1658surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001659 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660{
1661 struct weston_resize_grab *resize;
1662
Rafael Antognolli03b16592013-12-03 15:35:42 -02001663 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664 return 0;
1665
1666 if (edges == 0 || edges > 15 ||
1667 (edges & 3) == 3 || (edges & 12) == 12)
1668 return 0;
1669
1670 resize = malloc(sizeof *resize);
1671 if (!resize)
1672 return -1;
1673
1674 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001675 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1676 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677
1678 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001679 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680
1681 return 0;
1682}
1683
1684static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001685common_surface_resize(struct wl_resource *resource,
1686 struct wl_resource *seat_resource, uint32_t serial,
1687 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001689 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001690 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001691 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692
Rafael Antognolli03b16592013-12-03 15:35:42 -02001693 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001694 return;
1695
Kristian Høgsberge3148752013-05-06 23:19:49 -04001696 if (seat->pointer->button_count == 0 ||
1697 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001698 seat->pointer->focus == NULL)
1699 return;
1700
1701 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1702 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 return;
1704
Kristian Høgsberge3148752013-05-06 23:19:49 -04001705 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706 wl_resource_post_no_memory(resource);
1707}
1708
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001709static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001710shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1711 struct wl_resource *seat_resource, uint32_t serial,
1712 uint32_t edges)
1713{
1714 common_surface_resize(resource, seat_resource, serial, edges);
1715}
1716
1717static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001718end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1719
1720static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001721busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001722{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001723 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001724 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001726 wl_fixed_t sx, sy;
1727
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 view = weston_compositor_pick_view(pointer->seat->compositor,
1729 pointer->x, pointer->y,
1730 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001733 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001734}
1735
1736static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001737busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1738 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001739{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001740 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001742
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001744busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001745 uint32_t time, uint32_t button, uint32_t state)
1746{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001747 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001748 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001749 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001750
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001751 if (shsurf && button == BTN_LEFT && state) {
1752 activate(shsurf->shell, shsurf->surface, seat);
1753 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001754 } else if (shsurf && button == BTN_RIGHT && state) {
1755 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001756 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001757 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001758}
1759
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001760static void
1761busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1762{
1763 struct shell_grab *grab = (struct shell_grab *) base;
1764
1765 shell_grab_end(grab);
1766 free(grab);
1767}
1768
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001769static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001770 busy_cursor_grab_focus,
1771 busy_cursor_grab_motion,
1772 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001773 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774};
1775
1776static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001777set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778{
1779 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780
1781 grab = malloc(sizeof *grab);
1782 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001783 return;
1784
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001785 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1786 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001788
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001789static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001790end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001791{
1792 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1793
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001794 if (grab->grab.interface == &busy_cursor_grab_interface &&
1795 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001796 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001798 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001799}
1800
Scott Moreau9521d5e2012-04-19 13:06:17 -06001801static void
1802ping_timer_destroy(struct shell_surface *shsurf)
1803{
1804 if (!shsurf || !shsurf->ping_timer)
1805 return;
1806
1807 if (shsurf->ping_timer->source)
1808 wl_event_source_remove(shsurf->ping_timer->source);
1809
1810 free(shsurf->ping_timer);
1811 shsurf->ping_timer = NULL;
1812}
1813
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001814static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001815ping_timeout_handler(void *data)
1816{
1817 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001819
Scott Moreau9521d5e2012-04-19 13:06:17 -06001820 /* Client is not responding */
1821 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822
1823 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001824 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001825 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001826 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001827
1828 return 1;
1829}
1830
1831static void
1832ping_handler(struct weston_surface *surface, uint32_t serial)
1833{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001834 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001835 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001837
1838 if (!shsurf)
1839 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001840 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001841 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001842
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001843 if (shsurf->surface == shsurf->shell->grab_surface)
1844 return;
1845
Scott Moreauff1db4a2012-04-17 19:06:18 -06001846 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001847 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001848 if (!shsurf->ping_timer)
1849 return;
1850
1851 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001852 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1853 shsurf->ping_timer->source =
1854 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1855 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1856
Rafael Antognollie2a34552013-12-03 15:35:45 -02001857 if (shell_surface_is_wl_shell_surface(shsurf))
1858 wl_shell_surface_send_ping(shsurf->resource, serial);
1859 else if (shell_surface_is_xdg_surface(shsurf))
1860 xdg_surface_send_ping(shsurf->resource, serial);
1861 else if (shell_surface_is_xdg_popup(shsurf))
1862 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001863 }
1864}
1865
1866static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001867handle_pointer_focus(struct wl_listener *listener, void *data)
1868{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001869 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001871 struct weston_compositor *compositor;
1872 struct shell_surface *shsurf;
1873 uint32_t serial;
1874
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876 return;
1877
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 compositor = view->surface->compositor;
1879 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001881 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001882 set_busy_cursor(shsurf, pointer);
1883 } else {
1884 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001886 }
1887}
1888
1889static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001890create_pointer_focus_listener(struct weston_seat *seat)
1891{
1892 struct wl_listener *listener;
1893
Kristian Høgsberge3148752013-05-06 23:19:49 -04001894 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001895 return;
1896
1897 listener = malloc(sizeof *listener);
1898 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001899 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001900}
1901
1902static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001903xdg_surface_set_transient_for(struct wl_client *client,
1904 struct wl_resource *resource,
1905 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001907 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001908 struct weston_surface *parent;
1909
1910 if (parent_resource)
1911 parent = wl_resource_get_user_data(parent_resource);
1912 else
1913 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001914
1915 shell_surface_set_parent(shsurf, parent);
1916}
1917
1918static void
1919surface_pong(struct shell_surface *shsurf, uint32_t serial)
1920{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001921 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001922 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001923
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001924 if (shsurf->ping_timer == NULL)
1925 /* Just ignore unsolicited pong. */
1926 return;
1927
Scott Moreauff1db4a2012-04-17 19:06:18 -06001928 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001929 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001930 wl_list_for_each(seat, &ec->seat_list, link) {
1931 if(seat->pointer)
1932 end_busy_cursor(shsurf, seat->pointer);
1933 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001934 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001935 }
1936}
1937
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001938static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001939shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1940 uint32_t serial)
1941{
1942 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1943
1944 surface_pong(shsurf, serial);
1945
1946}
1947
1948static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001949set_title(struct shell_surface *shsurf, const char *title)
1950{
1951 free(shsurf->title);
1952 shsurf->title = strdup(title);
1953}
1954
1955static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001956shell_surface_set_title(struct wl_client *client,
1957 struct wl_resource *resource, const char *title)
1958{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001959 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001960
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001961 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001962}
1963
1964static void
1965shell_surface_set_class(struct wl_client *client,
1966 struct wl_resource *resource, const char *class)
1967{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001968 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001969
1970 free(shsurf->class);
1971 shsurf->class = strdup(class);
1972}
1973
Alex Wu4539b082012-03-01 12:57:46 +08001974static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001975restore_output_mode(struct weston_output *output)
1976{
Hardening57388e42013-09-18 23:56:36 +02001977 if (output->current_mode != output->original_mode ||
1978 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001979 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001980 output->original_mode,
1981 output->original_scale,
1982 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001983}
1984
1985static void
1986restore_all_output_modes(struct weston_compositor *compositor)
1987{
1988 struct weston_output *output;
1989
1990 wl_list_for_each(output, &compositor->output_list, link)
1991 restore_output_mode(output);
1992}
1993
Philip Withnallbecb77e2013-11-25 18:01:30 +00001994static int
1995get_output_panel_height(struct desktop_shell *shell,
1996 struct weston_output *output)
1997{
1998 struct weston_view *view;
1999 int panel_height = 0;
2000
2001 if (!output)
2002 return 0;
2003
2004 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2005 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002006 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002007 break;
2008 }
2009 }
2010
2011 return panel_height;
2012}
2013
Philip Withnall07926d92013-11-25 18:01:40 +00002014/* The surface will be inserted into the list immediately after the link
2015 * returned by this function (i.e. will be stacked immediately above the
2016 * returned link). */
2017static struct wl_list *
2018shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2019{
2020 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002021 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002022
2023 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002024 case SHELL_SURFACE_POPUP:
2025 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002026 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002027 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002028 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002029 /* Move the surface to its parent layer so
2030 * that surfaces which are transient for
2031 * fullscreen surfaces don't get hidden by the
2032 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002033
2034 /* TODO: Handle a parent with multiple views */
2035 parent = get_default_view(shsurf->parent);
2036 if (parent)
2037 return parent->layer_link.prev;
2038 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002039 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002040
2041 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002042 return &shsurf->shell->fullscreen_layer.view_list;
2043
Philip Withnall07926d92013-11-25 18:01:40 +00002044 case SHELL_SURFACE_NONE:
2045 default:
2046 /* Go to the fallback, below. */
2047 break;
2048 }
2049
2050 /* Move the surface to a normal workspace layer so that surfaces
2051 * which were previously fullscreen or transient are no longer
2052 * rendered on top. */
2053 ws = get_current_workspace(shsurf->shell);
2054 return &ws->layer.view_list;
2055}
2056
Philip Withnall648a4dd2013-11-25 18:01:44 +00002057static void
2058shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2059{
2060 struct shell_surface *child;
2061
2062 /* Move the child layers to the same workspace as shsurf. They will be
2063 * stacked above shsurf. */
2064 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2065 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2066 weston_view_geometry_dirty(child->view);
2067 wl_list_remove(&child->view->layer_link);
2068 wl_list_insert(shsurf->view->layer_link.prev,
2069 &child->view->layer_link);
2070 weston_view_geometry_dirty(child->view);
2071 weston_surface_damage(child->surface);
2072
2073 /* Recurse. We don’t expect this to recurse very far (if
2074 * at all) because that would imply we have transient
2075 * (or popup) children of transient surfaces, which
2076 * would be unusual. */
2077 shell_surface_update_child_surface_layers(child);
2078 }
2079 }
2080}
2081
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002082/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002083 * geometry to ensure the changes are redrawn.
2084 *
2085 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2086 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002087static void
2088shell_surface_update_layer(struct shell_surface *shsurf)
2089{
2090 struct wl_list *new_layer_link;
2091
2092 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2093
2094 if (new_layer_link == &shsurf->view->layer_link)
2095 return;
2096
2097 weston_view_geometry_dirty(shsurf->view);
2098 wl_list_remove(&shsurf->view->layer_link);
2099 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2100 weston_view_geometry_dirty(shsurf->view);
2101 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002102
2103 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002104}
2105
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002106static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002107shell_surface_set_parent(struct shell_surface *shsurf,
2108 struct weston_surface *parent)
2109{
2110 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002111
2112 wl_list_remove(&shsurf->children_link);
2113 wl_list_init(&shsurf->children_link);
2114
2115 /* Insert into the parent surface’s child list. */
2116 if (parent != NULL) {
2117 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2118 if (parent_shsurf != NULL)
2119 wl_list_insert(&parent_shsurf->children_list,
2120 &shsurf->children_link);
2121 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002122}
2123
2124static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002125shell_surface_set_output(struct shell_surface *shsurf,
2126 struct weston_output *output)
2127{
2128 struct weston_surface *es = shsurf->surface;
2129
2130 /* get the default output, if the client set it as NULL
2131 check whether the ouput is available */
2132 if (output)
2133 shsurf->output = output;
2134 else if (es->output)
2135 shsurf->output = es->output;
2136 else
2137 shsurf->output = get_default_output(es->compositor);
2138}
2139
2140static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002141surface_clear_next_states(struct shell_surface *shsurf)
2142{
2143 shsurf->next_state.maximized = false;
2144 shsurf->next_state.fullscreen = false;
2145
2146 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2147 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2148 shsurf->state_changed = true;
2149}
2150
2151static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002152set_toplevel(struct shell_surface *shsurf)
2153{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002154 shell_surface_set_parent(shsurf, NULL);
2155 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002156 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002157
2158 /* The layer_link is updated in set_surface_type(),
2159 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002160}
2161
2162static void
2163shell_surface_set_toplevel(struct wl_client *client,
2164 struct wl_resource *resource)
2165{
2166 struct shell_surface *surface = wl_resource_get_user_data(resource);
2167
2168 set_toplevel(surface);
2169}
2170
2171static void
2172set_transient(struct shell_surface *shsurf,
2173 struct weston_surface *parent, int x, int y, uint32_t flags)
2174{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002175 assert(parent != NULL);
2176
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002177 shell_surface_set_parent(shsurf, parent);
2178
2179 surface_clear_next_states(shsurf);
2180
Philip Withnallbecb77e2013-11-25 18:01:30 +00002181 shsurf->transient.x = x;
2182 shsurf->transient.y = y;
2183 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002184
Rafael Antognollied207b42013-12-03 15:35:43 -02002185 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002186 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002187 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002188
2189 /* The layer_link is updated in set_surface_type(),
2190 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002191}
2192
2193static void
2194shell_surface_set_transient(struct wl_client *client,
2195 struct wl_resource *resource,
2196 struct wl_resource *parent_resource,
2197 int x, int y, uint32_t flags)
2198{
2199 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2200 struct weston_surface *parent =
2201 wl_resource_get_user_data(parent_resource);
2202
2203 set_transient(shsurf, parent, x, y, flags);
2204}
2205
2206static void
2207set_fullscreen(struct shell_surface *shsurf,
2208 uint32_t method,
2209 uint32_t framerate,
2210 struct weston_output *output)
2211{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002212 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002213
2214 shsurf->fullscreen_output = shsurf->output;
2215 shsurf->fullscreen.type = method;
2216 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002217
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002218 shsurf->next_state.fullscreen = true;
2219 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002220 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002221
2222 shsurf->client->send_configure(shsurf->surface, 0,
2223 shsurf->output->width,
2224 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002225
2226 /* The layer_link is updated in set_surface_type(),
2227 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002228}
2229
2230static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002231weston_view_set_initial_position(struct weston_view *view,
2232 struct desktop_shell *shell);
2233
2234static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002235unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002236{
Philip Withnallf85fe842013-11-25 18:01:37 +00002237 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002238 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2239 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002240 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002241 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002242 shsurf->fullscreen_output = NULL;
2243
Alex Wu4539b082012-03-01 12:57:46 +08002244 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2245 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002246
Alex Wu4539b082012-03-01 12:57:46 +08002247 wl_list_remove(&shsurf->fullscreen.transform.link);
2248 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002249
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250 if (shsurf->fullscreen.black_view)
2251 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2252 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002253
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002254 if (shsurf->saved_position_valid)
2255 weston_view_set_position(shsurf->view,
2256 shsurf->saved_x, shsurf->saved_y);
2257 else
2258 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2259
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002260 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002261 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002262 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002263 shsurf->saved_rotation_valid = false;
2264 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002265
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002266 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002267}
2268
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002269static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002270shell_surface_set_fullscreen(struct wl_client *client,
2271 struct wl_resource *resource,
2272 uint32_t method,
2273 uint32_t framerate,
2274 struct wl_resource *output_resource)
2275{
2276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2277 struct weston_output *output;
2278
2279 if (output_resource)
2280 output = wl_resource_get_user_data(output_resource);
2281 else
2282 output = NULL;
2283
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002284 shell_surface_set_parent(shsurf, NULL);
2285
Rafael Antognolli03b16592013-12-03 15:35:42 -02002286 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287 set_fullscreen(shsurf, method, framerate, output);
2288}
2289
2290static void
2291set_popup(struct shell_surface *shsurf,
2292 struct weston_surface *parent,
2293 struct weston_seat *seat,
2294 uint32_t serial,
2295 int32_t x,
2296 int32_t y)
2297{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002298 assert(parent != NULL);
2299
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300 shsurf->popup.shseat = get_shell_seat(seat);
2301 shsurf->popup.serial = serial;
2302 shsurf->popup.x = x;
2303 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002304
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002305 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002306}
2307
2308static void
2309shell_surface_set_popup(struct wl_client *client,
2310 struct wl_resource *resource,
2311 struct wl_resource *seat_resource,
2312 uint32_t serial,
2313 struct wl_resource *parent_resource,
2314 int32_t x, int32_t y, uint32_t flags)
2315{
2316 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002317 struct weston_surface *parent =
2318 wl_resource_get_user_data(parent_resource);
2319
2320 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002321
Rafael Antognolli03b16592013-12-03 15:35:42 -02002322 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002323 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002324 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002325 wl_resource_get_user_data(seat_resource),
2326 serial, x, y);
2327}
2328
2329static void
2330set_maximized(struct shell_surface *shsurf,
2331 struct weston_output *output)
2332{
2333 struct desktop_shell *shell;
2334 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002335
Philip Withnall352e7ed2013-11-25 18:01:35 +00002336 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002337
2338 shell = shell_surface_get_shell(shsurf);
2339 panel_height = get_output_panel_height(shell, shsurf->output);
2340 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2341
2342 shsurf->client->send_configure(shsurf->surface, edges,
2343 shsurf->output->width,
2344 shsurf->output->height - panel_height);
2345
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002346 shsurf->next_state.maximized = true;
2347 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002348 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002349}
2350
2351static void
2352unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002353{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002354 /* undo all maximized things here */
2355 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002356
2357 if (shsurf->saved_position_valid)
2358 weston_view_set_position(shsurf->view,
2359 shsurf->saved_x, shsurf->saved_y);
2360 else
2361 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002362
2363 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002364 wl_list_insert(&shsurf->view->geometry.transformation_list,
2365 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002366 shsurf->saved_rotation_valid = false;
2367 }
2368
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002369 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002370}
2371
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372static void
2373shell_surface_set_maximized(struct wl_client *client,
2374 struct wl_resource *resource,
2375 struct wl_resource *output_resource)
2376{
2377 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2378 struct weston_output *output;
2379
2380 if (output_resource)
2381 output = wl_resource_get_user_data(output_resource);
2382 else
2383 output = NULL;
2384
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002385 shell_surface_set_parent(shsurf, NULL);
2386
Rafael Antognolli03b16592013-12-03 15:35:42 -02002387 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002388 set_maximized(shsurf, output);
2389}
2390
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002391/* This is only ever called from set_surface_type(), so there’s no need to
2392 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002393static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002394reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002395{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002396 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002397 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002398 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002400
Pekka Paalanen98262232011-12-01 10:42:22 +02002401 return 0;
2402}
2403
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002404static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002405set_full_output(struct shell_surface *shsurf)
2406{
2407 shsurf->saved_x = shsurf->view->geometry.x;
2408 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002409 shsurf->saved_width = shsurf->surface->width;
2410 shsurf->saved_height = shsurf->surface->height;
2411 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002412 shsurf->saved_position_valid = true;
2413
2414 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2415 wl_list_remove(&shsurf->rotation.transform.link);
2416 wl_list_init(&shsurf->rotation.transform.link);
2417 weston_view_geometry_dirty(shsurf->view);
2418 shsurf->saved_rotation_valid = true;
2419 }
2420}
2421
2422static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002423set_surface_type(struct shell_surface *shsurf)
2424{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002425 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002427
Philip Withnallbecb77e2013-11-25 18:01:30 +00002428 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002429
Rafael Antognolli03b16592013-12-03 15:35:42 -02002430 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002431 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002432
2433 switch (shsurf->type) {
2434 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002435 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002436 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002437 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002438 weston_view_set_position(shsurf->view,
2439 pev->geometry.x + shsurf->transient.x,
2440 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002441 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002442 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002443
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002444 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445 weston_view_set_position(shsurf->view, shsurf->transient.x,
2446 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002447 break;
2448
Philip Withnall0f640e22013-11-25 18:01:31 +00002449 case SHELL_SURFACE_POPUP:
2450 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002451 default:
2452 break;
2453 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002454
2455 /* Update the surface’s layer. */
2456 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002457}
2458
Tiago Vignattibe143262012-04-16 17:31:41 +03002459static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002460shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002461{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002462 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002463}
2464
Alex Wu21858432012-04-01 20:13:08 +08002465static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002466black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002467
Jason Ekstranda7af7042013-10-12 22:38:11 -05002468static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002469create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002470 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002471 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002472{
2473 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002474 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002475
2476 surface = weston_surface_create(ec);
2477 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002478 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002479 return NULL;
2480 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481 view = weston_view_create(surface);
2482 if (surface == NULL) {
2483 weston_log("no memory\n");
2484 weston_surface_destroy(surface);
2485 return NULL;
2486 }
Alex Wu4539b082012-03-01 12:57:46 +08002487
Alex Wu21858432012-04-01 20:13:08 +08002488 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002489 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002490 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002491 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002492 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002493 pixman_region32_fini(&surface->input);
2494 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002495
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002496 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002497 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002498
2499 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002500}
2501
Philip Withnalled8f1a92013-11-25 18:01:43 +00002502static void
2503shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2504{
2505 struct weston_output *output = shsurf->fullscreen_output;
2506
Rafael Antognolli03b16592013-12-03 15:35:42 -02002507 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002508
2509 if (!shsurf->fullscreen.black_view)
2510 shsurf->fullscreen.black_view =
2511 create_black_surface(shsurf->surface->compositor,
2512 shsurf->surface,
2513 output->x, output->y,
2514 output->width,
2515 output->height);
2516
2517 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2518 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2519 wl_list_insert(&shsurf->view->layer_link,
2520 &shsurf->fullscreen.black_view->layer_link);
2521 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2522 weston_surface_damage(shsurf->surface);
2523}
2524
Alex Wu4539b082012-03-01 12:57:46 +08002525/* Create black surface and append it to the associated fullscreen surface.
2526 * Handle size dismatch and positioning according to the method. */
2527static void
2528shell_configure_fullscreen(struct shell_surface *shsurf)
2529{
2530 struct weston_output *output = shsurf->fullscreen_output;
2531 struct weston_surface *surface = shsurf->surface;
2532 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002533 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002534 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002535
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002536 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2537 restore_output_mode(output);
2538
Philip Withnalled8f1a92013-11-25 18:01:43 +00002539 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002540
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002542 &surf_width, &surf_height);
2543
Alex Wu4539b082012-03-01 12:57:46 +08002544 switch (shsurf->fullscreen.type) {
2545 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002546 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002548 break;
2549 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002550 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002551 if (output->width == surf_width &&
2552 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002553 weston_view_set_position(shsurf->view,
2554 output->x - surf_x,
2555 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002556 break;
2557 }
2558
Alex Wu4539b082012-03-01 12:57:46 +08002559 matrix = &shsurf->fullscreen.transform.matrix;
2560 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002561
Scott Moreau1bad5db2012-08-18 01:04:05 -06002562 output_aspect = (float) output->width /
2563 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002564 /* XXX: Use surf_width and surf_height here? */
2565 surface_aspect = (float) surface->width /
2566 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002567 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002568 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002569 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002570 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002571 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002572 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002573
Alex Wu4539b082012-03-01 12:57:46 +08002574 weston_matrix_scale(matrix, scale, scale, 1);
2575 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002577 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002578 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2579 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002581
Alex Wu4539b082012-03-01 12:57:46 +08002582 break;
2583 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002584 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002585 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002586 surf_width * surface->buffer_viewport.scale,
2587 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002588 shsurf->fullscreen.framerate};
2589
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002590 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002591 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 weston_view_set_position(shsurf->view,
2593 output->x - surf_x,
2594 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002595 shsurf->fullscreen.black_view->surface->width = output->width;
2596 shsurf->fullscreen.black_view->surface->height = output->height;
2597 weston_view_set_position(shsurf->fullscreen.black_view,
2598 output->x - surf_x,
2599 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002600 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002601 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002602 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002603 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002604 }
Alex Wubd3354b2012-04-17 17:20:49 +08002605 }
Alex Wu4539b082012-03-01 12:57:46 +08002606 break;
2607 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002609 break;
2610 default:
2611 break;
2612 }
2613}
2614
Alex Wu4539b082012-03-01 12:57:46 +08002615static void
2616shell_map_fullscreen(struct shell_surface *shsurf)
2617{
Alex Wubd3354b2012-04-17 17:20:49 +08002618 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002619}
2620
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002621static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002622set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2623{
2624 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002625 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002626 shsurf->transient.x = x;
2627 shsurf->transient.y = y;
2628 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002629
2630 shell_surface_set_parent(shsurf, NULL);
2631
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002632 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002633 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002634}
2635
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002636static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002637
2638static void
2639destroy_shell_seat(struct wl_listener *listener, void *data)
2640{
2641 struct shell_seat *shseat =
2642 container_of(listener,
2643 struct shell_seat, seat_destroy_listener);
2644 struct shell_surface *shsurf, *prev = NULL;
2645
2646 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002647 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002648 shseat->popup_grab.client = NULL;
2649
2650 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2651 shsurf->popup.shseat = NULL;
2652 if (prev) {
2653 wl_list_init(&prev->popup.grab_link);
2654 }
2655 prev = shsurf;
2656 }
2657 wl_list_init(&prev->popup.grab_link);
2658 }
2659
2660 wl_list_remove(&shseat->seat_destroy_listener.link);
2661 free(shseat);
2662}
2663
2664static struct shell_seat *
2665create_shell_seat(struct weston_seat *seat)
2666{
2667 struct shell_seat *shseat;
2668
2669 shseat = calloc(1, sizeof *shseat);
2670 if (!shseat) {
2671 weston_log("no memory to allocate shell seat\n");
2672 return NULL;
2673 }
2674
2675 shseat->seat = seat;
2676 wl_list_init(&shseat->popup_grab.surfaces_list);
2677
2678 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2679 wl_signal_add(&seat->destroy_signal,
2680 &shseat->seat_destroy_listener);
2681
2682 return shseat;
2683}
2684
2685static struct shell_seat *
2686get_shell_seat(struct weston_seat *seat)
2687{
2688 struct wl_listener *listener;
2689
2690 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2691 if (listener == NULL)
2692 return create_shell_seat(seat);
2693
2694 return container_of(listener,
2695 struct shell_seat, seat_destroy_listener);
2696}
2697
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002698static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002699popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002701 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002702 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002703 struct shell_seat *shseat =
2704 container_of(grab, struct shell_seat, popup_grab.grab);
2705 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002706 wl_fixed_t sx, sy;
2707
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 view = weston_compositor_pick_view(pointer->seat->compositor,
2709 pointer->x, pointer->y,
2710 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002711
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 if (view && view->surface->resource &&
2713 wl_resource_get_client(view->surface->resource) == client) {
2714 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002715 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002716 weston_pointer_set_focus(pointer, NULL,
2717 wl_fixed_from_int(0),
2718 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002719 }
2720}
2721
2722static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002723popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2724 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002725{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002726 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002727 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002728 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002729
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002730 weston_pointer_move(pointer, x, y);
2731
Neil Roberts96d790e2013-09-19 17:32:00 +01002732 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 weston_view_from_global_fixed(pointer->focus,
2734 pointer->x, pointer->y,
2735 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002736 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002737 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738}
2739
2740static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002741popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002742 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002743{
2744 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002745 struct shell_seat *shseat =
2746 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002747 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002748 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002749 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002750 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002751
Neil Roberts96d790e2013-09-19 17:32:00 +01002752 resource_list = &grab->pointer->focus_resource_list;
2753 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002754 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002755 wl_resource_for_each(resource, resource_list) {
2756 wl_pointer_send_button(resource, serial,
2757 time, button, state);
2758 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002759 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002760 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002761 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002762 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002763 }
2764
Daniel Stone4dbadb12012-05-30 16:31:51 +01002765 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002766 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002767}
2768
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002769static void
2770popup_grab_cancel(struct weston_pointer_grab *grab)
2771{
2772 popup_grab_end(grab->pointer);
2773}
2774
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002775static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002776 popup_grab_focus,
2777 popup_grab_motion,
2778 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002779 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002780};
2781
2782static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002783shell_surface_send_popup_done(struct shell_surface *shsurf)
2784{
2785 if (shell_surface_is_wl_shell_surface(shsurf))
2786 wl_shell_surface_send_popup_done(shsurf->resource);
2787 else if (shell_surface_is_xdg_popup(shsurf))
2788 xdg_popup_send_popup_done(shsurf->resource,
2789 shsurf->popup.serial);
2790}
2791
2792static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002793popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002794{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002795 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002796 struct shell_seat *shseat =
2797 container_of(grab, struct shell_seat, popup_grab.grab);
2798 struct shell_surface *shsurf;
2799 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002800
2801 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002802 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002803 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002804 shseat->popup_grab.grab.interface = NULL;
2805 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002806 /* Send the popup_done event to all the popups open */
2807 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002808 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 shsurf->popup.shseat = NULL;
2810 if (prev) {
2811 wl_list_init(&prev->popup.grab_link);
2812 }
2813 prev = shsurf;
2814 }
2815 wl_list_init(&prev->popup.grab_link);
2816 wl_list_init(&shseat->popup_grab.surfaces_list);
2817 }
2818}
2819
2820static void
2821add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2822{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002823 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002824
2825 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002826 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002827 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002828 /* We must make sure here that this popup was opened after
2829 * a mouse press, and not just by moving around with other
2830 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002831 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002832 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002833
Rob Bradforddfe31052013-06-26 19:49:11 +01002834 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002835 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002836 } else {
2837 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002838 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002839}
2840
2841static void
2842remove_popup_grab(struct shell_surface *shsurf)
2843{
2844 struct shell_seat *shseat = shsurf->popup.shseat;
2845
2846 wl_list_remove(&shsurf->popup.grab_link);
2847 wl_list_init(&shsurf->popup.grab_link);
2848 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002849 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002850 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002851 }
2852}
2853
2854static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002855shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002857 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002859
Jason Ekstranda7af7042013-10-12 22:38:11 -05002860 shsurf->surface->output = parent_view->output;
2861 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002862
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 weston_view_set_transform_parent(shsurf->view, parent_view);
2864 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2865 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866
Kristian Høgsberge3148752013-05-06 23:19:49 -04002867 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002868 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002869 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002870 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002871 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002872 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002873}
2874
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002875static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002876 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002877 shell_surface_move,
2878 shell_surface_resize,
2879 shell_surface_set_toplevel,
2880 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002881 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002882 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002883 shell_surface_set_maximized,
2884 shell_surface_set_title,
2885 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002886};
2887
2888static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002889destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002890{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002891 struct shell_surface *child, *next;
2892
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002893 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2894
Giulio Camuffo5085a752013-03-25 21:42:45 +01002895 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2896 remove_popup_grab(shsurf);
2897 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002898
Alex Wubd3354b2012-04-17 17:20:49 +08002899 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002900 shell_surface_is_top_fullscreen(shsurf))
2901 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002902
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 if (shsurf->fullscreen.black_view)
2904 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002905
Alex Wubd3354b2012-04-17 17:20:49 +08002906 /* As destroy_resource() use wl_list_for_each_safe(),
2907 * we can always remove the listener.
2908 */
2909 wl_list_remove(&shsurf->surface_destroy_listener.link);
2910 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002911 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002912 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002913
Jason Ekstranda7af7042013-10-12 22:38:11 -05002914 weston_view_destroy(shsurf->view);
2915
Philip Withnall648a4dd2013-11-25 18:01:44 +00002916 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002917 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2918 wl_list_remove(&child->children_link);
2919 child->parent = NULL;
2920 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002921
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002922 wl_list_remove(&shsurf->link);
2923 free(shsurf);
2924}
2925
2926static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002927shell_destroy_shell_surface(struct wl_resource *resource)
2928{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002929 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002930
2931 destroy_shell_surface(shsurf);
2932}
2933
2934static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002935shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002936{
2937 struct shell_surface *shsurf = container_of(listener,
2938 struct shell_surface,
2939 surface_destroy_listener);
2940
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002941 if (shsurf->resource)
2942 wl_resource_destroy(shsurf->resource);
2943 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002944 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002945}
2946
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002947static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002948shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002949static void
2950shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002951
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002952struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002953get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002954{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002955 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002956 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002957 else
2958 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002959}
2960
Rafael Antognollie2a34552013-12-03 15:35:45 -02002961static struct shell_surface *
2962create_common_surface(void *shell, struct weston_surface *surface,
2963 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002964{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002965 struct shell_surface *shsurf;
2966
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002967 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002968 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002969 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002970 }
2971
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002972 shsurf = calloc(1, sizeof *shsurf);
2973 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002974 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002975 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002976 }
2977
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 shsurf->view = weston_view_create(surface);
2979 if (!shsurf->view) {
2980 weston_log("no memory to allocate shell surface\n");
2981 free(shsurf);
2982 return NULL;
2983 }
2984
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002985 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002986 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002987 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002988
Tiago Vignattibc052c92012-04-19 16:18:18 +03002989 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002990 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002991 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002992 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002993 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002994 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002995 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2996 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002997 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002998 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002999 wl_list_init(&shsurf->fullscreen.transform.link);
3000
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003001 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003002 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003003 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003004 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003005
3006 /* init link so its safe to always remove it in destroy_shell_surface */
3007 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003008 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003009
Pekka Paalanen460099f2012-01-20 16:48:25 +02003010 /* empty when not in use */
3011 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003012 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003013
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003014 wl_list_init(&shsurf->workspace_transform.link);
3015
Philip Withnall648a4dd2013-11-25 18:01:44 +00003016 wl_list_init(&shsurf->children_link);
3017 wl_list_init(&shsurf->children_list);
3018 shsurf->parent = NULL;
3019
Pekka Paalanen98262232011-12-01 10:42:22 +02003020 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003021
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003022 shsurf->client = client;
3023
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003024 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003025}
3026
Rafael Antognollie2a34552013-12-03 15:35:45 -02003027static struct shell_surface *
3028create_shell_surface(void *shell, struct weston_surface *surface,
3029 const struct weston_shell_client *client)
3030{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003031 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003032}
3033
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034static struct weston_view *
3035get_primary_view(void *shell, struct shell_surface *shsurf)
3036{
3037 return shsurf->view;
3038}
3039
Tiago Vignattibc052c92012-04-19 16:18:18 +03003040static void
3041shell_get_shell_surface(struct wl_client *client,
3042 struct wl_resource *resource,
3043 uint32_t id,
3044 struct wl_resource *surface_resource)
3045{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003046 struct weston_surface *surface =
3047 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003048 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003049 struct shell_surface *shsurf;
3050
3051 if (get_shell_surface(surface)) {
3052 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003053 WL_DISPLAY_ERROR_INVALID_OBJECT,
3054 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003055 return;
3056 }
3057
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003058 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003059 if (!shsurf) {
3060 wl_resource_post_error(surface_resource,
3061 WL_DISPLAY_ERROR_INVALID_OBJECT,
3062 "surface->configure already set");
3063 return;
3064 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003065
Jason Ekstranda85118c2013-06-27 20:17:02 -05003066 shsurf->resource =
3067 wl_resource_create(client,
3068 &wl_shell_surface_interface, 1, id);
3069 wl_resource_set_implementation(shsurf->resource,
3070 &shell_surface_implementation,
3071 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003072}
3073
Rafael Antognollie2a34552013-12-03 15:35:45 -02003074static bool
3075shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3076{
3077 return wl_resource_instance_of(shsurf->resource,
3078 &wl_shell_surface_interface,
3079 &shell_surface_implementation);
3080}
3081
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003082static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003083 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003084};
3085
Rafael Antognollie2a34552013-12-03 15:35:45 -02003086/****************************
3087 * xdg-shell implementation */
3088
3089static void
3090xdg_surface_destroy(struct wl_client *client,
3091 struct wl_resource *resource)
3092{
3093 wl_resource_destroy(resource);
3094}
3095
3096static void
3097xdg_surface_pong(struct wl_client *client,
3098 struct wl_resource *resource,
3099 uint32_t serial)
3100{
3101 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3102
3103 surface_pong(shsurf, serial);
3104}
3105
3106static void
3107xdg_surface_set_app_id(struct wl_client *client,
3108 struct wl_resource *resource,
3109 const char *app_id)
3110{
3111 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3112
3113 free(shsurf->class);
3114 shsurf->class = strdup(app_id);
3115}
3116
3117static void
3118xdg_surface_set_title(struct wl_client *client,
3119 struct wl_resource *resource, const char *title)
3120{
3121 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3122
3123 set_title(shsurf, title);
3124}
3125
3126static void
3127xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3128 struct wl_resource *seat_resource, uint32_t serial)
3129{
3130 common_surface_move(resource, seat_resource, serial);
3131}
3132
3133static void
3134xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3135 struct wl_resource *seat_resource, uint32_t serial,
3136 uint32_t edges)
3137{
3138 common_surface_resize(resource, seat_resource, serial, edges);
3139}
3140
3141static void
3142xdg_surface_set_output(struct wl_client *client,
3143 struct wl_resource *resource,
3144 struct wl_resource *output_resource)
3145{
3146 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3147 struct weston_output *output;
3148
3149 if (output_resource)
3150 output = wl_resource_get_user_data(output_resource);
3151 else
3152 output = NULL;
3153
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003154 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003155}
3156
3157static void
3158xdg_surface_set_fullscreen(struct wl_client *client,
3159 struct wl_resource *resource)
3160{
3161 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3162
3163 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3164 return;
3165
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003166 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003167 set_fullscreen(shsurf,
3168 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003169 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003170}
3171
3172static void
3173xdg_surface_unset_fullscreen(struct wl_client *client,
3174 struct wl_resource *resource)
3175{
3176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003177 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003178
3179 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3180 return;
3181
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003182 if (!shsurf->next_state.fullscreen)
3183 return;
3184
Rafael Antognollie2a34552013-12-03 15:35:45 -02003185 shsurf->next_state.fullscreen = false;
3186 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003187
3188 if (shsurf->saved_size_valid) {
3189 width = shsurf->saved_width;
3190 height = shsurf->saved_height;
3191 shsurf->saved_size_valid = false;
3192 } else {
3193 width = shsurf->surface->width;
3194 height = shsurf->surface->height;
3195 }
3196
3197 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003198}
3199
3200static void
3201xdg_surface_set_maximized(struct wl_client *client,
3202 struct wl_resource *resource)
3203{
3204 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3205
3206 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3207 return;
3208
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003209 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003210 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003211}
3212
3213static void
3214xdg_surface_unset_maximized(struct wl_client *client,
3215 struct wl_resource *resource)
3216{
3217 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003218 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003219
3220 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3221 return;
3222
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003223 if (!shsurf->next_state.maximized)
3224 return;
3225
Rafael Antognollie2a34552013-12-03 15:35:45 -02003226 shsurf->next_state.maximized = false;
3227 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003228
3229 if (shsurf->saved_size_valid) {
3230 width = shsurf->saved_width;
3231 height = shsurf->saved_height;
3232 shsurf->saved_size_valid = false;
3233 } else {
3234 width = shsurf->surface->width;
3235 height = shsurf->surface->height;
3236 }
3237
3238 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003239}
3240
3241static const struct xdg_surface_interface xdg_surface_implementation = {
3242 xdg_surface_destroy,
3243 xdg_surface_set_transient_for,
3244 xdg_surface_set_title,
3245 xdg_surface_set_app_id,
3246 xdg_surface_pong,
3247 xdg_surface_move,
3248 xdg_surface_resize,
3249 xdg_surface_set_output,
3250 xdg_surface_set_fullscreen,
3251 xdg_surface_unset_fullscreen,
3252 xdg_surface_set_maximized,
3253 xdg_surface_unset_maximized,
3254 NULL /* set_minimized */
3255};
3256
3257static void
3258xdg_send_configure(struct weston_surface *surface,
3259 uint32_t edges, int32_t width, int32_t height)
3260{
3261 struct shell_surface *shsurf = get_shell_surface(surface);
3262
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003263 assert(shsurf);
3264
Rafael Antognollie2a34552013-12-03 15:35:45 -02003265 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3266}
3267
3268static const struct weston_shell_client xdg_client = {
3269 xdg_send_configure
3270};
3271
3272static void
3273xdg_use_unstable_version(struct wl_client *client,
3274 struct wl_resource *resource,
3275 int32_t version)
3276{
3277 if (version > 1) {
3278 wl_resource_post_error(resource,
3279 1,
3280 "xdg-shell:: version not implemented yet.");
3281 return;
3282 }
3283}
3284
3285static struct shell_surface *
3286create_xdg_surface(void *shell, struct weston_surface *surface,
3287 const struct weston_shell_client *client)
3288{
3289 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003290
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003291 shsurf = create_common_surface(shell, surface, client);
3292 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003293
3294 return shsurf;
3295}
3296
3297static void
3298xdg_get_xdg_surface(struct wl_client *client,
3299 struct wl_resource *resource,
3300 uint32_t id,
3301 struct wl_resource *surface_resource)
3302{
3303 struct weston_surface *surface =
3304 wl_resource_get_user_data(surface_resource);
3305 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3306 struct shell_surface *shsurf;
3307
3308 if (get_shell_surface(surface)) {
3309 wl_resource_post_error(surface_resource,
3310 WL_DISPLAY_ERROR_INVALID_OBJECT,
3311 "desktop_shell::get_shell_surface already requested");
3312 return;
3313 }
3314
3315 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3316 if (!shsurf) {
3317 wl_resource_post_error(surface_resource,
3318 WL_DISPLAY_ERROR_INVALID_OBJECT,
3319 "surface->configure already set");
3320 return;
3321 }
3322
3323 shsurf->resource =
3324 wl_resource_create(client,
3325 &xdg_surface_interface, 1, id);
3326 wl_resource_set_implementation(shsurf->resource,
3327 &xdg_surface_implementation,
3328 shsurf, shell_destroy_shell_surface);
3329}
3330
3331static bool
3332shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3333{
3334 return wl_resource_instance_of(shsurf->resource,
3335 &xdg_surface_interface,
3336 &xdg_surface_implementation);
3337}
3338
3339/* xdg-popup implementation */
3340
3341static void
3342xdg_popup_destroy(struct wl_client *client,
3343 struct wl_resource *resource)
3344{
3345 wl_resource_destroy(resource);
3346}
3347
3348static void
3349xdg_popup_pong(struct wl_client *client,
3350 struct wl_resource *resource,
3351 uint32_t serial)
3352{
3353 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3354
3355 surface_pong(shsurf, serial);
3356}
3357
3358static const struct xdg_popup_interface xdg_popup_implementation = {
3359 xdg_popup_destroy,
3360 xdg_popup_pong
3361};
3362
3363static void
3364xdg_popup_send_configure(struct weston_surface *surface,
3365 uint32_t edges, int32_t width, int32_t height)
3366{
3367}
3368
3369static const struct weston_shell_client xdg_popup_client = {
3370 xdg_popup_send_configure
3371};
3372
3373static struct shell_surface *
3374create_xdg_popup(void *shell, struct weston_surface *surface,
3375 const struct weston_shell_client *client,
3376 struct weston_surface *parent,
3377 struct shell_seat *seat,
3378 uint32_t serial,
3379 int32_t x, int32_t y)
3380{
3381 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003382
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003383 shsurf = create_common_surface(shell, surface, client);
3384 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003385 shsurf->popup.shseat = seat;
3386 shsurf->popup.serial = serial;
3387 shsurf->popup.x = x;
3388 shsurf->popup.y = y;
3389 shell_surface_set_parent(shsurf, parent);
3390
3391 return shsurf;
3392}
3393
3394static void
3395xdg_get_xdg_popup(struct wl_client *client,
3396 struct wl_resource *resource,
3397 uint32_t id,
3398 struct wl_resource *surface_resource,
3399 struct wl_resource *parent_resource,
3400 struct wl_resource *seat_resource,
3401 uint32_t serial,
3402 int32_t x, int32_t y, uint32_t flags)
3403{
3404 struct weston_surface *surface =
3405 wl_resource_get_user_data(surface_resource);
3406 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3407 struct shell_surface *shsurf;
3408 struct weston_surface *parent;
3409 struct shell_seat *seat;
3410
3411 if (get_shell_surface(surface)) {
3412 wl_resource_post_error(surface_resource,
3413 WL_DISPLAY_ERROR_INVALID_OBJECT,
3414 "desktop_shell::get_shell_surface already requested");
3415 return;
3416 }
3417
3418 if (!parent_resource) {
3419 wl_resource_post_error(surface_resource,
3420 WL_DISPLAY_ERROR_INVALID_OBJECT,
3421 "xdg_shell::get_xdg_popup requires a parent shell surface");
3422 }
3423
3424 parent = wl_resource_get_user_data(parent_resource);
3425 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3426
3427 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3428 parent, seat, serial, x, y);
3429 if (!shsurf) {
3430 wl_resource_post_error(surface_resource,
3431 WL_DISPLAY_ERROR_INVALID_OBJECT,
3432 "surface->configure already set");
3433 return;
3434 }
3435
3436 shsurf->resource =
3437 wl_resource_create(client,
3438 &xdg_popup_interface, 1, id);
3439 wl_resource_set_implementation(shsurf->resource,
3440 &xdg_popup_implementation,
3441 shsurf, shell_destroy_shell_surface);
3442}
3443
3444static bool
3445shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3446{
3447 return wl_resource_instance_of(shsurf->resource,
3448 &xdg_popup_interface,
3449 &xdg_popup_implementation);
3450}
3451
3452static const struct xdg_shell_interface xdg_implementation = {
3453 xdg_use_unstable_version,
3454 xdg_get_xdg_surface,
3455 xdg_get_xdg_popup
3456};
3457
3458static int
3459xdg_shell_unversioned_dispatch(const void *implementation,
3460 void *_target, uint32_t opcode,
3461 const struct wl_message *message,
3462 union wl_argument *args)
3463{
3464 struct wl_resource *resource = _target;
3465 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3466
3467 if (opcode != 0) {
3468 wl_resource_post_error(resource,
3469 WL_DISPLAY_ERROR_INVALID_OBJECT,
3470 "must call use_unstable_version first");
3471 return 0;
3472 }
3473
3474#define XDG_SERVER_VERSION 1
3475
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003476 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3477 "shell implementation doesn't match protocol version");
3478
Rafael Antognollie2a34552013-12-03 15:35:45 -02003479 if (args[0].i != XDG_SERVER_VERSION) {
3480 wl_resource_post_error(resource,
3481 WL_DISPLAY_ERROR_INVALID_OBJECT,
3482 "incompatible version, server is %d "
3483 "client wants %d",
3484 XDG_SERVER_VERSION, args[0].i);
3485 return 0;
3486 }
3487
3488 wl_resource_set_implementation(resource, &xdg_implementation,
3489 shell, NULL);
3490
3491 return 1;
3492}
3493
3494/* end of xdg-shell implementation */
3495/***********************************/
3496
Kristian Høgsberg07937562011-04-12 17:25:42 -04003497static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003498shell_fade(struct desktop_shell *shell, enum fade_type type);
3499
3500static int
3501screensaver_timeout(void *data)
3502{
3503 struct desktop_shell *shell = data;
3504
3505 shell_fade(shell, FADE_OUT);
3506
3507 return 1;
3508}
3509
3510static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003511handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003512{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003513 struct desktop_shell *shell =
3514 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003515
Pekka Paalanen18027e52011-12-02 16:31:49 +02003516 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003517
3518 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003519 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003520}
3521
3522static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003523launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003524{
3525 if (shell->screensaver.binding)
3526 return;
3527
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003528 if (!shell->screensaver.path) {
3529 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003530 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003531 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003532
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003533 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003534 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003535 return;
3536 }
3537
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003538 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003539 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003540 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003541 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003542}
3543
3544static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003545terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003546{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003547 if (shell->screensaver.process.pid == 0)
3548 return;
3549
3550 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003551}
3552
3553static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003554configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003555{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003556 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003557
Jason Ekstranda7af7042013-10-12 22:38:11 -05003558 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3559 if (v->output == ev->output && v != ev) {
3560 weston_view_unmap(v);
3561 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003562 }
3563 }
3564
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003565 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003566
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 if (wl_list_empty(&ev->layer_link)) {
3568 wl_list_insert(&layer->view_list, &ev->layer_link);
3569 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003570 }
3571}
3572
3573static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003574background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003575{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003576 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 view = container_of(es->views.next, struct weston_view, surface_link);
3580
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003581 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003582}
3583
3584static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003585desktop_shell_set_background(struct wl_client *client,
3586 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003587 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003588 struct wl_resource *surface_resource)
3589{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003590 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003591 struct weston_surface *surface =
3592 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003593 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003594
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003595 if (surface->configure) {
3596 wl_resource_post_error(surface_resource,
3597 WL_DISPLAY_ERROR_INVALID_OBJECT,
3598 "surface role already assigned");
3599 return;
3600 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003601
Jason Ekstranda7af7042013-10-12 22:38:11 -05003602 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3603 weston_view_destroy(view);
3604 view = weston_view_create(surface);
3605
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003606 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003607 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003608 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003609 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003610 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003611 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003612 surface->output->width,
3613 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003614}
3615
3616static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003617panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003618{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003619 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003621
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 view = container_of(es->views.next, struct weston_view, surface_link);
3623
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003624 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003625}
3626
3627static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003628desktop_shell_set_panel(struct wl_client *client,
3629 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003630 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003631 struct wl_resource *surface_resource)
3632{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003633 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003634 struct weston_surface *surface =
3635 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003637
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003638 if (surface->configure) {
3639 wl_resource_post_error(surface_resource,
3640 WL_DISPLAY_ERROR_INVALID_OBJECT,
3641 "surface role already assigned");
3642 return;
3643 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003644
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3646 weston_view_destroy(view);
3647 view = weston_view_create(surface);
3648
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003649 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003650 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003651 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003652 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003653 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003654 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003655 surface->output->width,
3656 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003657}
3658
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003659static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003660lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003661{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003662 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003663 struct weston_view *view;
3664
3665 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003666
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003667 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003668 return;
3669
Jason Ekstranda7af7042013-10-12 22:38:11 -05003670 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003671
3672 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003673 wl_list_insert(&shell->lock_layer.view_list,
3674 &view->layer_link);
3675 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003676 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003677 }
3678}
3679
3680static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003681handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003682{
Tiago Vignattibe143262012-04-16 17:31:41 +03003683 struct desktop_shell *shell =
3684 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003685
Martin Minarik6d118362012-06-07 18:01:59 +02003686 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003687 shell->lock_surface = NULL;
3688}
3689
3690static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003691desktop_shell_set_lock_surface(struct wl_client *client,
3692 struct wl_resource *resource,
3693 struct wl_resource *surface_resource)
3694{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003695 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003696 struct weston_surface *surface =
3697 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003698
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003699 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003700
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003701 if (!shell->locked)
3702 return;
3703
Pekka Paalanen98262232011-12-01 10:42:22 +02003704 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003705
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003706 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003707 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003708 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003709
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003710 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003711 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003712 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003713}
3714
3715static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003716resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003717{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003718 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003719
Pekka Paalanen77346a62011-11-30 16:26:35 +02003720 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003721
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003722 wl_list_remove(&shell->lock_layer.link);
3723 wl_list_insert(&shell->compositor->cursor_layer.link,
3724 &shell->fullscreen_layer.link);
3725 wl_list_insert(&shell->fullscreen_layer.link,
3726 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003727 if (shell->showing_input_panels) {
3728 wl_list_insert(&shell->panel_layer.link,
3729 &shell->input_panel_layer.link);
3730 wl_list_insert(&shell->input_panel_layer.link,
3731 &ws->layer.link);
3732 } else {
3733 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3734 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003735
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003736 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003737
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003738 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003739 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003740 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003741}
3742
3743static void
3744desktop_shell_unlock(struct wl_client *client,
3745 struct wl_resource *resource)
3746{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003747 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003748
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003749 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003750
3751 if (shell->locked)
3752 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003753}
3754
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003755static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003756desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003757 struct wl_resource *resource,
3758 struct wl_resource *surface_resource)
3759{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003760 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003761
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003762 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003763 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003764}
3765
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003766static void
3767desktop_shell_desktop_ready(struct wl_client *client,
3768 struct wl_resource *resource)
3769{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003770 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003771
3772 shell_fade_startup(shell);
3773}
3774
Kristian Høgsberg75840622011-09-06 13:48:16 -04003775static const struct desktop_shell_interface desktop_shell_implementation = {
3776 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003777 desktop_shell_set_panel,
3778 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003779 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003780 desktop_shell_set_grab_surface,
3781 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782};
3783
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003784static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003785get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003786{
3787 struct shell_surface *shsurf;
3788
3789 shsurf = get_shell_surface(surface);
3790 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003791 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003792 return shsurf->type;
3793}
3794
Kristian Høgsberg75840622011-09-06 13:48:16 -04003795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003796move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003797{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003798 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003799 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003800 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003801
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003802 if (seat->pointer->focus == NULL)
3803 return;
3804
3805 focus = seat->pointer->focus->surface;
3806
Pekka Paalanen01388e22013-04-25 13:57:44 +03003807 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003808 if (surface == NULL)
3809 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003810
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003811 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003812 if (shsurf == NULL || shsurf->state.fullscreen ||
3813 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003814 return;
3815
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003816 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003817}
3818
3819static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003820maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3821{
3822 struct weston_surface *focus = seat->pointer->focus->surface;
3823 struct weston_surface *surface;
3824 struct shell_surface *shsurf;
3825
3826 surface = weston_surface_get_main_surface(focus);
3827 if (surface == NULL)
3828 return;
3829
3830 shsurf = get_shell_surface(surface);
3831 if (shsurf == NULL)
3832 return;
3833
3834 if (!shell_surface_is_xdg_surface(shsurf))
3835 return;
3836
3837 if (shsurf->state.maximized)
3838 xdg_surface_send_request_unset_maximized(shsurf->resource);
3839 else
3840 xdg_surface_send_request_set_maximized(shsurf->resource);
3841}
3842
3843static void
3844fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3845{
3846 struct weston_surface *focus = seat->pointer->focus->surface;
3847 struct weston_surface *surface;
3848 struct shell_surface *shsurf;
3849
3850 surface = weston_surface_get_main_surface(focus);
3851 if (surface == NULL)
3852 return;
3853
3854 shsurf = get_shell_surface(surface);
3855 if (shsurf == NULL)
3856 return;
3857
3858 if (!shell_surface_is_xdg_surface(shsurf))
3859 return;
3860
3861 if (shsurf->state.fullscreen)
3862 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3863 else
3864 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3865}
3866
3867static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003868touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3869{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003870 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003871 struct weston_surface *surface;
3872 struct shell_surface *shsurf;
3873
3874 surface = weston_surface_get_main_surface(focus);
3875 if (surface == NULL)
3876 return;
3877
3878 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003879 if (shsurf == NULL || shsurf->state.fullscreen ||
3880 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003881 return;
3882
3883 surface_touch_move(shsurf, (struct weston_seat *) seat);
3884}
3885
3886static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003887resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003888{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003889 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003890 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003891 uint32_t edges = 0;
3892 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003893 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003894
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003895 if (seat->pointer->focus == NULL)
3896 return;
3897
3898 focus = seat->pointer->focus->surface;
3899
Pekka Paalanen01388e22013-04-25 13:57:44 +03003900 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003901 if (surface == NULL)
3902 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003903
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003904 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003905 if (shsurf == NULL || shsurf->state.fullscreen ||
3906 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003907 return;
3908
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 weston_view_from_global(shsurf->view,
3910 wl_fixed_to_int(seat->pointer->grab_x),
3911 wl_fixed_to_int(seat->pointer->grab_y),
3912 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003913
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003914 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003915 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003916 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003917 edges |= 0;
3918 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003919 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003920
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003921 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003922 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003923 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003924 edges |= 0;
3925 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003926 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003927
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003928 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003929}
3930
3931static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003932surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003933 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003934{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003935 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003936 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003937 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003938 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003939
Pekka Paalanen01388e22013-04-25 13:57:44 +03003940 /* XXX: broken for windows containing sub-surfaces */
3941 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003942 if (surface == NULL)
3943 return;
3944
3945 shsurf = get_shell_surface(surface);
3946 if (!shsurf)
3947 return;
3948
Jason Ekstranda7af7042013-10-12 22:38:11 -05003949 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003950
Jason Ekstranda7af7042013-10-12 22:38:11 -05003951 if (shsurf->view->alpha > 1.0)
3952 shsurf->view->alpha = 1.0;
3953 if (shsurf->view->alpha < step)
3954 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003955
Jason Ekstranda7af7042013-10-12 22:38:11 -05003956 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003957 weston_surface_damage(surface);
3958}
3959
3960static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003961do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003962 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003963{
Daniel Stone37816df2012-05-16 18:45:18 +01003964 struct weston_seat *ws = (struct weston_seat *) seat;
3965 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003966 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003967 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003968
3969 wl_list_for_each(output, &compositor->output_list, link) {
3970 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003971 wl_fixed_to_double(seat->pointer->x),
3972 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003973 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003974 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003975 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003976 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003977 increment = -output->zoom.increment;
3978 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003979 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003980 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003981 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003982 else
3983 increment = 0;
3984
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003985 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003986
Scott Moreaue6603982012-06-11 13:07:51 -06003987 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003988 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003989 else if (output->zoom.level > output->zoom.max_level)
3990 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003991 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003992 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003993 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003994
Scott Moreaue6603982012-06-11 13:07:51 -06003995 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003996
Jason Ekstranda7af7042013-10-12 22:38:11 -05003997 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003998 }
3999 }
4000}
4001
Scott Moreauccbf29d2012-02-22 14:21:41 -07004002static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004003zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004004 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004005{
4006 do_zoom(seat, time, 0, axis, value);
4007}
4008
4009static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004010zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004011 void *data)
4012{
4013 do_zoom(seat, time, key, 0, 0);
4014}
4015
4016static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004017terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004018 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004019{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004020 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004021
Daniel Stone325fc2d2012-05-30 16:31:58 +01004022 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004023}
4024
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004025static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004026rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4027 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004028{
4029 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004030 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004031 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004032 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004033 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004034
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004035 weston_pointer_move(pointer, x, y);
4036
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004037 if (!shsurf)
4038 return;
4039
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004040 cx = 0.5f * shsurf->surface->width;
4041 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004042
Daniel Stone37816df2012-05-16 18:45:18 +01004043 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4044 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004045 r = sqrtf(dx * dx + dy * dy);
4046
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004047 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004049
4050 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004051 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004052 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004053
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004054 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004055 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004056
4057 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004058 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004059 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004060 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004061 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004062
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004063 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004064 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004065 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004066 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004067 wl_list_init(&shsurf->rotation.transform.link);
4068 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004069 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004070 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004071
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004072 /* We need to adjust the position of the surface
4073 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004074 cposx = shsurf->view->geometry.x + cx;
4075 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004076 dposx = rotate->center.x - cposx;
4077 dposy = rotate->center.y - cposy;
4078 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004079 weston_view_set_position(shsurf->view,
4080 shsurf->view->geometry.x + dposx,
4081 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004082 }
4083
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004084 /* Repaint implies weston_surface_update_transform(), which
4085 * lazily applies the damage due to rotation update.
4086 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004087 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004088}
4089
4090static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004091rotate_grab_button(struct weston_pointer_grab *grab,
4092 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004093{
4094 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004095 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004096 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004097 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004098 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004099
Daniel Stone4dbadb12012-05-30 16:31:51 +01004100 if (pointer->button_count == 0 &&
4101 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004102 if (shsurf)
4103 weston_matrix_multiply(&shsurf->rotation.rotation,
4104 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004105 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004106 free(rotate);
4107 }
4108}
4109
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004110static void
4111rotate_grab_cancel(struct weston_pointer_grab *grab)
4112{
4113 struct rotate_grab *rotate =
4114 container_of(grab, struct rotate_grab, base.grab);
4115
4116 shell_grab_end(&rotate->base);
4117 free(rotate);
4118}
4119
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004120static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004121 noop_grab_focus,
4122 rotate_grab_motion,
4123 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004124 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004125};
4126
4127static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004128surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004129{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004130 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004131 float dx, dy;
4132 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004133
Pekka Paalanen460099f2012-01-20 16:48:25 +02004134 rotate = malloc(sizeof *rotate);
4135 if (!rotate)
4136 return;
4137
Jason Ekstranda7af7042013-10-12 22:38:11 -05004138 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004139 surface->surface->width * 0.5f,
4140 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004141 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004142
Daniel Stone37816df2012-05-16 18:45:18 +01004143 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4144 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004145 r = sqrtf(dx * dx + dy * dy);
4146 if (r > 20.0f) {
4147 struct weston_matrix inverse;
4148
4149 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004150 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004151 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004152
4153 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004154 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004155 } else {
4156 weston_matrix_init(&surface->rotation.rotation);
4157 weston_matrix_init(&rotate->rotation);
4158 }
4159
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004160 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4161 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004162}
4163
4164static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004165rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004166 void *data)
4167{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004168 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004169 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004170 struct shell_surface *surface;
4171
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004172 if (seat->pointer->focus == NULL)
4173 return;
4174
4175 focus = seat->pointer->focus->surface;
4176
Pekka Paalanen01388e22013-04-25 13:57:44 +03004177 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004178 if (base_surface == NULL)
4179 return;
4180
4181 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004182 if (surface == NULL || surface->state.fullscreen ||
4183 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004184 return;
4185
4186 surface_rotate(surface, seat);
4187}
4188
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004189/* Move all fullscreen layers down to the current workspace in a non-reversible
4190 * manner. This should be used when implementing shell-wide overlays, such as
4191 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004192void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004193lower_fullscreen_layer(struct desktop_shell *shell)
4194{
4195 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004196 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004197
4198 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 wl_list_for_each_reverse_safe(view, prev,
4200 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004201 layer_link) {
4202 wl_list_remove(&view->layer_link);
4203 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4204 weston_view_damage_below(view);
4205 weston_surface_damage(view->surface);
4206 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004207}
4208
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004209void
Tiago Vignattibe143262012-04-16 17:31:41 +03004210activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004211 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004212{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004213 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004214 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004215 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004216 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004217 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004218
Pekka Paalanen01388e22013-04-25 13:57:44 +03004219 main_surface = weston_surface_get_main_surface(es);
4220
Daniel Stone37816df2012-05-16 18:45:18 +01004221 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004222
Jonas Ådahl8538b222012-08-29 22:13:03 +02004223 state = ensure_focus_state(shell, seat);
4224 if (state == NULL)
4225 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004226
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004227 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004228 state->keyboard_focus = es;
4229 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004230 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004231
Rafael Antognolli03b16592013-12-03 15:35:42 -02004232 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004233 assert(shsurf);
4234
Rafael Antognolli03b16592013-12-03 15:35:42 -02004235 if (shsurf->state.fullscreen)
4236 shell_configure_fullscreen(shsurf);
4237 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004238 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004239
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004240 if (shell->focus_animation_type != ANIMATION_NONE) {
4241 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004242 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004243 }
4244
4245 /* Update the surface’s layer. This brings it to the top of the stacking
4246 * order as appropriate. */
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004247 shell_surface_update_layer(shsurf);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004248}
4249
Alex Wu21858432012-04-01 20:13:08 +08004250/* no-op func for checking black surface */
4251static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004252black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004253{
4254}
4255
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004256static bool
Alex Wu21858432012-04-01 20:13:08 +08004257is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4258{
4259 if (es->configure == black_surface_configure) {
4260 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004261 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004262 return true;
4263 }
4264 return false;
4265}
4266
Kristian Høgsberg75840622011-09-06 13:48:16 -04004267static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004268activate_binding(struct weston_seat *seat,
4269 struct desktop_shell *shell,
4270 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004271{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004272 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004273
Alex Wu9c35e6b2012-03-05 14:13:13 +08004274 if (!focus)
4275 return;
4276
Pekka Paalanen01388e22013-04-25 13:57:44 +03004277 if (is_black_surface(focus, &main_surface))
4278 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004279
Pekka Paalanen01388e22013-04-25 13:57:44 +03004280 main_surface = weston_surface_get_main_surface(focus);
4281 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004282 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004283
Neil Robertsa28c6932013-10-03 16:43:04 +01004284 activate(shell, focus, seat);
4285}
4286
4287static void
4288click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4289 void *data)
4290{
4291 if (seat->pointer->grab != &seat->pointer->default_grab)
4292 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004293 if (seat->pointer->focus == NULL)
4294 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004295
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004296 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004297}
4298
4299static void
4300touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4301{
4302 if (seat->touch->grab != &seat->touch->default_grab)
4303 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004304 if (seat->touch->focus == NULL)
4305 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004306
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004307 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004308}
4309
4310static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004311lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004312{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004313 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004314
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004315 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004316 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004317 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004318 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004319
4320 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004321
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004322 /* Hide all surfaces by removing the fullscreen, panel and
4323 * toplevel layers. This way nothing else can show or receive
4324 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004325
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004326 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004327 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004328 if (shell->showing_input_panels)
4329 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004330 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004331 wl_list_insert(&shell->compositor->cursor_layer.link,
4332 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004333
Pekka Paalanen77346a62011-11-30 16:26:35 +02004334 launch_screensaver(shell);
4335
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004336 /* TODO: disable bindings that should not work while locked. */
4337
4338 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004339}
4340
4341static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004342unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004343{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004344 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004345 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004346 return;
4347 }
4348
4349 /* If desktop-shell client has gone away, unlock immediately. */
4350 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004351 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004352 return;
4353 }
4354
4355 if (shell->prepare_event_sent)
4356 return;
4357
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004358 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004359 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004360}
4361
4362static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004364{
4365 struct desktop_shell *shell = data;
4366
4367 shell->fade.animation = NULL;
4368
4369 switch (shell->fade.type) {
4370 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 weston_surface_destroy(shell->fade.view->surface);
4372 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004373 break;
4374 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004375 lock(shell);
4376 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004377 default:
4378 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004379 }
4380}
4381
Jason Ekstranda7af7042013-10-12 22:38:11 -05004382static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004383shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004384{
4385 struct weston_compositor *compositor = shell->compositor;
4386 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004388
4389 surface = weston_surface_create(compositor);
4390 if (!surface)
4391 return NULL;
4392
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 view = weston_view_create(surface);
4394 if (!view) {
4395 weston_surface_destroy(surface);
4396 return NULL;
4397 }
4398
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004399 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004400 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004401 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 wl_list_insert(&compositor->fade_layer.view_list,
4403 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004404 pixman_region32_init(&surface->input);
4405
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004407}
4408
4409static void
4410shell_fade(struct desktop_shell *shell, enum fade_type type)
4411{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004412 float tint;
4413
4414 switch (type) {
4415 case FADE_IN:
4416 tint = 0.0;
4417 break;
4418 case FADE_OUT:
4419 tint = 1.0;
4420 break;
4421 default:
4422 weston_log("shell: invalid fade type\n");
4423 return;
4424 }
4425
4426 shell->fade.type = type;
4427
Jason Ekstranda7af7042013-10-12 22:38:11 -05004428 if (shell->fade.view == NULL) {
4429 shell->fade.view = shell_fade_create_surface(shell);
4430 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004431 return;
4432
Jason Ekstranda7af7042013-10-12 22:38:11 -05004433 shell->fade.view->alpha = 1.0 - tint;
4434 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004435 }
4436
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004437 if (shell->fade.view->output == NULL) {
4438 /* If the black view gets a NULL output, we lost the
4439 * last output and we'll just cancel the fade. This
4440 * happens when you close the last window under the
4441 * X11 or Wayland backends. */
4442 shell->locked = false;
4443 weston_surface_destroy(shell->fade.view->surface);
4444 shell->fade.view = NULL;
4445 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004446 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004447 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004448 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004450 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004451 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004452 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004453}
4454
4455static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004456do_shell_fade_startup(void *data)
4457{
4458 struct desktop_shell *shell = data;
4459
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004460 if (shell->startup_animation_type == ANIMATION_FADE)
4461 shell_fade(shell, FADE_IN);
4462 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004463 weston_surface_destroy(shell->fade.view->surface);
4464 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004465 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004466}
4467
4468static void
4469shell_fade_startup(struct desktop_shell *shell)
4470{
4471 struct wl_event_loop *loop;
4472
4473 if (!shell->fade.startup_timer)
4474 return;
4475
4476 wl_event_source_remove(shell->fade.startup_timer);
4477 shell->fade.startup_timer = NULL;
4478
4479 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4480 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4481}
4482
4483static int
4484fade_startup_timeout(void *data)
4485{
4486 struct desktop_shell *shell = data;
4487
4488 shell_fade_startup(shell);
4489 return 0;
4490}
4491
4492static void
4493shell_fade_init(struct desktop_shell *shell)
4494{
4495 /* Make compositor output all black, and wait for the desktop-shell
4496 * client to signal it is ready, then fade in. The timer triggers a
4497 * fade-in, in case the desktop-shell client takes too long.
4498 */
4499
4500 struct wl_event_loop *loop;
4501
Jason Ekstranda7af7042013-10-12 22:38:11 -05004502 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004503 weston_log("%s: warning: fade surface already exists\n",
4504 __func__);
4505 return;
4506 }
4507
Jason Ekstranda7af7042013-10-12 22:38:11 -05004508 shell->fade.view = shell_fade_create_surface(shell);
4509 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004510 return;
4511
Jason Ekstranda7af7042013-10-12 22:38:11 -05004512 weston_view_update_transform(shell->fade.view);
4513 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004514
4515 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4516 shell->fade.startup_timer =
4517 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4518 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4519}
4520
4521static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004522idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004523{
4524 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004525 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004526 struct weston_seat *seat;
4527
4528 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4529 if (seat->pointer)
4530 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004531
4532 shell_fade(shell, FADE_OUT);
4533 /* lock() is called from shell_fade_done() */
4534}
4535
4536static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004537wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004538{
4539 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004540 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004541
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004542 unlock(shell);
4543}
4544
4545static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004547{
Giulio Camuffob8366642013-04-25 13:57:46 +03004548 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004549 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004550
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004552
4553 x = output->x + (output->width - width) / 2 - surf_x / 2;
4554 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004555
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004556 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004557}
4558
4559static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560weston_view_set_initial_position(struct weston_view *view,
4561 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004562{
4563 struct weston_compositor *compositor = shell->compositor;
4564 int ix = 0, iy = 0;
4565 int range_x, range_y;
4566 int dx, dy, x, y, panel_height;
4567 struct weston_output *output, *target_output = NULL;
4568 struct weston_seat *seat;
4569
4570 /* As a heuristic place the new window on the same output as the
4571 * pointer. Falling back to the output containing 0, 0.
4572 *
4573 * TODO: Do something clever for touch too?
4574 */
4575 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004576 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004577 ix = wl_fixed_to_int(seat->pointer->x);
4578 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004579 break;
4580 }
4581 }
4582
4583 wl_list_for_each(output, &compositor->output_list, link) {
4584 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4585 target_output = output;
4586 break;
4587 }
4588 }
4589
4590 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004591 weston_view_set_position(view, 10 + random() % 400,
4592 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004593 return;
4594 }
4595
4596 /* Valid range within output where the surface will still be onscreen.
4597 * If this is negative it means that the surface is bigger than
4598 * output.
4599 */
4600 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004601 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004602 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004603 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004604
Rob Bradford4cb88c72012-08-13 15:18:44 +01004605 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004606 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004607 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004608 dx = 0;
4609
4610 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004611 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004612 else
4613 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004614
4615 x = target_output->x + dx;
4616 y = target_output->y + dy;
4617
Jason Ekstranda7af7042013-10-12 22:38:11 -05004618 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004619}
4620
4621static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004622map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004623 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004624{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004625 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004626 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004627 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004628 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004629
Pekka Paalanen77346a62011-11-30 16:26:35 +02004630 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004632 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004633 if (shsurf->state.fullscreen) {
4634 center_on_output(shsurf->view, shsurf->fullscreen_output);
4635 shell_map_fullscreen(shsurf);
4636 } else if (shsurf->state.maximized) {
4637 /* use surface configure to set the geometry */
4638 panel_height = get_output_panel_height(shell, shsurf->output);
4639 surface_subsurfaces_boundingbox(shsurf->surface,
4640 &surf_x, &surf_y, NULL, NULL);
4641 weston_view_set_position(shsurf->view,
4642 shsurf->output->x - surf_x,
4643 shsurf->output->y +
4644 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004645 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004646 weston_view_set_initial_position(shsurf->view, shell);
4647 }
Juan Zhao96879df2012-02-07 08:45:41 +08004648 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004649 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004650 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004651 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004652 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004653 weston_view_set_position(shsurf->view,
4654 shsurf->view->geometry.x + sx,
4655 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004656 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004657 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004658 default:
4659 ;
4660 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004661
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004662 /* Surface stacking order, see also activate(). */
4663 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004664
Jason Ekstranda7af7042013-10-12 22:38:11 -05004665 if (shsurf->type != SHELL_SURFACE_NONE) {
4666 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004667 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004668 shsurf->surface->output = shsurf->output;
4669 shsurf->view->output = shsurf->output;
4670 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004671 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004672
Rafael Antognollied207b42013-12-03 15:35:43 -02004673 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4674 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4675 }
4676
Jason Ekstranda7af7042013-10-12 22:38:11 -05004677 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004678 /* XXX: xwayland's using the same fields for transient type */
4679 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004680 if (shsurf->transient.flags ==
4681 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4682 break;
4683 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004684 if (shsurf->state.relative &&
4685 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4686 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004687 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004688 break;
4689 wl_list_for_each(seat, &compositor->seat_list, link)
4690 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004691 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004692 case SHELL_SURFACE_POPUP:
4693 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004694 default:
4695 break;
4696 }
4697
Rafael Antognolli03b16592013-12-03 15:35:42 -02004698 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4699 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004700 {
4701 switch (shell->win_animation_type) {
4702 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004703 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004704 break;
4705 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004706 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004707 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004708 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004709 default:
4710 break;
4711 }
4712 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004713}
4714
4715static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004716configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004717 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004718{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004719 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004721 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004722
Pekka Paalanen77346a62011-11-30 16:26:35 +02004723 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004724
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004725 assert(shsurf);
4726
Rafael Antognolli03b16592013-12-03 15:35:42 -02004727 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004728 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004729 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004730 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004731 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4732 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004733 mx = shsurf->output->x - surf_x;
4734 my = shsurf->output->y +
4735 get_output_panel_height(shell,shsurf->output) - surf_y;
4736 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004737 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004738 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004739 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004740
Alex Wu4539b082012-03-01 12:57:46 +08004741 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004742 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004743 wl_list_for_each(view, &surface->views, surface_link)
4744 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004745
Rafael Antognolli03b16592013-12-03 15:35:42 -02004746 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004747 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004748 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004749}
4750
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004751static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004752shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004753{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004754 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004755 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004756 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004757
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004758 assert(shsurf);
4759
4760 shell = shsurf->shell;
4761
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004762 if (!weston_surface_is_mapped(es) &&
4763 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004764 remove_popup_grab(shsurf);
4765 }
4766
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004767 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004768 return;
4769
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004770 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004771 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004772 type_changed = 1;
4773 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004774
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004775 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004776 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004777 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004778 shsurf->last_width != es->width ||
4779 shsurf->last_height != es->height) {
4780 shsurf->last_width = es->width;
4781 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004782 float from_x, from_y;
4783 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004784
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4786 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004787 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004789 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004790 }
4791}
4792
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004793static void
4794shell_surface_output_destroyed(struct weston_surface *es)
4795{
4796 struct shell_surface *shsurf = get_shell_surface(es);
4797
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004798 assert(shsurf);
4799
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004800 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004801 shsurf->next_state.maximized = false;
4802 shsurf->next_state.fullscreen = false;
4803 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004804}
4805
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004806static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004807
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004808static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004809desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004810{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004811 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004812 struct desktop_shell *shell =
4813 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004814
4815 shell->child.process.pid = 0;
4816 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004817
4818 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4819 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004820 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004821 shell->child.deathstamp = time;
4822 shell->child.deathcount = 0;
4823 }
4824
4825 shell->child.deathcount++;
4826 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004827 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004828 return;
4829 }
4830
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004831 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004832 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004833 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004834}
4835
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004836static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004837desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4838{
4839 struct desktop_shell *shell;
4840
4841 shell = container_of(listener, struct desktop_shell,
4842 child.client_destroy_listener);
4843
4844 shell->child.client = NULL;
4845}
4846
4847static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004848launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004849{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004850 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004851
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004852 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004853 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004854 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004855 desktop_shell_sigchld);
4856
4857 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004858 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004859
4860 shell->child.client_destroy_listener.notify =
4861 desktop_shell_client_destroy;
4862 wl_client_add_destroy_listener(shell->child.client,
4863 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004864}
4865
4866static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004867bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4868{
Tiago Vignattibe143262012-04-16 17:31:41 +03004869 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004870 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004871
Jason Ekstranda85118c2013-06-27 20:17:02 -05004872 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4873 if (resource)
4874 wl_resource_set_implementation(resource, &shell_implementation,
4875 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004876}
4877
Kristian Høgsberg75840622011-09-06 13:48:16 -04004878static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004879bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4880{
4881 struct desktop_shell *shell = data;
4882 struct wl_resource *resource;
4883
4884 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4885 if (resource)
4886 wl_resource_set_dispatcher(resource,
4887 xdg_shell_unversioned_dispatch,
4888 NULL, shell, NULL);
4889}
4890
4891static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004892unbind_desktop_shell(struct wl_resource *resource)
4893{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004894 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004895
4896 if (shell->locked)
4897 resume_desktop(shell);
4898
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004899 shell->child.desktop_shell = NULL;
4900 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004901}
4902
4903static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004904bind_desktop_shell(struct wl_client *client,
4905 void *data, uint32_t version, uint32_t id)
4906{
Tiago Vignattibe143262012-04-16 17:31:41 +03004907 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004908 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004909
Jason Ekstranda85118c2013-06-27 20:17:02 -05004910 resource = wl_resource_create(client, &desktop_shell_interface,
4911 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004912
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004913 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004914 wl_resource_set_implementation(resource,
4915 &desktop_shell_implementation,
4916 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004917 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004918
4919 if (version < 2)
4920 shell_fade_startup(shell);
4921
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004922 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004923 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004924
4925 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4926 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004927 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004928}
4929
Pekka Paalanen6e168112011-11-24 11:34:05 +02004930static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004931screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004932{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004933 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004934 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004935
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004936 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004937 return;
4938
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004939 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004940 if (!shell->locked)
4941 return;
4942
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943 view = container_of(surface->views.next, struct weston_view, surface_link);
4944 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004945
Jason Ekstranda7af7042013-10-12 22:38:11 -05004946 if (wl_list_empty(&view->layer_link)) {
4947 wl_list_insert(shell->lock_layer.view_list.prev,
4948 &view->layer_link);
4949 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004950 wl_event_source_timer_update(shell->screensaver.timer,
4951 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004952 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004953 }
4954}
4955
4956static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004957screensaver_set_surface(struct wl_client *client,
4958 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004959 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004960 struct wl_resource *output_resource)
4961{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004962 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004963 struct weston_surface *surface =
4964 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004965 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004966 struct weston_view *view, *next;
4967
4968 /* Make sure we only have one view */
4969 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4970 weston_view_destroy(view);
4971 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004972
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004973 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004974 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004975 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004976}
4977
4978static const struct screensaver_interface screensaver_implementation = {
4979 screensaver_set_surface
4980};
4981
4982static void
4983unbind_screensaver(struct wl_resource *resource)
4984{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004985 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004986
Pekka Paalanen77346a62011-11-30 16:26:35 +02004987 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004988}
4989
4990static void
4991bind_screensaver(struct wl_client *client,
4992 void *data, uint32_t version, uint32_t id)
4993{
Tiago Vignattibe143262012-04-16 17:31:41 +03004994 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004995 struct wl_resource *resource;
4996
Jason Ekstranda85118c2013-06-27 20:17:02 -05004997 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004998
Pekka Paalanen77346a62011-11-30 16:26:35 +02004999 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005000 wl_resource_set_implementation(resource,
5001 &screensaver_implementation,
5002 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005003 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005004 return;
5005 }
5006
5007 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5008 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005009 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005010}
5011
Kristian Høgsberg07045392012-02-19 18:52:44 -05005012struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005013 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005014 struct weston_surface *current;
5015 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005016 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005017};
5018
5019static void
5020switcher_next(struct switcher *switcher)
5021{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005023 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005024 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005025 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005026
Jason Ekstranda7af7042013-10-12 22:38:11 -05005027 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005028 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005029 if (shsurf &&
5030 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5031 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005032 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005033 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005034 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005035 next = view->surface;
5036 prev = view->surface;
5037 view->alpha = 0.25;
5038 weston_view_geometry_dirty(view);
5039 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005040 }
Alex Wu1659daa2012-04-01 20:13:09 +08005041
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 if (is_black_surface(view->surface, NULL)) {
5043 view->alpha = 0.25;
5044 weston_view_geometry_dirty(view);
5045 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005046 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005047 }
5048
5049 if (next == NULL)
5050 next = first;
5051
Alex Wu07b26062012-03-12 16:06:01 +08005052 if (next == NULL)
5053 return;
5054
Kristian Høgsberg07045392012-02-19 18:52:44 -05005055 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005056 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005057
5058 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 wl_list_for_each(view, &next->views, surface_link)
5060 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005061
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005062 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005063 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005064 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005065}
5066
5067static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005068switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005069{
5070 struct switcher *switcher =
5071 container_of(listener, struct switcher, listener);
5072
5073 switcher_next(switcher);
5074}
5075
5076static void
Daniel Stone351eb612012-05-31 15:27:47 -04005077switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005078{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005079 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005080 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005081 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005082
Jason Ekstranda7af7042013-10-12 22:38:11 -05005083 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005084 if (is_focus_view(view))
5085 continue;
5086
Jason Ekstranda7af7042013-10-12 22:38:11 -05005087 view->alpha = 1.0;
5088 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005089 }
5090
Alex Wu07b26062012-03-12 16:06:01 +08005091 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005092 activate(switcher->shell, switcher->current,
5093 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005094 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005095 weston_keyboard_end_grab(keyboard);
5096 if (keyboard->input_method_resource)
5097 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005098 free(switcher);
5099}
5100
5101static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005102switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005103 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005104{
5105 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005106 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005107
Daniel Stonec9785ea2012-05-30 16:31:52 +01005108 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005109 switcher_next(switcher);
5110}
5111
5112static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005113switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005114 uint32_t mods_depressed, uint32_t mods_latched,
5115 uint32_t mods_locked, uint32_t group)
5116{
5117 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005118 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005119
Daniel Stone351eb612012-05-31 15:27:47 -04005120 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5121 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005122}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005123
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005124static void
5125switcher_cancel(struct weston_keyboard_grab *grab)
5126{
5127 struct switcher *switcher = container_of(grab, struct switcher, grab);
5128
5129 switcher_destroy(switcher);
5130}
5131
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005132static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005133 switcher_key,
5134 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005135 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005136};
5137
5138static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005139switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005140 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005141{
Tiago Vignattibe143262012-04-16 17:31:41 +03005142 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005143 struct switcher *switcher;
5144
5145 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005146 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005147 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005148 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005149 wl_list_init(&switcher->listener.link);
5150
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005151 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005152 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005153 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005154 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5155 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005156 switcher_next(switcher);
5157}
5158
Pekka Paalanen3c647232011-12-22 13:43:43 +02005159static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005160backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005161 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005162{
5163 struct weston_compositor *compositor = data;
5164 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005165 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005166
5167 /* TODO: we're limiting to simple use cases, where we assume just
5168 * control on the primary display. We'd have to extend later if we
5169 * ever get support for setting backlights on random desktop LCD
5170 * panels though */
5171 output = get_default_output(compositor);
5172 if (!output)
5173 return;
5174
5175 if (!output->set_backlight)
5176 return;
5177
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005178 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5179 backlight_new = output->backlight_current - 25;
5180 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5181 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005182
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005183 if (backlight_new < 5)
5184 backlight_new = 5;
5185 if (backlight_new > 255)
5186 backlight_new = 255;
5187
5188 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005189 output->set_backlight(output, output->backlight_current);
5190}
5191
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005192struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005193 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005194 struct weston_seat *seat;
5195 uint32_t key[2];
5196 int key_released[2];
5197};
5198
5199static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005200debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005201 uint32_t key, uint32_t state)
5202{
5203 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005204 struct weston_compositor *ec = db->seat->compositor;
5205 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005206 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005207 uint32_t serial;
5208 int send = 0, terminate = 0;
5209 int check_binding = 1;
5210 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005211 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005212
5213 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5214 /* Do not run bindings on key releases */
5215 check_binding = 0;
5216
5217 for (i = 0; i < 2; i++)
5218 if (key == db->key[i])
5219 db->key_released[i] = 1;
5220
5221 if (db->key_released[0] && db->key_released[1]) {
5222 /* All key releases been swalled so end the grab */
5223 terminate = 1;
5224 } else if (key != db->key[0] && key != db->key[1]) {
5225 /* Should not swallow release of other keys */
5226 send = 1;
5227 }
5228 } else if (key == db->key[0] && !db->key_released[0]) {
5229 /* Do not check bindings for the first press of the binding
5230 * key. This allows it to be used as a debug shortcut.
5231 * We still need to swallow this event. */
5232 check_binding = 0;
5233 } else if (db->key[1]) {
5234 /* If we already ran a binding don't process another one since
5235 * we can't keep track of all the binding keys that were
5236 * pressed in order to swallow the release events. */
5237 send = 1;
5238 check_binding = 0;
5239 }
5240
5241 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005242 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5243 key, state)) {
5244 /* We ran a binding so swallow the press and keep the
5245 * grab to swallow the released too. */
5246 send = 0;
5247 terminate = 0;
5248 db->key[1] = key;
5249 } else {
5250 /* Terminate the grab since the key pressed is not a
5251 * debug binding key. */
5252 send = 1;
5253 terminate = 1;
5254 }
5255 }
5256
5257 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005258 serial = wl_display_next_serial(display);
5259 resource_list = &grab->keyboard->focus_resource_list;
5260 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005261 wl_keyboard_send_key(resource, serial, time, key, state);
5262 }
5263 }
5264
5265 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005266 weston_keyboard_end_grab(grab->keyboard);
5267 if (grab->keyboard->input_method_resource)
5268 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005269 free(db);
5270 }
5271}
5272
5273static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005274debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005275 uint32_t mods_depressed, uint32_t mods_latched,
5276 uint32_t mods_locked, uint32_t group)
5277{
5278 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005279 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005280
Neil Roberts96d790e2013-09-19 17:32:00 +01005281 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005282
Neil Roberts96d790e2013-09-19 17:32:00 +01005283 wl_resource_for_each(resource, resource_list) {
5284 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5285 mods_latched, mods_locked, group);
5286 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005287}
5288
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005289static void
5290debug_binding_cancel(struct weston_keyboard_grab *grab)
5291{
5292 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5293
5294 weston_keyboard_end_grab(grab->keyboard);
5295 free(db);
5296}
5297
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005298struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005299 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005300 debug_binding_modifiers,
5301 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005302};
5303
5304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005305debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005306{
5307 struct debug_binding_grab *grab;
5308
5309 grab = calloc(1, sizeof *grab);
5310 if (!grab)
5311 return;
5312
5313 grab->seat = (struct weston_seat *) seat;
5314 grab->key[0] = key;
5315 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005316 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005317}
5318
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005319static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005320force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005321 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005322{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005323 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005324 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005325 struct desktop_shell *shell = data;
5326 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005327 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005328
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005329 focus_surface = seat->keyboard->focus;
5330 if (!focus_surface)
5331 return;
5332
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005333 wl_signal_emit(&compositor->kill_signal, focus_surface);
5334
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005335 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005336 wl_client_get_credentials(client, &pid, NULL, NULL);
5337
5338 /* Skip clients that we launched ourselves (the credentials of
5339 * the socketpair is ours) */
5340 if (pid == getpid())
5341 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005342
Daniel Stone325fc2d2012-05-30 16:31:58 +01005343 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005344}
5345
5346static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005347workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005348 uint32_t key, void *data)
5349{
5350 struct desktop_shell *shell = data;
5351 unsigned int new_index = shell->workspaces.current;
5352
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005353 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005354 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005355 if (new_index != 0)
5356 new_index--;
5357
5358 change_workspace(shell, new_index);
5359}
5360
5361static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005362workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005363 uint32_t key, void *data)
5364{
5365 struct desktop_shell *shell = data;
5366 unsigned int new_index = shell->workspaces.current;
5367
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005368 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005369 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005370 if (new_index < shell->workspaces.num - 1)
5371 new_index++;
5372
5373 change_workspace(shell, new_index);
5374}
5375
5376static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005377workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005378 uint32_t key, void *data)
5379{
5380 struct desktop_shell *shell = data;
5381 unsigned int new_index;
5382
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005383 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005384 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005385 new_index = key - KEY_F1;
5386 if (new_index >= shell->workspaces.num)
5387 new_index = shell->workspaces.num - 1;
5388
5389 change_workspace(shell, new_index);
5390}
5391
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005392static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005393workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005394 uint32_t key, void *data)
5395{
5396 struct desktop_shell *shell = data;
5397 unsigned int new_index = shell->workspaces.current;
5398
5399 if (shell->locked)
5400 return;
5401
5402 if (new_index != 0)
5403 new_index--;
5404
5405 take_surface_to_workspace_by_seat(shell, seat, new_index);
5406}
5407
5408static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005409workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005410 uint32_t key, void *data)
5411{
5412 struct desktop_shell *shell = data;
5413 unsigned int new_index = shell->workspaces.current;
5414
5415 if (shell->locked)
5416 return;
5417
5418 if (new_index < shell->workspaces.num - 1)
5419 new_index++;
5420
5421 take_surface_to_workspace_by_seat(shell, seat, new_index);
5422}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005423
5424static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005425handle_output_destroy(struct wl_listener *listener, void *data)
5426{
5427 struct shell_output *output_listener =
5428 container_of(listener, struct shell_output, destroy_listener);
5429
5430 wl_list_remove(&output_listener->destroy_listener.link);
5431 wl_list_remove(&output_listener->link);
5432 free(output_listener);
5433}
5434
5435static void
5436create_shell_output(struct desktop_shell *shell,
5437 struct weston_output *output)
5438{
5439 struct shell_output *shell_output;
5440
5441 shell_output = zalloc(sizeof *shell_output);
5442 if (shell_output == NULL)
5443 return;
5444
5445 shell_output->output = output;
5446 shell_output->shell = shell;
5447 shell_output->destroy_listener.notify = handle_output_destroy;
5448 wl_signal_add(&output->destroy_signal,
5449 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005450 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005451}
5452
5453static void
5454handle_output_create(struct wl_listener *listener, void *data)
5455{
5456 struct desktop_shell *shell =
5457 container_of(listener, struct desktop_shell, output_create_listener);
5458 struct weston_output *output = (struct weston_output *)data;
5459
5460 create_shell_output(shell, output);
5461}
5462
5463static void
5464setup_output_destroy_handler(struct weston_compositor *ec,
5465 struct desktop_shell *shell)
5466{
5467 struct weston_output *output;
5468
5469 wl_list_init(&shell->output_list);
5470 wl_list_for_each(output, &ec->output_list, link)
5471 create_shell_output(shell, output);
5472
5473 shell->output_create_listener.notify = handle_output_create;
5474 wl_signal_add(&ec->output_created_signal,
5475 &shell->output_create_listener);
5476}
5477
5478static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005479shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005480{
Tiago Vignattibe143262012-04-16 17:31:41 +03005481 struct desktop_shell *shell =
5482 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005483 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005484 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005485
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005486 /* Force state to unlocked so we don't try to fade */
5487 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005488 if (shell->child.client)
5489 wl_client_destroy(shell->child.client);
5490
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005491 wl_list_remove(&shell->idle_listener.link);
5492 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005493
5494 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005495
Xiong Zhang6b481422013-10-23 13:58:32 +08005496 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5497 wl_list_remove(&shell_output->destroy_listener.link);
5498 wl_list_remove(&shell_output->link);
5499 free(shell_output);
5500 }
5501
5502 wl_list_remove(&shell->output_create_listener.link);
5503
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005504 wl_array_for_each(ws, &shell->workspaces.array)
5505 workspace_destroy(*ws);
5506 wl_array_release(&shell->workspaces.array);
5507
Pekka Paalanen3c647232011-12-22 13:43:43 +02005508 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005509 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005510 free(shell);
5511}
5512
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005513static void
5514shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5515{
5516 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005517 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005518
5519 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005520 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5521 MODIFIER_CTRL | MODIFIER_ALT,
5522 terminate_binding, ec);
5523 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5524 click_to_activate_binding,
5525 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005526 weston_compositor_add_touch_binding(ec, 0,
5527 touch_to_activate_binding,
5528 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005529 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5530 MODIFIER_SUPER | MODIFIER_ALT,
5531 surface_opacity_binding, NULL);
5532 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5533 MODIFIER_SUPER, zoom_axis_binding,
5534 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005535
5536 /* configurable bindings */
5537 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005538 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5539 zoom_key_binding, NULL);
5540 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5541 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005542 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5543 maximize_binding, NULL);
5544 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5545 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005546 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5547 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005548 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005549 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5550 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005551 weston_compositor_add_button_binding(ec, BTN_LEFT,
5552 mod | MODIFIER_SHIFT,
5553 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005554
5555 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5556 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5557 rotate_binding, NULL);
5558
Daniel Stone325fc2d2012-05-30 16:31:58 +01005559 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5560 shell);
5561 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5562 ec);
5563 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5564 backlight_binding, ec);
5565 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5566 ec);
5567 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5568 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005569 weston_compositor_add_key_binding(ec, KEY_K, mod,
5570 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005571 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5572 workspace_up_binding, shell);
5573 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5574 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005575 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5576 workspace_move_surface_up_binding,
5577 shell);
5578 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5579 workspace_move_surface_down_binding,
5580 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005581
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005582 if (shell->exposay_modifier)
5583 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5584 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005585
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005586 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5587 if (shell->workspaces.num > 1) {
5588 num_workspace_bindings = shell->workspaces.num;
5589 if (num_workspace_bindings > 6)
5590 num_workspace_bindings = 6;
5591 for (i = 0; i < num_workspace_bindings; i++)
5592 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5593 workspace_f_binding,
5594 shell);
5595 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005596
5597 /* Debug bindings */
5598 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5599 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005600}
5601
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005602WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005603module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005604 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005605{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005606 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005607 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005608 struct workspace **pws;
5609 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005610 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005611
Peter Huttererf3d62272013-08-08 11:57:05 +10005612 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005613 if (shell == NULL)
5614 return -1;
5615
Kristian Høgsberg75840622011-09-06 13:48:16 -04005616 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005617
5618 shell->destroy_listener.notify = shell_destroy;
5619 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005620 shell->idle_listener.notify = idle_handler;
5621 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5622 shell->wake_listener.notify = wake_handler;
5623 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005624
Scott Moreauff1db4a2012-04-17 19:06:18 -06005625 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005626 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005627 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005628 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005629 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005630 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005631 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005632 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005633 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005634 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005635 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005636
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005637 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5638 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005639 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5640 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005641 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005642
5643 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005644 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005645
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005646 if (input_panel_setup(shell) < 0)
5647 return -1;
5648
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005649 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005650
Daniel Stonedf8133b2013-11-19 11:37:14 +01005651 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5652 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5653
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005654 for (i = 0; i < shell->workspaces.num; i++) {
5655 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5656 if (pws == NULL)
5657 return -1;
5658
5659 *pws = workspace_create();
5660 if (*pws == NULL)
5661 return -1;
5662 }
5663 activate_workspace(shell, 0);
5664
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005665 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005666 wl_list_init(&shell->workspaces.animation.link);
5667 shell->workspaces.animation.frame = animate_workspace_change_frame;
5668
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005669 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005670 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005671 return -1;
5672
Rafael Antognollie2a34552013-12-03 15:35:45 -02005673 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5674 shell, bind_xdg_shell) == NULL)
5675 return -1;
5676
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005677 if (wl_global_create(ec->wl_display,
5678 &desktop_shell_interface, 2,
5679 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005680 return -1;
5681
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005682 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5683 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005684 return -1;
5685
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005686 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5687 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005688 return -1;
5689
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005690 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005691
Xiong Zhang6b481422013-10-23 13:58:32 +08005692 setup_output_destroy_handler(ec, shell);
5693
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005694 loop = wl_display_get_event_loop(ec->wl_display);
5695 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005696
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005697 shell->screensaver.timer =
5698 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5699
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005700 wl_list_for_each(seat, &ec->seat_list, link)
5701 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005702
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005703 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005704
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005705 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005706
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005707 return 0;
5708}