blob: c82c00f0264d69bcbbbb67a868dc83b71b97f020 [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;
201 wl_fixed_t dx, dy;
202};
203
Pekka Paalanen460099f2012-01-20 16:48:25 +0200204struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500206 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200208 float x;
209 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210 } center;
211};
212
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213struct shell_seat {
214 struct weston_seat *seat;
215 struct wl_listener seat_destroy_listener;
216
217 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400218 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219 struct wl_list surfaces_list;
220 struct wl_client *client;
221 int32_t initial_up;
222 } popup_grab;
223};
224
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100225void
226set_alpha_if_fullscreen(struct shell_surface *shsurf)
227{
228 if (shsurf && shsurf->state.fullscreen)
229 shsurf->fullscreen.black_view->alpha = 0.25;
230}
231
Alex Wubd3354b2012-04-17 17:20:49 +0800232static struct desktop_shell *
233shell_surface_get_shell(struct shell_surface *shsurf);
234
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400236surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500237
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300238static void
239shell_fade_startup(struct desktop_shell *shell);
240
Philip Withnallbecb77e2013-11-25 18:01:30 +0000241static struct shell_seat *
242get_shell_seat(struct weston_seat *seat);
243
Philip Withnall648a4dd2013-11-25 18:01:44 +0000244static void
245shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
246
Alex Wubd3354b2012-04-17 17:20:49 +0800247static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200248shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
249
250static bool
251shell_surface_is_xdg_surface(struct shell_surface *shsurf);
252
253static bool
254shell_surface_is_xdg_popup(struct shell_surface *shsurf);
255
256static void
257shell_surface_set_parent(struct shell_surface *shsurf,
258 struct weston_surface *parent);
259
260static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800261shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
262{
263 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500264 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800265
266 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100267
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800269 return false;
270
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
272 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800273 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800275}
276
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500277static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400278destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279{
280 struct shell_grab *grab;
281
282 grab = container_of(listener, struct shell_grab,
283 shsurf_destroy_listener);
284
285 grab->shsurf = NULL;
286}
287
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800288struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289get_default_view(struct weston_surface *surface)
290{
291 struct shell_surface *shsurf;
292 struct weston_view *view;
293
294 if (!surface || wl_list_empty(&surface->views))
295 return NULL;
296
297 shsurf = get_shell_surface(surface);
298 if (shsurf)
299 return shsurf->view;
300
301 wl_list_for_each(view, &surface->views, surface_link)
302 if (weston_view_is_mapped(view))
303 return view;
304
305 return container_of(surface->views.next, struct weston_view, surface_link);
306}
307
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300308static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400309popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400310
311static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300312shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300314 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300317{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318 struct desktop_shell *shell = shsurf->shell;
319
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400320 popup_grab_end(pointer);
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322 grab->grab.interface = interface;
323 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400324 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500325 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400326 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700328 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400330 if (shell->child.desktop_shell) {
331 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
332 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 weston_pointer_set_focus(pointer,
334 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400335 wl_fixed_from_int(0),
336 wl_fixed_from_int(0));
337 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338}
339
340static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300341shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700343 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400344 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700345 grab->shsurf->grabbed = 0;
346 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300347
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700348 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349}
350
351static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700352shell_touch_grab_start(struct shell_touch_grab *grab,
353 const struct weston_touch_grab_interface *interface,
354 struct shell_surface *shsurf,
355 struct weston_touch *touch)
356{
357 struct desktop_shell *shell = shsurf->shell;
358
359 grab->grab.interface = interface;
360 grab->shsurf = shsurf;
361 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
362 wl_signal_add(&shsurf->destroy_signal,
363 &grab->shsurf_destroy_listener);
364
365 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700367
368 weston_touch_start_grab(touch, &grab->grab);
369 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500370 weston_touch_set_focus(touch->seat,
371 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700372}
373
374static void
375shell_touch_grab_end(struct shell_touch_grab *grab)
376{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700377 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700378 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700379 grab->shsurf->grabbed = 0;
380 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700381
382 weston_touch_end_grab(grab->touch);
383}
384
385static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800387 struct weston_output *output);
388
Daniel Stone496ca172012-05-30 16:31:42 +0100389static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300390get_modifier(char *modifier)
391{
392 if (!modifier)
393 return MODIFIER_SUPER;
394
395 if (!strcmp("ctrl", modifier))
396 return MODIFIER_CTRL;
397 else if (!strcmp("alt", modifier))
398 return MODIFIER_ALT;
399 else if (!strcmp("super", modifier))
400 return MODIFIER_SUPER;
401 else
402 return MODIFIER_SUPER;
403}
404
Juan Zhaoe10d2792012-04-25 19:09:52 +0800405static enum animation_type
406get_animation_type(char *animation)
407{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800408 if (!strcmp("zoom", animation))
409 return ANIMATION_ZOOM;
410 else if (!strcmp("fade", animation))
411 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100412 else if (!strcmp("dim-layer", animation))
413 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800414 else
415 return ANIMATION_NONE;
416}
417
Alex Wu4539b082012-03-01 12:57:46 +0800418static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400419shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200420{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400421 struct weston_config_section *section;
422 int duration;
423 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200424
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400425 section = weston_config_get_section(shell->compositor->config,
426 "screensaver", NULL, NULL);
427 weston_config_section_get_string(section,
428 "path", &shell->screensaver.path, NULL);
429 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200430 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400431
432 section = weston_config_get_section(shell->compositor->config,
433 "shell", NULL, NULL);
434 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100435 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100436 shell->client = s;
437 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400438 "binding-modifier", &s, "super");
439 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200440 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400441 weston_config_section_get_string(section, "animation", &s, "none");
442 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200443 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700444 weston_config_section_get_string(section,
445 "startup-animation", &s, "fade");
446 shell->startup_animation_type = get_animation_type(s);
447 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700448 if (shell->startup_animation_type == ANIMATION_ZOOM)
449 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100450 weston_config_section_get_string(section, "focus-animation", &s, "none");
451 shell->focus_animation_type = get_animation_type(s);
452 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 weston_config_section_get_uint(section, "num-workspaces",
454 &shell->workspaces.num,
455 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200456}
457
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800458struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100459get_default_output(struct weston_compositor *compositor)
460{
461 return container_of(compositor->output_list.next,
462 struct weston_output, link);
463}
464
465
466/* no-op func for checking focus surface */
467static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600468focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100469{
470}
471
472static struct focus_surface *
473get_focus_surface(struct weston_surface *surface)
474{
475 if (surface->configure == focus_surface_configure)
476 return surface->configure_private;
477 else
478 return NULL;
479}
480
481static bool
482is_focus_surface (struct weston_surface *es)
483{
484 return (es->configure == focus_surface_configure);
485}
486
487static bool
488is_focus_view (struct weston_view *view)
489{
490 return is_focus_surface (view->surface);
491}
492
493static struct focus_surface *
494create_focus_surface(struct weston_compositor *ec,
495 struct weston_output *output)
496{
497 struct focus_surface *fsurf = NULL;
498 struct weston_surface *surface = NULL;
499
500 fsurf = malloc(sizeof *fsurf);
501 if (!fsurf)
502 return NULL;
503
504 fsurf->surface = weston_surface_create(ec);
505 surface = fsurf->surface;
506 if (surface == NULL) {
507 free(fsurf);
508 return NULL;
509 }
510
511 surface->configure = focus_surface_configure;
512 surface->output = output;
513 surface->configure_private = fsurf;
514
515 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100516 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100517
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600518 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600519 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100520 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
521 pixman_region32_fini(&surface->opaque);
522 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
523 output->width, output->height);
524 pixman_region32_fini(&surface->input);
525 pixman_region32_init(&surface->input);
526
527 wl_list_init(&fsurf->workspace_transform.link);
528
529 return fsurf;
530}
531
532static void
533focus_surface_destroy(struct focus_surface *fsurf)
534{
535 weston_surface_destroy(fsurf->surface);
536 free(fsurf);
537}
538
539static void
540focus_animation_done(struct weston_view_animation *animation, void *data)
541{
542 struct workspace *ws = data;
543
544 ws->focus_animation = NULL;
545}
546
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200548focus_state_destroy(struct focus_state *state)
549{
550 wl_list_remove(&state->seat_destroy_listener.link);
551 wl_list_remove(&state->surface_destroy_listener.link);
552 free(state);
553}
554
555static void
556focus_state_seat_destroy(struct wl_listener *listener, void *data)
557{
558 struct focus_state *state = container_of(listener,
559 struct focus_state,
560 seat_destroy_listener);
561
562 wl_list_remove(&state->link);
563 focus_state_destroy(state);
564}
565
566static void
567focus_state_surface_destroy(struct wl_listener *listener, void *data)
568{
569 struct focus_state *state = container_of(listener,
570 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400571 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400572 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 struct weston_surface *main_surface, *next;
574 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200575
Pekka Paalanen01388e22013-04-25 13:57:44 +0300576 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
577
Kristian Høgsberge3778222012-07-31 17:29:30 -0400578 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
580 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400581 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100582 if (is_focus_view(view))
583 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400584
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400586 break;
587 }
588
Pekka Paalanen01388e22013-04-25 13:57:44 +0300589 /* if the focus was a sub-surface, activate its main surface */
590 if (main_surface != state->keyboard_focus)
591 next = main_surface;
592
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100593 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400594 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100595 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400596 activate(shell, next, state->seat);
597 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100598 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
599 if (state->ws->focus_animation)
600 weston_view_animation_destroy(state->ws->focus_animation);
601
602 state->ws->focus_animation = weston_fade_run(
603 state->ws->fsurf_front->view,
604 state->ws->fsurf_front->view->alpha, 0.0, 300,
605 focus_animation_done, state->ws);
606 }
607
Kristian Høgsberge3778222012-07-31 17:29:30 -0400608 wl_list_remove(&state->link);
609 focus_state_destroy(state);
610 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200611}
612
613static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400614focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200615{
Jonas Ådahl04769742012-06-13 00:01:24 +0200616 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200617
618 state = malloc(sizeof *state);
619 if (state == NULL)
620 return NULL;
621
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100622 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400623 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200624 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400625 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200626
627 state->seat_destroy_listener.notify = focus_state_seat_destroy;
628 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400629 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200630 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400631 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200632
633 return state;
634}
635
Jonas Ådahl8538b222012-08-29 22:13:03 +0200636static struct focus_state *
637ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
638{
639 struct workspace *ws = get_current_workspace(shell);
640 struct focus_state *state;
641
642 wl_list_for_each(state, &ws->focus_list, link)
643 if (state->seat == seat)
644 break;
645
646 if (&state->link == &ws->focus_list)
647 state = focus_state_create(seat, ws);
648
649 return state;
650}
651
Jonas Ådahl04769742012-06-13 00:01:24 +0200652static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400653restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200654{
655 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400656 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400659 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200660
Kristian Høgsberge3148752013-05-06 23:19:49 -0400661 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200662 }
663}
664
665static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200666replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
667 struct weston_seat *seat)
668{
669 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400670 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200671
672 wl_list_for_each(state, &ws->focus_list, link) {
673 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400674 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500675 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200676 return;
677 }
678 }
679}
680
681static void
682drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
683 struct weston_surface *surface)
684{
685 struct focus_state *state;
686
687 wl_list_for_each(state, &ws->focus_list, link)
688 if (state->keyboard_focus == surface)
689 state->keyboard_focus = NULL;
690}
691
692static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
694 struct weston_view *from, struct weston_view *to)
695{
696 struct weston_output *output;
697 bool focus_surface_created = false;
698
699 /* FIXME: Only support dim animation using two layers */
700 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
701 return;
702
703 output = get_default_output(shell->compositor);
704 if (ws->fsurf_front == NULL && (from || to)) {
705 ws->fsurf_front = create_focus_surface(shell->compositor, output);
706 ws->fsurf_back = create_focus_surface(shell->compositor, output);
707 ws->fsurf_front->view->alpha = 0.0;
708 ws->fsurf_back->view->alpha = 0.0;
709 focus_surface_created = true;
710 } else {
711 wl_list_remove(&ws->fsurf_front->view->layer_link);
712 wl_list_remove(&ws->fsurf_back->view->layer_link);
713 }
714
715 if (ws->focus_animation) {
716 weston_view_animation_destroy(ws->focus_animation);
717 ws->focus_animation = NULL;
718 }
719
720 if (to)
721 wl_list_insert(&to->layer_link,
722 &ws->fsurf_front->view->layer_link);
723 else if (from)
724 wl_list_insert(&ws->layer.view_list,
725 &ws->fsurf_front->view->layer_link);
726
727 if (focus_surface_created) {
728 ws->focus_animation = weston_fade_run(
729 ws->fsurf_front->view,
730 ws->fsurf_front->view->alpha, 0.6, 300,
731 focus_animation_done, ws);
732 } else if (from) {
733 wl_list_insert(&from->layer_link,
734 &ws->fsurf_back->view->layer_link);
735 ws->focus_animation = weston_stable_fade_run(
736 ws->fsurf_front->view, 0.0,
737 ws->fsurf_back->view, 0.6,
738 focus_animation_done, ws);
739 } else if (to) {
740 wl_list_insert(&ws->layer.view_list,
741 &ws->fsurf_back->view->layer_link);
742 ws->focus_animation = weston_stable_fade_run(
743 ws->fsurf_front->view, 0.0,
744 ws->fsurf_back->view, 0.6,
745 focus_animation_done, ws);
746 }
747}
748
749static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200750workspace_destroy(struct workspace *ws)
751{
Jonas Ådahl04769742012-06-13 00:01:24 +0200752 struct focus_state *state, *next;
753
754 wl_list_for_each_safe(state, next, &ws->focus_list, link)
755 focus_state_destroy(state);
756
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 if (ws->fsurf_front)
758 focus_surface_destroy(ws->fsurf_front);
759 if (ws->fsurf_back)
760 focus_surface_destroy(ws->fsurf_back);
761
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200762 free(ws);
763}
764
Jonas Ådahl04769742012-06-13 00:01:24 +0200765static void
766seat_destroyed(struct wl_listener *listener, void *data)
767{
768 struct weston_seat *seat = data;
769 struct focus_state *state, *next;
770 struct workspace *ws = container_of(listener,
771 struct workspace,
772 seat_destroyed_listener);
773
774 wl_list_for_each_safe(state, next, &ws->focus_list, link)
775 if (state->seat == seat)
776 wl_list_remove(&state->link);
777}
778
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200779static struct workspace *
780workspace_create(void)
781{
782 struct workspace *ws = malloc(sizeof *ws);
783 if (ws == NULL)
784 return NULL;
785
786 weston_layer_init(&ws->layer, NULL);
787
Jonas Ådahl04769742012-06-13 00:01:24 +0200788 wl_list_init(&ws->focus_list);
789 wl_list_init(&ws->seat_destroyed_listener.link);
790 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100791 ws->fsurf_front = NULL;
792 ws->fsurf_back = NULL;
793 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200794
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795 return ws;
796}
797
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200798static int
799workspace_is_empty(struct workspace *ws)
800{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200802}
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804static struct workspace *
805get_workspace(struct desktop_shell *shell, unsigned int index)
806{
807 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200808 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200809 pws += index;
810 return *pws;
811}
812
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800813struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200814get_current_workspace(struct desktop_shell *shell)
815{
816 return get_workspace(shell, shell->workspaces.current);
817}
818
819static void
820activate_workspace(struct desktop_shell *shell, unsigned int index)
821{
822 struct workspace *ws;
823
824 ws = get_workspace(shell, index);
825 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
826
827 shell->workspaces.current = index;
828}
829
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200830static unsigned int
831get_output_height(struct weston_output *output)
832{
833 return abs(output->region.extents.y1 - output->region.extents.y2);
834}
835
836static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200838{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200839 struct weston_transform *transform;
840
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 if (is_focus_view(view)) {
842 struct focus_surface *fsurf = get_focus_surface(view->surface);
843 transform = &fsurf->workspace_transform;
844 } else {
845 struct shell_surface *shsurf = get_shell_surface(view->surface);
846 transform = &shsurf->workspace_transform;
847 }
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 weston_matrix_init(&transform->matrix);
854 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857}
858
859static void
860workspace_translate_out(struct workspace *ws, double fraction)
861{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863 unsigned int height;
864 double d;
865
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
867 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 d = height * fraction;
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200871 }
872}
873
874static void
875workspace_translate_in(struct workspace *ws, double fraction)
876{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200878 unsigned int height;
879 double d;
880
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
882 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200883
884 if (fraction > 0)
885 d = -(height - height * fraction);
886 else
887 d = height + height * fraction;
888
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100889 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200890 }
891}
892
893static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894broadcast_current_workspace_state(struct desktop_shell *shell)
895{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700896 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700898 wl_resource_for_each(resource, &shell->workspaces.client_list)
899 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900 shell->workspaces.current,
901 shell->workspaces.num);
902}
903
904static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905reverse_workspace_change_animation(struct desktop_shell *shell,
906 unsigned int index,
907 struct workspace *from,
908 struct workspace *to)
909{
910 shell->workspaces.current = index;
911
912 shell->workspaces.anim_to = to;
913 shell->workspaces.anim_from = from;
914 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
915 shell->workspaces.anim_timestamp = 0;
916
Scott Moreau4272e632012-08-13 09:58:41 -0600917 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200918}
919
920static void
921workspace_deactivate_transforms(struct workspace *ws)
922{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100924 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100927 if (is_focus_view(view)) {
928 struct focus_surface *fsurf = get_focus_surface(view->surface);
929 transform = &fsurf->workspace_transform;
930 } else {
931 struct shell_surface *shsurf = get_shell_surface(view->surface);
932 transform = &shsurf->workspace_transform;
933 }
934
935 if (!wl_list_empty(&transform->link)) {
936 wl_list_remove(&transform->link);
937 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 }
941}
942
943static void
944finish_workspace_change_animation(struct desktop_shell *shell,
945 struct workspace *from,
946 struct workspace *to)
947{
Scott Moreau4272e632012-08-13 09:58:41 -0600948 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949
950 wl_list_remove(&shell->workspaces.animation.link);
951 workspace_deactivate_transforms(from);
952 workspace_deactivate_transforms(to);
953 shell->workspaces.anim_to = NULL;
954
955 wl_list_remove(&shell->workspaces.anim_from->layer.link);
956}
957
958static void
959animate_workspace_change_frame(struct weston_animation *animation,
960 struct weston_output *output, uint32_t msecs)
961{
962 struct desktop_shell *shell =
963 container_of(animation, struct desktop_shell,
964 workspaces.animation);
965 struct workspace *from = shell->workspaces.anim_from;
966 struct workspace *to = shell->workspaces.anim_to;
967 uint32_t t;
968 double x, y;
969
970 if (workspace_is_empty(from) && workspace_is_empty(to)) {
971 finish_workspace_change_animation(shell, from, to);
972 return;
973 }
974
975 if (shell->workspaces.anim_timestamp == 0) {
976 if (shell->workspaces.anim_current == 0.0)
977 shell->workspaces.anim_timestamp = msecs;
978 else
979 shell->workspaces.anim_timestamp =
980 msecs -
981 /* Invers of movement function 'y' below. */
982 (asin(1.0 - shell->workspaces.anim_current) *
983 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
984 M_2_PI);
985 }
986
987 t = msecs - shell->workspaces.anim_timestamp;
988
989 /*
990 * x = [0, π/2]
991 * y(x) = sin(x)
992 */
993 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
994 y = sin(x);
995
996 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1000 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1001 shell->workspaces.anim_current = y;
1002
Scott Moreau4272e632012-08-13 09:58:41 -06001003 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001005 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007}
1008
1009static void
1010animate_workspace_change(struct desktop_shell *shell,
1011 unsigned int index,
1012 struct workspace *from,
1013 struct workspace *to)
1014{
1015 struct weston_output *output;
1016
1017 int dir;
1018
1019 if (index > shell->workspaces.current)
1020 dir = -1;
1021 else
1022 dir = 1;
1023
1024 shell->workspaces.current = index;
1025
1026 shell->workspaces.anim_dir = dir;
1027 shell->workspaces.anim_from = from;
1028 shell->workspaces.anim_to = to;
1029 shell->workspaces.anim_current = 0.0;
1030 shell->workspaces.anim_timestamp = 0;
1031
1032 output = container_of(shell->compositor->output_list.next,
1033 struct weston_output, link);
1034 wl_list_insert(&output->animation_list,
1035 &shell->workspaces.animation.link);
1036
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001037 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038
1039 workspace_translate_in(to, 0);
1040
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001041 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001042
Scott Moreau4272e632012-08-13 09:58:41 -06001043 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044}
1045
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001046static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001047update_workspace(struct desktop_shell *shell, unsigned int index,
1048 struct workspace *from, struct workspace *to)
1049{
1050 shell->workspaces.current = index;
1051 wl_list_insert(&from->layer.link, &to->layer.link);
1052 wl_list_remove(&from->layer.link);
1053}
1054
1055static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001056change_workspace(struct desktop_shell *shell, unsigned int index)
1057{
1058 struct workspace *from;
1059 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001060 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001061
1062 if (index == shell->workspaces.current)
1063 return;
1064
1065 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067 return;
1068
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001069 from = get_current_workspace(shell);
1070 to = get_workspace(shell, index);
1071
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072 if (shell->workspaces.anim_from == to &&
1073 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001074 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001076 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077 return;
1078 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080 if (shell->workspaces.anim_to != NULL)
1081 finish_workspace_change_animation(shell,
1082 shell->workspaces.anim_from,
1083 shell->workspaces.anim_to);
1084
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001085 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001086
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001087 if (shell->focus_animation_type != ANIMATION_NONE) {
1088 wl_list_for_each(state, &from->focus_list, link)
1089 if (state->keyboard_focus)
1090 animate_focus_change(shell, from,
1091 get_default_view(state->keyboard_focus), NULL);
1092
1093 wl_list_for_each(state, &to->focus_list, link)
1094 if (state->keyboard_focus)
1095 animate_focus_change(shell, to,
1096 NULL, get_default_view(state->keyboard_focus));
1097 }
1098
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001099 if (workspace_is_empty(to) && workspace_is_empty(from))
1100 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 else
1102 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001103
1104 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001105}
1106
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001107static bool
1108workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1109{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001111 struct wl_list *e;
1112
1113 if (wl_list_empty(list))
1114 return false;
1115
1116 e = list->next;
1117
1118 if (e->next != list)
1119 return false;
1120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001122}
1123
1124static void
Philip Withnall659163d2013-11-25 18:01:36 +00001125move_surface_to_workspace(struct desktop_shell *shell,
1126 struct shell_surface *shsurf,
1127 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001128{
1129 struct workspace *from;
1130 struct workspace *to;
1131 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001132 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001133 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001134
1135 if (workspace == shell->workspaces.current)
1136 return;
1137
Philip Withnall659163d2013-11-25 18:01:36 +00001138 view = get_default_view(shsurf->surface);
1139 if (!view)
1140 return;
1141
1142 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1143
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001144 if (workspace >= shell->workspaces.num)
1145 workspace = shell->workspaces.num - 1;
1146
Jonas Ådahle9d22502012-08-29 22:13:01 +02001147 from = get_current_workspace(shell);
1148 to = get_workspace(shell, workspace);
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 wl_list_remove(&view->layer_link);
1151 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152
Philip Withnall648a4dd2013-11-25 18:01:44 +00001153 shell_surface_update_child_surface_layers(shsurf);
1154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001156 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1157 if (!seat->keyboard)
1158 continue;
1159
1160 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001162 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001163 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001166}
1167
1168static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001169take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001170 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171 unsigned int index)
1172{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001173 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175 struct shell_surface *shsurf;
1176 struct workspace *from;
1177 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001178 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179
Pekka Paalanen01388e22013-04-25 13:57:44 +03001180 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view = get_default_view(surface);
1182 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 index == shell->workspaces.current ||
1184 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185 return;
1186
1187 from = get_current_workspace(shell);
1188 to = get_workspace(shell, index);
1189
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 wl_list_remove(&view->layer_link);
1191 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192
Philip Withnall659163d2013-11-25 18:01:36 +00001193 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001194 if (shsurf != NULL)
1195 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001196
Jonas Ådahle9d22502012-08-29 22:13:01 +02001197 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 drop_focus_state(shell, from, surface);
1199
1200 if (shell->workspaces.anim_from == to &&
1201 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001202 wl_list_remove(&to->layer.link);
1203 wl_list_insert(from->layer.link.prev, &to->layer.link);
1204
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001205 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206 broadcast_current_workspace_state(shell);
1207
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208 return;
1209 }
1210
1211 if (shell->workspaces.anim_to != NULL)
1212 finish_workspace_change_animation(shell,
1213 shell->workspaces.anim_from,
1214 shell->workspaces.anim_to);
1215
1216 if (workspace_is_empty(from) &&
1217 workspace_has_only(to, surface))
1218 update_workspace(shell, index, from, to);
1219 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001220 if (shsurf != NULL &&
1221 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222 wl_list_insert(&shell->workspaces.anim_sticky_list,
1223 &shsurf->workspace_transform.link);
1224
1225 animate_workspace_change(shell, index, from, to);
1226 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001227
1228 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001229
1230 state = ensure_focus_state(shell, seat);
1231 if (state != NULL)
1232 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001233}
1234
1235static void
1236workspace_manager_move_surface(struct wl_client *client,
1237 struct wl_resource *resource,
1238 struct wl_resource *surface_resource,
1239 uint32_t workspace)
1240{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001241 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001242 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001243 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001244 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001245 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246
Pekka Paalanen01388e22013-04-25 13:57:44 +03001247 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001248 shell_surface = get_shell_surface(main_surface);
1249 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001251
1252 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001253}
1254
1255static const struct workspace_manager_interface workspace_manager_implementation = {
1256 workspace_manager_move_surface,
1257};
1258
1259static void
1260unbind_resource(struct wl_resource *resource)
1261{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001262 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001263}
1264
1265static void
1266bind_workspace_manager(struct wl_client *client,
1267 void *data, uint32_t version, uint32_t id)
1268{
1269 struct desktop_shell *shell = data;
1270 struct wl_resource *resource;
1271
Jason Ekstranda85118c2013-06-27 20:17:02 -05001272 resource = wl_resource_create(client,
1273 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001274
1275 if (resource == NULL) {
1276 weston_log("couldn't add workspace manager object");
1277 return;
1278 }
1279
Jason Ekstranda85118c2013-06-27 20:17:02 -05001280 wl_resource_set_implementation(resource,
1281 &workspace_manager_implementation,
1282 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001283 wl_list_insert(&shell->workspaces.client_list,
1284 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001285
1286 workspace_manager_send_state(resource,
1287 shell->workspaces.current,
1288 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289}
1290
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001291static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001292touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1293 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1294{
1295}
1296
1297static void
1298touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1299{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001300 struct weston_touch_move_grab *move =
1301 (struct weston_touch_move_grab *) container_of(
1302 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001303
Jonas Ådahl9484b692013-12-02 22:05:03 +01001304 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001305 shell_touch_grab_end(&move->base);
1306 free(move);
1307 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001308}
1309
1310static void
1311touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1312 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1313{
1314 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1315 struct shell_surface *shsurf = move->base.shsurf;
1316 struct weston_surface *es;
1317 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1318 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1319
1320 if (!shsurf)
1321 return;
1322
1323 es = shsurf->surface;
1324
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001325 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001326
1327 weston_compositor_schedule_repaint(es->compositor);
1328}
1329
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001330static void
1331touch_move_grab_cancel(struct weston_touch_grab *grab)
1332{
1333 struct weston_touch_move_grab *move =
1334 (struct weston_touch_move_grab *) container_of(
1335 grab, struct shell_touch_grab, grab);
1336
1337 shell_touch_grab_end(&move->base);
1338 free(move);
1339}
1340
Rusty Lynch1084da52013-08-15 09:10:08 -07001341static const struct weston_touch_grab_interface touch_move_grab_interface = {
1342 touch_move_grab_down,
1343 touch_move_grab_up,
1344 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001345 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001346};
1347
1348static int
1349surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1350{
1351 struct weston_touch_move_grab *move;
1352
1353 if (!shsurf)
1354 return -1;
1355
Rafael Antognolli03b16592013-12-03 15:35:42 -02001356 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001357 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001358 if (shsurf->grabbed)
1359 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001360
1361 move = malloc(sizeof *move);
1362 if (!move)
1363 return -1;
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001366 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001368 seat->touch->grab_y;
1369
1370 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1371 seat->touch);
1372
1373 return 0;
1374}
1375
1376static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001377noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001378{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001379}
1380
1381static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001382move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1383 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001386 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001387 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001388 int dx, dy;
1389
1390 weston_pointer_move(pointer, x, y);
1391 dx = wl_fixed_to_int(pointer->x + move->dx);
1392 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001393
1394 if (!shsurf)
1395 return;
1396
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001397 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001400}
1401
1402static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001403move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001404 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001405{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001406 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1407 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001408 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001409 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001410
Daniel Stone4dbadb12012-05-30 16:31:51 +01001411 if (pointer->button_count == 0 &&
1412 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001413 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001414 free(grab);
1415 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001416}
1417
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001418static void
1419move_grab_cancel(struct weston_pointer_grab *grab)
1420{
1421 struct shell_grab *shell_grab =
1422 container_of(grab, struct shell_grab, grab);
1423
1424 shell_grab_end(shell_grab);
1425 free(grab);
1426}
1427
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001428static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001429 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001430 move_grab_motion,
1431 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001432 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433};
1434
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001435static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001436surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001437{
1438 struct weston_move_grab *move;
1439
1440 if (!shsurf)
1441 return -1;
1442
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001443 if (shsurf->grabbed)
1444 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001445 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001446 return 0;
1447
1448 move = malloc(sizeof *move);
1449 if (!move)
1450 return -1;
1451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001453 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001456
1457 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001458 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001459
1460 return 0;
1461}
1462
1463static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001464common_surface_move(struct wl_resource *resource,
1465 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001466{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001467 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001468 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001469 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001470
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001471 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001472 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001473 seat->pointer->button_count > 0 &&
1474 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001476 if ((surface == shsurf->surface) &&
1477 (surface_move(shsurf, seat) < 0))
1478 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001479 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001480 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001481 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001483 if ((surface == shsurf->surface) &&
1484 (surface_touch_move(shsurf, seat) < 0))
1485 wl_resource_post_no_memory(resource);
1486 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001487}
1488
Rafael Antognollie2a34552013-12-03 15:35:45 -02001489static void
1490shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1491 struct wl_resource *seat_resource, uint32_t serial)
1492{
1493 common_surface_move(resource, seat_resource, serial);
1494}
1495
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001496struct weston_resize_grab {
1497 struct shell_grab base;
1498 uint32_t edges;
1499 int32_t width, height;
1500};
1501
1502static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001503resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1504 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001505{
1506 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001507 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001508 struct shell_surface *shsurf = resize->base.shsurf;
1509 int32_t width, height;
1510 wl_fixed_t from_x, from_y;
1511 wl_fixed_t to_x, to_y;
1512
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001513 weston_pointer_move(pointer, x, y);
1514
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001515 if (!shsurf)
1516 return;
1517
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 weston_view_from_global_fixed(shsurf->view,
1519 pointer->grab_x, pointer->grab_y,
1520 &from_x, &from_y);
1521 weston_view_from_global_fixed(shsurf->view,
1522 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523
1524 width = resize->width;
1525 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1526 width += wl_fixed_to_int(from_x - to_x);
1527 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1528 width += wl_fixed_to_int(to_x - from_x);
1529 }
1530
1531 height = resize->height;
1532 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1533 height += wl_fixed_to_int(from_y - to_y);
1534 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1535 height += wl_fixed_to_int(to_y - from_y);
1536 }
1537
1538 shsurf->client->send_configure(shsurf->surface,
1539 resize->edges, width, height);
1540}
1541
1542static void
1543send_configure(struct weston_surface *surface,
1544 uint32_t edges, int32_t width, int32_t height)
1545{
1546 struct shell_surface *shsurf = get_shell_surface(surface);
1547
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001548 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549 edges, width, height);
1550}
1551
1552static const struct weston_shell_client shell_client = {
1553 send_configure
1554};
1555
1556static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001557resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558 uint32_t time, uint32_t button, uint32_t state_w)
1559{
1560 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001561 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562 enum wl_pointer_button_state state = state_w;
1563
1564 if (pointer->button_count == 0 &&
1565 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1566 shell_grab_end(&resize->base);
1567 free(grab);
1568 }
1569}
1570
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001571static void
1572resize_grab_cancel(struct weston_pointer_grab *grab)
1573{
1574 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1575
1576 shell_grab_end(&resize->base);
1577 free(grab);
1578}
1579
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001580static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 noop_grab_focus,
1582 resize_grab_motion,
1583 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001584 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585};
1586
Giulio Camuffob8366642013-04-25 13:57:46 +03001587/*
1588 * Returns the bounding box of a surface and all its sub-surfaces,
1589 * in the surface coordinates system. */
1590static void
1591surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1592 int32_t *y, int32_t *w, int32_t *h) {
1593 pixman_region32_t region;
1594 pixman_box32_t *box;
1595 struct weston_subsurface *subsurface;
1596
1597 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 surface->width,
1599 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001600
1601 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1602 pixman_region32_union_rect(&region, &region,
1603 subsurface->position.x,
1604 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 subsurface->surface->width,
1606 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001607 }
1608
1609 box = pixman_region32_extents(&region);
1610 if (x)
1611 *x = box->x1;
1612 if (y)
1613 *y = box->y1;
1614 if (w)
1615 *w = box->x2 - box->x1;
1616 if (h)
1617 *h = box->y2 - box->y1;
1618
1619 pixman_region32_fini(&region);
1620}
1621
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622static int
1623surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001624 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625{
1626 struct weston_resize_grab *resize;
1627
Rafael Antognolli03b16592013-12-03 15:35:42 -02001628 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629 return 0;
1630
1631 if (edges == 0 || edges > 15 ||
1632 (edges & 3) == 3 || (edges & 12) == 12)
1633 return 0;
1634
1635 resize = malloc(sizeof *resize);
1636 if (!resize)
1637 return -1;
1638
1639 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001640 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1641 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642
1643 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001644 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645
1646 return 0;
1647}
1648
1649static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001650common_surface_resize(struct wl_resource *resource,
1651 struct wl_resource *seat_resource, uint32_t serial,
1652 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001654 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001655 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001656 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657
Rafael Antognolli03b16592013-12-03 15:35:42 -02001658 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659 return;
1660
Kristian Høgsberge3148752013-05-06 23:19:49 -04001661 if (seat->pointer->button_count == 0 ||
1662 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001663 seat->pointer->focus == NULL)
1664 return;
1665
1666 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1667 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668 return;
1669
Kristian Høgsberge3148752013-05-06 23:19:49 -04001670 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 wl_resource_post_no_memory(resource);
1672}
1673
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001674static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001675shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1676 struct wl_resource *seat_resource, uint32_t serial,
1677 uint32_t edges)
1678{
1679 common_surface_resize(resource, seat_resource, serial, edges);
1680}
1681
1682static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001683end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1684
1685static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001686busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001687{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001688 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001689 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001691 wl_fixed_t sx, sy;
1692
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 view = weston_compositor_pick_view(pointer->seat->compositor,
1694 pointer->x, pointer->y,
1695 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001696
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001698 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001699}
1700
1701static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001702busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1703 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001704{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001705 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001706}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001707
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001708static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001709busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001710 uint32_t time, uint32_t button, uint32_t state)
1711{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001712 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001713 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001714 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001715
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001716 if (shsurf && button == BTN_LEFT && state) {
1717 activate(shsurf->shell, shsurf->surface, seat);
1718 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001719 } else if (shsurf && button == BTN_RIGHT && state) {
1720 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001721 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001722 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001723}
1724
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001725static void
1726busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1727{
1728 struct shell_grab *grab = (struct shell_grab *) base;
1729
1730 shell_grab_end(grab);
1731 free(grab);
1732}
1733
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001734static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001735 busy_cursor_grab_focus,
1736 busy_cursor_grab_motion,
1737 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001738 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739};
1740
1741static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001742set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743{
1744 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001745
1746 grab = malloc(sizeof *grab);
1747 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748 return;
1749
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001750 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1751 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001752}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001755end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756{
1757 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1758
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001759 if (grab->grab.interface == &busy_cursor_grab_interface &&
1760 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001761 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764}
1765
Scott Moreau9521d5e2012-04-19 13:06:17 -06001766static void
1767ping_timer_destroy(struct shell_surface *shsurf)
1768{
1769 if (!shsurf || !shsurf->ping_timer)
1770 return;
1771
1772 if (shsurf->ping_timer->source)
1773 wl_event_source_remove(shsurf->ping_timer->source);
1774
1775 free(shsurf->ping_timer);
1776 shsurf->ping_timer = NULL;
1777}
1778
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001779static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001780ping_timeout_handler(void *data)
1781{
1782 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001784
Scott Moreau9521d5e2012-04-19 13:06:17 -06001785 /* Client is not responding */
1786 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787
1788 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001789 if (seat->pointer->focus &&
1790 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001791 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001792
1793 return 1;
1794}
1795
1796static void
1797ping_handler(struct weston_surface *surface, uint32_t serial)
1798{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001799 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001800 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001802
1803 if (!shsurf)
1804 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001805 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001806 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001807
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001808 if (shsurf->surface == shsurf->shell->grab_surface)
1809 return;
1810
Scott Moreauff1db4a2012-04-17 19:06:18 -06001811 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001812 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001813 if (!shsurf->ping_timer)
1814 return;
1815
1816 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001817 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1818 shsurf->ping_timer->source =
1819 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1820 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1821
Rafael Antognollie2a34552013-12-03 15:35:45 -02001822 if (shell_surface_is_wl_shell_surface(shsurf))
1823 wl_shell_surface_send_ping(shsurf->resource, serial);
1824 else if (shell_surface_is_xdg_surface(shsurf))
1825 xdg_surface_send_ping(shsurf->resource, serial);
1826 else if (shell_surface_is_xdg_popup(shsurf))
1827 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001828 }
1829}
1830
1831static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832handle_pointer_focus(struct wl_listener *listener, void *data)
1833{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001834 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 struct weston_compositor *compositor;
1837 struct shell_surface *shsurf;
1838 uint32_t serial;
1839
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001841 return;
1842
Jason Ekstranda7af7042013-10-12 22:38:11 -05001843 compositor = view->surface->compositor;
1844 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001846 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001847 set_busy_cursor(shsurf, pointer);
1848 } else {
1849 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851 }
1852}
1853
1854static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001855create_pointer_focus_listener(struct weston_seat *seat)
1856{
1857 struct wl_listener *listener;
1858
Kristian Høgsberge3148752013-05-06 23:19:49 -04001859 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001860 return;
1861
1862 listener = malloc(sizeof *listener);
1863 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001864 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001865}
1866
1867static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001868xdg_surface_set_transient_for(struct wl_client *client,
1869 struct wl_resource *resource,
1870 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001871{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001872 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001873 struct weston_surface *parent;
1874
1875 if (parent_resource)
1876 parent = wl_resource_get_user_data(parent_resource);
1877 else
1878 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001879
1880 shell_surface_set_parent(shsurf, parent);
1881}
1882
1883static void
1884surface_pong(struct shell_surface *shsurf, uint32_t serial)
1885{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001886 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001887 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001889 if (shsurf->ping_timer == NULL)
1890 /* Just ignore unsolicited pong. */
1891 return;
1892
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001895 wl_list_for_each(seat, &ec->seat_list, link) {
1896 if(seat->pointer)
1897 end_busy_cursor(shsurf, seat->pointer);
1898 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001899 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900 }
1901}
1902
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001903static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001904shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1905 uint32_t serial)
1906{
1907 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1908
1909 surface_pong(shsurf, serial);
1910
1911}
1912
1913static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001914set_title(struct shell_surface *shsurf, const char *title)
1915{
1916 free(shsurf->title);
1917 shsurf->title = strdup(title);
1918}
1919
1920static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001921shell_surface_set_title(struct wl_client *client,
1922 struct wl_resource *resource, const char *title)
1923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001925
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001926 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001927}
1928
1929static void
1930shell_surface_set_class(struct wl_client *client,
1931 struct wl_resource *resource, const char *class)
1932{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001934
1935 free(shsurf->class);
1936 shsurf->class = strdup(class);
1937}
1938
Alex Wu4539b082012-03-01 12:57:46 +08001939static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001940restore_output_mode(struct weston_output *output)
1941{
Hardening57388e42013-09-18 23:56:36 +02001942 if (output->current_mode != output->original_mode ||
1943 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001944 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001945 output->original_mode,
1946 output->original_scale,
1947 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001948}
1949
1950static void
1951restore_all_output_modes(struct weston_compositor *compositor)
1952{
1953 struct weston_output *output;
1954
1955 wl_list_for_each(output, &compositor->output_list, link)
1956 restore_output_mode(output);
1957}
1958
Philip Withnallbecb77e2013-11-25 18:01:30 +00001959static int
1960get_output_panel_height(struct desktop_shell *shell,
1961 struct weston_output *output)
1962{
1963 struct weston_view *view;
1964 int panel_height = 0;
1965
1966 if (!output)
1967 return 0;
1968
1969 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1970 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001971 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001972 break;
1973 }
1974 }
1975
1976 return panel_height;
1977}
1978
Philip Withnall07926d92013-11-25 18:01:40 +00001979/* The surface will be inserted into the list immediately after the link
1980 * returned by this function (i.e. will be stacked immediately above the
1981 * returned link). */
1982static struct wl_list *
1983shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1984{
1985 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08001986 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00001987
1988 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08001989 case SHELL_SURFACE_POPUP:
1990 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02001991 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02001992 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02001993 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08001994 /* Move the surface to its parent layer so
1995 * that surfaces which are transient for
1996 * fullscreen surfaces don't get hidden by the
1997 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02001998
1999 /* TODO: Handle a parent with multiple views */
2000 parent = get_default_view(shsurf->parent);
2001 if (parent)
2002 return parent->layer_link.prev;
2003 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002004 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002005
2006 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002007 return &shsurf->shell->fullscreen_layer.view_list;
2008
Philip Withnall07926d92013-11-25 18:01:40 +00002009 case SHELL_SURFACE_NONE:
2010 default:
2011 /* Go to the fallback, below. */
2012 break;
2013 }
2014
2015 /* Move the surface to a normal workspace layer so that surfaces
2016 * which were previously fullscreen or transient are no longer
2017 * rendered on top. */
2018 ws = get_current_workspace(shsurf->shell);
2019 return &ws->layer.view_list;
2020}
2021
Philip Withnall648a4dd2013-11-25 18:01:44 +00002022static void
2023shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2024{
2025 struct shell_surface *child;
2026
2027 /* Move the child layers to the same workspace as shsurf. They will be
2028 * stacked above shsurf. */
2029 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2030 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2031 weston_view_geometry_dirty(child->view);
2032 wl_list_remove(&child->view->layer_link);
2033 wl_list_insert(shsurf->view->layer_link.prev,
2034 &child->view->layer_link);
2035 weston_view_geometry_dirty(child->view);
2036 weston_surface_damage(child->surface);
2037
2038 /* Recurse. We don’t expect this to recurse very far (if
2039 * at all) because that would imply we have transient
2040 * (or popup) children of transient surfaces, which
2041 * would be unusual. */
2042 shell_surface_update_child_surface_layers(child);
2043 }
2044 }
2045}
2046
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002047/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002048 * geometry to ensure the changes are redrawn.
2049 *
2050 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2051 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002052static void
2053shell_surface_update_layer(struct shell_surface *shsurf)
2054{
2055 struct wl_list *new_layer_link;
2056
2057 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2058
2059 if (new_layer_link == &shsurf->view->layer_link)
2060 return;
2061
2062 weston_view_geometry_dirty(shsurf->view);
2063 wl_list_remove(&shsurf->view->layer_link);
2064 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2065 weston_view_geometry_dirty(shsurf->view);
2066 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002067
2068 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002069}
2070
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002071static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002072shell_surface_set_parent(struct shell_surface *shsurf,
2073 struct weston_surface *parent)
2074{
2075 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002076
2077 wl_list_remove(&shsurf->children_link);
2078 wl_list_init(&shsurf->children_link);
2079
2080 /* Insert into the parent surface’s child list. */
2081 if (parent != NULL) {
2082 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2083 if (parent_shsurf != NULL)
2084 wl_list_insert(&parent_shsurf->children_list,
2085 &shsurf->children_link);
2086 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002087}
2088
2089static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002090shell_surface_set_output(struct shell_surface *shsurf,
2091 struct weston_output *output)
2092{
2093 struct weston_surface *es = shsurf->surface;
2094
2095 /* get the default output, if the client set it as NULL
2096 check whether the ouput is available */
2097 if (output)
2098 shsurf->output = output;
2099 else if (es->output)
2100 shsurf->output = es->output;
2101 else
2102 shsurf->output = get_default_output(es->compositor);
2103}
2104
2105static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002106surface_clear_next_states(struct shell_surface *shsurf)
2107{
2108 shsurf->next_state.maximized = false;
2109 shsurf->next_state.fullscreen = false;
2110
2111 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2112 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2113 shsurf->state_changed = true;
2114}
2115
2116static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002117set_toplevel(struct shell_surface *shsurf)
2118{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002119 shell_surface_set_parent(shsurf, NULL);
2120 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002121 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002122
2123 /* The layer_link is updated in set_surface_type(),
2124 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125}
2126
2127static void
2128shell_surface_set_toplevel(struct wl_client *client,
2129 struct wl_resource *resource)
2130{
2131 struct shell_surface *surface = wl_resource_get_user_data(resource);
2132
2133 set_toplevel(surface);
2134}
2135
2136static void
2137set_transient(struct shell_surface *shsurf,
2138 struct weston_surface *parent, int x, int y, uint32_t flags)
2139{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002140 assert(parent != NULL);
2141
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002142 shell_surface_set_parent(shsurf, parent);
2143
2144 surface_clear_next_states(shsurf);
2145
Philip Withnallbecb77e2013-11-25 18:01:30 +00002146 shsurf->transient.x = x;
2147 shsurf->transient.y = y;
2148 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002149
Rafael Antognollied207b42013-12-03 15:35:43 -02002150 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002151 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002152 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002153
2154 /* The layer_link is updated in set_surface_type(),
2155 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002156}
2157
2158static void
2159shell_surface_set_transient(struct wl_client *client,
2160 struct wl_resource *resource,
2161 struct wl_resource *parent_resource,
2162 int x, int y, uint32_t flags)
2163{
2164 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2165 struct weston_surface *parent =
2166 wl_resource_get_user_data(parent_resource);
2167
2168 set_transient(shsurf, parent, x, y, flags);
2169}
2170
2171static void
2172set_fullscreen(struct shell_surface *shsurf,
2173 uint32_t method,
2174 uint32_t framerate,
2175 struct weston_output *output)
2176{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002177 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002178
2179 shsurf->fullscreen_output = shsurf->output;
2180 shsurf->fullscreen.type = method;
2181 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002182
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002183 shsurf->next_state.fullscreen = true;
2184 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002185 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002186
2187 shsurf->client->send_configure(shsurf->surface, 0,
2188 shsurf->output->width,
2189 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002190
2191 /* The layer_link is updated in set_surface_type(),
2192 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002193}
2194
2195static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002196weston_view_set_initial_position(struct weston_view *view,
2197 struct desktop_shell *shell);
2198
2199static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002200unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002201{
Philip Withnallf85fe842013-11-25 18:01:37 +00002202 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002203 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2204 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002205 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002206 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002207 shsurf->fullscreen_output = NULL;
2208
Alex Wu4539b082012-03-01 12:57:46 +08002209 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2210 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002211
Alex Wu4539b082012-03-01 12:57:46 +08002212 wl_list_remove(&shsurf->fullscreen.transform.link);
2213 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 if (shsurf->fullscreen.black_view)
2216 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2217 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002218
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002219 if (shsurf->saved_position_valid)
2220 weston_view_set_position(shsurf->view,
2221 shsurf->saved_x, shsurf->saved_y);
2222 else
2223 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2224
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002225 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002227 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002228 shsurf->saved_rotation_valid = false;
2229 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002230
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002231 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002232}
2233
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002234static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002235shell_surface_set_fullscreen(struct wl_client *client,
2236 struct wl_resource *resource,
2237 uint32_t method,
2238 uint32_t framerate,
2239 struct wl_resource *output_resource)
2240{
2241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2242 struct weston_output *output;
2243
2244 if (output_resource)
2245 output = wl_resource_get_user_data(output_resource);
2246 else
2247 output = NULL;
2248
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002249 shell_surface_set_parent(shsurf, NULL);
2250
Rafael Antognolli03b16592013-12-03 15:35:42 -02002251 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002252 set_fullscreen(shsurf, method, framerate, output);
2253}
2254
2255static void
2256set_popup(struct shell_surface *shsurf,
2257 struct weston_surface *parent,
2258 struct weston_seat *seat,
2259 uint32_t serial,
2260 int32_t x,
2261 int32_t y)
2262{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002263 assert(parent != NULL);
2264
Philip Withnallbecb77e2013-11-25 18:01:30 +00002265 shsurf->popup.shseat = get_shell_seat(seat);
2266 shsurf->popup.serial = serial;
2267 shsurf->popup.x = x;
2268 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002269
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002270 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002271}
2272
2273static void
2274shell_surface_set_popup(struct wl_client *client,
2275 struct wl_resource *resource,
2276 struct wl_resource *seat_resource,
2277 uint32_t serial,
2278 struct wl_resource *parent_resource,
2279 int32_t x, int32_t y, uint32_t flags)
2280{
2281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002282 struct weston_surface *parent =
2283 wl_resource_get_user_data(parent_resource);
2284
2285 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002286
Rafael Antognolli03b16592013-12-03 15:35:42 -02002287 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002288 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002289 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002290 wl_resource_get_user_data(seat_resource),
2291 serial, x, y);
2292}
2293
2294static void
2295set_maximized(struct shell_surface *shsurf,
2296 struct weston_output *output)
2297{
2298 struct desktop_shell *shell;
2299 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300
Philip Withnall352e7ed2013-11-25 18:01:35 +00002301 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302
2303 shell = shell_surface_get_shell(shsurf);
2304 panel_height = get_output_panel_height(shell, shsurf->output);
2305 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2306
2307 shsurf->client->send_configure(shsurf->surface, edges,
2308 shsurf->output->width,
2309 shsurf->output->height - panel_height);
2310
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002311 shsurf->next_state.maximized = true;
2312 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002313 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314}
2315
2316static void
2317unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002318{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002319 /* undo all maximized things here */
2320 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002321
2322 if (shsurf->saved_position_valid)
2323 weston_view_set_position(shsurf->view,
2324 shsurf->saved_x, shsurf->saved_y);
2325 else
2326 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002327
2328 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 wl_list_insert(&shsurf->view->geometry.transformation_list,
2330 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002331 shsurf->saved_rotation_valid = false;
2332 }
2333
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002334 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002335}
2336
Philip Withnallbecb77e2013-11-25 18:01:30 +00002337static void
2338shell_surface_set_maximized(struct wl_client *client,
2339 struct wl_resource *resource,
2340 struct wl_resource *output_resource)
2341{
2342 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2343 struct weston_output *output;
2344
2345 if (output_resource)
2346 output = wl_resource_get_user_data(output_resource);
2347 else
2348 output = NULL;
2349
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002350 shell_surface_set_parent(shsurf, NULL);
2351
Rafael Antognolli03b16592013-12-03 15:35:42 -02002352 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 set_maximized(shsurf, output);
2354}
2355
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002356/* This is only ever called from set_surface_type(), so there’s no need to
2357 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002358static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002360{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002361 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002362 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002363 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002364 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002365
Pekka Paalanen98262232011-12-01 10:42:22 +02002366 return 0;
2367}
2368
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002369static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002370set_full_output(struct shell_surface *shsurf)
2371{
2372 shsurf->saved_x = shsurf->view->geometry.x;
2373 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002374 shsurf->saved_width = shsurf->surface->width;
2375 shsurf->saved_height = shsurf->surface->height;
2376 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002377 shsurf->saved_position_valid = true;
2378
2379 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2380 wl_list_remove(&shsurf->rotation.transform.link);
2381 wl_list_init(&shsurf->rotation.transform.link);
2382 weston_view_geometry_dirty(shsurf->view);
2383 shsurf->saved_rotation_valid = true;
2384 }
2385}
2386
2387static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002388set_surface_type(struct shell_surface *shsurf)
2389{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002390 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002392
Philip Withnallbecb77e2013-11-25 18:01:30 +00002393 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002394
Rafael Antognolli03b16592013-12-03 15:35:42 -02002395 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002396 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002397
2398 switch (shsurf->type) {
2399 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002400 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002401 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002402 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 weston_view_set_position(shsurf->view,
2404 pev->geometry.x + shsurf->transient.x,
2405 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002406 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002407 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002408
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002409 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 weston_view_set_position(shsurf->view, shsurf->transient.x,
2411 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002412 break;
2413
Philip Withnall0f640e22013-11-25 18:01:31 +00002414 case SHELL_SURFACE_POPUP:
2415 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002416 default:
2417 break;
2418 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419
2420 /* Update the surface’s layer. */
2421 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002422}
2423
Tiago Vignattibe143262012-04-16 17:31:41 +03002424static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002425shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002426{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002427 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002428}
2429
Alex Wu21858432012-04-01 20:13:08 +08002430static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002431black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002432
Jason Ekstranda7af7042013-10-12 22:38:11 -05002433static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002434create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002435 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002436 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002437{
2438 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002440
2441 surface = weston_surface_create(ec);
2442 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002443 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002444 return NULL;
2445 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446 view = weston_view_create(surface);
2447 if (surface == NULL) {
2448 weston_log("no memory\n");
2449 weston_surface_destroy(surface);
2450 return NULL;
2451 }
Alex Wu4539b082012-03-01 12:57:46 +08002452
Alex Wu21858432012-04-01 20:13:08 +08002453 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002454 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002455 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002456 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002457 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002458 pixman_region32_fini(&surface->input);
2459 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002460
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002461 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002462 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463
2464 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002465}
2466
Philip Withnalled8f1a92013-11-25 18:01:43 +00002467static void
2468shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2469{
2470 struct weston_output *output = shsurf->fullscreen_output;
2471
Rafael Antognolli03b16592013-12-03 15:35:42 -02002472 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002473
2474 if (!shsurf->fullscreen.black_view)
2475 shsurf->fullscreen.black_view =
2476 create_black_surface(shsurf->surface->compositor,
2477 shsurf->surface,
2478 output->x, output->y,
2479 output->width,
2480 output->height);
2481
2482 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2483 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2484 wl_list_insert(&shsurf->view->layer_link,
2485 &shsurf->fullscreen.black_view->layer_link);
2486 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2487 weston_surface_damage(shsurf->surface);
2488}
2489
Alex Wu4539b082012-03-01 12:57:46 +08002490/* Create black surface and append it to the associated fullscreen surface.
2491 * Handle size dismatch and positioning according to the method. */
2492static void
2493shell_configure_fullscreen(struct shell_surface *shsurf)
2494{
2495 struct weston_output *output = shsurf->fullscreen_output;
2496 struct weston_surface *surface = shsurf->surface;
2497 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002498 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002499 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002500
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002501 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2502 restore_output_mode(output);
2503
Philip Withnalled8f1a92013-11-25 18:01:43 +00002504 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002505
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002507 &surf_width, &surf_height);
2508
Alex Wu4539b082012-03-01 12:57:46 +08002509 switch (shsurf->fullscreen.type) {
2510 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002511 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002513 break;
2514 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002515 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002516 if (output->width == surf_width &&
2517 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002518 weston_view_set_position(shsurf->view,
2519 output->x - surf_x,
2520 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002521 break;
2522 }
2523
Alex Wu4539b082012-03-01 12:57:46 +08002524 matrix = &shsurf->fullscreen.transform.matrix;
2525 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002526
Scott Moreau1bad5db2012-08-18 01:04:05 -06002527 output_aspect = (float) output->width /
2528 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 /* XXX: Use surf_width and surf_height here? */
2530 surface_aspect = (float) surface->width /
2531 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002532 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002533 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002534 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002535 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002536 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002537 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002538
Alex Wu4539b082012-03-01 12:57:46 +08002539 weston_matrix_scale(matrix, scale, scale, 1);
2540 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002542 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002543 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2544 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546
Alex Wu4539b082012-03-01 12:57:46 +08002547 break;
2548 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002549 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002550 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002551 surf_width * surface->buffer_viewport.scale,
2552 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002553 shsurf->fullscreen.framerate};
2554
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002555 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002556 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 weston_view_set_position(shsurf->view,
2558 output->x - surf_x,
2559 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002560 shsurf->fullscreen.black_view->surface->width = output->width;
2561 shsurf->fullscreen.black_view->surface->height = output->height;
2562 weston_view_set_position(shsurf->fullscreen.black_view,
2563 output->x - surf_x,
2564 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002565 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002566 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002567 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002569 }
Alex Wubd3354b2012-04-17 17:20:49 +08002570 }
Alex Wu4539b082012-03-01 12:57:46 +08002571 break;
2572 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002574 break;
2575 default:
2576 break;
2577 }
2578}
2579
Alex Wu4539b082012-03-01 12:57:46 +08002580static void
2581shell_map_fullscreen(struct shell_surface *shsurf)
2582{
Alex Wubd3354b2012-04-17 17:20:49 +08002583 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002584}
2585
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002586static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002587set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2588{
2589 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002590 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002591 shsurf->transient.x = x;
2592 shsurf->transient.y = y;
2593 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002594
2595 shell_surface_set_parent(shsurf, NULL);
2596
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002597 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002598 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002599}
2600
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002601static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002602
2603static void
2604destroy_shell_seat(struct wl_listener *listener, void *data)
2605{
2606 struct shell_seat *shseat =
2607 container_of(listener,
2608 struct shell_seat, seat_destroy_listener);
2609 struct shell_surface *shsurf, *prev = NULL;
2610
2611 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002612 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002613 shseat->popup_grab.client = NULL;
2614
2615 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2616 shsurf->popup.shseat = NULL;
2617 if (prev) {
2618 wl_list_init(&prev->popup.grab_link);
2619 }
2620 prev = shsurf;
2621 }
2622 wl_list_init(&prev->popup.grab_link);
2623 }
2624
2625 wl_list_remove(&shseat->seat_destroy_listener.link);
2626 free(shseat);
2627}
2628
2629static struct shell_seat *
2630create_shell_seat(struct weston_seat *seat)
2631{
2632 struct shell_seat *shseat;
2633
2634 shseat = calloc(1, sizeof *shseat);
2635 if (!shseat) {
2636 weston_log("no memory to allocate shell seat\n");
2637 return NULL;
2638 }
2639
2640 shseat->seat = seat;
2641 wl_list_init(&shseat->popup_grab.surfaces_list);
2642
2643 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2644 wl_signal_add(&seat->destroy_signal,
2645 &shseat->seat_destroy_listener);
2646
2647 return shseat;
2648}
2649
2650static struct shell_seat *
2651get_shell_seat(struct weston_seat *seat)
2652{
2653 struct wl_listener *listener;
2654
2655 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2656 if (listener == NULL)
2657 return create_shell_seat(seat);
2658
2659 return container_of(listener,
2660 struct shell_seat, seat_destroy_listener);
2661}
2662
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002663static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002664popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002665{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002666 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002668 struct shell_seat *shseat =
2669 container_of(grab, struct shell_seat, popup_grab.grab);
2670 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002671 wl_fixed_t sx, sy;
2672
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 view = weston_compositor_pick_view(pointer->seat->compositor,
2674 pointer->x, pointer->y,
2675 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002676
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 if (view && view->surface->resource &&
2678 wl_resource_get_client(view->surface->resource) == client) {
2679 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002681 weston_pointer_set_focus(pointer, NULL,
2682 wl_fixed_from_int(0),
2683 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684 }
2685}
2686
2687static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002688popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2689 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002690{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002691 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002692 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002693 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002695 weston_pointer_move(pointer, x, y);
2696
Neil Roberts96d790e2013-09-19 17:32:00 +01002697 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 weston_view_from_global_fixed(pointer->focus,
2699 pointer->x, pointer->y,
2700 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002701 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002702 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002703}
2704
2705static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002706popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002707 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002708{
2709 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002710 struct shell_seat *shseat =
2711 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002712 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002713 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002715 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002716
Neil Roberts96d790e2013-09-19 17:32:00 +01002717 resource_list = &grab->pointer->focus_resource_list;
2718 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002719 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002720 wl_resource_for_each(resource, resource_list) {
2721 wl_pointer_send_button(resource, serial,
2722 time, button, state);
2723 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002724 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002725 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002726 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002727 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728 }
2729
Daniel Stone4dbadb12012-05-30 16:31:51 +01002730 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002731 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002732}
2733
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002734static void
2735popup_grab_cancel(struct weston_pointer_grab *grab)
2736{
2737 popup_grab_end(grab->pointer);
2738}
2739
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002740static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002741 popup_grab_focus,
2742 popup_grab_motion,
2743 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002744 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002745};
2746
2747static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748shell_surface_send_popup_done(struct shell_surface *shsurf)
2749{
2750 if (shell_surface_is_wl_shell_surface(shsurf))
2751 wl_shell_surface_send_popup_done(shsurf->resource);
2752 else if (shell_surface_is_xdg_popup(shsurf))
2753 xdg_popup_send_popup_done(shsurf->resource,
2754 shsurf->popup.serial);
2755}
2756
2757static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002758popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002759{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002760 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002761 struct shell_seat *shseat =
2762 container_of(grab, struct shell_seat, popup_grab.grab);
2763 struct shell_surface *shsurf;
2764 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002765
2766 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002767 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002768 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002769 shseat->popup_grab.grab.interface = NULL;
2770 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002771 /* Send the popup_done event to all the popups open */
2772 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002773 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002774 shsurf->popup.shseat = NULL;
2775 if (prev) {
2776 wl_list_init(&prev->popup.grab_link);
2777 }
2778 prev = shsurf;
2779 }
2780 wl_list_init(&prev->popup.grab_link);
2781 wl_list_init(&shseat->popup_grab.surfaces_list);
2782 }
2783}
2784
2785static void
2786add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2787{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002788 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002789
2790 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002791 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002792 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002793 /* We must make sure here that this popup was opened after
2794 * a mouse press, and not just by moving around with other
2795 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002796 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002797 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002798
Rob Bradforddfe31052013-06-26 19:49:11 +01002799 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002800 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002801 } else {
2802 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002803 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002804}
2805
2806static void
2807remove_popup_grab(struct shell_surface *shsurf)
2808{
2809 struct shell_seat *shseat = shsurf->popup.shseat;
2810
2811 wl_list_remove(&shsurf->popup.grab_link);
2812 wl_list_init(&shsurf->popup.grab_link);
2813 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002814 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002815 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002816 }
2817}
2818
2819static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002820shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002822 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 shsurf->surface->output = parent_view->output;
2826 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002827
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 weston_view_set_transform_parent(shsurf->view, parent_view);
2829 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2830 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002831
Kristian Høgsberge3148752013-05-06 23:19:49 -04002832 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002833 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002834 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002835 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002836 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002837 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002838}
2839
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002840static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002841 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002842 shell_surface_move,
2843 shell_surface_resize,
2844 shell_surface_set_toplevel,
2845 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002847 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002848 shell_surface_set_maximized,
2849 shell_surface_set_title,
2850 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002851};
2852
2853static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002854destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002855{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002856 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2857
Giulio Camuffo5085a752013-03-25 21:42:45 +01002858 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2859 remove_popup_grab(shsurf);
2860 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002861
Alex Wubd3354b2012-04-17 17:20:49 +08002862 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002863 shell_surface_is_top_fullscreen(shsurf))
2864 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002865
Jason Ekstranda7af7042013-10-12 22:38:11 -05002866 if (shsurf->fullscreen.black_view)
2867 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002868
Alex Wubd3354b2012-04-17 17:20:49 +08002869 /* As destroy_resource() use wl_list_for_each_safe(),
2870 * we can always remove the listener.
2871 */
2872 wl_list_remove(&shsurf->surface_destroy_listener.link);
2873 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002874 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002875 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002876
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 weston_view_destroy(shsurf->view);
2878
Philip Withnall648a4dd2013-11-25 18:01:44 +00002879 wl_list_remove(&shsurf->children_link);
2880
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002881 wl_list_remove(&shsurf->link);
2882 free(shsurf);
2883}
2884
2885static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002886shell_destroy_shell_surface(struct wl_resource *resource)
2887{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002888 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002889
2890 destroy_shell_surface(shsurf);
2891}
2892
2893static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002894shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002895{
2896 struct shell_surface *shsurf = container_of(listener,
2897 struct shell_surface,
2898 surface_destroy_listener);
2899
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002900 if (shsurf->resource)
2901 wl_resource_destroy(shsurf->resource);
2902 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002903 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002904}
2905
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002906static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002907shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002908static void
2909shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002910
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002911struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002912get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002913{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002914 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002915 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002916 else
2917 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002918}
2919
Rafael Antognollie2a34552013-12-03 15:35:45 -02002920static struct shell_surface *
2921create_common_surface(void *shell, struct weston_surface *surface,
2922 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002923{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002924 struct shell_surface *shsurf;
2925
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002926 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002927 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002928 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002929 }
2930
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002931 shsurf = calloc(1, sizeof *shsurf);
2932 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002933 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002934 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002935 }
2936
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 shsurf->view = weston_view_create(surface);
2938 if (!shsurf->view) {
2939 weston_log("no memory to allocate shell surface\n");
2940 free(shsurf);
2941 return NULL;
2942 }
2943
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002944 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002945 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002946 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002947
Tiago Vignattibc052c92012-04-19 16:18:18 +03002948 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002949 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002950 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002951 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002952 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002953 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002954 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2955 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002957 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002958 wl_list_init(&shsurf->fullscreen.transform.link);
2959
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002960 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002961 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002962 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002963 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002964
2965 /* init link so its safe to always remove it in destroy_shell_surface */
2966 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002967 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002968
Pekka Paalanen460099f2012-01-20 16:48:25 +02002969 /* empty when not in use */
2970 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002971 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002972
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002973 wl_list_init(&shsurf->workspace_transform.link);
2974
Philip Withnall648a4dd2013-11-25 18:01:44 +00002975 wl_list_init(&shsurf->children_link);
2976 wl_list_init(&shsurf->children_list);
2977 shsurf->parent = NULL;
2978
Pekka Paalanen98262232011-12-01 10:42:22 +02002979 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002980
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002981 shsurf->client = client;
2982
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002983 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002984}
2985
Rafael Antognollie2a34552013-12-03 15:35:45 -02002986static struct shell_surface *
2987create_shell_surface(void *shell, struct weston_surface *surface,
2988 const struct weston_shell_client *client)
2989{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002990 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002991}
2992
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993static struct weston_view *
2994get_primary_view(void *shell, struct shell_surface *shsurf)
2995{
2996 return shsurf->view;
2997}
2998
Tiago Vignattibc052c92012-04-19 16:18:18 +03002999static void
3000shell_get_shell_surface(struct wl_client *client,
3001 struct wl_resource *resource,
3002 uint32_t id,
3003 struct wl_resource *surface_resource)
3004{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003005 struct weston_surface *surface =
3006 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003007 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003008 struct shell_surface *shsurf;
3009
3010 if (get_shell_surface(surface)) {
3011 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003012 WL_DISPLAY_ERROR_INVALID_OBJECT,
3013 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003014 return;
3015 }
3016
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003017 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003018 if (!shsurf) {
3019 wl_resource_post_error(surface_resource,
3020 WL_DISPLAY_ERROR_INVALID_OBJECT,
3021 "surface->configure already set");
3022 return;
3023 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003024
Jason Ekstranda85118c2013-06-27 20:17:02 -05003025 shsurf->resource =
3026 wl_resource_create(client,
3027 &wl_shell_surface_interface, 1, id);
3028 wl_resource_set_implementation(shsurf->resource,
3029 &shell_surface_implementation,
3030 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003031}
3032
Rafael Antognollie2a34552013-12-03 15:35:45 -02003033static bool
3034shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3035{
3036 return wl_resource_instance_of(shsurf->resource,
3037 &wl_shell_surface_interface,
3038 &shell_surface_implementation);
3039}
3040
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003041static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003042 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003043};
3044
Rafael Antognollie2a34552013-12-03 15:35:45 -02003045/****************************
3046 * xdg-shell implementation */
3047
3048static void
3049xdg_surface_destroy(struct wl_client *client,
3050 struct wl_resource *resource)
3051{
3052 wl_resource_destroy(resource);
3053}
3054
3055static void
3056xdg_surface_pong(struct wl_client *client,
3057 struct wl_resource *resource,
3058 uint32_t serial)
3059{
3060 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3061
3062 surface_pong(shsurf, serial);
3063}
3064
3065static void
3066xdg_surface_set_app_id(struct wl_client *client,
3067 struct wl_resource *resource,
3068 const char *app_id)
3069{
3070 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3071
3072 free(shsurf->class);
3073 shsurf->class = strdup(app_id);
3074}
3075
3076static void
3077xdg_surface_set_title(struct wl_client *client,
3078 struct wl_resource *resource, const char *title)
3079{
3080 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3081
3082 set_title(shsurf, title);
3083}
3084
3085static void
3086xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3087 struct wl_resource *seat_resource, uint32_t serial)
3088{
3089 common_surface_move(resource, seat_resource, serial);
3090}
3091
3092static void
3093xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3094 struct wl_resource *seat_resource, uint32_t serial,
3095 uint32_t edges)
3096{
3097 common_surface_resize(resource, seat_resource, serial, edges);
3098}
3099
3100static void
3101xdg_surface_set_output(struct wl_client *client,
3102 struct wl_resource *resource,
3103 struct wl_resource *output_resource)
3104{
3105 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3106 struct weston_output *output;
3107
3108 if (output_resource)
3109 output = wl_resource_get_user_data(output_resource);
3110 else
3111 output = NULL;
3112
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003113 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003114}
3115
3116static void
3117xdg_surface_set_fullscreen(struct wl_client *client,
3118 struct wl_resource *resource)
3119{
3120 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3121
3122 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3123 return;
3124
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003125 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003126 set_fullscreen(shsurf,
3127 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003128 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003129}
3130
3131static void
3132xdg_surface_unset_fullscreen(struct wl_client *client,
3133 struct wl_resource *resource)
3134{
3135 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003136 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003137
3138 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3139 return;
3140
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003141 if (!shsurf->next_state.fullscreen)
3142 return;
3143
Rafael Antognollie2a34552013-12-03 15:35:45 -02003144 shsurf->next_state.fullscreen = false;
3145 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003146
3147 if (shsurf->saved_size_valid) {
3148 width = shsurf->saved_width;
3149 height = shsurf->saved_height;
3150 shsurf->saved_size_valid = false;
3151 } else {
3152 width = shsurf->surface->width;
3153 height = shsurf->surface->height;
3154 }
3155
3156 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003157}
3158
3159static void
3160xdg_surface_set_maximized(struct wl_client *client,
3161 struct wl_resource *resource)
3162{
3163 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3164
3165 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3166 return;
3167
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003168 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003169 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003170}
3171
3172static void
3173xdg_surface_unset_maximized(struct wl_client *client,
3174 struct wl_resource *resource)
3175{
3176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003177 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003178
3179 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3180 return;
3181
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003182 if (!shsurf->next_state.maximized)
3183 return;
3184
Rafael Antognollie2a34552013-12-03 15:35:45 -02003185 shsurf->next_state.maximized = false;
3186 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003187
3188 if (shsurf->saved_size_valid) {
3189 width = shsurf->saved_width;
3190 height = shsurf->saved_height;
3191 shsurf->saved_size_valid = false;
3192 } else {
3193 width = shsurf->surface->width;
3194 height = shsurf->surface->height;
3195 }
3196
3197 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003198}
3199
3200static const struct xdg_surface_interface xdg_surface_implementation = {
3201 xdg_surface_destroy,
3202 xdg_surface_set_transient_for,
3203 xdg_surface_set_title,
3204 xdg_surface_set_app_id,
3205 xdg_surface_pong,
3206 xdg_surface_move,
3207 xdg_surface_resize,
3208 xdg_surface_set_output,
3209 xdg_surface_set_fullscreen,
3210 xdg_surface_unset_fullscreen,
3211 xdg_surface_set_maximized,
3212 xdg_surface_unset_maximized,
3213 NULL /* set_minimized */
3214};
3215
3216static void
3217xdg_send_configure(struct weston_surface *surface,
3218 uint32_t edges, int32_t width, int32_t height)
3219{
3220 struct shell_surface *shsurf = get_shell_surface(surface);
3221
3222 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3223}
3224
3225static const struct weston_shell_client xdg_client = {
3226 xdg_send_configure
3227};
3228
3229static void
3230xdg_use_unstable_version(struct wl_client *client,
3231 struct wl_resource *resource,
3232 int32_t version)
3233{
3234 if (version > 1) {
3235 wl_resource_post_error(resource,
3236 1,
3237 "xdg-shell:: version not implemented yet.");
3238 return;
3239 }
3240}
3241
3242static struct shell_surface *
3243create_xdg_surface(void *shell, struct weston_surface *surface,
3244 const struct weston_shell_client *client)
3245{
3246 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003247
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003248 shsurf = create_common_surface(shell, surface, client);
3249 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003250
3251 return shsurf;
3252}
3253
3254static void
3255xdg_get_xdg_surface(struct wl_client *client,
3256 struct wl_resource *resource,
3257 uint32_t id,
3258 struct wl_resource *surface_resource)
3259{
3260 struct weston_surface *surface =
3261 wl_resource_get_user_data(surface_resource);
3262 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3263 struct shell_surface *shsurf;
3264
3265 if (get_shell_surface(surface)) {
3266 wl_resource_post_error(surface_resource,
3267 WL_DISPLAY_ERROR_INVALID_OBJECT,
3268 "desktop_shell::get_shell_surface already requested");
3269 return;
3270 }
3271
3272 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3273 if (!shsurf) {
3274 wl_resource_post_error(surface_resource,
3275 WL_DISPLAY_ERROR_INVALID_OBJECT,
3276 "surface->configure already set");
3277 return;
3278 }
3279
3280 shsurf->resource =
3281 wl_resource_create(client,
3282 &xdg_surface_interface, 1, id);
3283 wl_resource_set_implementation(shsurf->resource,
3284 &xdg_surface_implementation,
3285 shsurf, shell_destroy_shell_surface);
3286}
3287
3288static bool
3289shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3290{
3291 return wl_resource_instance_of(shsurf->resource,
3292 &xdg_surface_interface,
3293 &xdg_surface_implementation);
3294}
3295
3296/* xdg-popup implementation */
3297
3298static void
3299xdg_popup_destroy(struct wl_client *client,
3300 struct wl_resource *resource)
3301{
3302 wl_resource_destroy(resource);
3303}
3304
3305static void
3306xdg_popup_pong(struct wl_client *client,
3307 struct wl_resource *resource,
3308 uint32_t serial)
3309{
3310 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3311
3312 surface_pong(shsurf, serial);
3313}
3314
3315static const struct xdg_popup_interface xdg_popup_implementation = {
3316 xdg_popup_destroy,
3317 xdg_popup_pong
3318};
3319
3320static void
3321xdg_popup_send_configure(struct weston_surface *surface,
3322 uint32_t edges, int32_t width, int32_t height)
3323{
3324}
3325
3326static const struct weston_shell_client xdg_popup_client = {
3327 xdg_popup_send_configure
3328};
3329
3330static struct shell_surface *
3331create_xdg_popup(void *shell, struct weston_surface *surface,
3332 const struct weston_shell_client *client,
3333 struct weston_surface *parent,
3334 struct shell_seat *seat,
3335 uint32_t serial,
3336 int32_t x, int32_t y)
3337{
3338 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003339
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003340 shsurf = create_common_surface(shell, surface, client);
3341 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003342 shsurf->popup.shseat = seat;
3343 shsurf->popup.serial = serial;
3344 shsurf->popup.x = x;
3345 shsurf->popup.y = y;
3346 shell_surface_set_parent(shsurf, parent);
3347
3348 return shsurf;
3349}
3350
3351static void
3352xdg_get_xdg_popup(struct wl_client *client,
3353 struct wl_resource *resource,
3354 uint32_t id,
3355 struct wl_resource *surface_resource,
3356 struct wl_resource *parent_resource,
3357 struct wl_resource *seat_resource,
3358 uint32_t serial,
3359 int32_t x, int32_t y, uint32_t flags)
3360{
3361 struct weston_surface *surface =
3362 wl_resource_get_user_data(surface_resource);
3363 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3364 struct shell_surface *shsurf;
3365 struct weston_surface *parent;
3366 struct shell_seat *seat;
3367
3368 if (get_shell_surface(surface)) {
3369 wl_resource_post_error(surface_resource,
3370 WL_DISPLAY_ERROR_INVALID_OBJECT,
3371 "desktop_shell::get_shell_surface already requested");
3372 return;
3373 }
3374
3375 if (!parent_resource) {
3376 wl_resource_post_error(surface_resource,
3377 WL_DISPLAY_ERROR_INVALID_OBJECT,
3378 "xdg_shell::get_xdg_popup requires a parent shell surface");
3379 }
3380
3381 parent = wl_resource_get_user_data(parent_resource);
3382 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3383
3384 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3385 parent, seat, serial, x, y);
3386 if (!shsurf) {
3387 wl_resource_post_error(surface_resource,
3388 WL_DISPLAY_ERROR_INVALID_OBJECT,
3389 "surface->configure already set");
3390 return;
3391 }
3392
3393 shsurf->resource =
3394 wl_resource_create(client,
3395 &xdg_popup_interface, 1, id);
3396 wl_resource_set_implementation(shsurf->resource,
3397 &xdg_popup_implementation,
3398 shsurf, shell_destroy_shell_surface);
3399}
3400
3401static bool
3402shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3403{
3404 return wl_resource_instance_of(shsurf->resource,
3405 &xdg_popup_interface,
3406 &xdg_popup_implementation);
3407}
3408
3409static const struct xdg_shell_interface xdg_implementation = {
3410 xdg_use_unstable_version,
3411 xdg_get_xdg_surface,
3412 xdg_get_xdg_popup
3413};
3414
3415static int
3416xdg_shell_unversioned_dispatch(const void *implementation,
3417 void *_target, uint32_t opcode,
3418 const struct wl_message *message,
3419 union wl_argument *args)
3420{
3421 struct wl_resource *resource = _target;
3422 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3423
3424 if (opcode != 0) {
3425 wl_resource_post_error(resource,
3426 WL_DISPLAY_ERROR_INVALID_OBJECT,
3427 "must call use_unstable_version first");
3428 return 0;
3429 }
3430
3431#define XDG_SERVER_VERSION 1
3432
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003433 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3434 "shell implementation doesn't match protocol version");
3435
Rafael Antognollie2a34552013-12-03 15:35:45 -02003436 if (args[0].i != XDG_SERVER_VERSION) {
3437 wl_resource_post_error(resource,
3438 WL_DISPLAY_ERROR_INVALID_OBJECT,
3439 "incompatible version, server is %d "
3440 "client wants %d",
3441 XDG_SERVER_VERSION, args[0].i);
3442 return 0;
3443 }
3444
3445 wl_resource_set_implementation(resource, &xdg_implementation,
3446 shell, NULL);
3447
3448 return 1;
3449}
3450
3451/* end of xdg-shell implementation */
3452/***********************************/
3453
Kristian Høgsberg07937562011-04-12 17:25:42 -04003454static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003455shell_fade(struct desktop_shell *shell, enum fade_type type);
3456
3457static int
3458screensaver_timeout(void *data)
3459{
3460 struct desktop_shell *shell = data;
3461
3462 shell_fade(shell, FADE_OUT);
3463
3464 return 1;
3465}
3466
3467static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003468handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003469{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003470 struct desktop_shell *shell =
3471 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003472
Pekka Paalanen18027e52011-12-02 16:31:49 +02003473 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003474
3475 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003476 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003477}
3478
3479static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003480launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003481{
3482 if (shell->screensaver.binding)
3483 return;
3484
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003485 if (!shell->screensaver.path) {
3486 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003487 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003488 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003489
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003490 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003491 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003492 return;
3493 }
3494
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003495 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003496 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003497 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003498 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003499}
3500
3501static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003502terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003503{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003504 if (shell->screensaver.process.pid == 0)
3505 return;
3506
3507 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003508}
3509
3510static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003511configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003512{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003513 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003514
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3516 if (v->output == ev->output && v != ev) {
3517 weston_view_unmap(v);
3518 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003519 }
3520 }
3521
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003522 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003523
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524 if (wl_list_empty(&ev->layer_link)) {
3525 wl_list_insert(&layer->view_list, &ev->layer_link);
3526 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003527 }
3528}
3529
3530static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003531background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003532{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003533 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003534 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003535
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 view = container_of(es->views.next, struct weston_view, surface_link);
3537
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003538 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003539}
3540
3541static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003542desktop_shell_set_background(struct wl_client *client,
3543 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003544 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003545 struct wl_resource *surface_resource)
3546{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003547 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003548 struct weston_surface *surface =
3549 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003551
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003552 if (surface->configure) {
3553 wl_resource_post_error(surface_resource,
3554 WL_DISPLAY_ERROR_INVALID_OBJECT,
3555 "surface role already assigned");
3556 return;
3557 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003558
Jason Ekstranda7af7042013-10-12 22:38:11 -05003559 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3560 weston_view_destroy(view);
3561 view = weston_view_create(surface);
3562
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003563 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003564 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003565 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003566 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003567 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003568 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003569 surface->output->width,
3570 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003571}
3572
3573static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003574panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003575{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003576 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 view = container_of(es->views.next, struct weston_view, surface_link);
3580
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003581 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003582}
3583
3584static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003585desktop_shell_set_panel(struct wl_client *client,
3586 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003587 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003588 struct wl_resource *surface_resource)
3589{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003590 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003591 struct weston_surface *surface =
3592 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003593 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003594
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003595 if (surface->configure) {
3596 wl_resource_post_error(surface_resource,
3597 WL_DISPLAY_ERROR_INVALID_OBJECT,
3598 "surface role already assigned");
3599 return;
3600 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003601
Jason Ekstranda7af7042013-10-12 22:38:11 -05003602 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3603 weston_view_destroy(view);
3604 view = weston_view_create(surface);
3605
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003606 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003607 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003608 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003609 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003610 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003611 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003612 surface->output->width,
3613 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003614}
3615
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003616static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003617lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003618{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003619 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 struct weston_view *view;
3621
3622 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003623
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003624 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003625 return;
3626
Jason Ekstranda7af7042013-10-12 22:38:11 -05003627 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003628
3629 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630 wl_list_insert(&shell->lock_layer.view_list,
3631 &view->layer_link);
3632 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003633 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003634 }
3635}
3636
3637static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003638handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003639{
Tiago Vignattibe143262012-04-16 17:31:41 +03003640 struct desktop_shell *shell =
3641 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003642
Martin Minarik6d118362012-06-07 18:01:59 +02003643 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003644 shell->lock_surface = NULL;
3645}
3646
3647static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003648desktop_shell_set_lock_surface(struct wl_client *client,
3649 struct wl_resource *resource,
3650 struct wl_resource *surface_resource)
3651{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003652 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003653 struct weston_surface *surface =
3654 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003655
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003656 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003657
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003658 if (!shell->locked)
3659 return;
3660
Pekka Paalanen98262232011-12-01 10:42:22 +02003661 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003662
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003663 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003664 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003665 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003666
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003667 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003668 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003669 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003670}
3671
3672static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003673resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003674{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003675 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003676
Pekka Paalanen77346a62011-11-30 16:26:35 +02003677 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003678
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003679 wl_list_remove(&shell->lock_layer.link);
3680 wl_list_insert(&shell->compositor->cursor_layer.link,
3681 &shell->fullscreen_layer.link);
3682 wl_list_insert(&shell->fullscreen_layer.link,
3683 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003684 if (shell->showing_input_panels) {
3685 wl_list_insert(&shell->panel_layer.link,
3686 &shell->input_panel_layer.link);
3687 wl_list_insert(&shell->input_panel_layer.link,
3688 &ws->layer.link);
3689 } else {
3690 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3691 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003692
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003693 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003694
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003695 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003696 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003697 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003698}
3699
3700static void
3701desktop_shell_unlock(struct wl_client *client,
3702 struct wl_resource *resource)
3703{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003704 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003705
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003706 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003707
3708 if (shell->locked)
3709 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003710}
3711
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003712static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003713desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003714 struct wl_resource *resource,
3715 struct wl_resource *surface_resource)
3716{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003717 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003718
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003719 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003720 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003721}
3722
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003723static void
3724desktop_shell_desktop_ready(struct wl_client *client,
3725 struct wl_resource *resource)
3726{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003727 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003728
3729 shell_fade_startup(shell);
3730}
3731
Kristian Høgsberg75840622011-09-06 13:48:16 -04003732static const struct desktop_shell_interface desktop_shell_implementation = {
3733 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003734 desktop_shell_set_panel,
3735 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003736 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003737 desktop_shell_set_grab_surface,
3738 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003739};
3740
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003741static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003742get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003743{
3744 struct shell_surface *shsurf;
3745
3746 shsurf = get_shell_surface(surface);
3747 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003748 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003749 return shsurf->type;
3750}
3751
Kristian Høgsberg75840622011-09-06 13:48:16 -04003752static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003753move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003754{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003755 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003756 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003757 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003758
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003759 if (seat->pointer->focus == NULL)
3760 return;
3761
3762 focus = seat->pointer->focus->surface;
3763
Pekka Paalanen01388e22013-04-25 13:57:44 +03003764 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003765 if (surface == NULL)
3766 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003767
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003768 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003769 if (shsurf == NULL || shsurf->state.fullscreen ||
3770 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003771 return;
3772
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003773 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003774}
3775
3776static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003777maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3778{
3779 struct weston_surface *focus = seat->pointer->focus->surface;
3780 struct weston_surface *surface;
3781 struct shell_surface *shsurf;
3782
3783 surface = weston_surface_get_main_surface(focus);
3784 if (surface == NULL)
3785 return;
3786
3787 shsurf = get_shell_surface(surface);
3788 if (shsurf == NULL)
3789 return;
3790
3791 if (!shell_surface_is_xdg_surface(shsurf))
3792 return;
3793
3794 if (shsurf->state.maximized)
3795 xdg_surface_send_request_unset_maximized(shsurf->resource);
3796 else
3797 xdg_surface_send_request_set_maximized(shsurf->resource);
3798}
3799
3800static void
3801fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3802{
3803 struct weston_surface *focus = seat->pointer->focus->surface;
3804 struct weston_surface *surface;
3805 struct shell_surface *shsurf;
3806
3807 surface = weston_surface_get_main_surface(focus);
3808 if (surface == NULL)
3809 return;
3810
3811 shsurf = get_shell_surface(surface);
3812 if (shsurf == NULL)
3813 return;
3814
3815 if (!shell_surface_is_xdg_surface(shsurf))
3816 return;
3817
3818 if (shsurf->state.fullscreen)
3819 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3820 else
3821 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3822}
3823
3824static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003825touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3826{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003827 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003828 struct weston_surface *surface;
3829 struct shell_surface *shsurf;
3830
3831 surface = weston_surface_get_main_surface(focus);
3832 if (surface == NULL)
3833 return;
3834
3835 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003836 if (shsurf == NULL || shsurf->state.fullscreen ||
3837 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003838 return;
3839
3840 surface_touch_move(shsurf, (struct weston_seat *) seat);
3841}
3842
3843static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003844resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003845{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003846 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003847 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003848 uint32_t edges = 0;
3849 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003850 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003851
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003852 if (seat->pointer->focus == NULL)
3853 return;
3854
3855 focus = seat->pointer->focus->surface;
3856
Pekka Paalanen01388e22013-04-25 13:57:44 +03003857 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003858 if (surface == NULL)
3859 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003860
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003861 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003862 if (shsurf == NULL || shsurf->state.fullscreen ||
3863 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003864 return;
3865
Jason Ekstranda7af7042013-10-12 22:38:11 -05003866 weston_view_from_global(shsurf->view,
3867 wl_fixed_to_int(seat->pointer->grab_x),
3868 wl_fixed_to_int(seat->pointer->grab_y),
3869 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003870
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003871 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003872 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003873 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003874 edges |= 0;
3875 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003876 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003877
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003878 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003879 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003880 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003881 edges |= 0;
3882 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003883 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003884
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003885 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003886}
3887
3888static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003889surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003890 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003891{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003892 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003893 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003894 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003895 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003896
Pekka Paalanen01388e22013-04-25 13:57:44 +03003897 /* XXX: broken for windows containing sub-surfaces */
3898 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003899 if (surface == NULL)
3900 return;
3901
3902 shsurf = get_shell_surface(surface);
3903 if (!shsurf)
3904 return;
3905
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003907
Jason Ekstranda7af7042013-10-12 22:38:11 -05003908 if (shsurf->view->alpha > 1.0)
3909 shsurf->view->alpha = 1.0;
3910 if (shsurf->view->alpha < step)
3911 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003912
Jason Ekstranda7af7042013-10-12 22:38:11 -05003913 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003914 weston_surface_damage(surface);
3915}
3916
3917static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003918do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003919 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003920{
Daniel Stone37816df2012-05-16 18:45:18 +01003921 struct weston_seat *ws = (struct weston_seat *) seat;
3922 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003923 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003924 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003925
3926 wl_list_for_each(output, &compositor->output_list, link) {
3927 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003928 wl_fixed_to_double(seat->pointer->x),
3929 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003930 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003931 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003932 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003933 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003934 increment = -output->zoom.increment;
3935 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003936 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003937 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003938 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003939 else
3940 increment = 0;
3941
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003942 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003943
Scott Moreaue6603982012-06-11 13:07:51 -06003944 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003945 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003946 else if (output->zoom.level > output->zoom.max_level)
3947 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003948 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003949 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003950 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951
Scott Moreaue6603982012-06-11 13:07:51 -06003952 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003955 }
3956 }
3957}
3958
Scott Moreauccbf29d2012-02-22 14:21:41 -07003959static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003960zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003961 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003962{
3963 do_zoom(seat, time, 0, axis, value);
3964}
3965
3966static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003967zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003968 void *data)
3969{
3970 do_zoom(seat, time, key, 0, 0);
3971}
3972
3973static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003974terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003975 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003976{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003977 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003978
Daniel Stone325fc2d2012-05-30 16:31:58 +01003979 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003980}
3981
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003982static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003983rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3984 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003985{
3986 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003987 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003988 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003989 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003990 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003991
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003992 weston_pointer_move(pointer, x, y);
3993
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003994 if (!shsurf)
3995 return;
3996
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003997 cx = 0.5f * shsurf->surface->width;
3998 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003999
Daniel Stone37816df2012-05-16 18:45:18 +01004000 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4001 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004002 r = sqrtf(dx * dx + dy * dy);
4003
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004004 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004005 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004006
4007 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004008 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004009 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004010
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004011 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004012 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004013
4014 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004015 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004016 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004017 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004018 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004019
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004020 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004022 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004023 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004024 wl_list_init(&shsurf->rotation.transform.link);
4025 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004026 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004027 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004028
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004029 /* We need to adjust the position of the surface
4030 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004031 cposx = shsurf->view->geometry.x + cx;
4032 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004033 dposx = rotate->center.x - cposx;
4034 dposy = rotate->center.y - cposy;
4035 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004036 weston_view_set_position(shsurf->view,
4037 shsurf->view->geometry.x + dposx,
4038 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004039 }
4040
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004041 /* Repaint implies weston_surface_update_transform(), which
4042 * lazily applies the damage due to rotation update.
4043 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004044 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004045}
4046
4047static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004048rotate_grab_button(struct weston_pointer_grab *grab,
4049 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004050{
4051 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004052 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004053 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004054 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004055 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004056
Daniel Stone4dbadb12012-05-30 16:31:51 +01004057 if (pointer->button_count == 0 &&
4058 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004059 if (shsurf)
4060 weston_matrix_multiply(&shsurf->rotation.rotation,
4061 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004062 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004063 free(rotate);
4064 }
4065}
4066
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004067static void
4068rotate_grab_cancel(struct weston_pointer_grab *grab)
4069{
4070 struct rotate_grab *rotate =
4071 container_of(grab, struct rotate_grab, base.grab);
4072
4073 shell_grab_end(&rotate->base);
4074 free(rotate);
4075}
4076
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004077static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004078 noop_grab_focus,
4079 rotate_grab_motion,
4080 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004081 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004082};
4083
4084static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004085surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004086{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004087 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004088 float dx, dy;
4089 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004090
Pekka Paalanen460099f2012-01-20 16:48:25 +02004091 rotate = malloc(sizeof *rotate);
4092 if (!rotate)
4093 return;
4094
Jason Ekstranda7af7042013-10-12 22:38:11 -05004095 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004096 surface->surface->width * 0.5f,
4097 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004098 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004099
Daniel Stone37816df2012-05-16 18:45:18 +01004100 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4101 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004102 r = sqrtf(dx * dx + dy * dy);
4103 if (r > 20.0f) {
4104 struct weston_matrix inverse;
4105
4106 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004107 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004108 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004109
4110 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004111 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004112 } else {
4113 weston_matrix_init(&surface->rotation.rotation);
4114 weston_matrix_init(&rotate->rotation);
4115 }
4116
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004117 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4118 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004119}
4120
4121static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004122rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004123 void *data)
4124{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004125 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004126 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004127 struct shell_surface *surface;
4128
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004129 if (seat->pointer->focus == NULL)
4130 return;
4131
4132 focus = seat->pointer->focus->surface;
4133
Pekka Paalanen01388e22013-04-25 13:57:44 +03004134 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004135 if (base_surface == NULL)
4136 return;
4137
4138 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004139 if (surface == NULL || surface->state.fullscreen ||
4140 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004141 return;
4142
4143 surface_rotate(surface, seat);
4144}
4145
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004146/* Move all fullscreen layers down to the current workspace in a non-reversible
4147 * manner. This should be used when implementing shell-wide overlays, such as
4148 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004149void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004150lower_fullscreen_layer(struct desktop_shell *shell)
4151{
4152 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004154
4155 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004156 wl_list_for_each_reverse_safe(view, prev,
4157 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004158 layer_link) {
4159 wl_list_remove(&view->layer_link);
4160 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4161 weston_view_damage_below(view);
4162 weston_surface_damage(view->surface);
4163 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004164}
4165
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004166void
Tiago Vignattibe143262012-04-16 17:31:41 +03004167activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004168 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004169{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004170 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004171 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004172 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004173 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004174 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004175
Pekka Paalanen01388e22013-04-25 13:57:44 +03004176 main_surface = weston_surface_get_main_surface(es);
4177
Daniel Stone37816df2012-05-16 18:45:18 +01004178 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004179
Jonas Ådahl8538b222012-08-29 22:13:03 +02004180 state = ensure_focus_state(shell, seat);
4181 if (state == NULL)
4182 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004183
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004184 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004185 state->keyboard_focus = es;
4186 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004187 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004188
Rafael Antognolli03b16592013-12-03 15:35:42 -02004189 shsurf = get_shell_surface(main_surface);
4190 if (shsurf->state.fullscreen)
4191 shell_configure_fullscreen(shsurf);
4192 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004193 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004194
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004195 if (shell->focus_animation_type != ANIMATION_NONE) {
4196 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004197 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004198 }
4199
4200 /* Update the surface’s layer. This brings it to the top of the stacking
4201 * order as appropriate. */
4202 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004203}
4204
Alex Wu21858432012-04-01 20:13:08 +08004205/* no-op func for checking black surface */
4206static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004207black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004208{
4209}
4210
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004211static bool
Alex Wu21858432012-04-01 20:13:08 +08004212is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4213{
4214 if (es->configure == black_surface_configure) {
4215 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004216 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004217 return true;
4218 }
4219 return false;
4220}
4221
Kristian Høgsberg75840622011-09-06 13:48:16 -04004222static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004223activate_binding(struct weston_seat *seat,
4224 struct desktop_shell *shell,
4225 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004226{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004227 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004228
Alex Wu9c35e6b2012-03-05 14:13:13 +08004229 if (!focus)
4230 return;
4231
Pekka Paalanen01388e22013-04-25 13:57:44 +03004232 if (is_black_surface(focus, &main_surface))
4233 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004234
Pekka Paalanen01388e22013-04-25 13:57:44 +03004235 main_surface = weston_surface_get_main_surface(focus);
4236 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004237 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004238
Neil Robertsa28c6932013-10-03 16:43:04 +01004239 activate(shell, focus, seat);
4240}
4241
4242static void
4243click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4244 void *data)
4245{
4246 if (seat->pointer->grab != &seat->pointer->default_grab)
4247 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004248 if (seat->pointer->focus == NULL)
4249 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004250
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004251 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004252}
4253
4254static void
4255touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4256{
4257 if (seat->touch->grab != &seat->touch->default_grab)
4258 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004259 if (seat->touch->focus == NULL)
4260 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004261
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004262 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004263}
4264
4265static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004266lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004267{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004268 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004269
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004270 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004271 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004272 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004273 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004274
4275 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004276
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004277 /* Hide all surfaces by removing the fullscreen, panel and
4278 * toplevel layers. This way nothing else can show or receive
4279 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004280
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004281 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004282 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004283 if (shell->showing_input_panels)
4284 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004285 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004286 wl_list_insert(&shell->compositor->cursor_layer.link,
4287 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004288
Pekka Paalanen77346a62011-11-30 16:26:35 +02004289 launch_screensaver(shell);
4290
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004291 /* TODO: disable bindings that should not work while locked. */
4292
4293 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004294}
4295
4296static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004297unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004298{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004299 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004300 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004301 return;
4302 }
4303
4304 /* If desktop-shell client has gone away, unlock immediately. */
4305 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004306 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004307 return;
4308 }
4309
4310 if (shell->prepare_event_sent)
4311 return;
4312
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004313 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004314 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004315}
4316
4317static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004319{
4320 struct desktop_shell *shell = data;
4321
4322 shell->fade.animation = NULL;
4323
4324 switch (shell->fade.type) {
4325 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 weston_surface_destroy(shell->fade.view->surface);
4327 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004328 break;
4329 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004330 lock(shell);
4331 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004332 default:
4333 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004334 }
4335}
4336
Jason Ekstranda7af7042013-10-12 22:38:11 -05004337static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004338shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004339{
4340 struct weston_compositor *compositor = shell->compositor;
4341 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004343
4344 surface = weston_surface_create(compositor);
4345 if (!surface)
4346 return NULL;
4347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 view = weston_view_create(surface);
4349 if (!view) {
4350 weston_surface_destroy(surface);
4351 return NULL;
4352 }
4353
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004354 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004355 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004356 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 wl_list_insert(&compositor->fade_layer.view_list,
4358 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004359 pixman_region32_init(&surface->input);
4360
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004362}
4363
4364static void
4365shell_fade(struct desktop_shell *shell, enum fade_type type)
4366{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004367 float tint;
4368
4369 switch (type) {
4370 case FADE_IN:
4371 tint = 0.0;
4372 break;
4373 case FADE_OUT:
4374 tint = 1.0;
4375 break;
4376 default:
4377 weston_log("shell: invalid fade type\n");
4378 return;
4379 }
4380
4381 shell->fade.type = type;
4382
Jason Ekstranda7af7042013-10-12 22:38:11 -05004383 if (shell->fade.view == NULL) {
4384 shell->fade.view = shell_fade_create_surface(shell);
4385 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004386 return;
4387
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 shell->fade.view->alpha = 1.0 - tint;
4389 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004390 }
4391
4392 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004393 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004394 else
4395 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004397 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004398 shell_fade_done, shell);
4399}
4400
4401static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004402do_shell_fade_startup(void *data)
4403{
4404 struct desktop_shell *shell = data;
4405
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004406 if (shell->startup_animation_type == ANIMATION_FADE)
4407 shell_fade(shell, FADE_IN);
4408 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 weston_surface_destroy(shell->fade.view->surface);
4410 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004411 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004412}
4413
4414static void
4415shell_fade_startup(struct desktop_shell *shell)
4416{
4417 struct wl_event_loop *loop;
4418
4419 if (!shell->fade.startup_timer)
4420 return;
4421
4422 wl_event_source_remove(shell->fade.startup_timer);
4423 shell->fade.startup_timer = NULL;
4424
4425 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4426 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4427}
4428
4429static int
4430fade_startup_timeout(void *data)
4431{
4432 struct desktop_shell *shell = data;
4433
4434 shell_fade_startup(shell);
4435 return 0;
4436}
4437
4438static void
4439shell_fade_init(struct desktop_shell *shell)
4440{
4441 /* Make compositor output all black, and wait for the desktop-shell
4442 * client to signal it is ready, then fade in. The timer triggers a
4443 * fade-in, in case the desktop-shell client takes too long.
4444 */
4445
4446 struct wl_event_loop *loop;
4447
Jason Ekstranda7af7042013-10-12 22:38:11 -05004448 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004449 weston_log("%s: warning: fade surface already exists\n",
4450 __func__);
4451 return;
4452 }
4453
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 shell->fade.view = shell_fade_create_surface(shell);
4455 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004456 return;
4457
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458 weston_view_update_transform(shell->fade.view);
4459 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004460
4461 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4462 shell->fade.startup_timer =
4463 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4464 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4465}
4466
4467static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004468idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004469{
4470 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004471 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004472
4473 shell_fade(shell, FADE_OUT);
4474 /* lock() is called from shell_fade_done() */
4475}
4476
4477static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004478wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004479{
4480 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004481 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004482
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004483 unlock(shell);
4484}
4485
4486static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004487center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004488{
Giulio Camuffob8366642013-04-25 13:57:46 +03004489 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004490 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004491
Jason Ekstranda7af7042013-10-12 22:38:11 -05004492 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004493
4494 x = output->x + (output->width - width) / 2 - surf_x / 2;
4495 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004496
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004497 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004498}
4499
4500static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004501weston_view_set_initial_position(struct weston_view *view,
4502 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004503{
4504 struct weston_compositor *compositor = shell->compositor;
4505 int ix = 0, iy = 0;
4506 int range_x, range_y;
4507 int dx, dy, x, y, panel_height;
4508 struct weston_output *output, *target_output = NULL;
4509 struct weston_seat *seat;
4510
4511 /* As a heuristic place the new window on the same output as the
4512 * pointer. Falling back to the output containing 0, 0.
4513 *
4514 * TODO: Do something clever for touch too?
4515 */
4516 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004517 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004518 ix = wl_fixed_to_int(seat->pointer->x);
4519 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004520 break;
4521 }
4522 }
4523
4524 wl_list_for_each(output, &compositor->output_list, link) {
4525 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4526 target_output = output;
4527 break;
4528 }
4529 }
4530
4531 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 weston_view_set_position(view, 10 + random() % 400,
4533 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004534 return;
4535 }
4536
4537 /* Valid range within output where the surface will still be onscreen.
4538 * If this is negative it means that the surface is bigger than
4539 * output.
4540 */
4541 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004542 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004543 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004544 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004545
Rob Bradford4cb88c72012-08-13 15:18:44 +01004546 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004547 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004548 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004549 dx = 0;
4550
4551 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004552 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004553 else
4554 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004555
4556 x = target_output->x + dx;
4557 y = target_output->y + dy;
4558
Jason Ekstranda7af7042013-10-12 22:38:11 -05004559 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004560}
4561
4562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004563map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004564 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004565{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004566 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004567 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004568 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004569 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004570
Pekka Paalanen77346a62011-11-30 16:26:35 +02004571 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004573 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004574 if (shsurf->state.fullscreen) {
4575 center_on_output(shsurf->view, shsurf->fullscreen_output);
4576 shell_map_fullscreen(shsurf);
4577 } else if (shsurf->state.maximized) {
4578 /* use surface configure to set the geometry */
4579 panel_height = get_output_panel_height(shell, shsurf->output);
4580 surface_subsurfaces_boundingbox(shsurf->surface,
4581 &surf_x, &surf_y, NULL, NULL);
4582 weston_view_set_position(shsurf->view,
4583 shsurf->output->x - surf_x,
4584 shsurf->output->y +
4585 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004586 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004587 weston_view_set_initial_position(shsurf->view, shell);
4588 }
Juan Zhao96879df2012-02-07 08:45:41 +08004589 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004590 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004591 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004592 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004593 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004594 weston_view_set_position(shsurf->view,
4595 shsurf->view->geometry.x + sx,
4596 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004597 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004598 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004599 default:
4600 ;
4601 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004602
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004603 /* Surface stacking order, see also activate(). */
4604 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004605
Jason Ekstranda7af7042013-10-12 22:38:11 -05004606 if (shsurf->type != SHELL_SURFACE_NONE) {
4607 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004608 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004609 shsurf->surface->output = shsurf->output;
4610 shsurf->view->output = shsurf->output;
4611 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004612 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004613
Rafael Antognollied207b42013-12-03 15:35:43 -02004614 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4615 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4616 }
4617
Jason Ekstranda7af7042013-10-12 22:38:11 -05004618 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004619 /* XXX: xwayland's using the same fields for transient type */
4620 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004621 if (shsurf->transient.flags ==
4622 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4623 break;
4624 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004625 if (shsurf->state.relative &&
4626 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4627 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004628 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004629 break;
4630 wl_list_for_each(seat, &compositor->seat_list, link)
4631 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004632 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004633 case SHELL_SURFACE_POPUP:
4634 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004635 default:
4636 break;
4637 }
4638
Rafael Antognolli03b16592013-12-03 15:35:42 -02004639 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4640 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004641 {
4642 switch (shell->win_animation_type) {
4643 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004644 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004645 break;
4646 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004647 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004648 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004649 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004650 default:
4651 break;
4652 }
4653 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004654}
4655
4656static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004657configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004658 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004659{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004660 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004661 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004662 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004663
Pekka Paalanen77346a62011-11-30 16:26:35 +02004664 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004665
Rafael Antognolli03b16592013-12-03 15:35:42 -02004666 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004667 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004668 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004669 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004670 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4671 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004672 mx = shsurf->output->x - surf_x;
4673 my = shsurf->output->y +
4674 get_output_panel_height(shell,shsurf->output) - surf_y;
4675 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004676 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004677 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004678 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004679
Alex Wu4539b082012-03-01 12:57:46 +08004680 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004681 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004682 wl_list_for_each(view, &surface->views, surface_link)
4683 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004684
Rafael Antognolli03b16592013-12-03 15:35:42 -02004685 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004686 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004687 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004688}
4689
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004690static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004691shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004692{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004693 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004694 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004695
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004696 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004697
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004698 if (!weston_surface_is_mapped(es) &&
4699 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004700 remove_popup_grab(shsurf);
4701 }
4702
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004703 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004704 return;
4705
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004706 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004707 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004708 type_changed = 1;
4709 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004710
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004711 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004712 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004713 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004714 shsurf->last_width != es->width ||
4715 shsurf->last_height != es->height) {
4716 shsurf->last_width = es->width;
4717 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004718 float from_x, from_y;
4719 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4722 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004723 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004725 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004726 }
4727}
4728
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004729static void
4730shell_surface_output_destroyed(struct weston_surface *es)
4731{
4732 struct shell_surface *shsurf = get_shell_surface(es);
4733
4734 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004735 shsurf->next_state.maximized = false;
4736 shsurf->next_state.fullscreen = false;
4737 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004738}
4739
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004740static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004741
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004742static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004743desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004744{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004745 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004746 struct desktop_shell *shell =
4747 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004748
4749 shell->child.process.pid = 0;
4750 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004751
4752 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4753 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004754 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004755 shell->child.deathstamp = time;
4756 shell->child.deathcount = 0;
4757 }
4758
4759 shell->child.deathcount++;
4760 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004761 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004762 return;
4763 }
4764
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004765 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004766 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004767 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004768}
4769
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004770static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004771desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4772{
4773 struct desktop_shell *shell;
4774
4775 shell = container_of(listener, struct desktop_shell,
4776 child.client_destroy_listener);
4777
4778 shell->child.client = NULL;
4779}
4780
4781static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004782launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004783{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004784 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004785
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004786 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004787 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004788 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004789 desktop_shell_sigchld);
4790
4791 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004792 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004793
4794 shell->child.client_destroy_listener.notify =
4795 desktop_shell_client_destroy;
4796 wl_client_add_destroy_listener(shell->child.client,
4797 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004798}
4799
4800static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004801bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4802{
Tiago Vignattibe143262012-04-16 17:31:41 +03004803 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004804 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004805
Jason Ekstranda85118c2013-06-27 20:17:02 -05004806 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4807 if (resource)
4808 wl_resource_set_implementation(resource, &shell_implementation,
4809 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004810}
4811
Kristian Høgsberg75840622011-09-06 13:48:16 -04004812static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004813bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4814{
4815 struct desktop_shell *shell = data;
4816 struct wl_resource *resource;
4817
4818 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4819 if (resource)
4820 wl_resource_set_dispatcher(resource,
4821 xdg_shell_unversioned_dispatch,
4822 NULL, shell, NULL);
4823}
4824
4825static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004826unbind_desktop_shell(struct wl_resource *resource)
4827{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004828 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004829
4830 if (shell->locked)
4831 resume_desktop(shell);
4832
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004833 shell->child.desktop_shell = NULL;
4834 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004835}
4836
4837static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004838bind_desktop_shell(struct wl_client *client,
4839 void *data, uint32_t version, uint32_t id)
4840{
Tiago Vignattibe143262012-04-16 17:31:41 +03004841 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004842 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004843
Jason Ekstranda85118c2013-06-27 20:17:02 -05004844 resource = wl_resource_create(client, &desktop_shell_interface,
4845 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004846
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004847 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004848 wl_resource_set_implementation(resource,
4849 &desktop_shell_implementation,
4850 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004851 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004852
4853 if (version < 2)
4854 shell_fade_startup(shell);
4855
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004856 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004857 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004858
4859 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4860 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004861 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004862}
4863
Pekka Paalanen6e168112011-11-24 11:34:05 +02004864static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004865screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004866{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004867 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004869
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004870 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004871 return;
4872
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004873 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004874 if (!shell->locked)
4875 return;
4876
Jason Ekstranda7af7042013-10-12 22:38:11 -05004877 view = container_of(surface->views.next, struct weston_view, surface_link);
4878 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004879
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 if (wl_list_empty(&view->layer_link)) {
4881 wl_list_insert(shell->lock_layer.view_list.prev,
4882 &view->layer_link);
4883 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004884 wl_event_source_timer_update(shell->screensaver.timer,
4885 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004886 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004887 }
4888}
4889
4890static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004891screensaver_set_surface(struct wl_client *client,
4892 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004893 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004894 struct wl_resource *output_resource)
4895{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004896 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004897 struct weston_surface *surface =
4898 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004899 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 struct weston_view *view, *next;
4901
4902 /* Make sure we only have one view */
4903 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4904 weston_view_destroy(view);
4905 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004906
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004907 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004908 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004909 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004910}
4911
4912static const struct screensaver_interface screensaver_implementation = {
4913 screensaver_set_surface
4914};
4915
4916static void
4917unbind_screensaver(struct wl_resource *resource)
4918{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004919 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004920
Pekka Paalanen77346a62011-11-30 16:26:35 +02004921 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004922}
4923
4924static void
4925bind_screensaver(struct wl_client *client,
4926 void *data, uint32_t version, uint32_t id)
4927{
Tiago Vignattibe143262012-04-16 17:31:41 +03004928 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004929 struct wl_resource *resource;
4930
Jason Ekstranda85118c2013-06-27 20:17:02 -05004931 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004932
Pekka Paalanen77346a62011-11-30 16:26:35 +02004933 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004934 wl_resource_set_implementation(resource,
4935 &screensaver_implementation,
4936 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004937 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004938 return;
4939 }
4940
4941 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4942 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004943 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004944}
4945
Kristian Høgsberg07045392012-02-19 18:52:44 -05004946struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004947 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004948 struct weston_surface *current;
4949 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004950 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004951};
4952
4953static void
4954switcher_next(struct switcher *switcher)
4955{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004957 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004958 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004959 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004960
Jason Ekstranda7af7042013-10-12 22:38:11 -05004961 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004962 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02004963 if (shsurf &&
4964 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4965 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004966 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004967 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004968 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004969 next = view->surface;
4970 prev = view->surface;
4971 view->alpha = 0.25;
4972 weston_view_geometry_dirty(view);
4973 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004974 }
Alex Wu1659daa2012-04-01 20:13:09 +08004975
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 if (is_black_surface(view->surface, NULL)) {
4977 view->alpha = 0.25;
4978 weston_view_geometry_dirty(view);
4979 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004980 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004981 }
4982
4983 if (next == NULL)
4984 next = first;
4985
Alex Wu07b26062012-03-12 16:06:01 +08004986 if (next == NULL)
4987 return;
4988
Kristian Høgsberg07045392012-02-19 18:52:44 -05004989 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004990 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004991
4992 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004993 wl_list_for_each(view, &next->views, surface_link)
4994 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004995
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004996 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004997 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004998 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004999}
5000
5001static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005002switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005003{
5004 struct switcher *switcher =
5005 container_of(listener, struct switcher, listener);
5006
5007 switcher_next(switcher);
5008}
5009
5010static void
Daniel Stone351eb612012-05-31 15:27:47 -04005011switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005012{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005014 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005015 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005016
Jason Ekstranda7af7042013-10-12 22:38:11 -05005017 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005018 if (is_focus_view(view))
5019 continue;
5020
Jason Ekstranda7af7042013-10-12 22:38:11 -05005021 view->alpha = 1.0;
5022 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005023 }
5024
Alex Wu07b26062012-03-12 16:06:01 +08005025 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005026 activate(switcher->shell, switcher->current,
5027 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005028 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005029 weston_keyboard_end_grab(keyboard);
5030 if (keyboard->input_method_resource)
5031 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005032 free(switcher);
5033}
5034
5035static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005036switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005037 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005038{
5039 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005040 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005041
Daniel Stonec9785ea2012-05-30 16:31:52 +01005042 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005043 switcher_next(switcher);
5044}
5045
5046static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005047switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005048 uint32_t mods_depressed, uint32_t mods_latched,
5049 uint32_t mods_locked, uint32_t group)
5050{
5051 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005052 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005053
Daniel Stone351eb612012-05-31 15:27:47 -04005054 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5055 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005056}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005057
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005058static void
5059switcher_cancel(struct weston_keyboard_grab *grab)
5060{
5061 struct switcher *switcher = container_of(grab, struct switcher, grab);
5062
5063 switcher_destroy(switcher);
5064}
5065
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005066static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005067 switcher_key,
5068 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005069 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005070};
5071
5072static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005073switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005074 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005075{
Tiago Vignattibe143262012-04-16 17:31:41 +03005076 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005077 struct switcher *switcher;
5078
5079 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005080 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005082 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005083 wl_list_init(&switcher->listener.link);
5084
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005085 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005086 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005087 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005088 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5089 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005090 switcher_next(switcher);
5091}
5092
Pekka Paalanen3c647232011-12-22 13:43:43 +02005093static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005094backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005095 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005096{
5097 struct weston_compositor *compositor = data;
5098 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005099 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005100
5101 /* TODO: we're limiting to simple use cases, where we assume just
5102 * control on the primary display. We'd have to extend later if we
5103 * ever get support for setting backlights on random desktop LCD
5104 * panels though */
5105 output = get_default_output(compositor);
5106 if (!output)
5107 return;
5108
5109 if (!output->set_backlight)
5110 return;
5111
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005112 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5113 backlight_new = output->backlight_current - 25;
5114 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5115 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005116
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005117 if (backlight_new < 5)
5118 backlight_new = 5;
5119 if (backlight_new > 255)
5120 backlight_new = 255;
5121
5122 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005123 output->set_backlight(output, output->backlight_current);
5124}
5125
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005126struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005127 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005128 struct weston_seat *seat;
5129 uint32_t key[2];
5130 int key_released[2];
5131};
5132
5133static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005134debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005135 uint32_t key, uint32_t state)
5136{
5137 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005138 struct weston_compositor *ec = db->seat->compositor;
5139 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005140 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005141 uint32_t serial;
5142 int send = 0, terminate = 0;
5143 int check_binding = 1;
5144 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005145 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005146
5147 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5148 /* Do not run bindings on key releases */
5149 check_binding = 0;
5150
5151 for (i = 0; i < 2; i++)
5152 if (key == db->key[i])
5153 db->key_released[i] = 1;
5154
5155 if (db->key_released[0] && db->key_released[1]) {
5156 /* All key releases been swalled so end the grab */
5157 terminate = 1;
5158 } else if (key != db->key[0] && key != db->key[1]) {
5159 /* Should not swallow release of other keys */
5160 send = 1;
5161 }
5162 } else if (key == db->key[0] && !db->key_released[0]) {
5163 /* Do not check bindings for the first press of the binding
5164 * key. This allows it to be used as a debug shortcut.
5165 * We still need to swallow this event. */
5166 check_binding = 0;
5167 } else if (db->key[1]) {
5168 /* If we already ran a binding don't process another one since
5169 * we can't keep track of all the binding keys that were
5170 * pressed in order to swallow the release events. */
5171 send = 1;
5172 check_binding = 0;
5173 }
5174
5175 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005176 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5177 key, state)) {
5178 /* We ran a binding so swallow the press and keep the
5179 * grab to swallow the released too. */
5180 send = 0;
5181 terminate = 0;
5182 db->key[1] = key;
5183 } else {
5184 /* Terminate the grab since the key pressed is not a
5185 * debug binding key. */
5186 send = 1;
5187 terminate = 1;
5188 }
5189 }
5190
5191 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005192 serial = wl_display_next_serial(display);
5193 resource_list = &grab->keyboard->focus_resource_list;
5194 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005195 wl_keyboard_send_key(resource, serial, time, key, state);
5196 }
5197 }
5198
5199 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005200 weston_keyboard_end_grab(grab->keyboard);
5201 if (grab->keyboard->input_method_resource)
5202 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005203 free(db);
5204 }
5205}
5206
5207static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005208debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005209 uint32_t mods_depressed, uint32_t mods_latched,
5210 uint32_t mods_locked, uint32_t group)
5211{
5212 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005213 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005214
Neil Roberts96d790e2013-09-19 17:32:00 +01005215 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005216
Neil Roberts96d790e2013-09-19 17:32:00 +01005217 wl_resource_for_each(resource, resource_list) {
5218 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5219 mods_latched, mods_locked, group);
5220 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005221}
5222
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005223static void
5224debug_binding_cancel(struct weston_keyboard_grab *grab)
5225{
5226 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5227
5228 weston_keyboard_end_grab(grab->keyboard);
5229 free(db);
5230}
5231
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005232struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005233 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005234 debug_binding_modifiers,
5235 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005236};
5237
5238static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005239debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005240{
5241 struct debug_binding_grab *grab;
5242
5243 grab = calloc(1, sizeof *grab);
5244 if (!grab)
5245 return;
5246
5247 grab->seat = (struct weston_seat *) seat;
5248 grab->key[0] = key;
5249 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005250 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005251}
5252
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005253static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005254force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005255 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005256{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005257 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005258 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005259 struct desktop_shell *shell = data;
5260 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005261 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005262
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005263 focus_surface = seat->keyboard->focus;
5264 if (!focus_surface)
5265 return;
5266
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005267 wl_signal_emit(&compositor->kill_signal, focus_surface);
5268
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005269 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005270 wl_client_get_credentials(client, &pid, NULL, NULL);
5271
5272 /* Skip clients that we launched ourselves (the credentials of
5273 * the socketpair is ours) */
5274 if (pid == getpid())
5275 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005276
Daniel Stone325fc2d2012-05-30 16:31:58 +01005277 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005278}
5279
5280static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005281workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005282 uint32_t key, void *data)
5283{
5284 struct desktop_shell *shell = data;
5285 unsigned int new_index = shell->workspaces.current;
5286
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005287 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005288 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005289 if (new_index != 0)
5290 new_index--;
5291
5292 change_workspace(shell, new_index);
5293}
5294
5295static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005296workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005297 uint32_t key, void *data)
5298{
5299 struct desktop_shell *shell = data;
5300 unsigned int new_index = shell->workspaces.current;
5301
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005302 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005303 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005304 if (new_index < shell->workspaces.num - 1)
5305 new_index++;
5306
5307 change_workspace(shell, new_index);
5308}
5309
5310static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005311workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005312 uint32_t key, void *data)
5313{
5314 struct desktop_shell *shell = data;
5315 unsigned int new_index;
5316
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005317 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005318 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005319 new_index = key - KEY_F1;
5320 if (new_index >= shell->workspaces.num)
5321 new_index = shell->workspaces.num - 1;
5322
5323 change_workspace(shell, new_index);
5324}
5325
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005326static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005327workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005328 uint32_t key, void *data)
5329{
5330 struct desktop_shell *shell = data;
5331 unsigned int new_index = shell->workspaces.current;
5332
5333 if (shell->locked)
5334 return;
5335
5336 if (new_index != 0)
5337 new_index--;
5338
5339 take_surface_to_workspace_by_seat(shell, seat, new_index);
5340}
5341
5342static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005343workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005344 uint32_t key, void *data)
5345{
5346 struct desktop_shell *shell = data;
5347 unsigned int new_index = shell->workspaces.current;
5348
5349 if (shell->locked)
5350 return;
5351
5352 if (new_index < shell->workspaces.num - 1)
5353 new_index++;
5354
5355 take_surface_to_workspace_by_seat(shell, seat, new_index);
5356}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005357
5358static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005359handle_output_destroy(struct wl_listener *listener, void *data)
5360{
5361 struct shell_output *output_listener =
5362 container_of(listener, struct shell_output, destroy_listener);
5363
5364 wl_list_remove(&output_listener->destroy_listener.link);
5365 wl_list_remove(&output_listener->link);
5366 free(output_listener);
5367}
5368
5369static void
5370create_shell_output(struct desktop_shell *shell,
5371 struct weston_output *output)
5372{
5373 struct shell_output *shell_output;
5374
5375 shell_output = zalloc(sizeof *shell_output);
5376 if (shell_output == NULL)
5377 return;
5378
5379 shell_output->output = output;
5380 shell_output->shell = shell;
5381 shell_output->destroy_listener.notify = handle_output_destroy;
5382 wl_signal_add(&output->destroy_signal,
5383 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005384 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005385}
5386
5387static void
5388handle_output_create(struct wl_listener *listener, void *data)
5389{
5390 struct desktop_shell *shell =
5391 container_of(listener, struct desktop_shell, output_create_listener);
5392 struct weston_output *output = (struct weston_output *)data;
5393
5394 create_shell_output(shell, output);
5395}
5396
5397static void
5398setup_output_destroy_handler(struct weston_compositor *ec,
5399 struct desktop_shell *shell)
5400{
5401 struct weston_output *output;
5402
5403 wl_list_init(&shell->output_list);
5404 wl_list_for_each(output, &ec->output_list, link)
5405 create_shell_output(shell, output);
5406
5407 shell->output_create_listener.notify = handle_output_create;
5408 wl_signal_add(&ec->output_created_signal,
5409 &shell->output_create_listener);
5410}
5411
5412static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005413shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005414{
Tiago Vignattibe143262012-04-16 17:31:41 +03005415 struct desktop_shell *shell =
5416 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005417 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005418 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005419
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005420 if (shell->child.client)
5421 wl_client_destroy(shell->child.client);
5422
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005423 wl_list_remove(&shell->idle_listener.link);
5424 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005425
5426 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005427
Xiong Zhang6b481422013-10-23 13:58:32 +08005428 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5429 wl_list_remove(&shell_output->destroy_listener.link);
5430 wl_list_remove(&shell_output->link);
5431 free(shell_output);
5432 }
5433
5434 wl_list_remove(&shell->output_create_listener.link);
5435
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005436 wl_array_for_each(ws, &shell->workspaces.array)
5437 workspace_destroy(*ws);
5438 wl_array_release(&shell->workspaces.array);
5439
Pekka Paalanen3c647232011-12-22 13:43:43 +02005440 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005441 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005442 free(shell);
5443}
5444
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005445static void
5446shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5447{
5448 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005449 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005450
5451 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005452 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5453 MODIFIER_CTRL | MODIFIER_ALT,
5454 terminate_binding, ec);
5455 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5456 click_to_activate_binding,
5457 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005458 weston_compositor_add_touch_binding(ec, 0,
5459 touch_to_activate_binding,
5460 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005461 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5462 MODIFIER_SUPER | MODIFIER_ALT,
5463 surface_opacity_binding, NULL);
5464 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5465 MODIFIER_SUPER, zoom_axis_binding,
5466 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005467
5468 /* configurable bindings */
5469 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005470 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5471 zoom_key_binding, NULL);
5472 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5473 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005474 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5475 NULL);
5476 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5477 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005478 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5479 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005480 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005481 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5482 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005483
5484 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5485 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5486 rotate_binding, NULL);
5487
Daniel Stone325fc2d2012-05-30 16:31:58 +01005488 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5489 shell);
5490 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5491 ec);
5492 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5493 backlight_binding, ec);
5494 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5495 ec);
5496 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5497 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005498 weston_compositor_add_key_binding(ec, KEY_K, mod,
5499 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005500 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5501 workspace_up_binding, shell);
5502 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5503 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005504 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5505 workspace_move_surface_up_binding,
5506 shell);
5507 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5508 workspace_move_surface_down_binding,
5509 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005510
Daniel Stonedf8133b2013-11-19 11:37:14 +01005511 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5512
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005513 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5514 if (shell->workspaces.num > 1) {
5515 num_workspace_bindings = shell->workspaces.num;
5516 if (num_workspace_bindings > 6)
5517 num_workspace_bindings = 6;
5518 for (i = 0; i < num_workspace_bindings; i++)
5519 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5520 workspace_f_binding,
5521 shell);
5522 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005523
5524 /* Debug bindings */
5525 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5526 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005527}
5528
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005529WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005530module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005531 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005532{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005533 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005534 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005535 struct workspace **pws;
5536 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005537 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005538
Peter Huttererf3d62272013-08-08 11:57:05 +10005539 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005540 if (shell == NULL)
5541 return -1;
5542
Kristian Høgsberg75840622011-09-06 13:48:16 -04005543 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005544
5545 shell->destroy_listener.notify = shell_destroy;
5546 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005547 shell->idle_listener.notify = idle_handler;
5548 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5549 shell->wake_listener.notify = wake_handler;
5550 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005551
Scott Moreauff1db4a2012-04-17 19:06:18 -06005552 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005553 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005554 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005555 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005556 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005557 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005558 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005559 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005560 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005561 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005562 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005563
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005564 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5565 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005566 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5567 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005568 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005569
5570 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005571 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005572
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005573 if (input_panel_setup(shell) < 0)
5574 return -1;
5575
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005576 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005577
Daniel Stonedf8133b2013-11-19 11:37:14 +01005578 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5579 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5580
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005581 for (i = 0; i < shell->workspaces.num; i++) {
5582 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5583 if (pws == NULL)
5584 return -1;
5585
5586 *pws = workspace_create();
5587 if (*pws == NULL)
5588 return -1;
5589 }
5590 activate_workspace(shell, 0);
5591
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005592 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005593 wl_list_init(&shell->workspaces.animation.link);
5594 shell->workspaces.animation.frame = animate_workspace_change_frame;
5595
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005596 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005597 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005598 return -1;
5599
Rafael Antognollie2a34552013-12-03 15:35:45 -02005600 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5601 shell, bind_xdg_shell) == NULL)
5602 return -1;
5603
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005604 if (wl_global_create(ec->wl_display,
5605 &desktop_shell_interface, 2,
5606 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005607 return -1;
5608
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005609 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5610 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005611 return -1;
5612
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005613 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5614 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005615 return -1;
5616
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005617 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005618
Xiong Zhang6b481422013-10-23 13:58:32 +08005619 setup_output_destroy_handler(ec, shell);
5620
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005621 loop = wl_display_get_event_loop(ec->wl_display);
5622 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005623
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005624 shell->screensaver.timer =
5625 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5626
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005627 wl_list_for_each(seat, &ec->seat_list, link)
5628 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005629
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005630 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005631
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005632 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005633
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005634 return 0;
5635}