blob: c2755433b0d67e6068af4281423a12f13f1ee655 [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øgsbergd500bf12014-01-22 12:25:20 -0800671focus_state_set_focus(struct focus_state *state,
672 struct weston_surface *surface)
673{
674 if (state->keyboard_focus) {
675 wl_list_remove(&state->surface_destroy_listener.link);
676 wl_list_init(&state->surface_destroy_listener.link);
677 }
678
679 state->keyboard_focus = surface;
680 if (surface)
681 wl_signal_add(&surface->destroy_signal,
682 &state->surface_destroy_listener);
683}
684
685static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400686restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200687{
688 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400689 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200690
691 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800692 if (state->seat->keyboard == NULL)
693 continue;
694
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400695 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200696
Kristian Høgsberge3148752013-05-06 23:19:49 -0400697 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 }
699}
700
701static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200702replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
703 struct weston_seat *seat)
704{
705 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200706
707 wl_list_for_each(state, &ws->focus_list, link) {
708 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800709 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200710 return;
711 }
712 }
713}
714
715static void
716drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
717 struct weston_surface *surface)
718{
719 struct focus_state *state;
720
721 wl_list_for_each(state, &ws->focus_list, link)
722 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800723 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200724}
725
726static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100727animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
728 struct weston_view *from, struct weston_view *to)
729{
730 struct weston_output *output;
731 bool focus_surface_created = false;
732
733 /* FIXME: Only support dim animation using two layers */
734 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
735 return;
736
737 output = get_default_output(shell->compositor);
738 if (ws->fsurf_front == NULL && (from || to)) {
739 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800740 if (ws->fsurf_front == NULL)
741 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100742 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800743
744 ws->fsurf_back = create_focus_surface(shell->compositor, output);
745 if (ws->fsurf_back == NULL) {
746 focus_surface_destroy(ws->fsurf_front);
747 return;
748 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100751 focus_surface_created = true;
752 } else {
753 wl_list_remove(&ws->fsurf_front->view->layer_link);
754 wl_list_remove(&ws->fsurf_back->view->layer_link);
755 }
756
757 if (ws->focus_animation) {
758 weston_view_animation_destroy(ws->focus_animation);
759 ws->focus_animation = NULL;
760 }
761
762 if (to)
763 wl_list_insert(&to->layer_link,
764 &ws->fsurf_front->view->layer_link);
765 else if (from)
766 wl_list_insert(&ws->layer.view_list,
767 &ws->fsurf_front->view->layer_link);
768
769 if (focus_surface_created) {
770 ws->focus_animation = weston_fade_run(
771 ws->fsurf_front->view,
772 ws->fsurf_front->view->alpha, 0.6, 300,
773 focus_animation_done, ws);
774 } else if (from) {
775 wl_list_insert(&from->layer_link,
776 &ws->fsurf_back->view->layer_link);
777 ws->focus_animation = weston_stable_fade_run(
778 ws->fsurf_front->view, 0.0,
779 ws->fsurf_back->view, 0.6,
780 focus_animation_done, ws);
781 } else if (to) {
782 wl_list_insert(&ws->layer.view_list,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
788 }
789}
790
791static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792workspace_destroy(struct workspace *ws)
793{
Jonas Ådahl04769742012-06-13 00:01:24 +0200794 struct focus_state *state, *next;
795
796 wl_list_for_each_safe(state, next, &ws->focus_list, link)
797 focus_state_destroy(state);
798
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100799 if (ws->fsurf_front)
800 focus_surface_destroy(ws->fsurf_front);
801 if (ws->fsurf_back)
802 focus_surface_destroy(ws->fsurf_back);
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804 free(ws);
805}
806
Jonas Ådahl04769742012-06-13 00:01:24 +0200807static void
808seat_destroyed(struct wl_listener *listener, void *data)
809{
810 struct weston_seat *seat = data;
811 struct focus_state *state, *next;
812 struct workspace *ws = container_of(listener,
813 struct workspace,
814 seat_destroyed_listener);
815
816 wl_list_for_each_safe(state, next, &ws->focus_list, link)
817 if (state->seat == seat)
818 wl_list_remove(&state->link);
819}
820
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200821static struct workspace *
822workspace_create(void)
823{
824 struct workspace *ws = malloc(sizeof *ws);
825 if (ws == NULL)
826 return NULL;
827
828 weston_layer_init(&ws->layer, NULL);
829
Jonas Ådahl04769742012-06-13 00:01:24 +0200830 wl_list_init(&ws->focus_list);
831 wl_list_init(&ws->seat_destroyed_listener.link);
832 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 ws->fsurf_front = NULL;
834 ws->fsurf_back = NULL;
835 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200836
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200837 return ws;
838}
839
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200840static int
841workspace_is_empty(struct workspace *ws)
842{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500843 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200844}
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846static struct workspace *
847get_workspace(struct desktop_shell *shell, unsigned int index)
848{
849 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200850 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851 pws += index;
852 return *pws;
853}
854
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800855struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856get_current_workspace(struct desktop_shell *shell)
857{
858 return get_workspace(shell, shell->workspaces.current);
859}
860
861static void
862activate_workspace(struct desktop_shell *shell, unsigned int index)
863{
864 struct workspace *ws;
865
866 ws = get_workspace(shell, index);
867 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
868
869 shell->workspaces.current = index;
870}
871
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200872static unsigned int
873get_output_height(struct weston_output *output)
874{
875 return abs(output->region.extents.y1 - output->region.extents.y2);
876}
877
878static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100879view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881 struct weston_transform *transform;
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 if (is_focus_view(view)) {
884 struct focus_surface *fsurf = get_focus_surface(view->surface);
885 transform = &fsurf->workspace_transform;
886 } else {
887 struct shell_surface *shsurf = get_shell_surface(view->surface);
888 transform = &shsurf->workspace_transform;
889 }
890
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100893 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100895 weston_matrix_init(&transform->matrix);
896 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899}
900
901static void
902workspace_translate_out(struct workspace *ws, double fraction)
903{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905 unsigned int height;
906 double d;
907
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
909 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200910 d = height * fraction;
911
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913 }
914}
915
916static void
917workspace_translate_in(struct workspace *ws, double fraction)
918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 unsigned int height;
921 double d;
922
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
924 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925
926 if (fraction > 0)
927 d = -(height - height * fraction);
928 else
929 d = height + height * fraction;
930
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100931 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932 }
933}
934
935static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200936broadcast_current_workspace_state(struct desktop_shell *shell)
937{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700938 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700940 wl_resource_for_each(resource, &shell->workspaces.client_list)
941 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942 shell->workspaces.current,
943 shell->workspaces.num);
944}
945
946static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947reverse_workspace_change_animation(struct desktop_shell *shell,
948 unsigned int index,
949 struct workspace *from,
950 struct workspace *to)
951{
952 shell->workspaces.current = index;
953
954 shell->workspaces.anim_to = to;
955 shell->workspaces.anim_from = from;
956 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
957 shell->workspaces.anim_timestamp = 0;
958
Scott Moreau4272e632012-08-13 09:58:41 -0600959 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200960}
961
962static void
963workspace_deactivate_transforms(struct workspace *ws)
964{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 if (is_focus_view(view)) {
970 struct focus_surface *fsurf = get_focus_surface(view->surface);
971 transform = &fsurf->workspace_transform;
972 } else {
973 struct shell_surface *shsurf = get_shell_surface(view->surface);
974 transform = &shsurf->workspace_transform;
975 }
976
977 if (!wl_list_empty(&transform->link)) {
978 wl_list_remove(&transform->link);
979 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200980 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982 }
983}
984
985static void
986finish_workspace_change_animation(struct desktop_shell *shell,
987 struct workspace *from,
988 struct workspace *to)
989{
Scott Moreau4272e632012-08-13 09:58:41 -0600990 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991
992 wl_list_remove(&shell->workspaces.animation.link);
993 workspace_deactivate_transforms(from);
994 workspace_deactivate_transforms(to);
995 shell->workspaces.anim_to = NULL;
996
997 wl_list_remove(&shell->workspaces.anim_from->layer.link);
998}
999
1000static void
1001animate_workspace_change_frame(struct weston_animation *animation,
1002 struct weston_output *output, uint32_t msecs)
1003{
1004 struct desktop_shell *shell =
1005 container_of(animation, struct desktop_shell,
1006 workspaces.animation);
1007 struct workspace *from = shell->workspaces.anim_from;
1008 struct workspace *to = shell->workspaces.anim_to;
1009 uint32_t t;
1010 double x, y;
1011
1012 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1013 finish_workspace_change_animation(shell, from, to);
1014 return;
1015 }
1016
1017 if (shell->workspaces.anim_timestamp == 0) {
1018 if (shell->workspaces.anim_current == 0.0)
1019 shell->workspaces.anim_timestamp = msecs;
1020 else
1021 shell->workspaces.anim_timestamp =
1022 msecs -
1023 /* Invers of movement function 'y' below. */
1024 (asin(1.0 - shell->workspaces.anim_current) *
1025 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1026 M_2_PI);
1027 }
1028
1029 t = msecs - shell->workspaces.anim_timestamp;
1030
1031 /*
1032 * x = [0, π/2]
1033 * y(x) = sin(x)
1034 */
1035 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1036 y = sin(x);
1037
1038 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001039 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
1041 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1042 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1043 shell->workspaces.anim_current = y;
1044
Scott Moreau4272e632012-08-13 09:58:41 -06001045 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001047 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001048 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001049}
1050
1051static void
1052animate_workspace_change(struct desktop_shell *shell,
1053 unsigned int index,
1054 struct workspace *from,
1055 struct workspace *to)
1056{
1057 struct weston_output *output;
1058
1059 int dir;
1060
1061 if (index > shell->workspaces.current)
1062 dir = -1;
1063 else
1064 dir = 1;
1065
1066 shell->workspaces.current = index;
1067
1068 shell->workspaces.anim_dir = dir;
1069 shell->workspaces.anim_from = from;
1070 shell->workspaces.anim_to = to;
1071 shell->workspaces.anim_current = 0.0;
1072 shell->workspaces.anim_timestamp = 0;
1073
1074 output = container_of(shell->compositor->output_list.next,
1075 struct weston_output, link);
1076 wl_list_insert(&output->animation_list,
1077 &shell->workspaces.animation.link);
1078
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001079 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
1081 workspace_translate_in(to, 0);
1082
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001083 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001084
Scott Moreau4272e632012-08-13 09:58:41 -06001085 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086}
1087
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001088static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001089update_workspace(struct desktop_shell *shell, unsigned int index,
1090 struct workspace *from, struct workspace *to)
1091{
1092 shell->workspaces.current = index;
1093 wl_list_insert(&from->layer.link, &to->layer.link);
1094 wl_list_remove(&from->layer.link);
1095}
1096
1097static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001098change_workspace(struct desktop_shell *shell, unsigned int index)
1099{
1100 struct workspace *from;
1101 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001102 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001103
1104 if (index == shell->workspaces.current)
1105 return;
1106
1107 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109 return;
1110
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001111 from = get_current_workspace(shell);
1112 to = get_workspace(shell, index);
1113
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 if (shell->workspaces.anim_from == to &&
1115 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001116 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001118 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 return;
1120 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001121
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 if (shell->workspaces.anim_to != NULL)
1123 finish_workspace_change_animation(shell,
1124 shell->workspaces.anim_from,
1125 shell->workspaces.anim_to);
1126
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001127 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001128
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001129 if (shell->focus_animation_type != ANIMATION_NONE) {
1130 wl_list_for_each(state, &from->focus_list, link)
1131 if (state->keyboard_focus)
1132 animate_focus_change(shell, from,
1133 get_default_view(state->keyboard_focus), NULL);
1134
1135 wl_list_for_each(state, &to->focus_list, link)
1136 if (state->keyboard_focus)
1137 animate_focus_change(shell, to,
1138 NULL, get_default_view(state->keyboard_focus));
1139 }
1140
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001141 if (workspace_is_empty(to) && workspace_is_empty(from))
1142 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 else
1144 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001145
1146 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001147}
1148
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001149static bool
1150workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1151{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001153 struct wl_list *e;
1154
1155 if (wl_list_empty(list))
1156 return false;
1157
1158 e = list->next;
1159
1160 if (e->next != list)
1161 return false;
1162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001164}
1165
1166static void
Philip Withnall659163d2013-11-25 18:01:36 +00001167move_surface_to_workspace(struct desktop_shell *shell,
1168 struct shell_surface *shsurf,
1169 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001170{
1171 struct workspace *from;
1172 struct workspace *to;
1173 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001174 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001175 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001176
1177 if (workspace == shell->workspaces.current)
1178 return;
1179
Philip Withnall659163d2013-11-25 18:01:36 +00001180 view = get_default_view(shsurf->surface);
1181 if (!view)
1182 return;
1183
1184 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1185
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001186 if (workspace >= shell->workspaces.num)
1187 workspace = shell->workspaces.num - 1;
1188
Jonas Ådahle9d22502012-08-29 22:13:01 +02001189 from = get_current_workspace(shell);
1190 to = get_workspace(shell, workspace);
1191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 wl_list_remove(&view->layer_link);
1193 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001194
Philip Withnall648a4dd2013-11-25 18:01:44 +00001195 shell_surface_update_child_surface_layers(shsurf);
1196
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001198 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1199 if (!seat->keyboard)
1200 continue;
1201
1202 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001204 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001205 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001208}
1209
1210static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001212 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001213 unsigned int index)
1214{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001215 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217 struct shell_surface *shsurf;
1218 struct workspace *from;
1219 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001220 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221
Pekka Paalanen01388e22013-04-25 13:57:44 +03001222 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view = get_default_view(surface);
1224 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001225 index == shell->workspaces.current ||
1226 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 return;
1228
1229 from = get_current_workspace(shell);
1230 to = get_workspace(shell, index);
1231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 wl_list_remove(&view->layer_link);
1233 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234
Philip Withnall659163d2013-11-25 18:01:36 +00001235 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001236 if (shsurf != NULL)
1237 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001238
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001240 drop_focus_state(shell, from, surface);
1241
1242 if (shell->workspaces.anim_from == to &&
1243 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001244 wl_list_remove(&to->layer.link);
1245 wl_list_insert(from->layer.link.prev, &to->layer.link);
1246
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248 broadcast_current_workspace_state(shell);
1249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250 return;
1251 }
1252
1253 if (shell->workspaces.anim_to != NULL)
1254 finish_workspace_change_animation(shell,
1255 shell->workspaces.anim_from,
1256 shell->workspaces.anim_to);
1257
1258 if (workspace_is_empty(from) &&
1259 workspace_has_only(to, surface))
1260 update_workspace(shell, index, from, to);
1261 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001262 if (shsurf != NULL &&
1263 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264 wl_list_insert(&shell->workspaces.anim_sticky_list,
1265 &shsurf->workspace_transform.link);
1266
1267 animate_workspace_change(shell, index, from, to);
1268 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269
1270 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001271
1272 state = ensure_focus_state(shell, seat);
1273 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001274 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275}
1276
1277static void
1278workspace_manager_move_surface(struct wl_client *client,
1279 struct wl_resource *resource,
1280 struct wl_resource *surface_resource,
1281 uint32_t workspace)
1282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001283 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001285 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001286 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001287 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288
Pekka Paalanen01388e22013-04-25 13:57:44 +03001289 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001290 shell_surface = get_shell_surface(main_surface);
1291 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001293
1294 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295}
1296
1297static const struct workspace_manager_interface workspace_manager_implementation = {
1298 workspace_manager_move_surface,
1299};
1300
1301static void
1302unbind_resource(struct wl_resource *resource)
1303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001304 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305}
1306
1307static void
1308bind_workspace_manager(struct wl_client *client,
1309 void *data, uint32_t version, uint32_t id)
1310{
1311 struct desktop_shell *shell = data;
1312 struct wl_resource *resource;
1313
Jason Ekstranda85118c2013-06-27 20:17:02 -05001314 resource = wl_resource_create(client,
1315 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001316
1317 if (resource == NULL) {
1318 weston_log("couldn't add workspace manager object");
1319 return;
1320 }
1321
Jason Ekstranda85118c2013-06-27 20:17:02 -05001322 wl_resource_set_implementation(resource,
1323 &workspace_manager_implementation,
1324 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001325 wl_list_insert(&shell->workspaces.client_list,
1326 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
1328 workspace_manager_send_state(resource,
1329 shell->workspaces.current,
1330 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331}
1332
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001333static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001334touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1335 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1336{
1337}
1338
1339static void
1340touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1341{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001342 struct weston_touch_move_grab *move =
1343 (struct weston_touch_move_grab *) container_of(
1344 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001345
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001346 if (touch_id == 0)
1347 move->active = 0;
1348
Jonas Ådahl9484b692013-12-02 22:05:03 +01001349 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001350 shell_touch_grab_end(&move->base);
1351 free(move);
1352 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001353}
1354
1355static void
1356touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1357 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1358{
1359 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1360 struct shell_surface *shsurf = move->base.shsurf;
1361 struct weston_surface *es;
1362 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1363 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1364
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001365 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001366 return;
1367
1368 es = shsurf->surface;
1369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001370 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001371
1372 weston_compositor_schedule_repaint(es->compositor);
1373}
1374
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001375static void
1376touch_move_grab_cancel(struct weston_touch_grab *grab)
1377{
1378 struct weston_touch_move_grab *move =
1379 (struct weston_touch_move_grab *) container_of(
1380 grab, struct shell_touch_grab, grab);
1381
1382 shell_touch_grab_end(&move->base);
1383 free(move);
1384}
1385
Rusty Lynch1084da52013-08-15 09:10:08 -07001386static const struct weston_touch_grab_interface touch_move_grab_interface = {
1387 touch_move_grab_down,
1388 touch_move_grab_up,
1389 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001390 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001391};
1392
1393static int
1394surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1395{
1396 struct weston_touch_move_grab *move;
1397
1398 if (!shsurf)
1399 return -1;
1400
Rafael Antognolli03b16592013-12-03 15:35:42 -02001401 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001402 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001403 if (shsurf->grabbed)
1404 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001405
1406 move = malloc(sizeof *move);
1407 if (!move)
1408 return -1;
1409
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001410 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001412 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001414 seat->touch->grab_y;
1415
1416 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1417 seat->touch);
1418
1419 return 0;
1420}
1421
1422static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001423noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001424{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425}
1426
1427static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001428move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1429 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001431 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001432 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001433 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001434 int dx, dy;
1435
1436 weston_pointer_move(pointer, x, y);
1437 dx = wl_fixed_to_int(pointer->x + move->dx);
1438 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001439
1440 if (!shsurf)
1441 return;
1442
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001443 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001444
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001446}
1447
1448static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001449move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001450 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001452 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1453 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001454 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001455 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001456
Daniel Stone4dbadb12012-05-30 16:31:51 +01001457 if (pointer->button_count == 0 &&
1458 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001459 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001460 free(grab);
1461 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001462}
1463
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001464static void
1465move_grab_cancel(struct weston_pointer_grab *grab)
1466{
1467 struct shell_grab *shell_grab =
1468 container_of(grab, struct shell_grab, grab);
1469
1470 shell_grab_end(shell_grab);
1471 free(grab);
1472}
1473
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001474static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001475 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001476 move_grab_motion,
1477 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001478 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001479};
1480
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001481static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001482surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001483{
1484 struct weston_move_grab *move;
1485
1486 if (!shsurf)
1487 return -1;
1488
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001489 if (shsurf->grabbed)
1490 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001491 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001492 return 0;
1493
1494 move = malloc(sizeof *move);
1495 if (!move)
1496 return -1;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001499 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001501 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001502
1503 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001504 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001505
1506 return 0;
1507}
1508
1509static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001510common_surface_move(struct wl_resource *resource,
1511 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001513 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001514 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001515 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001516
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001517 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001518 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001519 seat->pointer->button_count > 0 &&
1520 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001522 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001523 (surface_move(shsurf, seat) < 0))
1524 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001525 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001526 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001527 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001529 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 (surface_touch_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
1532 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001533}
1534
Rafael Antognollie2a34552013-12-03 15:35:45 -02001535static void
1536shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1537 struct wl_resource *seat_resource, uint32_t serial)
1538{
1539 common_surface_move(resource, seat_resource, serial);
1540}
1541
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542struct weston_resize_grab {
1543 struct shell_grab base;
1544 uint32_t edges;
1545 int32_t width, height;
1546};
1547
1548static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001549resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1550 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551{
1552 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001553 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001554 struct shell_surface *shsurf = resize->base.shsurf;
1555 int32_t width, height;
1556 wl_fixed_t from_x, from_y;
1557 wl_fixed_t to_x, to_y;
1558
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001559 weston_pointer_move(pointer, x, y);
1560
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 if (!shsurf)
1562 return;
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 weston_view_from_global_fixed(shsurf->view,
1565 pointer->grab_x, pointer->grab_y,
1566 &from_x, &from_y);
1567 weston_view_from_global_fixed(shsurf->view,
1568 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569
1570 width = resize->width;
1571 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1572 width += wl_fixed_to_int(from_x - to_x);
1573 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1574 width += wl_fixed_to_int(to_x - from_x);
1575 }
1576
1577 height = resize->height;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1579 height += wl_fixed_to_int(from_y - to_y);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1581 height += wl_fixed_to_int(to_y - from_y);
1582 }
1583
1584 shsurf->client->send_configure(shsurf->surface,
1585 resize->edges, width, height);
1586}
1587
1588static void
1589send_configure(struct weston_surface *surface,
1590 uint32_t edges, int32_t width, int32_t height)
1591{
1592 struct shell_surface *shsurf = get_shell_surface(surface);
1593
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001594 assert(shsurf);
1595
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001596 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 edges, width, height);
1598}
1599
1600static const struct weston_shell_client shell_client = {
1601 send_configure
1602};
1603
1604static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001605resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001606 uint32_t time, uint32_t button, uint32_t state_w)
1607{
1608 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001609 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 enum wl_pointer_button_state state = state_w;
1611
1612 if (pointer->button_count == 0 &&
1613 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1614 shell_grab_end(&resize->base);
1615 free(grab);
1616 }
1617}
1618
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001619static void
1620resize_grab_cancel(struct weston_pointer_grab *grab)
1621{
1622 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1623
1624 shell_grab_end(&resize->base);
1625 free(grab);
1626}
1627
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001628static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629 noop_grab_focus,
1630 resize_grab_motion,
1631 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001632 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633};
1634
Giulio Camuffob8366642013-04-25 13:57:46 +03001635/*
1636 * Returns the bounding box of a surface and all its sub-surfaces,
1637 * in the surface coordinates system. */
1638static void
1639surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1640 int32_t *y, int32_t *w, int32_t *h) {
1641 pixman_region32_t region;
1642 pixman_box32_t *box;
1643 struct weston_subsurface *subsurface;
1644
1645 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 surface->width,
1647 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001648
1649 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1650 pixman_region32_union_rect(&region, &region,
1651 subsurface->position.x,
1652 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 subsurface->surface->width,
1654 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001655 }
1656
1657 box = pixman_region32_extents(&region);
1658 if (x)
1659 *x = box->x1;
1660 if (y)
1661 *y = box->y1;
1662 if (w)
1663 *w = box->x2 - box->x1;
1664 if (h)
1665 *h = box->y2 - box->y1;
1666
1667 pixman_region32_fini(&region);
1668}
1669
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001670static int
1671surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001672 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673{
1674 struct weston_resize_grab *resize;
1675
Rafael Antognolli03b16592013-12-03 15:35:42 -02001676 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677 return 0;
1678
1679 if (edges == 0 || edges > 15 ||
1680 (edges & 3) == 3 || (edges & 12) == 12)
1681 return 0;
1682
1683 resize = malloc(sizeof *resize);
1684 if (!resize)
1685 return -1;
1686
1687 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001688 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1689 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001690
1691 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001692 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693
1694 return 0;
1695}
1696
1697static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001698common_surface_resize(struct wl_resource *resource,
1699 struct wl_resource *seat_resource, uint32_t serial,
1700 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001702 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001703 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001704 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705
Rafael Antognolli03b16592013-12-03 15:35:42 -02001706 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707 return;
1708
Kristian Høgsberge3148752013-05-06 23:19:49 -04001709 if (seat->pointer->button_count == 0 ||
1710 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001711 seat->pointer->focus == NULL)
1712 return;
1713
1714 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1715 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716 return;
1717
Kristian Høgsberge3148752013-05-06 23:19:49 -04001718 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 wl_resource_post_no_memory(resource);
1720}
1721
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001722static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001723shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1724 struct wl_resource *seat_resource, uint32_t serial,
1725 uint32_t edges)
1726{
1727 common_surface_resize(resource, seat_resource, serial, edges);
1728}
1729
1730static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001731end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1732
1733static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001734busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001735{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001736 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001737 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739 wl_fixed_t sx, sy;
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view = weston_compositor_pick_view(pointer->seat->compositor,
1742 pointer->x, pointer->y,
1743 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001744
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001746 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001747}
1748
1749static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001750busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1751 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001752{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001753 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001757busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001758 uint32_t time, uint32_t button, uint32_t state)
1759{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001760 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001761 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001762 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001763
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001764 if (shsurf && button == BTN_LEFT && state) {
1765 activate(shsurf->shell, shsurf->surface, seat);
1766 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001767 } else if (shsurf && button == BTN_RIGHT && state) {
1768 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771}
1772
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001773static void
1774busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1775{
1776 struct shell_grab *grab = (struct shell_grab *) base;
1777
1778 shell_grab_end(grab);
1779 free(grab);
1780}
1781
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001782static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783 busy_cursor_grab_focus,
1784 busy_cursor_grab_motion,
1785 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001786 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787};
1788
1789static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001790set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001791{
1792 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793
1794 grab = malloc(sizeof *grab);
1795 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001796 return;
1797
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001798 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1799 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001801
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001803end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804{
1805 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1806
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001807 if (grab->grab.interface == &busy_cursor_grab_interface &&
1808 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001809 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001812}
1813
Scott Moreau9521d5e2012-04-19 13:06:17 -06001814static void
1815ping_timer_destroy(struct shell_surface *shsurf)
1816{
1817 if (!shsurf || !shsurf->ping_timer)
1818 return;
1819
1820 if (shsurf->ping_timer->source)
1821 wl_event_source_remove(shsurf->ping_timer->source);
1822
1823 free(shsurf->ping_timer);
1824 shsurf->ping_timer = NULL;
1825}
1826
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001827static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001828ping_timeout_handler(void *data)
1829{
1830 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001832
Scott Moreau9521d5e2012-04-19 13:06:17 -06001833 /* Client is not responding */
1834 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835
1836 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001837 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001838 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001839 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001840
1841 return 1;
1842}
1843
1844static void
1845ping_handler(struct weston_surface *surface, uint32_t serial)
1846{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001847 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001848 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001850
1851 if (!shsurf)
1852 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001853 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001854 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001855
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001856 if (shsurf->surface == shsurf->shell->grab_surface)
1857 return;
1858
Scott Moreauff1db4a2012-04-17 19:06:18 -06001859 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001860 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001861 if (!shsurf->ping_timer)
1862 return;
1863
1864 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001865 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1866 shsurf->ping_timer->source =
1867 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1868 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1869
Rafael Antognollie2a34552013-12-03 15:35:45 -02001870 if (shell_surface_is_wl_shell_surface(shsurf))
1871 wl_shell_surface_send_ping(shsurf->resource, serial);
1872 else if (shell_surface_is_xdg_surface(shsurf))
1873 xdg_surface_send_ping(shsurf->resource, serial);
1874 else if (shell_surface_is_xdg_popup(shsurf))
1875 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001876 }
1877}
1878
1879static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880handle_pointer_focus(struct wl_listener *listener, void *data)
1881{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001882 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884 struct weston_compositor *compositor;
1885 struct shell_surface *shsurf;
1886 uint32_t serial;
1887
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001889 return;
1890
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 compositor = view->surface->compositor;
1892 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001893
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001894 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001895 set_busy_cursor(shsurf, pointer);
1896 } else {
1897 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001899 }
1900}
1901
1902static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001903create_pointer_focus_listener(struct weston_seat *seat)
1904{
1905 struct wl_listener *listener;
1906
Kristian Høgsberge3148752013-05-06 23:19:49 -04001907 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001908 return;
1909
1910 listener = malloc(sizeof *listener);
1911 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001912 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001913}
1914
1915static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001916xdg_surface_set_transient_for(struct wl_client *client,
1917 struct wl_resource *resource,
1918 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001919{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001920 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001921 struct weston_surface *parent;
1922
1923 if (parent_resource)
1924 parent = wl_resource_get_user_data(parent_resource);
1925 else
1926 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001927
1928 shell_surface_set_parent(shsurf, parent);
1929}
1930
1931static void
1932surface_pong(struct shell_surface *shsurf, uint32_t serial)
1933{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001934 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001935 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001936
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001937 if (shsurf->ping_timer == NULL)
1938 /* Just ignore unsolicited pong. */
1939 return;
1940
Scott Moreauff1db4a2012-04-17 19:06:18 -06001941 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001942 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001943 wl_list_for_each(seat, &ec->seat_list, link) {
1944 if(seat->pointer)
1945 end_busy_cursor(shsurf, seat->pointer);
1946 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001947 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 }
1949}
1950
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001951static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001952shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1953 uint32_t serial)
1954{
1955 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1956
1957 surface_pong(shsurf, serial);
1958
1959}
1960
1961static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001962set_title(struct shell_surface *shsurf, const char *title)
1963{
1964 free(shsurf->title);
1965 shsurf->title = strdup(title);
1966}
1967
1968static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001969shell_surface_set_title(struct wl_client *client,
1970 struct wl_resource *resource, const char *title)
1971{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001972 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001973
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001974 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001975}
1976
1977static void
1978shell_surface_set_class(struct wl_client *client,
1979 struct wl_resource *resource, const char *class)
1980{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001981 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001982
1983 free(shsurf->class);
1984 shsurf->class = strdup(class);
1985}
1986
Alex Wu4539b082012-03-01 12:57:46 +08001987static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001988restore_output_mode(struct weston_output *output)
1989{
Hardening57388e42013-09-18 23:56:36 +02001990 if (output->current_mode != output->original_mode ||
1991 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001992 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001993 output->original_mode,
1994 output->original_scale,
1995 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001996}
1997
1998static void
1999restore_all_output_modes(struct weston_compositor *compositor)
2000{
2001 struct weston_output *output;
2002
2003 wl_list_for_each(output, &compositor->output_list, link)
2004 restore_output_mode(output);
2005}
2006
Philip Withnallbecb77e2013-11-25 18:01:30 +00002007static int
2008get_output_panel_height(struct desktop_shell *shell,
2009 struct weston_output *output)
2010{
2011 struct weston_view *view;
2012 int panel_height = 0;
2013
2014 if (!output)
2015 return 0;
2016
2017 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2018 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002019 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002020 break;
2021 }
2022 }
2023
2024 return panel_height;
2025}
2026
Philip Withnall07926d92013-11-25 18:01:40 +00002027/* The surface will be inserted into the list immediately after the link
2028 * returned by this function (i.e. will be stacked immediately above the
2029 * returned link). */
2030static struct wl_list *
2031shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2032{
2033 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002034 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002035
2036 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002037 case SHELL_SURFACE_POPUP:
2038 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002039 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002040 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002041 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002042 /* Move the surface to its parent layer so
2043 * that surfaces which are transient for
2044 * fullscreen surfaces don't get hidden by the
2045 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002046
2047 /* TODO: Handle a parent with multiple views */
2048 parent = get_default_view(shsurf->parent);
2049 if (parent)
2050 return parent->layer_link.prev;
2051 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002052 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002053
2054 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002055 return &shsurf->shell->fullscreen_layer.view_list;
2056
Philip Withnall07926d92013-11-25 18:01:40 +00002057 case SHELL_SURFACE_NONE:
2058 default:
2059 /* Go to the fallback, below. */
2060 break;
2061 }
2062
2063 /* Move the surface to a normal workspace layer so that surfaces
2064 * which were previously fullscreen or transient are no longer
2065 * rendered on top. */
2066 ws = get_current_workspace(shsurf->shell);
2067 return &ws->layer.view_list;
2068}
2069
Philip Withnall648a4dd2013-11-25 18:01:44 +00002070static void
2071shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2072{
2073 struct shell_surface *child;
2074
2075 /* Move the child layers to the same workspace as shsurf. They will be
2076 * stacked above shsurf. */
2077 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2078 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2079 weston_view_geometry_dirty(child->view);
2080 wl_list_remove(&child->view->layer_link);
2081 wl_list_insert(shsurf->view->layer_link.prev,
2082 &child->view->layer_link);
2083 weston_view_geometry_dirty(child->view);
2084 weston_surface_damage(child->surface);
2085
2086 /* Recurse. We don’t expect this to recurse very far (if
2087 * at all) because that would imply we have transient
2088 * (or popup) children of transient surfaces, which
2089 * would be unusual. */
2090 shell_surface_update_child_surface_layers(child);
2091 }
2092 }
2093}
2094
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002095/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002096 * geometry to ensure the changes are redrawn.
2097 *
2098 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2099 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002100static void
2101shell_surface_update_layer(struct shell_surface *shsurf)
2102{
2103 struct wl_list *new_layer_link;
2104
2105 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2106
2107 if (new_layer_link == &shsurf->view->layer_link)
2108 return;
2109
2110 weston_view_geometry_dirty(shsurf->view);
2111 wl_list_remove(&shsurf->view->layer_link);
2112 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2113 weston_view_geometry_dirty(shsurf->view);
2114 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002115
2116 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002117}
2118
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002119static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002120shell_surface_set_parent(struct shell_surface *shsurf,
2121 struct weston_surface *parent)
2122{
2123 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002124
2125 wl_list_remove(&shsurf->children_link);
2126 wl_list_init(&shsurf->children_link);
2127
2128 /* Insert into the parent surface’s child list. */
2129 if (parent != NULL) {
2130 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2131 if (parent_shsurf != NULL)
2132 wl_list_insert(&parent_shsurf->children_list,
2133 &shsurf->children_link);
2134 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002135}
2136
2137static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002138shell_surface_set_output(struct shell_surface *shsurf,
2139 struct weston_output *output)
2140{
2141 struct weston_surface *es = shsurf->surface;
2142
2143 /* get the default output, if the client set it as NULL
2144 check whether the ouput is available */
2145 if (output)
2146 shsurf->output = output;
2147 else if (es->output)
2148 shsurf->output = es->output;
2149 else
2150 shsurf->output = get_default_output(es->compositor);
2151}
2152
2153static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002154surface_clear_next_states(struct shell_surface *shsurf)
2155{
2156 shsurf->next_state.maximized = false;
2157 shsurf->next_state.fullscreen = false;
2158
2159 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2160 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2161 shsurf->state_changed = true;
2162}
2163
2164static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002165set_toplevel(struct shell_surface *shsurf)
2166{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002167 shell_surface_set_parent(shsurf, NULL);
2168 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002169 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002170
2171 /* The layer_link is updated in set_surface_type(),
2172 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002173}
2174
2175static void
2176shell_surface_set_toplevel(struct wl_client *client,
2177 struct wl_resource *resource)
2178{
2179 struct shell_surface *surface = wl_resource_get_user_data(resource);
2180
2181 set_toplevel(surface);
2182}
2183
2184static void
2185set_transient(struct shell_surface *shsurf,
2186 struct weston_surface *parent, int x, int y, uint32_t flags)
2187{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002188 assert(parent != NULL);
2189
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002190 shell_surface_set_parent(shsurf, parent);
2191
2192 surface_clear_next_states(shsurf);
2193
Philip Withnallbecb77e2013-11-25 18:01:30 +00002194 shsurf->transient.x = x;
2195 shsurf->transient.y = y;
2196 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002197
Rafael Antognollied207b42013-12-03 15:35:43 -02002198 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002199 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002200 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201
2202 /* The layer_link is updated in set_surface_type(),
2203 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002204}
2205
2206static void
2207shell_surface_set_transient(struct wl_client *client,
2208 struct wl_resource *resource,
2209 struct wl_resource *parent_resource,
2210 int x, int y, uint32_t flags)
2211{
2212 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2213 struct weston_surface *parent =
2214 wl_resource_get_user_data(parent_resource);
2215
2216 set_transient(shsurf, parent, x, y, flags);
2217}
2218
2219static void
2220set_fullscreen(struct shell_surface *shsurf,
2221 uint32_t method,
2222 uint32_t framerate,
2223 struct weston_output *output)
2224{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002225 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002226
2227 shsurf->fullscreen_output = shsurf->output;
2228 shsurf->fullscreen.type = method;
2229 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002230
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002231 shsurf->next_state.fullscreen = true;
2232 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002233 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002234
2235 shsurf->client->send_configure(shsurf->surface, 0,
2236 shsurf->output->width,
2237 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002238
2239 /* The layer_link is updated in set_surface_type(),
2240 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002241}
2242
2243static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002244weston_view_set_initial_position(struct weston_view *view,
2245 struct desktop_shell *shell);
2246
2247static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002249{
Philip Withnallf85fe842013-11-25 18:01:37 +00002250 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002251 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2252 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002253 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002254 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002255 shsurf->fullscreen_output = NULL;
2256
Alex Wu4539b082012-03-01 12:57:46 +08002257 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2258 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002259
Alex Wu4539b082012-03-01 12:57:46 +08002260 wl_list_remove(&shsurf->fullscreen.transform.link);
2261 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002262
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 if (shsurf->fullscreen.black_view)
2264 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2265 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002266
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002267 if (shsurf->saved_position_valid)
2268 weston_view_set_position(shsurf->view,
2269 shsurf->saved_x, shsurf->saved_y);
2270 else
2271 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2272
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002273 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002275 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002276 shsurf->saved_rotation_valid = false;
2277 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002278
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002279 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002280}
2281
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002282static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002283shell_surface_set_fullscreen(struct wl_client *client,
2284 struct wl_resource *resource,
2285 uint32_t method,
2286 uint32_t framerate,
2287 struct wl_resource *output_resource)
2288{
2289 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2290 struct weston_output *output;
2291
2292 if (output_resource)
2293 output = wl_resource_get_user_data(output_resource);
2294 else
2295 output = NULL;
2296
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002297 shell_surface_set_parent(shsurf, NULL);
2298
Rafael Antognolli03b16592013-12-03 15:35:42 -02002299 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300 set_fullscreen(shsurf, method, framerate, output);
2301}
2302
2303static void
2304set_popup(struct shell_surface *shsurf,
2305 struct weston_surface *parent,
2306 struct weston_seat *seat,
2307 uint32_t serial,
2308 int32_t x,
2309 int32_t y)
2310{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002311 assert(parent != NULL);
2312
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313 shsurf->popup.shseat = get_shell_seat(seat);
2314 shsurf->popup.serial = serial;
2315 shsurf->popup.x = x;
2316 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002317
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002318 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002319}
2320
2321static void
2322shell_surface_set_popup(struct wl_client *client,
2323 struct wl_resource *resource,
2324 struct wl_resource *seat_resource,
2325 uint32_t serial,
2326 struct wl_resource *parent_resource,
2327 int32_t x, int32_t y, uint32_t flags)
2328{
2329 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002330 struct weston_surface *parent =
2331 wl_resource_get_user_data(parent_resource);
2332
2333 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002334
Rafael Antognolli03b16592013-12-03 15:35:42 -02002335 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002337 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002338 wl_resource_get_user_data(seat_resource),
2339 serial, x, y);
2340}
2341
2342static void
2343set_maximized(struct shell_surface *shsurf,
2344 struct weston_output *output)
2345{
2346 struct desktop_shell *shell;
2347 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002348
Philip Withnall352e7ed2013-11-25 18:01:35 +00002349 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002350
2351 shell = shell_surface_get_shell(shsurf);
2352 panel_height = get_output_panel_height(shell, shsurf->output);
2353 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2354
2355 shsurf->client->send_configure(shsurf->surface, edges,
2356 shsurf->output->width,
2357 shsurf->output->height - panel_height);
2358
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002359 shsurf->next_state.maximized = true;
2360 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002361 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002362}
2363
2364static void
2365unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002366{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002367 /* undo all maximized things here */
2368 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002369
2370 if (shsurf->saved_position_valid)
2371 weston_view_set_position(shsurf->view,
2372 shsurf->saved_x, shsurf->saved_y);
2373 else
2374 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002375
2376 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 wl_list_insert(&shsurf->view->geometry.transformation_list,
2378 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002379 shsurf->saved_rotation_valid = false;
2380 }
2381
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002382 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002383}
2384
Philip Withnallbecb77e2013-11-25 18:01:30 +00002385static void
2386shell_surface_set_maximized(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *output_resource)
2389{
2390 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2391 struct weston_output *output;
2392
2393 if (output_resource)
2394 output = wl_resource_get_user_data(output_resource);
2395 else
2396 output = NULL;
2397
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002398 shell_surface_set_parent(shsurf, NULL);
2399
Rafael Antognolli03b16592013-12-03 15:35:42 -02002400 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401 set_maximized(shsurf, output);
2402}
2403
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404/* This is only ever called from set_surface_type(), so there’s no need to
2405 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002406static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002407reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002408{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002409 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002410 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002411 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002413
Pekka Paalanen98262232011-12-01 10:42:22 +02002414 return 0;
2415}
2416
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002417static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418set_full_output(struct shell_surface *shsurf)
2419{
2420 shsurf->saved_x = shsurf->view->geometry.x;
2421 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002422 shsurf->saved_width = shsurf->surface->width;
2423 shsurf->saved_height = shsurf->surface->height;
2424 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002425 shsurf->saved_position_valid = true;
2426
2427 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2428 wl_list_remove(&shsurf->rotation.transform.link);
2429 wl_list_init(&shsurf->rotation.transform.link);
2430 weston_view_geometry_dirty(shsurf->view);
2431 shsurf->saved_rotation_valid = true;
2432 }
2433}
2434
2435static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002436set_surface_type(struct shell_surface *shsurf)
2437{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002438 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002440
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002442
Rafael Antognolli03b16592013-12-03 15:35:42 -02002443 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002444 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002445
2446 switch (shsurf->type) {
2447 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002448 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002449 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002450 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002451 weston_view_set_position(shsurf->view,
2452 pev->geometry.x + shsurf->transient.x,
2453 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002454 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002455 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002456
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002457 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 weston_view_set_position(shsurf->view, shsurf->transient.x,
2459 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002460 break;
2461
Philip Withnall0f640e22013-11-25 18:01:31 +00002462 case SHELL_SURFACE_POPUP:
2463 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002464 default:
2465 break;
2466 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002467
2468 /* Update the surface’s layer. */
2469 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002470}
2471
Tiago Vignattibe143262012-04-16 17:31:41 +03002472static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002473shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002474{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002475 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002476}
2477
Alex Wu21858432012-04-01 20:13:08 +08002478static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002479black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002480
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002482create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002483 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002484 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002485{
2486 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002488
2489 surface = weston_surface_create(ec);
2490 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002491 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002492 return NULL;
2493 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494 view = weston_view_create(surface);
2495 if (surface == NULL) {
2496 weston_log("no memory\n");
2497 weston_surface_destroy(surface);
2498 return NULL;
2499 }
Alex Wu4539b082012-03-01 12:57:46 +08002500
Alex Wu21858432012-04-01 20:13:08 +08002501 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002502 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002503 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002504 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002505 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002506 pixman_region32_fini(&surface->input);
2507 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002508
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002509 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002510 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511
2512 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002513}
2514
Philip Withnalled8f1a92013-11-25 18:01:43 +00002515static void
2516shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2517{
2518 struct weston_output *output = shsurf->fullscreen_output;
2519
Rafael Antognolli03b16592013-12-03 15:35:42 -02002520 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002521
2522 if (!shsurf->fullscreen.black_view)
2523 shsurf->fullscreen.black_view =
2524 create_black_surface(shsurf->surface->compositor,
2525 shsurf->surface,
2526 output->x, output->y,
2527 output->width,
2528 output->height);
2529
2530 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2531 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2532 wl_list_insert(&shsurf->view->layer_link,
2533 &shsurf->fullscreen.black_view->layer_link);
2534 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2535 weston_surface_damage(shsurf->surface);
2536}
2537
Alex Wu4539b082012-03-01 12:57:46 +08002538/* Create black surface and append it to the associated fullscreen surface.
2539 * Handle size dismatch and positioning according to the method. */
2540static void
2541shell_configure_fullscreen(struct shell_surface *shsurf)
2542{
2543 struct weston_output *output = shsurf->fullscreen_output;
2544 struct weston_surface *surface = shsurf->surface;
2545 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002547 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002548
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002549 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2550 restore_output_mode(output);
2551
Philip Withnalled8f1a92013-11-25 18:01:43 +00002552 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002553
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002555 &surf_width, &surf_height);
2556
Alex Wu4539b082012-03-01 12:57:46 +08002557 switch (shsurf->fullscreen.type) {
2558 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002559 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002561 break;
2562 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002563 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002564 if (output->width == surf_width &&
2565 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 weston_view_set_position(shsurf->view,
2567 output->x - surf_x,
2568 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002569 break;
2570 }
2571
Alex Wu4539b082012-03-01 12:57:46 +08002572 matrix = &shsurf->fullscreen.transform.matrix;
2573 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002574
Scott Moreau1bad5db2012-08-18 01:04:05 -06002575 output_aspect = (float) output->width /
2576 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 /* XXX: Use surf_width and surf_height here? */
2578 surface_aspect = (float) surface->width /
2579 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002580 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002581 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002582 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002583 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002584 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002585 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002586
Alex Wu4539b082012-03-01 12:57:46 +08002587 weston_matrix_scale(matrix, scale, scale, 1);
2588 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002590 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002591 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2592 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002594
Alex Wu4539b082012-03-01 12:57:46 +08002595 break;
2596 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002597 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002598 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002599 surf_width * surface->buffer_viewport.scale,
2600 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002601 shsurf->fullscreen.framerate};
2602
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002603 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002604 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 weston_view_set_position(shsurf->view,
2606 output->x - surf_x,
2607 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002608 shsurf->fullscreen.black_view->surface->width = output->width;
2609 shsurf->fullscreen.black_view->surface->height = output->height;
2610 weston_view_set_position(shsurf->fullscreen.black_view,
2611 output->x - surf_x,
2612 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002613 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002614 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002615 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002617 }
Alex Wubd3354b2012-04-17 17:20:49 +08002618 }
Alex Wu4539b082012-03-01 12:57:46 +08002619 break;
2620 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002622 break;
2623 default:
2624 break;
2625 }
2626}
2627
Alex Wu4539b082012-03-01 12:57:46 +08002628static void
2629shell_map_fullscreen(struct shell_surface *shsurf)
2630{
Alex Wubd3354b2012-04-17 17:20:49 +08002631 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002632}
2633
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002634static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002635set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2636{
2637 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002638 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002639 shsurf->transient.x = x;
2640 shsurf->transient.y = y;
2641 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002642
2643 shell_surface_set_parent(shsurf, NULL);
2644
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002645 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002646 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002647}
2648
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002649static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002650
2651static void
2652destroy_shell_seat(struct wl_listener *listener, void *data)
2653{
2654 struct shell_seat *shseat =
2655 container_of(listener,
2656 struct shell_seat, seat_destroy_listener);
2657 struct shell_surface *shsurf, *prev = NULL;
2658
2659 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002660 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002661 shseat->popup_grab.client = NULL;
2662
2663 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2664 shsurf->popup.shseat = NULL;
2665 if (prev) {
2666 wl_list_init(&prev->popup.grab_link);
2667 }
2668 prev = shsurf;
2669 }
2670 wl_list_init(&prev->popup.grab_link);
2671 }
2672
2673 wl_list_remove(&shseat->seat_destroy_listener.link);
2674 free(shseat);
2675}
2676
2677static struct shell_seat *
2678create_shell_seat(struct weston_seat *seat)
2679{
2680 struct shell_seat *shseat;
2681
2682 shseat = calloc(1, sizeof *shseat);
2683 if (!shseat) {
2684 weston_log("no memory to allocate shell seat\n");
2685 return NULL;
2686 }
2687
2688 shseat->seat = seat;
2689 wl_list_init(&shseat->popup_grab.surfaces_list);
2690
2691 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2692 wl_signal_add(&seat->destroy_signal,
2693 &shseat->seat_destroy_listener);
2694
2695 return shseat;
2696}
2697
2698static struct shell_seat *
2699get_shell_seat(struct weston_seat *seat)
2700{
2701 struct wl_listener *listener;
2702
2703 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2704 if (listener == NULL)
2705 return create_shell_seat(seat);
2706
2707 return container_of(listener,
2708 struct shell_seat, seat_destroy_listener);
2709}
2710
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002711static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002712popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002714 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002716 struct shell_seat *shseat =
2717 container_of(grab, struct shell_seat, popup_grab.grab);
2718 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002719 wl_fixed_t sx, sy;
2720
Jason Ekstranda7af7042013-10-12 22:38:11 -05002721 view = weston_compositor_pick_view(pointer->seat->compositor,
2722 pointer->x, pointer->y,
2723 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002724
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 if (view && view->surface->resource &&
2726 wl_resource_get_client(view->surface->resource) == client) {
2727 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002729 weston_pointer_set_focus(pointer, NULL,
2730 wl_fixed_from_int(0),
2731 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002732 }
2733}
2734
2735static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002736popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2737 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002739 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002740 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002741 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002742
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002743 weston_pointer_move(pointer, x, y);
2744
Neil Roberts96d790e2013-09-19 17:32:00 +01002745 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 weston_view_from_global_fixed(pointer->focus,
2747 pointer->x, pointer->y,
2748 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002749 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002750 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002751}
2752
2753static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002754popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002755 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002756{
2757 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002758 struct shell_seat *shseat =
2759 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002760 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002762 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002763 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002764
Neil Roberts96d790e2013-09-19 17:32:00 +01002765 resource_list = &grab->pointer->focus_resource_list;
2766 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002767 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002768 wl_resource_for_each(resource, resource_list) {
2769 wl_pointer_send_button(resource, serial,
2770 time, button, state);
2771 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002772 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002774 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002775 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002776 }
2777
Daniel Stone4dbadb12012-05-30 16:31:51 +01002778 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002779 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002780}
2781
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002782static void
2783popup_grab_cancel(struct weston_pointer_grab *grab)
2784{
2785 popup_grab_end(grab->pointer);
2786}
2787
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002788static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789 popup_grab_focus,
2790 popup_grab_motion,
2791 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002792 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002793};
2794
2795static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002796shell_surface_send_popup_done(struct shell_surface *shsurf)
2797{
2798 if (shell_surface_is_wl_shell_surface(shsurf))
2799 wl_shell_surface_send_popup_done(shsurf->resource);
2800 else if (shell_surface_is_xdg_popup(shsurf))
2801 xdg_popup_send_popup_done(shsurf->resource,
2802 shsurf->popup.serial);
2803}
2804
2805static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002806popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002807{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002808 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 struct shell_seat *shseat =
2810 container_of(grab, struct shell_seat, popup_grab.grab);
2811 struct shell_surface *shsurf;
2812 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002813
2814 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002815 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002816 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002817 shseat->popup_grab.grab.interface = NULL;
2818 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002819 /* Send the popup_done event to all the popups open */
2820 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002821 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002822 shsurf->popup.shseat = NULL;
2823 if (prev) {
2824 wl_list_init(&prev->popup.grab_link);
2825 }
2826 prev = shsurf;
2827 }
2828 wl_list_init(&prev->popup.grab_link);
2829 wl_list_init(&shseat->popup_grab.surfaces_list);
2830 }
2831}
2832
2833static void
2834add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2835{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002836 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002837
2838 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002839 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002840 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002841 /* We must make sure here that this popup was opened after
2842 * a mouse press, and not just by moving around with other
2843 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002844 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002845 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002846
Rob Bradforddfe31052013-06-26 19:49:11 +01002847 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002849 } else {
2850 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002851 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002852}
2853
2854static void
2855remove_popup_grab(struct shell_surface *shsurf)
2856{
2857 struct shell_seat *shseat = shsurf->popup.shseat;
2858
2859 wl_list_remove(&shsurf->popup.grab_link);
2860 wl_list_init(&shsurf->popup.grab_link);
2861 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002863 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002864 }
2865}
2866
2867static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002868shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002869{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002870 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872
Jason Ekstranda7af7042013-10-12 22:38:11 -05002873 shsurf->surface->output = parent_view->output;
2874 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002875
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 weston_view_set_transform_parent(shsurf->view, parent_view);
2877 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2878 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879
Kristian Høgsberge3148752013-05-06 23:19:49 -04002880 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002881 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002882 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002883 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002885 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002886}
2887
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002888static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002889 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002890 shell_surface_move,
2891 shell_surface_resize,
2892 shell_surface_set_toplevel,
2893 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002894 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002895 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002896 shell_surface_set_maximized,
2897 shell_surface_set_title,
2898 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002899};
2900
2901static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002902destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002903{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002904 struct shell_surface *child, *next;
2905
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002906 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2907
Giulio Camuffo5085a752013-03-25 21:42:45 +01002908 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2909 remove_popup_grab(shsurf);
2910 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911
Alex Wubd3354b2012-04-17 17:20:49 +08002912 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002913 shell_surface_is_top_fullscreen(shsurf))
2914 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002915
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 if (shsurf->fullscreen.black_view)
2917 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002918
Alex Wubd3354b2012-04-17 17:20:49 +08002919 /* As destroy_resource() use wl_list_for_each_safe(),
2920 * we can always remove the listener.
2921 */
2922 wl_list_remove(&shsurf->surface_destroy_listener.link);
2923 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002924 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002925 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002926
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 weston_view_destroy(shsurf->view);
2928
Philip Withnall648a4dd2013-11-25 18:01:44 +00002929 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002930 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2931 wl_list_remove(&child->children_link);
2932 child->parent = NULL;
2933 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002934
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002935 wl_list_remove(&shsurf->link);
2936 free(shsurf);
2937}
2938
2939static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002940shell_destroy_shell_surface(struct wl_resource *resource)
2941{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002942 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002943
2944 destroy_shell_surface(shsurf);
2945}
2946
2947static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002948shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002949{
2950 struct shell_surface *shsurf = container_of(listener,
2951 struct shell_surface,
2952 surface_destroy_listener);
2953
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002954 if (shsurf->resource)
2955 wl_resource_destroy(shsurf->resource);
2956 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002957 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002958}
2959
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002960static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002961shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002962static void
2963shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002964
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002965struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002966get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002967{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002968 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002969 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002970 else
2971 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002972}
2973
Rafael Antognollie2a34552013-12-03 15:35:45 -02002974static struct shell_surface *
2975create_common_surface(void *shell, struct weston_surface *surface,
2976 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002977{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002978 struct shell_surface *shsurf;
2979
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002980 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002981 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002982 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002983 }
2984
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002985 shsurf = calloc(1, sizeof *shsurf);
2986 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002987 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002988 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002989 }
2990
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 shsurf->view = weston_view_create(surface);
2992 if (!shsurf->view) {
2993 weston_log("no memory to allocate shell surface\n");
2994 free(shsurf);
2995 return NULL;
2996 }
2997
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002998 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002999 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02003000 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003001
Tiago Vignattibc052c92012-04-19 16:18:18 +03003002 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003003 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003004 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003005 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003006 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003007 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003008 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3009 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003011 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003012 wl_list_init(&shsurf->fullscreen.transform.link);
3013
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003014 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003015 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003016 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003017 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003018
3019 /* init link so its safe to always remove it in destroy_shell_surface */
3020 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003021 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003022
Pekka Paalanen460099f2012-01-20 16:48:25 +02003023 /* empty when not in use */
3024 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003025 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003026
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003027 wl_list_init(&shsurf->workspace_transform.link);
3028
Philip Withnall648a4dd2013-11-25 18:01:44 +00003029 wl_list_init(&shsurf->children_link);
3030 wl_list_init(&shsurf->children_list);
3031 shsurf->parent = NULL;
3032
Pekka Paalanen98262232011-12-01 10:42:22 +02003033 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003034
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003035 shsurf->client = client;
3036
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003037 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003038}
3039
Rafael Antognollie2a34552013-12-03 15:35:45 -02003040static struct shell_surface *
3041create_shell_surface(void *shell, struct weston_surface *surface,
3042 const struct weston_shell_client *client)
3043{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003044 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003045}
3046
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047static struct weston_view *
3048get_primary_view(void *shell, struct shell_surface *shsurf)
3049{
3050 return shsurf->view;
3051}
3052
Tiago Vignattibc052c92012-04-19 16:18:18 +03003053static void
3054shell_get_shell_surface(struct wl_client *client,
3055 struct wl_resource *resource,
3056 uint32_t id,
3057 struct wl_resource *surface_resource)
3058{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003059 struct weston_surface *surface =
3060 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003061 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003062 struct shell_surface *shsurf;
3063
3064 if (get_shell_surface(surface)) {
3065 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003066 WL_DISPLAY_ERROR_INVALID_OBJECT,
3067 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003068 return;
3069 }
3070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003071 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003072 if (!shsurf) {
3073 wl_resource_post_error(surface_resource,
3074 WL_DISPLAY_ERROR_INVALID_OBJECT,
3075 "surface->configure already set");
3076 return;
3077 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003078
Jason Ekstranda85118c2013-06-27 20:17:02 -05003079 shsurf->resource =
3080 wl_resource_create(client,
3081 &wl_shell_surface_interface, 1, id);
3082 wl_resource_set_implementation(shsurf->resource,
3083 &shell_surface_implementation,
3084 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003085}
3086
Rafael Antognollie2a34552013-12-03 15:35:45 -02003087static bool
3088shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3089{
3090 return wl_resource_instance_of(shsurf->resource,
3091 &wl_shell_surface_interface,
3092 &shell_surface_implementation);
3093}
3094
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003095static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003096 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003097};
3098
Rafael Antognollie2a34552013-12-03 15:35:45 -02003099/****************************
3100 * xdg-shell implementation */
3101
3102static void
3103xdg_surface_destroy(struct wl_client *client,
3104 struct wl_resource *resource)
3105{
3106 wl_resource_destroy(resource);
3107}
3108
3109static void
3110xdg_surface_pong(struct wl_client *client,
3111 struct wl_resource *resource,
3112 uint32_t serial)
3113{
3114 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3115
3116 surface_pong(shsurf, serial);
3117}
3118
3119static void
3120xdg_surface_set_app_id(struct wl_client *client,
3121 struct wl_resource *resource,
3122 const char *app_id)
3123{
3124 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3125
3126 free(shsurf->class);
3127 shsurf->class = strdup(app_id);
3128}
3129
3130static void
3131xdg_surface_set_title(struct wl_client *client,
3132 struct wl_resource *resource, const char *title)
3133{
3134 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3135
3136 set_title(shsurf, title);
3137}
3138
3139static void
3140xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3141 struct wl_resource *seat_resource, uint32_t serial)
3142{
3143 common_surface_move(resource, seat_resource, serial);
3144}
3145
3146static void
3147xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3148 struct wl_resource *seat_resource, uint32_t serial,
3149 uint32_t edges)
3150{
3151 common_surface_resize(resource, seat_resource, serial, edges);
3152}
3153
3154static void
3155xdg_surface_set_output(struct wl_client *client,
3156 struct wl_resource *resource,
3157 struct wl_resource *output_resource)
3158{
3159 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3160 struct weston_output *output;
3161
3162 if (output_resource)
3163 output = wl_resource_get_user_data(output_resource);
3164 else
3165 output = NULL;
3166
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003167 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003168}
3169
3170static void
3171xdg_surface_set_fullscreen(struct wl_client *client,
3172 struct wl_resource *resource)
3173{
3174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3175
3176 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3177 return;
3178
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003179 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180 set_fullscreen(shsurf,
3181 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003182 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003183}
3184
3185static void
3186xdg_surface_unset_fullscreen(struct wl_client *client,
3187 struct wl_resource *resource)
3188{
3189 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003190 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003191
3192 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3193 return;
3194
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003195 if (!shsurf->next_state.fullscreen)
3196 return;
3197
Rafael Antognollie2a34552013-12-03 15:35:45 -02003198 shsurf->next_state.fullscreen = false;
3199 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003200
3201 if (shsurf->saved_size_valid) {
3202 width = shsurf->saved_width;
3203 height = shsurf->saved_height;
3204 shsurf->saved_size_valid = false;
3205 } else {
3206 width = shsurf->surface->width;
3207 height = shsurf->surface->height;
3208 }
3209
3210 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003211}
3212
3213static void
3214xdg_surface_set_maximized(struct wl_client *client,
3215 struct wl_resource *resource)
3216{
3217 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3218
3219 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3220 return;
3221
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003222 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003223 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003224}
3225
3226static void
3227xdg_surface_unset_maximized(struct wl_client *client,
3228 struct wl_resource *resource)
3229{
3230 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003231 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003232
3233 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3234 return;
3235
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003236 if (!shsurf->next_state.maximized)
3237 return;
3238
Rafael Antognollie2a34552013-12-03 15:35:45 -02003239 shsurf->next_state.maximized = false;
3240 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003241
3242 if (shsurf->saved_size_valid) {
3243 width = shsurf->saved_width;
3244 height = shsurf->saved_height;
3245 shsurf->saved_size_valid = false;
3246 } else {
3247 width = shsurf->surface->width;
3248 height = shsurf->surface->height;
3249 }
3250
3251 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003252}
3253
3254static const struct xdg_surface_interface xdg_surface_implementation = {
3255 xdg_surface_destroy,
3256 xdg_surface_set_transient_for,
3257 xdg_surface_set_title,
3258 xdg_surface_set_app_id,
3259 xdg_surface_pong,
3260 xdg_surface_move,
3261 xdg_surface_resize,
3262 xdg_surface_set_output,
3263 xdg_surface_set_fullscreen,
3264 xdg_surface_unset_fullscreen,
3265 xdg_surface_set_maximized,
3266 xdg_surface_unset_maximized,
3267 NULL /* set_minimized */
3268};
3269
3270static void
3271xdg_send_configure(struct weston_surface *surface,
3272 uint32_t edges, int32_t width, int32_t height)
3273{
3274 struct shell_surface *shsurf = get_shell_surface(surface);
3275
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003276 assert(shsurf);
3277
Rafael Antognollie2a34552013-12-03 15:35:45 -02003278 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3279}
3280
3281static const struct weston_shell_client xdg_client = {
3282 xdg_send_configure
3283};
3284
3285static void
3286xdg_use_unstable_version(struct wl_client *client,
3287 struct wl_resource *resource,
3288 int32_t version)
3289{
3290 if (version > 1) {
3291 wl_resource_post_error(resource,
3292 1,
3293 "xdg-shell:: version not implemented yet.");
3294 return;
3295 }
3296}
3297
3298static struct shell_surface *
3299create_xdg_surface(void *shell, struct weston_surface *surface,
3300 const struct weston_shell_client *client)
3301{
3302 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003303
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003304 shsurf = create_common_surface(shell, surface, client);
3305 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003306
3307 return shsurf;
3308}
3309
3310static void
3311xdg_get_xdg_surface(struct wl_client *client,
3312 struct wl_resource *resource,
3313 uint32_t id,
3314 struct wl_resource *surface_resource)
3315{
3316 struct weston_surface *surface =
3317 wl_resource_get_user_data(surface_resource);
3318 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3319 struct shell_surface *shsurf;
3320
3321 if (get_shell_surface(surface)) {
3322 wl_resource_post_error(surface_resource,
3323 WL_DISPLAY_ERROR_INVALID_OBJECT,
3324 "desktop_shell::get_shell_surface already requested");
3325 return;
3326 }
3327
3328 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3329 if (!shsurf) {
3330 wl_resource_post_error(surface_resource,
3331 WL_DISPLAY_ERROR_INVALID_OBJECT,
3332 "surface->configure already set");
3333 return;
3334 }
3335
3336 shsurf->resource =
3337 wl_resource_create(client,
3338 &xdg_surface_interface, 1, id);
3339 wl_resource_set_implementation(shsurf->resource,
3340 &xdg_surface_implementation,
3341 shsurf, shell_destroy_shell_surface);
3342}
3343
3344static bool
3345shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3346{
3347 return wl_resource_instance_of(shsurf->resource,
3348 &xdg_surface_interface,
3349 &xdg_surface_implementation);
3350}
3351
3352/* xdg-popup implementation */
3353
3354static void
3355xdg_popup_destroy(struct wl_client *client,
3356 struct wl_resource *resource)
3357{
3358 wl_resource_destroy(resource);
3359}
3360
3361static void
3362xdg_popup_pong(struct wl_client *client,
3363 struct wl_resource *resource,
3364 uint32_t serial)
3365{
3366 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3367
3368 surface_pong(shsurf, serial);
3369}
3370
3371static const struct xdg_popup_interface xdg_popup_implementation = {
3372 xdg_popup_destroy,
3373 xdg_popup_pong
3374};
3375
3376static void
3377xdg_popup_send_configure(struct weston_surface *surface,
3378 uint32_t edges, int32_t width, int32_t height)
3379{
3380}
3381
3382static const struct weston_shell_client xdg_popup_client = {
3383 xdg_popup_send_configure
3384};
3385
3386static struct shell_surface *
3387create_xdg_popup(void *shell, struct weston_surface *surface,
3388 const struct weston_shell_client *client,
3389 struct weston_surface *parent,
3390 struct shell_seat *seat,
3391 uint32_t serial,
3392 int32_t x, int32_t y)
3393{
3394 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003395
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003396 shsurf = create_common_surface(shell, surface, client);
3397 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003398 shsurf->popup.shseat = seat;
3399 shsurf->popup.serial = serial;
3400 shsurf->popup.x = x;
3401 shsurf->popup.y = y;
3402 shell_surface_set_parent(shsurf, parent);
3403
3404 return shsurf;
3405}
3406
3407static void
3408xdg_get_xdg_popup(struct wl_client *client,
3409 struct wl_resource *resource,
3410 uint32_t id,
3411 struct wl_resource *surface_resource,
3412 struct wl_resource *parent_resource,
3413 struct wl_resource *seat_resource,
3414 uint32_t serial,
3415 int32_t x, int32_t y, uint32_t flags)
3416{
3417 struct weston_surface *surface =
3418 wl_resource_get_user_data(surface_resource);
3419 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3420 struct shell_surface *shsurf;
3421 struct weston_surface *parent;
3422 struct shell_seat *seat;
3423
3424 if (get_shell_surface(surface)) {
3425 wl_resource_post_error(surface_resource,
3426 WL_DISPLAY_ERROR_INVALID_OBJECT,
3427 "desktop_shell::get_shell_surface already requested");
3428 return;
3429 }
3430
3431 if (!parent_resource) {
3432 wl_resource_post_error(surface_resource,
3433 WL_DISPLAY_ERROR_INVALID_OBJECT,
3434 "xdg_shell::get_xdg_popup requires a parent shell surface");
3435 }
3436
3437 parent = wl_resource_get_user_data(parent_resource);
3438 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3439
3440 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3441 parent, seat, serial, x, y);
3442 if (!shsurf) {
3443 wl_resource_post_error(surface_resource,
3444 WL_DISPLAY_ERROR_INVALID_OBJECT,
3445 "surface->configure already set");
3446 return;
3447 }
3448
3449 shsurf->resource =
3450 wl_resource_create(client,
3451 &xdg_popup_interface, 1, id);
3452 wl_resource_set_implementation(shsurf->resource,
3453 &xdg_popup_implementation,
3454 shsurf, shell_destroy_shell_surface);
3455}
3456
3457static bool
3458shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3459{
3460 return wl_resource_instance_of(shsurf->resource,
3461 &xdg_popup_interface,
3462 &xdg_popup_implementation);
3463}
3464
3465static const struct xdg_shell_interface xdg_implementation = {
3466 xdg_use_unstable_version,
3467 xdg_get_xdg_surface,
3468 xdg_get_xdg_popup
3469};
3470
3471static int
3472xdg_shell_unversioned_dispatch(const void *implementation,
3473 void *_target, uint32_t opcode,
3474 const struct wl_message *message,
3475 union wl_argument *args)
3476{
3477 struct wl_resource *resource = _target;
3478 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3479
3480 if (opcode != 0) {
3481 wl_resource_post_error(resource,
3482 WL_DISPLAY_ERROR_INVALID_OBJECT,
3483 "must call use_unstable_version first");
3484 return 0;
3485 }
3486
3487#define XDG_SERVER_VERSION 1
3488
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003489 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3490 "shell implementation doesn't match protocol version");
3491
Rafael Antognollie2a34552013-12-03 15:35:45 -02003492 if (args[0].i != XDG_SERVER_VERSION) {
3493 wl_resource_post_error(resource,
3494 WL_DISPLAY_ERROR_INVALID_OBJECT,
3495 "incompatible version, server is %d "
3496 "client wants %d",
3497 XDG_SERVER_VERSION, args[0].i);
3498 return 0;
3499 }
3500
3501 wl_resource_set_implementation(resource, &xdg_implementation,
3502 shell, NULL);
3503
3504 return 1;
3505}
3506
3507/* end of xdg-shell implementation */
3508/***********************************/
3509
Kristian Høgsberg07937562011-04-12 17:25:42 -04003510static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003511shell_fade(struct desktop_shell *shell, enum fade_type type);
3512
3513static int
3514screensaver_timeout(void *data)
3515{
3516 struct desktop_shell *shell = data;
3517
3518 shell_fade(shell, FADE_OUT);
3519
3520 return 1;
3521}
3522
3523static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003524handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003525{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003526 struct desktop_shell *shell =
3527 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003528
Pekka Paalanen18027e52011-12-02 16:31:49 +02003529 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003530
3531 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003532 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003533}
3534
3535static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003536launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003537{
3538 if (shell->screensaver.binding)
3539 return;
3540
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003541 if (!shell->screensaver.path) {
3542 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003543 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003544 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003545
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003546 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003547 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003548 return;
3549 }
3550
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003551 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003552 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003553 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003554 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003555}
3556
3557static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003558terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003559{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003560 if (shell->screensaver.process.pid == 0)
3561 return;
3562
3563 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003564}
3565
3566static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003567configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003570
Jason Ekstranda7af7042013-10-12 22:38:11 -05003571 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3572 if (v->output == ev->output && v != ev) {
3573 weston_view_unmap(v);
3574 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003575 }
3576 }
3577
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003578 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003579
Jason Ekstranda7af7042013-10-12 22:38:11 -05003580 if (wl_list_empty(&ev->layer_link)) {
3581 wl_list_insert(&layer->view_list, &ev->layer_link);
3582 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003583 }
3584}
3585
3586static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003587background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003588{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003589 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003590 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003591
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 view = container_of(es->views.next, struct weston_view, surface_link);
3593
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003594 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003595}
3596
3597static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003598desktop_shell_set_background(struct wl_client *client,
3599 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003600 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003601 struct wl_resource *surface_resource)
3602{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003603 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003604 struct weston_surface *surface =
3605 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003607
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003608 if (surface->configure) {
3609 wl_resource_post_error(surface_resource,
3610 WL_DISPLAY_ERROR_INVALID_OBJECT,
3611 "surface role already assigned");
3612 return;
3613 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003614
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3616 weston_view_destroy(view);
3617 view = weston_view_create(surface);
3618
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003619 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003620 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003621 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003623 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003624 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003625 surface->output->width,
3626 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003627}
3628
3629static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003630panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003631{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003632 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003634
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 view = container_of(es->views.next, struct weston_view, surface_link);
3636
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003637 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003638}
3639
3640static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003641desktop_shell_set_panel(struct wl_client *client,
3642 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003643 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003644 struct wl_resource *surface_resource)
3645{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003646 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003647 struct weston_surface *surface =
3648 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003649 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003650
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003651 if (surface->configure) {
3652 wl_resource_post_error(surface_resource,
3653 WL_DISPLAY_ERROR_INVALID_OBJECT,
3654 "surface role already assigned");
3655 return;
3656 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003657
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3659 weston_view_destroy(view);
3660 view = weston_view_create(surface);
3661
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003662 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003663 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003664 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003665 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003666 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003667 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003668 surface->output->width,
3669 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003670}
3671
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003672static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003673lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003674{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003675 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003676 struct weston_view *view;
3677
3678 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003679
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003680 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003681 return;
3682
Jason Ekstranda7af7042013-10-12 22:38:11 -05003683 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003684
3685 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003686 wl_list_insert(&shell->lock_layer.view_list,
3687 &view->layer_link);
3688 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003689 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003690 }
3691}
3692
3693static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003694handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003695{
Tiago Vignattibe143262012-04-16 17:31:41 +03003696 struct desktop_shell *shell =
3697 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003698
Martin Minarik6d118362012-06-07 18:01:59 +02003699 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003700 shell->lock_surface = NULL;
3701}
3702
3703static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003704desktop_shell_set_lock_surface(struct wl_client *client,
3705 struct wl_resource *resource,
3706 struct wl_resource *surface_resource)
3707{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003708 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003709 struct weston_surface *surface =
3710 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003711
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003712 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003713
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003714 if (!shell->locked)
3715 return;
3716
Pekka Paalanen98262232011-12-01 10:42:22 +02003717 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003718
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003719 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003720 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003721 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003722
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003723 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003724 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003725 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003726}
3727
3728static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003729resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003730{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003731 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003732
Pekka Paalanen77346a62011-11-30 16:26:35 +02003733 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003734
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003735 wl_list_remove(&shell->lock_layer.link);
3736 wl_list_insert(&shell->compositor->cursor_layer.link,
3737 &shell->fullscreen_layer.link);
3738 wl_list_insert(&shell->fullscreen_layer.link,
3739 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003740 if (shell->showing_input_panels) {
3741 wl_list_insert(&shell->panel_layer.link,
3742 &shell->input_panel_layer.link);
3743 wl_list_insert(&shell->input_panel_layer.link,
3744 &ws->layer.link);
3745 } else {
3746 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3747 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003748
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003749 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003750
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003751 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003752 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003753 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003754}
3755
3756static void
3757desktop_shell_unlock(struct wl_client *client,
3758 struct wl_resource *resource)
3759{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003760 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003761
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003762 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003763
3764 if (shell->locked)
3765 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003766}
3767
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003768static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003769desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003770 struct wl_resource *resource,
3771 struct wl_resource *surface_resource)
3772{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003773 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003774
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003775 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003776 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003777}
3778
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003779static void
3780desktop_shell_desktop_ready(struct wl_client *client,
3781 struct wl_resource *resource)
3782{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003783 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003784
3785 shell_fade_startup(shell);
3786}
3787
Kristian Høgsberg75840622011-09-06 13:48:16 -04003788static const struct desktop_shell_interface desktop_shell_implementation = {
3789 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003790 desktop_shell_set_panel,
3791 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003792 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003793 desktop_shell_set_grab_surface,
3794 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003795};
3796
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003797static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003798get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003799{
3800 struct shell_surface *shsurf;
3801
3802 shsurf = get_shell_surface(surface);
3803 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003804 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003805 return shsurf->type;
3806}
3807
Kristian Høgsberg75840622011-09-06 13:48:16 -04003808static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003809move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003810{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003811 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003812 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003813 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003814
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003815 if (seat->pointer->focus == NULL)
3816 return;
3817
3818 focus = seat->pointer->focus->surface;
3819
Pekka Paalanen01388e22013-04-25 13:57:44 +03003820 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003821 if (surface == NULL)
3822 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003823
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003824 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003825 if (shsurf == NULL || shsurf->state.fullscreen ||
3826 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003827 return;
3828
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003829 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003830}
3831
3832static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003833maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3834{
3835 struct weston_surface *focus = seat->pointer->focus->surface;
3836 struct weston_surface *surface;
3837 struct shell_surface *shsurf;
3838
3839 surface = weston_surface_get_main_surface(focus);
3840 if (surface == NULL)
3841 return;
3842
3843 shsurf = get_shell_surface(surface);
3844 if (shsurf == NULL)
3845 return;
3846
3847 if (!shell_surface_is_xdg_surface(shsurf))
3848 return;
3849
3850 if (shsurf->state.maximized)
3851 xdg_surface_send_request_unset_maximized(shsurf->resource);
3852 else
3853 xdg_surface_send_request_set_maximized(shsurf->resource);
3854}
3855
3856static void
3857fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3858{
3859 struct weston_surface *focus = seat->pointer->focus->surface;
3860 struct weston_surface *surface;
3861 struct shell_surface *shsurf;
3862
3863 surface = weston_surface_get_main_surface(focus);
3864 if (surface == NULL)
3865 return;
3866
3867 shsurf = get_shell_surface(surface);
3868 if (shsurf == NULL)
3869 return;
3870
3871 if (!shell_surface_is_xdg_surface(shsurf))
3872 return;
3873
3874 if (shsurf->state.fullscreen)
3875 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3876 else
3877 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3878}
3879
3880static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003881touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3882{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003883 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003884 struct weston_surface *surface;
3885 struct shell_surface *shsurf;
3886
3887 surface = weston_surface_get_main_surface(focus);
3888 if (surface == NULL)
3889 return;
3890
3891 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003892 if (shsurf == NULL || shsurf->state.fullscreen ||
3893 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003894 return;
3895
3896 surface_touch_move(shsurf, (struct weston_seat *) seat);
3897}
3898
3899static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003900resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003901{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003902 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003903 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003904 uint32_t edges = 0;
3905 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003906 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003907
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003908 if (seat->pointer->focus == NULL)
3909 return;
3910
3911 focus = seat->pointer->focus->surface;
3912
Pekka Paalanen01388e22013-04-25 13:57:44 +03003913 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003914 if (surface == NULL)
3915 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003916
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003917 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003918 if (shsurf == NULL || shsurf->state.fullscreen ||
3919 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003920 return;
3921
Jason Ekstranda7af7042013-10-12 22:38:11 -05003922 weston_view_from_global(shsurf->view,
3923 wl_fixed_to_int(seat->pointer->grab_x),
3924 wl_fixed_to_int(seat->pointer->grab_y),
3925 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003926
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003927 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003928 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003929 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003930 edges |= 0;
3931 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003932 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003933
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003934 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003935 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003936 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003937 edges |= 0;
3938 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003939 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003940
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003941 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003942}
3943
3944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003945surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003946 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003947{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003948 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003949 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003950 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003951 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003952
Pekka Paalanen01388e22013-04-25 13:57:44 +03003953 /* XXX: broken for windows containing sub-surfaces */
3954 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003955 if (surface == NULL)
3956 return;
3957
3958 shsurf = get_shell_surface(surface);
3959 if (!shsurf)
3960 return;
3961
Jason Ekstranda7af7042013-10-12 22:38:11 -05003962 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003963
Jason Ekstranda7af7042013-10-12 22:38:11 -05003964 if (shsurf->view->alpha > 1.0)
3965 shsurf->view->alpha = 1.0;
3966 if (shsurf->view->alpha < step)
3967 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003968
Jason Ekstranda7af7042013-10-12 22:38:11 -05003969 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003970 weston_surface_damage(surface);
3971}
3972
3973static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003974do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003975 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003976{
Daniel Stone37816df2012-05-16 18:45:18 +01003977 struct weston_seat *ws = (struct weston_seat *) seat;
3978 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003979 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003980 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003981
3982 wl_list_for_each(output, &compositor->output_list, link) {
3983 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003984 wl_fixed_to_double(seat->pointer->x),
3985 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003986 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003987 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003988 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003989 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003990 increment = -output->zoom.increment;
3991 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003992 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003993 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003994 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003995 else
3996 increment = 0;
3997
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003998 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003999
Scott Moreaue6603982012-06-11 13:07:51 -06004000 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004001 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004002 else if (output->zoom.level > output->zoom.max_level)
4003 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004004 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004005 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004006 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004007
Scott Moreaue6603982012-06-11 13:07:51 -06004008 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004009
Jason Ekstranda7af7042013-10-12 22:38:11 -05004010 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004011 }
4012 }
4013}
4014
Scott Moreauccbf29d2012-02-22 14:21:41 -07004015static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004016zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004017 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004018{
4019 do_zoom(seat, time, 0, axis, value);
4020}
4021
4022static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004023zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004024 void *data)
4025{
4026 do_zoom(seat, time, key, 0, 0);
4027}
4028
4029static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004030terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004031 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004032{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004033 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004034
Daniel Stone325fc2d2012-05-30 16:31:58 +01004035 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004036}
4037
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004038static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004039rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4040 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004041{
4042 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004043 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004044 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004045 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004046 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004047
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004048 weston_pointer_move(pointer, x, y);
4049
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004050 if (!shsurf)
4051 return;
4052
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004053 cx = 0.5f * shsurf->surface->width;
4054 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004055
Daniel Stone37816df2012-05-16 18:45:18 +01004056 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4057 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004058 r = sqrtf(dx * dx + dy * dy);
4059
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004060 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004061 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004062
4063 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004064 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004065 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004066
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004067 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004068 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004069
4070 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004071 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004072 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004073 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004074 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004075
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004076 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004077 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004078 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004079 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004080 wl_list_init(&shsurf->rotation.transform.link);
4081 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004082 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004083 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004084
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004085 /* We need to adjust the position of the surface
4086 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004087 cposx = shsurf->view->geometry.x + cx;
4088 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004089 dposx = rotate->center.x - cposx;
4090 dposy = rotate->center.y - cposy;
4091 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004092 weston_view_set_position(shsurf->view,
4093 shsurf->view->geometry.x + dposx,
4094 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004095 }
4096
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004097 /* Repaint implies weston_surface_update_transform(), which
4098 * lazily applies the damage due to rotation update.
4099 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004100 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004101}
4102
4103static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004104rotate_grab_button(struct weston_pointer_grab *grab,
4105 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004106{
4107 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004108 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004109 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004110 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004111 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004112
Daniel Stone4dbadb12012-05-30 16:31:51 +01004113 if (pointer->button_count == 0 &&
4114 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004115 if (shsurf)
4116 weston_matrix_multiply(&shsurf->rotation.rotation,
4117 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004118 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004119 free(rotate);
4120 }
4121}
4122
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004123static void
4124rotate_grab_cancel(struct weston_pointer_grab *grab)
4125{
4126 struct rotate_grab *rotate =
4127 container_of(grab, struct rotate_grab, base.grab);
4128
4129 shell_grab_end(&rotate->base);
4130 free(rotate);
4131}
4132
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004133static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004134 noop_grab_focus,
4135 rotate_grab_motion,
4136 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004137 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004138};
4139
4140static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004141surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004142{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004143 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004144 float dx, dy;
4145 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004146
Pekka Paalanen460099f2012-01-20 16:48:25 +02004147 rotate = malloc(sizeof *rotate);
4148 if (!rotate)
4149 return;
4150
Jason Ekstranda7af7042013-10-12 22:38:11 -05004151 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004152 surface->surface->width * 0.5f,
4153 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004154 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004155
Daniel Stone37816df2012-05-16 18:45:18 +01004156 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4157 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004158 r = sqrtf(dx * dx + dy * dy);
4159 if (r > 20.0f) {
4160 struct weston_matrix inverse;
4161
4162 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004163 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004164 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004165
4166 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004167 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004168 } else {
4169 weston_matrix_init(&surface->rotation.rotation);
4170 weston_matrix_init(&rotate->rotation);
4171 }
4172
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004173 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4174 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004175}
4176
4177static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004178rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004179 void *data)
4180{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004181 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004182 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004183 struct shell_surface *surface;
4184
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004185 if (seat->pointer->focus == NULL)
4186 return;
4187
4188 focus = seat->pointer->focus->surface;
4189
Pekka Paalanen01388e22013-04-25 13:57:44 +03004190 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004191 if (base_surface == NULL)
4192 return;
4193
4194 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004195 if (surface == NULL || surface->state.fullscreen ||
4196 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004197 return;
4198
4199 surface_rotate(surface, seat);
4200}
4201
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004202/* Move all fullscreen layers down to the current workspace in a non-reversible
4203 * manner. This should be used when implementing shell-wide overlays, such as
4204 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004205void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004206lower_fullscreen_layer(struct desktop_shell *shell)
4207{
4208 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004210
4211 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004212 wl_list_for_each_reverse_safe(view, prev,
4213 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004214 layer_link) {
4215 wl_list_remove(&view->layer_link);
4216 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4217 weston_view_damage_below(view);
4218 weston_surface_damage(view->surface);
4219 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004220}
4221
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004222void
Tiago Vignattibe143262012-04-16 17:31:41 +03004223activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004224 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004225{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004226 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004227 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004228 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004229 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004230 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004231
Pekka Paalanen01388e22013-04-25 13:57:44 +03004232 main_surface = weston_surface_get_main_surface(es);
4233
Daniel Stone37816df2012-05-16 18:45:18 +01004234 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004235
Jonas Ådahl8538b222012-08-29 22:13:03 +02004236 state = ensure_focus_state(shell, seat);
4237 if (state == NULL)
4238 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004239
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004240 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004241 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004242
Rafael Antognolli03b16592013-12-03 15:35:42 -02004243 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004244 assert(shsurf);
4245
Rafael Antognolli03b16592013-12-03 15:35:42 -02004246 if (shsurf->state.fullscreen)
4247 shell_configure_fullscreen(shsurf);
4248 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004249 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004250
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004251 if (shell->focus_animation_type != ANIMATION_NONE) {
4252 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004253 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004254 }
4255
4256 /* Update the surface’s layer. This brings it to the top of the stacking
4257 * order as appropriate. */
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004258 shell_surface_update_layer(shsurf);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004259}
4260
Alex Wu21858432012-04-01 20:13:08 +08004261/* no-op func for checking black surface */
4262static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004263black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004264{
4265}
4266
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004267static bool
Alex Wu21858432012-04-01 20:13:08 +08004268is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4269{
4270 if (es->configure == black_surface_configure) {
4271 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004272 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004273 return true;
4274 }
4275 return false;
4276}
4277
Kristian Høgsberg75840622011-09-06 13:48:16 -04004278static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004279activate_binding(struct weston_seat *seat,
4280 struct desktop_shell *shell,
4281 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004282{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004283 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004284
Alex Wu9c35e6b2012-03-05 14:13:13 +08004285 if (!focus)
4286 return;
4287
Pekka Paalanen01388e22013-04-25 13:57:44 +03004288 if (is_black_surface(focus, &main_surface))
4289 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004290
Pekka Paalanen01388e22013-04-25 13:57:44 +03004291 main_surface = weston_surface_get_main_surface(focus);
4292 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004293 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004294
Neil Robertsa28c6932013-10-03 16:43:04 +01004295 activate(shell, focus, seat);
4296}
4297
4298static void
4299click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4300 void *data)
4301{
4302 if (seat->pointer->grab != &seat->pointer->default_grab)
4303 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004304 if (seat->pointer->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->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004308}
4309
4310static void
4311touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4312{
4313 if (seat->touch->grab != &seat->touch->default_grab)
4314 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004315 if (seat->touch->focus == NULL)
4316 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004317
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004318 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004319}
4320
4321static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004322lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004323{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004324 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004325
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004326 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004327 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004328 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004329 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004330
4331 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004332
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004333 /* Hide all surfaces by removing the fullscreen, panel and
4334 * toplevel layers. This way nothing else can show or receive
4335 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004336
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004337 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004338 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004339 if (shell->showing_input_panels)
4340 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004341 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004342 wl_list_insert(&shell->compositor->cursor_layer.link,
4343 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004344
Pekka Paalanen77346a62011-11-30 16:26:35 +02004345 launch_screensaver(shell);
4346
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004347 /* TODO: disable bindings that should not work while locked. */
4348
4349 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004350}
4351
4352static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004353unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004354{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004355 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004356 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004357 return;
4358 }
4359
4360 /* If desktop-shell client has gone away, unlock immediately. */
4361 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004362 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004363 return;
4364 }
4365
4366 if (shell->prepare_event_sent)
4367 return;
4368
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004369 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004370 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004371}
4372
4373static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004375{
4376 struct desktop_shell *shell = data;
4377
4378 shell->fade.animation = NULL;
4379
4380 switch (shell->fade.type) {
4381 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004382 weston_surface_destroy(shell->fade.view->surface);
4383 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004384 break;
4385 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004386 lock(shell);
4387 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004388 default:
4389 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004390 }
4391}
4392
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004394shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004395{
4396 struct weston_compositor *compositor = shell->compositor;
4397 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004399
4400 surface = weston_surface_create(compositor);
4401 if (!surface)
4402 return NULL;
4403
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 view = weston_view_create(surface);
4405 if (!view) {
4406 weston_surface_destroy(surface);
4407 return NULL;
4408 }
4409
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004410 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004411 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004412 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 wl_list_insert(&compositor->fade_layer.view_list,
4414 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004415 pixman_region32_init(&surface->input);
4416
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004418}
4419
4420static void
4421shell_fade(struct desktop_shell *shell, enum fade_type type)
4422{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004423 float tint;
4424
4425 switch (type) {
4426 case FADE_IN:
4427 tint = 0.0;
4428 break;
4429 case FADE_OUT:
4430 tint = 1.0;
4431 break;
4432 default:
4433 weston_log("shell: invalid fade type\n");
4434 return;
4435 }
4436
4437 shell->fade.type = type;
4438
Jason Ekstranda7af7042013-10-12 22:38:11 -05004439 if (shell->fade.view == NULL) {
4440 shell->fade.view = shell_fade_create_surface(shell);
4441 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004442 return;
4443
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444 shell->fade.view->alpha = 1.0 - tint;
4445 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004446 }
4447
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004448 if (shell->fade.view->output == NULL) {
4449 /* If the black view gets a NULL output, we lost the
4450 * last output and we'll just cancel the fade. This
4451 * happens when you close the last window under the
4452 * X11 or Wayland backends. */
4453 shell->locked = false;
4454 weston_surface_destroy(shell->fade.view->surface);
4455 shell->fade.view = NULL;
4456 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004457 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004458 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004459 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004460 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004461 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004462 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004463 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004464}
4465
4466static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004467do_shell_fade_startup(void *data)
4468{
4469 struct desktop_shell *shell = data;
4470
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004471 if (shell->startup_animation_type == ANIMATION_FADE)
4472 shell_fade(shell, FADE_IN);
4473 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 weston_surface_destroy(shell->fade.view->surface);
4475 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004476 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004477}
4478
4479static void
4480shell_fade_startup(struct desktop_shell *shell)
4481{
4482 struct wl_event_loop *loop;
4483
4484 if (!shell->fade.startup_timer)
4485 return;
4486
4487 wl_event_source_remove(shell->fade.startup_timer);
4488 shell->fade.startup_timer = NULL;
4489
4490 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4491 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4492}
4493
4494static int
4495fade_startup_timeout(void *data)
4496{
4497 struct desktop_shell *shell = data;
4498
4499 shell_fade_startup(shell);
4500 return 0;
4501}
4502
4503static void
4504shell_fade_init(struct desktop_shell *shell)
4505{
4506 /* Make compositor output all black, and wait for the desktop-shell
4507 * client to signal it is ready, then fade in. The timer triggers a
4508 * fade-in, in case the desktop-shell client takes too long.
4509 */
4510
4511 struct wl_event_loop *loop;
4512
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004514 weston_log("%s: warning: fade surface already exists\n",
4515 __func__);
4516 return;
4517 }
4518
Jason Ekstranda7af7042013-10-12 22:38:11 -05004519 shell->fade.view = shell_fade_create_surface(shell);
4520 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004521 return;
4522
Jason Ekstranda7af7042013-10-12 22:38:11 -05004523 weston_view_update_transform(shell->fade.view);
4524 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004525
4526 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4527 shell->fade.startup_timer =
4528 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4529 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4530}
4531
4532static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004533idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004534{
4535 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004536 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004537 struct weston_seat *seat;
4538
4539 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4540 if (seat->pointer)
4541 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004542
4543 shell_fade(shell, FADE_OUT);
4544 /* lock() is called from shell_fade_done() */
4545}
4546
4547static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004548wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004549{
4550 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004551 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004552
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004553 unlock(shell);
4554}
4555
4556static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004557center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004558{
Giulio Camuffob8366642013-04-25 13:57:46 +03004559 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004560 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004561
Jason Ekstranda7af7042013-10-12 22:38:11 -05004562 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004563
4564 x = output->x + (output->width - width) / 2 - surf_x / 2;
4565 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004566
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004567 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004568}
4569
4570static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004571weston_view_set_initial_position(struct weston_view *view,
4572 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004573{
4574 struct weston_compositor *compositor = shell->compositor;
4575 int ix = 0, iy = 0;
4576 int range_x, range_y;
4577 int dx, dy, x, y, panel_height;
4578 struct weston_output *output, *target_output = NULL;
4579 struct weston_seat *seat;
4580
4581 /* As a heuristic place the new window on the same output as the
4582 * pointer. Falling back to the output containing 0, 0.
4583 *
4584 * TODO: Do something clever for touch too?
4585 */
4586 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004587 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004588 ix = wl_fixed_to_int(seat->pointer->x);
4589 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004590 break;
4591 }
4592 }
4593
4594 wl_list_for_each(output, &compositor->output_list, link) {
4595 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4596 target_output = output;
4597 break;
4598 }
4599 }
4600
4601 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004602 weston_view_set_position(view, 10 + random() % 400,
4603 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004604 return;
4605 }
4606
4607 /* Valid range within output where the surface will still be onscreen.
4608 * If this is negative it means that the surface is bigger than
4609 * output.
4610 */
4611 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004612 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004613 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004614 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004615
Rob Bradford4cb88c72012-08-13 15:18:44 +01004616 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004617 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004618 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004619 dx = 0;
4620
4621 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004622 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004623 else
4624 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004625
4626 x = target_output->x + dx;
4627 y = target_output->y + dy;
4628
Jason Ekstranda7af7042013-10-12 22:38:11 -05004629 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004630}
4631
4632static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004633map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004634 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004635{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004636 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004637 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004638 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004639 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004640
Pekka Paalanen77346a62011-11-30 16:26:35 +02004641 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004642 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004643 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004644 if (shsurf->state.fullscreen) {
4645 center_on_output(shsurf->view, shsurf->fullscreen_output);
4646 shell_map_fullscreen(shsurf);
4647 } else if (shsurf->state.maximized) {
4648 /* use surface configure to set the geometry */
4649 panel_height = get_output_panel_height(shell, shsurf->output);
4650 surface_subsurfaces_boundingbox(shsurf->surface,
4651 &surf_x, &surf_y, NULL, NULL);
4652 weston_view_set_position(shsurf->view,
4653 shsurf->output->x - surf_x,
4654 shsurf->output->y +
4655 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004656 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004657 weston_view_set_initial_position(shsurf->view, shell);
4658 }
Juan Zhao96879df2012-02-07 08:45:41 +08004659 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004660 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004661 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004662 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004663 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004664 weston_view_set_position(shsurf->view,
4665 shsurf->view->geometry.x + sx,
4666 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004667 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004668 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004669 default:
4670 ;
4671 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004672
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004673 /* Surface stacking order, see also activate(). */
4674 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004675
Jason Ekstranda7af7042013-10-12 22:38:11 -05004676 if (shsurf->type != SHELL_SURFACE_NONE) {
4677 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004678 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004679 shsurf->surface->output = shsurf->output;
4680 shsurf->view->output = shsurf->output;
4681 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004682 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004683
Rafael Antognollied207b42013-12-03 15:35:43 -02004684 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4685 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4686 }
4687
Jason Ekstranda7af7042013-10-12 22:38:11 -05004688 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004689 /* XXX: xwayland's using the same fields for transient type */
4690 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004691 if (shsurf->transient.flags ==
4692 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4693 break;
4694 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004695 if (shsurf->state.relative &&
4696 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4697 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004698 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004699 break;
4700 wl_list_for_each(seat, &compositor->seat_list, link)
4701 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004702 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004703 case SHELL_SURFACE_POPUP:
4704 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004705 default:
4706 break;
4707 }
4708
Rafael Antognolli03b16592013-12-03 15:35:42 -02004709 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4710 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004711 {
4712 switch (shell->win_animation_type) {
4713 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004714 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004715 break;
4716 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004717 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004718 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004719 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004720 default:
4721 break;
4722 }
4723 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004724}
4725
4726static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004727configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004728 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004729{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004730 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004731 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004732 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004733
Pekka Paalanen77346a62011-11-30 16:26:35 +02004734 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004735
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004736 assert(shsurf);
4737
Rafael Antognolli03b16592013-12-03 15:35:42 -02004738 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004739 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004740 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004741 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004742 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4743 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004744 mx = shsurf->output->x - surf_x;
4745 my = shsurf->output->y +
4746 get_output_panel_height(shell,shsurf->output) - surf_y;
4747 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004748 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004749 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004750 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004751
Alex Wu4539b082012-03-01 12:57:46 +08004752 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004753 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754 wl_list_for_each(view, &surface->views, surface_link)
4755 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004756
Rafael Antognolli03b16592013-12-03 15:35:42 -02004757 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004758 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004759 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004760}
4761
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004762static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004763shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004764{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004765 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004766 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004767 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004768
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004769 assert(shsurf);
4770
4771 shell = shsurf->shell;
4772
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004773 if (!weston_surface_is_mapped(es) &&
4774 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004775 remove_popup_grab(shsurf);
4776 }
4777
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004778 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004779 return;
4780
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004781 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004782 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004783 type_changed = 1;
4784 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004785
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004786 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004787 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004788 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004789 shsurf->last_width != es->width ||
4790 shsurf->last_height != es->height) {
4791 shsurf->last_width = es->width;
4792 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004793 float from_x, from_y;
4794 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004795
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4797 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004798 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004799 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004800 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004801 }
4802}
4803
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004804static void
4805shell_surface_output_destroyed(struct weston_surface *es)
4806{
4807 struct shell_surface *shsurf = get_shell_surface(es);
4808
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004809 assert(shsurf);
4810
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004811 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004812 shsurf->next_state.maximized = false;
4813 shsurf->next_state.fullscreen = false;
4814 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004815}
4816
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004817static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004818
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004819static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004820desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004821{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004822 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004823 struct desktop_shell *shell =
4824 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004825
4826 shell->child.process.pid = 0;
4827 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004828
4829 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4830 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004831 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004832 shell->child.deathstamp = time;
4833 shell->child.deathcount = 0;
4834 }
4835
4836 shell->child.deathcount++;
4837 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004838 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004839 return;
4840 }
4841
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004842 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004843 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004844 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004845}
4846
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004847static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004848desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4849{
4850 struct desktop_shell *shell;
4851
4852 shell = container_of(listener, struct desktop_shell,
4853 child.client_destroy_listener);
4854
4855 shell->child.client = NULL;
4856}
4857
4858static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004859launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004860{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004861 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004862
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004863 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004864 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004865 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004866 desktop_shell_sigchld);
4867
4868 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004869 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004870
4871 shell->child.client_destroy_listener.notify =
4872 desktop_shell_client_destroy;
4873 wl_client_add_destroy_listener(shell->child.client,
4874 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004875}
4876
4877static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004878bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4879{
Tiago Vignattibe143262012-04-16 17:31:41 +03004880 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004881 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004882
Jason Ekstranda85118c2013-06-27 20:17:02 -05004883 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4884 if (resource)
4885 wl_resource_set_implementation(resource, &shell_implementation,
4886 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004887}
4888
Kristian Høgsberg75840622011-09-06 13:48:16 -04004889static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004890bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4891{
4892 struct desktop_shell *shell = data;
4893 struct wl_resource *resource;
4894
4895 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4896 if (resource)
4897 wl_resource_set_dispatcher(resource,
4898 xdg_shell_unversioned_dispatch,
4899 NULL, shell, NULL);
4900}
4901
4902static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004903unbind_desktop_shell(struct wl_resource *resource)
4904{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004905 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004906
4907 if (shell->locked)
4908 resume_desktop(shell);
4909
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004910 shell->child.desktop_shell = NULL;
4911 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004912}
4913
4914static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004915bind_desktop_shell(struct wl_client *client,
4916 void *data, uint32_t version, uint32_t id)
4917{
Tiago Vignattibe143262012-04-16 17:31:41 +03004918 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004919 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004920
Jason Ekstranda85118c2013-06-27 20:17:02 -05004921 resource = wl_resource_create(client, &desktop_shell_interface,
4922 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004923
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004924 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004925 wl_resource_set_implementation(resource,
4926 &desktop_shell_implementation,
4927 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004928 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004929
4930 if (version < 2)
4931 shell_fade_startup(shell);
4932
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004933 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004934 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004935
4936 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4937 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004938 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004939}
4940
Pekka Paalanen6e168112011-11-24 11:34:05 +02004941static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004942screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004943{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004944 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004946
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004947 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004948 return;
4949
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004950 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004951 if (!shell->locked)
4952 return;
4953
Jason Ekstranda7af7042013-10-12 22:38:11 -05004954 view = container_of(surface->views.next, struct weston_view, surface_link);
4955 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004956
Jason Ekstranda7af7042013-10-12 22:38:11 -05004957 if (wl_list_empty(&view->layer_link)) {
4958 wl_list_insert(shell->lock_layer.view_list.prev,
4959 &view->layer_link);
4960 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004961 wl_event_source_timer_update(shell->screensaver.timer,
4962 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004963 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004964 }
4965}
4966
4967static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004968screensaver_set_surface(struct wl_client *client,
4969 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004970 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004971 struct wl_resource *output_resource)
4972{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004973 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004974 struct weston_surface *surface =
4975 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004976 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004977 struct weston_view *view, *next;
4978
4979 /* Make sure we only have one view */
4980 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4981 weston_view_destroy(view);
4982 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004983
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004984 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004985 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004986 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004987}
4988
4989static const struct screensaver_interface screensaver_implementation = {
4990 screensaver_set_surface
4991};
4992
4993static void
4994unbind_screensaver(struct wl_resource *resource)
4995{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004996 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004997
Pekka Paalanen77346a62011-11-30 16:26:35 +02004998 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004999}
5000
5001static void
5002bind_screensaver(struct wl_client *client,
5003 void *data, uint32_t version, uint32_t id)
5004{
Tiago Vignattibe143262012-04-16 17:31:41 +03005005 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005006 struct wl_resource *resource;
5007
Jason Ekstranda85118c2013-06-27 20:17:02 -05005008 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005009
Pekka Paalanen77346a62011-11-30 16:26:35 +02005010 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005011 wl_resource_set_implementation(resource,
5012 &screensaver_implementation,
5013 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005014 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005015 return;
5016 }
5017
5018 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5019 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005020 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005021}
5022
Kristian Høgsberg07045392012-02-19 18:52:44 -05005023struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005024 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005025 struct weston_surface *current;
5026 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005027 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005028};
5029
5030static void
5031switcher_next(struct switcher *switcher)
5032{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005033 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005034 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005035 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005036 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005037
Jason Ekstranda7af7042013-10-12 22:38:11 -05005038 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005039 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005040 if (shsurf &&
5041 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5042 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005043 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005044 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005045 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005046 next = view->surface;
5047 prev = view->surface;
5048 view->alpha = 0.25;
5049 weston_view_geometry_dirty(view);
5050 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005051 }
Alex Wu1659daa2012-04-01 20:13:09 +08005052
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053 if (is_black_surface(view->surface, NULL)) {
5054 view->alpha = 0.25;
5055 weston_view_geometry_dirty(view);
5056 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005057 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005058 }
5059
5060 if (next == NULL)
5061 next = first;
5062
Alex Wu07b26062012-03-12 16:06:01 +08005063 if (next == NULL)
5064 return;
5065
Kristian Høgsberg07045392012-02-19 18:52:44 -05005066 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005067 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005068
5069 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005070 wl_list_for_each(view, &next->views, surface_link)
5071 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005072
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005073 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005074 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005075 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076}
5077
5078static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005079switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005080{
5081 struct switcher *switcher =
5082 container_of(listener, struct switcher, listener);
5083
5084 switcher_next(switcher);
5085}
5086
5087static void
Daniel Stone351eb612012-05-31 15:27:47 -04005088switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005089{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005090 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005091 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005092 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093
Jason Ekstranda7af7042013-10-12 22:38:11 -05005094 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005095 if (is_focus_view(view))
5096 continue;
5097
Jason Ekstranda7af7042013-10-12 22:38:11 -05005098 view->alpha = 1.0;
5099 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005100 }
5101
Alex Wu07b26062012-03-12 16:06:01 +08005102 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005103 activate(switcher->shell, switcher->current,
5104 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005105 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005106 weston_keyboard_end_grab(keyboard);
5107 if (keyboard->input_method_resource)
5108 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005109 free(switcher);
5110}
5111
5112static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005113switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005114 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005115{
5116 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005117 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005118
Daniel Stonec9785ea2012-05-30 16:31:52 +01005119 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005120 switcher_next(switcher);
5121}
5122
5123static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005124switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005125 uint32_t mods_depressed, uint32_t mods_latched,
5126 uint32_t mods_locked, uint32_t group)
5127{
5128 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005129 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005130
Daniel Stone351eb612012-05-31 15:27:47 -04005131 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5132 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005133}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005134
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005135static void
5136switcher_cancel(struct weston_keyboard_grab *grab)
5137{
5138 struct switcher *switcher = container_of(grab, struct switcher, grab);
5139
5140 switcher_destroy(switcher);
5141}
5142
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005143static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005144 switcher_key,
5145 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005146 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005147};
5148
5149static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005150switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005151 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005152{
Tiago Vignattibe143262012-04-16 17:31:41 +03005153 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005154 struct switcher *switcher;
5155
5156 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005157 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005158 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005159 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005160 wl_list_init(&switcher->listener.link);
5161
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005162 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005163 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005164 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005165 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5166 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005167 switcher_next(switcher);
5168}
5169
Pekka Paalanen3c647232011-12-22 13:43:43 +02005170static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005171backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005172 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005173{
5174 struct weston_compositor *compositor = data;
5175 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005176 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005177
5178 /* TODO: we're limiting to simple use cases, where we assume just
5179 * control on the primary display. We'd have to extend later if we
5180 * ever get support for setting backlights on random desktop LCD
5181 * panels though */
5182 output = get_default_output(compositor);
5183 if (!output)
5184 return;
5185
5186 if (!output->set_backlight)
5187 return;
5188
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005189 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5190 backlight_new = output->backlight_current - 25;
5191 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5192 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005193
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005194 if (backlight_new < 5)
5195 backlight_new = 5;
5196 if (backlight_new > 255)
5197 backlight_new = 255;
5198
5199 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005200 output->set_backlight(output, output->backlight_current);
5201}
5202
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005203struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005204 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005205 struct weston_seat *seat;
5206 uint32_t key[2];
5207 int key_released[2];
5208};
5209
5210static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005211debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005212 uint32_t key, uint32_t state)
5213{
5214 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005215 struct weston_compositor *ec = db->seat->compositor;
5216 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005217 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005218 uint32_t serial;
5219 int send = 0, terminate = 0;
5220 int check_binding = 1;
5221 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005222 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005223
5224 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5225 /* Do not run bindings on key releases */
5226 check_binding = 0;
5227
5228 for (i = 0; i < 2; i++)
5229 if (key == db->key[i])
5230 db->key_released[i] = 1;
5231
5232 if (db->key_released[0] && db->key_released[1]) {
5233 /* All key releases been swalled so end the grab */
5234 terminate = 1;
5235 } else if (key != db->key[0] && key != db->key[1]) {
5236 /* Should not swallow release of other keys */
5237 send = 1;
5238 }
5239 } else if (key == db->key[0] && !db->key_released[0]) {
5240 /* Do not check bindings for the first press of the binding
5241 * key. This allows it to be used as a debug shortcut.
5242 * We still need to swallow this event. */
5243 check_binding = 0;
5244 } else if (db->key[1]) {
5245 /* If we already ran a binding don't process another one since
5246 * we can't keep track of all the binding keys that were
5247 * pressed in order to swallow the release events. */
5248 send = 1;
5249 check_binding = 0;
5250 }
5251
5252 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005253 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5254 key, state)) {
5255 /* We ran a binding so swallow the press and keep the
5256 * grab to swallow the released too. */
5257 send = 0;
5258 terminate = 0;
5259 db->key[1] = key;
5260 } else {
5261 /* Terminate the grab since the key pressed is not a
5262 * debug binding key. */
5263 send = 1;
5264 terminate = 1;
5265 }
5266 }
5267
5268 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005269 serial = wl_display_next_serial(display);
5270 resource_list = &grab->keyboard->focus_resource_list;
5271 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005272 wl_keyboard_send_key(resource, serial, time, key, state);
5273 }
5274 }
5275
5276 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005277 weston_keyboard_end_grab(grab->keyboard);
5278 if (grab->keyboard->input_method_resource)
5279 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005280 free(db);
5281 }
5282}
5283
5284static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005285debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005286 uint32_t mods_depressed, uint32_t mods_latched,
5287 uint32_t mods_locked, uint32_t group)
5288{
5289 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005290 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005291
Neil Roberts96d790e2013-09-19 17:32:00 +01005292 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005293
Neil Roberts96d790e2013-09-19 17:32:00 +01005294 wl_resource_for_each(resource, resource_list) {
5295 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5296 mods_latched, mods_locked, group);
5297 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005298}
5299
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005300static void
5301debug_binding_cancel(struct weston_keyboard_grab *grab)
5302{
5303 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5304
5305 weston_keyboard_end_grab(grab->keyboard);
5306 free(db);
5307}
5308
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005309struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005310 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005311 debug_binding_modifiers,
5312 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005313};
5314
5315static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005316debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005317{
5318 struct debug_binding_grab *grab;
5319
5320 grab = calloc(1, sizeof *grab);
5321 if (!grab)
5322 return;
5323
5324 grab->seat = (struct weston_seat *) seat;
5325 grab->key[0] = key;
5326 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005327 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005328}
5329
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005331force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005332 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005333{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005334 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005335 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005336 struct desktop_shell *shell = data;
5337 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005338 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005339
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005340 focus_surface = seat->keyboard->focus;
5341 if (!focus_surface)
5342 return;
5343
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005344 wl_signal_emit(&compositor->kill_signal, focus_surface);
5345
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005346 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005347 wl_client_get_credentials(client, &pid, NULL, NULL);
5348
5349 /* Skip clients that we launched ourselves (the credentials of
5350 * the socketpair is ours) */
5351 if (pid == getpid())
5352 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005353
Daniel Stone325fc2d2012-05-30 16:31:58 +01005354 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005355}
5356
5357static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005358workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005359 uint32_t key, void *data)
5360{
5361 struct desktop_shell *shell = data;
5362 unsigned int new_index = shell->workspaces.current;
5363
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005364 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005365 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005366 if (new_index != 0)
5367 new_index--;
5368
5369 change_workspace(shell, new_index);
5370}
5371
5372static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005373workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005374 uint32_t key, void *data)
5375{
5376 struct desktop_shell *shell = data;
5377 unsigned int new_index = shell->workspaces.current;
5378
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005379 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005380 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005381 if (new_index < shell->workspaces.num - 1)
5382 new_index++;
5383
5384 change_workspace(shell, new_index);
5385}
5386
5387static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005388workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005389 uint32_t key, void *data)
5390{
5391 struct desktop_shell *shell = data;
5392 unsigned int new_index;
5393
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005394 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005395 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005396 new_index = key - KEY_F1;
5397 if (new_index >= shell->workspaces.num)
5398 new_index = shell->workspaces.num - 1;
5399
5400 change_workspace(shell, new_index);
5401}
5402
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005403static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005404workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005405 uint32_t key, void *data)
5406{
5407 struct desktop_shell *shell = data;
5408 unsigned int new_index = shell->workspaces.current;
5409
5410 if (shell->locked)
5411 return;
5412
5413 if (new_index != 0)
5414 new_index--;
5415
5416 take_surface_to_workspace_by_seat(shell, seat, new_index);
5417}
5418
5419static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005420workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005421 uint32_t key, void *data)
5422{
5423 struct desktop_shell *shell = data;
5424 unsigned int new_index = shell->workspaces.current;
5425
5426 if (shell->locked)
5427 return;
5428
5429 if (new_index < shell->workspaces.num - 1)
5430 new_index++;
5431
5432 take_surface_to_workspace_by_seat(shell, seat, new_index);
5433}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005434
5435static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005436handle_output_destroy(struct wl_listener *listener, void *data)
5437{
5438 struct shell_output *output_listener =
5439 container_of(listener, struct shell_output, destroy_listener);
5440
5441 wl_list_remove(&output_listener->destroy_listener.link);
5442 wl_list_remove(&output_listener->link);
5443 free(output_listener);
5444}
5445
5446static void
5447create_shell_output(struct desktop_shell *shell,
5448 struct weston_output *output)
5449{
5450 struct shell_output *shell_output;
5451
5452 shell_output = zalloc(sizeof *shell_output);
5453 if (shell_output == NULL)
5454 return;
5455
5456 shell_output->output = output;
5457 shell_output->shell = shell;
5458 shell_output->destroy_listener.notify = handle_output_destroy;
5459 wl_signal_add(&output->destroy_signal,
5460 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005461 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005462}
5463
5464static void
5465handle_output_create(struct wl_listener *listener, void *data)
5466{
5467 struct desktop_shell *shell =
5468 container_of(listener, struct desktop_shell, output_create_listener);
5469 struct weston_output *output = (struct weston_output *)data;
5470
5471 create_shell_output(shell, output);
5472}
5473
5474static void
5475setup_output_destroy_handler(struct weston_compositor *ec,
5476 struct desktop_shell *shell)
5477{
5478 struct weston_output *output;
5479
5480 wl_list_init(&shell->output_list);
5481 wl_list_for_each(output, &ec->output_list, link)
5482 create_shell_output(shell, output);
5483
5484 shell->output_create_listener.notify = handle_output_create;
5485 wl_signal_add(&ec->output_created_signal,
5486 &shell->output_create_listener);
5487}
5488
5489static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005490shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005491{
Tiago Vignattibe143262012-04-16 17:31:41 +03005492 struct desktop_shell *shell =
5493 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005494 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005495 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005496
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005497 /* Force state to unlocked so we don't try to fade */
5498 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005499 if (shell->child.client)
5500 wl_client_destroy(shell->child.client);
5501
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005502 wl_list_remove(&shell->idle_listener.link);
5503 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005504
5505 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005506
Xiong Zhang6b481422013-10-23 13:58:32 +08005507 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5508 wl_list_remove(&shell_output->destroy_listener.link);
5509 wl_list_remove(&shell_output->link);
5510 free(shell_output);
5511 }
5512
5513 wl_list_remove(&shell->output_create_listener.link);
5514
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005515 wl_array_for_each(ws, &shell->workspaces.array)
5516 workspace_destroy(*ws);
5517 wl_array_release(&shell->workspaces.array);
5518
Pekka Paalanen3c647232011-12-22 13:43:43 +02005519 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005520 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005521 free(shell);
5522}
5523
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005524static void
5525shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5526{
5527 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005528 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005529
5530 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005531 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5532 MODIFIER_CTRL | MODIFIER_ALT,
5533 terminate_binding, ec);
5534 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5535 click_to_activate_binding,
5536 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005537 weston_compositor_add_touch_binding(ec, 0,
5538 touch_to_activate_binding,
5539 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005540 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5541 MODIFIER_SUPER | MODIFIER_ALT,
5542 surface_opacity_binding, NULL);
5543 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5544 MODIFIER_SUPER, zoom_axis_binding,
5545 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005546
5547 /* configurable bindings */
5548 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005549 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5550 zoom_key_binding, NULL);
5551 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5552 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005553 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5554 maximize_binding, NULL);
5555 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5556 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005557 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5558 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005559 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005560 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5561 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005562 weston_compositor_add_button_binding(ec, BTN_LEFT,
5563 mod | MODIFIER_SHIFT,
5564 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005565
5566 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5567 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5568 rotate_binding, NULL);
5569
Daniel Stone325fc2d2012-05-30 16:31:58 +01005570 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5571 shell);
5572 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5573 ec);
5574 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5575 backlight_binding, ec);
5576 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5577 ec);
5578 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5579 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005580 weston_compositor_add_key_binding(ec, KEY_K, mod,
5581 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005582 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5583 workspace_up_binding, shell);
5584 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5585 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005586 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5587 workspace_move_surface_up_binding,
5588 shell);
5589 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5590 workspace_move_surface_down_binding,
5591 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005592
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005593 if (shell->exposay_modifier)
5594 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5595 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005596
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005597 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5598 if (shell->workspaces.num > 1) {
5599 num_workspace_bindings = shell->workspaces.num;
5600 if (num_workspace_bindings > 6)
5601 num_workspace_bindings = 6;
5602 for (i = 0; i < num_workspace_bindings; i++)
5603 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5604 workspace_f_binding,
5605 shell);
5606 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005607
5608 /* Debug bindings */
5609 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5610 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005611}
5612
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005613WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005614module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005615 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005616{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005617 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005618 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005619 struct workspace **pws;
5620 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005621 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005622
Peter Huttererf3d62272013-08-08 11:57:05 +10005623 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005624 if (shell == NULL)
5625 return -1;
5626
Kristian Høgsberg75840622011-09-06 13:48:16 -04005627 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005628
5629 shell->destroy_listener.notify = shell_destroy;
5630 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005631 shell->idle_listener.notify = idle_handler;
5632 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5633 shell->wake_listener.notify = wake_handler;
5634 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005635
Scott Moreauff1db4a2012-04-17 19:06:18 -06005636 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005637 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005638 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005639 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005640 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005641 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005642 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005643 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005644 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005645 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005646 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005647
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005648 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5649 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005650 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5651 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005652 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005653
5654 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005655 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005656
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005657 if (input_panel_setup(shell) < 0)
5658 return -1;
5659
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005660 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005661
Daniel Stonedf8133b2013-11-19 11:37:14 +01005662 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5663 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5664
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005665 for (i = 0; i < shell->workspaces.num; i++) {
5666 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5667 if (pws == NULL)
5668 return -1;
5669
5670 *pws = workspace_create();
5671 if (*pws == NULL)
5672 return -1;
5673 }
5674 activate_workspace(shell, 0);
5675
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005676 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005677 wl_list_init(&shell->workspaces.animation.link);
5678 shell->workspaces.animation.frame = animate_workspace_change_frame;
5679
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005680 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005681 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005682 return -1;
5683
Rafael Antognollie2a34552013-12-03 15:35:45 -02005684 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5685 shell, bind_xdg_shell) == NULL)
5686 return -1;
5687
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005688 if (wl_global_create(ec->wl_display,
5689 &desktop_shell_interface, 2,
5690 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005691 return -1;
5692
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005693 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5694 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005695 return -1;
5696
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005697 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5698 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005699 return -1;
5700
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005701 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005702
Xiong Zhang6b481422013-10-23 13:58:32 +08005703 setup_output_destroy_handler(ec, shell);
5704
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005705 loop = wl_display_get_event_loop(ec->wl_display);
5706 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005707
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005708 shell->screensaver.timer =
5709 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5710
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005711 wl_list_for_each(seat, &ec->seat_list, link)
5712 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005713
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005714 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005715
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005716 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005717
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005718 return 0;
5719}