blob: 4cc23d54cde0fb10ce8e48dd83515ff41ce0e137 [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 &&
1472 seat->pointer->button_count > 0 &&
1473 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001475 if ((surface == shsurf->surface) &&
1476 (surface_move(shsurf, seat) < 0))
1477 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001478 } else if (seat->touch &&
1479 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001481 if ((surface == shsurf->surface) &&
1482 (surface_touch_move(shsurf, seat) < 0))
1483 wl_resource_post_no_memory(resource);
1484 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001485}
1486
Rafael Antognollie2a34552013-12-03 15:35:45 -02001487static void
1488shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1489 struct wl_resource *seat_resource, uint32_t serial)
1490{
1491 common_surface_move(resource, seat_resource, serial);
1492}
1493
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001494struct weston_resize_grab {
1495 struct shell_grab base;
1496 uint32_t edges;
1497 int32_t width, height;
1498};
1499
1500static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001501resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1502 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001503{
1504 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001505 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001506 struct shell_surface *shsurf = resize->base.shsurf;
1507 int32_t width, height;
1508 wl_fixed_t from_x, from_y;
1509 wl_fixed_t to_x, to_y;
1510
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001511 weston_pointer_move(pointer, x, y);
1512
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001513 if (!shsurf)
1514 return;
1515
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 weston_view_from_global_fixed(shsurf->view,
1517 pointer->grab_x, pointer->grab_y,
1518 &from_x, &from_y);
1519 weston_view_from_global_fixed(shsurf->view,
1520 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001521
1522 width = resize->width;
1523 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1524 width += wl_fixed_to_int(from_x - to_x);
1525 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1526 width += wl_fixed_to_int(to_x - from_x);
1527 }
1528
1529 height = resize->height;
1530 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1531 height += wl_fixed_to_int(from_y - to_y);
1532 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1533 height += wl_fixed_to_int(to_y - from_y);
1534 }
1535
1536 shsurf->client->send_configure(shsurf->surface,
1537 resize->edges, width, height);
1538}
1539
1540static void
1541send_configure(struct weston_surface *surface,
1542 uint32_t edges, int32_t width, int32_t height)
1543{
1544 struct shell_surface *shsurf = get_shell_surface(surface);
1545
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001546 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547 edges, width, height);
1548}
1549
1550static const struct weston_shell_client shell_client = {
1551 send_configure
1552};
1553
1554static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001555resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556 uint32_t time, uint32_t button, uint32_t state_w)
1557{
1558 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001559 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001560 enum wl_pointer_button_state state = state_w;
1561
1562 if (pointer->button_count == 0 &&
1563 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1564 shell_grab_end(&resize->base);
1565 free(grab);
1566 }
1567}
1568
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001569static void
1570resize_grab_cancel(struct weston_pointer_grab *grab)
1571{
1572 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1573
1574 shell_grab_end(&resize->base);
1575 free(grab);
1576}
1577
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001578static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579 noop_grab_focus,
1580 resize_grab_motion,
1581 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001582 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001583};
1584
Giulio Camuffob8366642013-04-25 13:57:46 +03001585/*
1586 * Returns the bounding box of a surface and all its sub-surfaces,
1587 * in the surface coordinates system. */
1588static void
1589surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1590 int32_t *y, int32_t *w, int32_t *h) {
1591 pixman_region32_t region;
1592 pixman_box32_t *box;
1593 struct weston_subsurface *subsurface;
1594
1595 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 surface->width,
1597 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001598
1599 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1600 pixman_region32_union_rect(&region, &region,
1601 subsurface->position.x,
1602 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 subsurface->surface->width,
1604 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001605 }
1606
1607 box = pixman_region32_extents(&region);
1608 if (x)
1609 *x = box->x1;
1610 if (y)
1611 *y = box->y1;
1612 if (w)
1613 *w = box->x2 - box->x1;
1614 if (h)
1615 *h = box->y2 - box->y1;
1616
1617 pixman_region32_fini(&region);
1618}
1619
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001620static int
1621surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001622 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001623{
1624 struct weston_resize_grab *resize;
1625
Rafael Antognolli03b16592013-12-03 15:35:42 -02001626 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001627 return 0;
1628
1629 if (edges == 0 || edges > 15 ||
1630 (edges & 3) == 3 || (edges & 12) == 12)
1631 return 0;
1632
1633 resize = malloc(sizeof *resize);
1634 if (!resize)
1635 return -1;
1636
1637 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001638 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1639 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640
1641 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001642 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001643
1644 return 0;
1645}
1646
1647static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001648common_surface_resize(struct wl_resource *resource,
1649 struct wl_resource *seat_resource, uint32_t serial,
1650 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001652 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001653 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001654 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655
Rafael Antognolli03b16592013-12-03 15:35:42 -02001656 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657 return;
1658
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001660 if (seat->pointer->button_count == 0 ||
1661 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001662 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 return;
1664
Kristian Høgsberge3148752013-05-06 23:19:49 -04001665 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001666 wl_resource_post_no_memory(resource);
1667}
1668
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001669static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001670shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1671 struct wl_resource *seat_resource, uint32_t serial,
1672 uint32_t edges)
1673{
1674 common_surface_resize(resource, seat_resource, serial, edges);
1675}
1676
1677static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001678end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1679
1680static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001681busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001682{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001683 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001684 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001686 wl_fixed_t sx, sy;
1687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 view = weston_compositor_pick_view(pointer->seat->compositor,
1689 pointer->x, pointer->y,
1690 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001693 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001694}
1695
1696static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001697busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1698 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001699{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001700 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001701}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001702
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001703static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001704busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001705 uint32_t time, uint32_t button, uint32_t state)
1706{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001707 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001708 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001709 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001710
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001711 if (shsurf && button == BTN_LEFT && state) {
1712 activate(shsurf->shell, shsurf->surface, seat);
1713 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001714 } else if (shsurf && button == BTN_RIGHT && state) {
1715 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001716 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001717 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001718}
1719
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001720static void
1721busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1722{
1723 struct shell_grab *grab = (struct shell_grab *) base;
1724
1725 shell_grab_end(grab);
1726 free(grab);
1727}
1728
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001729static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001730 busy_cursor_grab_focus,
1731 busy_cursor_grab_motion,
1732 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001733 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001734};
1735
1736static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001737set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001738{
1739 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740
1741 grab = malloc(sizeof *grab);
1742 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001743 return;
1744
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001745 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1746 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001747}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001749static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001750end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001751{
1752 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1753
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001754 if (grab->grab.interface == &busy_cursor_grab_interface &&
1755 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001756 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759}
1760
Scott Moreau9521d5e2012-04-19 13:06:17 -06001761static void
1762ping_timer_destroy(struct shell_surface *shsurf)
1763{
1764 if (!shsurf || !shsurf->ping_timer)
1765 return;
1766
1767 if (shsurf->ping_timer->source)
1768 wl_event_source_remove(shsurf->ping_timer->source);
1769
1770 free(shsurf->ping_timer);
1771 shsurf->ping_timer = NULL;
1772}
1773
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001774static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001775ping_timeout_handler(void *data)
1776{
1777 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001779
Scott Moreau9521d5e2012-04-19 13:06:17 -06001780 /* Client is not responding */
1781 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782
1783 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001784 if (seat->pointer->focus &&
1785 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001786 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001787
1788 return 1;
1789}
1790
1791static void
1792ping_handler(struct weston_surface *surface, uint32_t serial)
1793{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001794 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001795 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001797
1798 if (!shsurf)
1799 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001800 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001801 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001802
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001803 if (shsurf->surface == shsurf->shell->grab_surface)
1804 return;
1805
Scott Moreauff1db4a2012-04-17 19:06:18 -06001806 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001807 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001808 if (!shsurf->ping_timer)
1809 return;
1810
1811 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001812 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1813 shsurf->ping_timer->source =
1814 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1815 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1816
Rafael Antognollie2a34552013-12-03 15:35:45 -02001817 if (shell_surface_is_wl_shell_surface(shsurf))
1818 wl_shell_surface_send_ping(shsurf->resource, serial);
1819 else if (shell_surface_is_xdg_surface(shsurf))
1820 xdg_surface_send_ping(shsurf->resource, serial);
1821 else if (shell_surface_is_xdg_popup(shsurf))
1822 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001823 }
1824}
1825
1826static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827handle_pointer_focus(struct wl_listener *listener, void *data)
1828{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001829 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831 struct weston_compositor *compositor;
1832 struct shell_surface *shsurf;
1833 uint32_t serial;
1834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 return;
1837
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 compositor = view->surface->compositor;
1839 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001840
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001841 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842 set_busy_cursor(shsurf, pointer);
1843 } else {
1844 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846 }
1847}
1848
1849static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001850create_pointer_focus_listener(struct weston_seat *seat)
1851{
1852 struct wl_listener *listener;
1853
Kristian Høgsberge3148752013-05-06 23:19:49 -04001854 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001855 return;
1856
1857 listener = malloc(sizeof *listener);
1858 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001859 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001860}
1861
1862static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001863xdg_surface_set_transient_for(struct wl_client *client,
1864 struct wl_resource *resource,
1865 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001866{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001867 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001868 struct weston_surface *parent;
1869
1870 if (parent_resource)
1871 parent = wl_resource_get_user_data(parent_resource);
1872 else
1873 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001874
1875 shell_surface_set_parent(shsurf, parent);
1876}
1877
1878static void
1879surface_pong(struct shell_surface *shsurf, uint32_t serial)
1880{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001882 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001884 if (shsurf->ping_timer == NULL)
1885 /* Just ignore unsolicited pong. */
1886 return;
1887
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001889 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001890 wl_list_for_each(seat, &ec->seat_list, link) {
1891 if(seat->pointer)
1892 end_busy_cursor(shsurf, seat->pointer);
1893 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001894 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895 }
1896}
1897
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001898static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001899shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1900 uint32_t serial)
1901{
1902 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1903
1904 surface_pong(shsurf, serial);
1905
1906}
1907
1908static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001909set_title(struct shell_surface *shsurf, const char *title)
1910{
1911 free(shsurf->title);
1912 shsurf->title = strdup(title);
1913}
1914
1915static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001916shell_surface_set_title(struct wl_client *client,
1917 struct wl_resource *resource, const char *title)
1918{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001919 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001920
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001921 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001922}
1923
1924static void
1925shell_surface_set_class(struct wl_client *client,
1926 struct wl_resource *resource, const char *class)
1927{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001928 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001929
1930 free(shsurf->class);
1931 shsurf->class = strdup(class);
1932}
1933
Alex Wu4539b082012-03-01 12:57:46 +08001934static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001935restore_output_mode(struct weston_output *output)
1936{
Hardening57388e42013-09-18 23:56:36 +02001937 if (output->current_mode != output->original_mode ||
1938 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001939 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001940 output->original_mode,
1941 output->original_scale,
1942 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001943}
1944
1945static void
1946restore_all_output_modes(struct weston_compositor *compositor)
1947{
1948 struct weston_output *output;
1949
1950 wl_list_for_each(output, &compositor->output_list, link)
1951 restore_output_mode(output);
1952}
1953
Philip Withnallbecb77e2013-11-25 18:01:30 +00001954static int
1955get_output_panel_height(struct desktop_shell *shell,
1956 struct weston_output *output)
1957{
1958 struct weston_view *view;
1959 int panel_height = 0;
1960
1961 if (!output)
1962 return 0;
1963
1964 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1965 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001966 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001967 break;
1968 }
1969 }
1970
1971 return panel_height;
1972}
1973
Philip Withnall07926d92013-11-25 18:01:40 +00001974/* The surface will be inserted into the list immediately after the link
1975 * returned by this function (i.e. will be stacked immediately above the
1976 * returned link). */
1977static struct wl_list *
1978shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1979{
1980 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08001981 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00001982
1983 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08001984 case SHELL_SURFACE_POPUP:
1985 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02001986 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02001987 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02001988 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08001989 /* Move the surface to its parent layer so
1990 * that surfaces which are transient for
1991 * fullscreen surfaces don't get hidden by the
1992 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02001993
1994 /* TODO: Handle a parent with multiple views */
1995 parent = get_default_view(shsurf->parent);
1996 if (parent)
1997 return parent->layer_link.prev;
1998 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02001999 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002000
2001 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002002 return &shsurf->shell->fullscreen_layer.view_list;
2003
Philip Withnall07926d92013-11-25 18:01:40 +00002004 case SHELL_SURFACE_NONE:
2005 default:
2006 /* Go to the fallback, below. */
2007 break;
2008 }
2009
2010 /* Move the surface to a normal workspace layer so that surfaces
2011 * which were previously fullscreen or transient are no longer
2012 * rendered on top. */
2013 ws = get_current_workspace(shsurf->shell);
2014 return &ws->layer.view_list;
2015}
2016
Philip Withnall648a4dd2013-11-25 18:01:44 +00002017static void
2018shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2019{
2020 struct shell_surface *child;
2021
2022 /* Move the child layers to the same workspace as shsurf. They will be
2023 * stacked above shsurf. */
2024 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2025 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2026 weston_view_geometry_dirty(child->view);
2027 wl_list_remove(&child->view->layer_link);
2028 wl_list_insert(shsurf->view->layer_link.prev,
2029 &child->view->layer_link);
2030 weston_view_geometry_dirty(child->view);
2031 weston_surface_damage(child->surface);
2032
2033 /* Recurse. We don’t expect this to recurse very far (if
2034 * at all) because that would imply we have transient
2035 * (or popup) children of transient surfaces, which
2036 * would be unusual. */
2037 shell_surface_update_child_surface_layers(child);
2038 }
2039 }
2040}
2041
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002042/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002043 * geometry to ensure the changes are redrawn.
2044 *
2045 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2046 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002047static void
2048shell_surface_update_layer(struct shell_surface *shsurf)
2049{
2050 struct wl_list *new_layer_link;
2051
2052 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2053
2054 if (new_layer_link == &shsurf->view->layer_link)
2055 return;
2056
2057 weston_view_geometry_dirty(shsurf->view);
2058 wl_list_remove(&shsurf->view->layer_link);
2059 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2060 weston_view_geometry_dirty(shsurf->view);
2061 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002062
2063 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002064}
2065
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002066static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002067shell_surface_set_parent(struct shell_surface *shsurf,
2068 struct weston_surface *parent)
2069{
2070 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002071
2072 wl_list_remove(&shsurf->children_link);
2073 wl_list_init(&shsurf->children_link);
2074
2075 /* Insert into the parent surface’s child list. */
2076 if (parent != NULL) {
2077 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2078 if (parent_shsurf != NULL)
2079 wl_list_insert(&parent_shsurf->children_list,
2080 &shsurf->children_link);
2081 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002082}
2083
2084static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002085shell_surface_set_output(struct shell_surface *shsurf,
2086 struct weston_output *output)
2087{
2088 struct weston_surface *es = shsurf->surface;
2089
2090 /* get the default output, if the client set it as NULL
2091 check whether the ouput is available */
2092 if (output)
2093 shsurf->output = output;
2094 else if (es->output)
2095 shsurf->output = es->output;
2096 else
2097 shsurf->output = get_default_output(es->compositor);
2098}
2099
2100static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002101surface_clear_next_states(struct shell_surface *shsurf)
2102{
2103 shsurf->next_state.maximized = false;
2104 shsurf->next_state.fullscreen = false;
2105
2106 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2107 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2108 shsurf->state_changed = true;
2109}
2110
2111static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002112set_toplevel(struct shell_surface *shsurf)
2113{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002114 shell_surface_set_parent(shsurf, NULL);
2115 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002116 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002117
2118 /* The layer_link is updated in set_surface_type(),
2119 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002120}
2121
2122static void
2123shell_surface_set_toplevel(struct wl_client *client,
2124 struct wl_resource *resource)
2125{
2126 struct shell_surface *surface = wl_resource_get_user_data(resource);
2127
2128 set_toplevel(surface);
2129}
2130
2131static void
2132set_transient(struct shell_surface *shsurf,
2133 struct weston_surface *parent, int x, int y, uint32_t flags)
2134{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002135 assert(parent != NULL);
2136
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002137 shell_surface_set_parent(shsurf, parent);
2138
2139 surface_clear_next_states(shsurf);
2140
Philip Withnallbecb77e2013-11-25 18:01:30 +00002141 shsurf->transient.x = x;
2142 shsurf->transient.y = y;
2143 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002144
Rafael Antognollied207b42013-12-03 15:35:43 -02002145 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002146 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002147 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002148
2149 /* The layer_link is updated in set_surface_type(),
2150 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002151}
2152
2153static void
2154shell_surface_set_transient(struct wl_client *client,
2155 struct wl_resource *resource,
2156 struct wl_resource *parent_resource,
2157 int x, int y, uint32_t flags)
2158{
2159 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2160 struct weston_surface *parent =
2161 wl_resource_get_user_data(parent_resource);
2162
2163 set_transient(shsurf, parent, x, y, flags);
2164}
2165
2166static void
2167set_fullscreen(struct shell_surface *shsurf,
2168 uint32_t method,
2169 uint32_t framerate,
2170 struct weston_output *output)
2171{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002172 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002173
2174 shsurf->fullscreen_output = shsurf->output;
2175 shsurf->fullscreen.type = method;
2176 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002177
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002178 shsurf->next_state.fullscreen = true;
2179 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002180 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002181
2182 shsurf->client->send_configure(shsurf->surface, 0,
2183 shsurf->output->width,
2184 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002185
2186 /* The layer_link is updated in set_surface_type(),
2187 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002188}
2189
2190static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002191weston_view_set_initial_position(struct weston_view *view,
2192 struct desktop_shell *shell);
2193
2194static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002195unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002196{
Philip Withnallf85fe842013-11-25 18:01:37 +00002197 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002198 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2199 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002200 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002201 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002202 shsurf->fullscreen_output = NULL;
2203
Alex Wu4539b082012-03-01 12:57:46 +08002204 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2205 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002206
Alex Wu4539b082012-03-01 12:57:46 +08002207 wl_list_remove(&shsurf->fullscreen.transform.link);
2208 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002209
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 if (shsurf->fullscreen.black_view)
2211 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2212 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002213
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002214 if (shsurf->saved_position_valid)
2215 weston_view_set_position(shsurf->view,
2216 shsurf->saved_x, shsurf->saved_y);
2217 else
2218 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2219
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002220 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002222 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002223 shsurf->saved_rotation_valid = false;
2224 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002225
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002226 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002227}
2228
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002229static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002230shell_surface_set_fullscreen(struct wl_client *client,
2231 struct wl_resource *resource,
2232 uint32_t method,
2233 uint32_t framerate,
2234 struct wl_resource *output_resource)
2235{
2236 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2237 struct weston_output *output;
2238
2239 if (output_resource)
2240 output = wl_resource_get_user_data(output_resource);
2241 else
2242 output = NULL;
2243
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002244 shell_surface_set_parent(shsurf, NULL);
2245
Rafael Antognolli03b16592013-12-03 15:35:42 -02002246 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002247 set_fullscreen(shsurf, method, framerate, output);
2248}
2249
2250static void
2251set_popup(struct shell_surface *shsurf,
2252 struct weston_surface *parent,
2253 struct weston_seat *seat,
2254 uint32_t serial,
2255 int32_t x,
2256 int32_t y)
2257{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002258 assert(parent != NULL);
2259
Philip Withnallbecb77e2013-11-25 18:01:30 +00002260 shsurf->popup.shseat = get_shell_seat(seat);
2261 shsurf->popup.serial = serial;
2262 shsurf->popup.x = x;
2263 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002264
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002265 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002266}
2267
2268static void
2269shell_surface_set_popup(struct wl_client *client,
2270 struct wl_resource *resource,
2271 struct wl_resource *seat_resource,
2272 uint32_t serial,
2273 struct wl_resource *parent_resource,
2274 int32_t x, int32_t y, uint32_t flags)
2275{
2276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002277 struct weston_surface *parent =
2278 wl_resource_get_user_data(parent_resource);
2279
2280 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002281
Rafael Antognolli03b16592013-12-03 15:35:42 -02002282 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002283 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002284 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285 wl_resource_get_user_data(seat_resource),
2286 serial, x, y);
2287}
2288
2289static void
2290set_maximized(struct shell_surface *shsurf,
2291 struct weston_output *output)
2292{
2293 struct desktop_shell *shell;
2294 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295
Philip Withnall352e7ed2013-11-25 18:01:35 +00002296 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002297
2298 shell = shell_surface_get_shell(shsurf);
2299 panel_height = get_output_panel_height(shell, shsurf->output);
2300 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2301
2302 shsurf->client->send_configure(shsurf->surface, edges,
2303 shsurf->output->width,
2304 shsurf->output->height - panel_height);
2305
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002306 shsurf->next_state.maximized = true;
2307 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002308 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309}
2310
2311static void
2312unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002313{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002314 /* undo all maximized things here */
2315 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002316
2317 if (shsurf->saved_position_valid)
2318 weston_view_set_position(shsurf->view,
2319 shsurf->saved_x, shsurf->saved_y);
2320 else
2321 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002322
2323 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 wl_list_insert(&shsurf->view->geometry.transformation_list,
2325 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002326 shsurf->saved_rotation_valid = false;
2327 }
2328
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002329 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002330}
2331
Philip Withnallbecb77e2013-11-25 18:01:30 +00002332static void
2333shell_surface_set_maximized(struct wl_client *client,
2334 struct wl_resource *resource,
2335 struct wl_resource *output_resource)
2336{
2337 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2338 struct weston_output *output;
2339
2340 if (output_resource)
2341 output = wl_resource_get_user_data(output_resource);
2342 else
2343 output = NULL;
2344
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002345 shell_surface_set_parent(shsurf, NULL);
2346
Rafael Antognolli03b16592013-12-03 15:35:42 -02002347 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002348 set_maximized(shsurf, output);
2349}
2350
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002351/* This is only ever called from set_surface_type(), so there’s no need to
2352 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002353static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002354reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002355{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002356 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002357 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002358 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002360
Pekka Paalanen98262232011-12-01 10:42:22 +02002361 return 0;
2362}
2363
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002364static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002365set_full_output(struct shell_surface *shsurf)
2366{
2367 shsurf->saved_x = shsurf->view->geometry.x;
2368 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002369 shsurf->saved_width = shsurf->surface->width;
2370 shsurf->saved_height = shsurf->surface->height;
2371 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002372 shsurf->saved_position_valid = true;
2373
2374 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2375 wl_list_remove(&shsurf->rotation.transform.link);
2376 wl_list_init(&shsurf->rotation.transform.link);
2377 weston_view_geometry_dirty(shsurf->view);
2378 shsurf->saved_rotation_valid = true;
2379 }
2380}
2381
2382static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002383set_surface_type(struct shell_surface *shsurf)
2384{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002385 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002387
Philip Withnallbecb77e2013-11-25 18:01:30 +00002388 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002389
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002391 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002392
2393 switch (shsurf->type) {
2394 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002395 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002396 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002397 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398 weston_view_set_position(shsurf->view,
2399 pev->geometry.x + shsurf->transient.x,
2400 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002401 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002402 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002403
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002404 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 weston_view_set_position(shsurf->view, shsurf->transient.x,
2406 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002407 break;
2408
Philip Withnall0f640e22013-11-25 18:01:31 +00002409 case SHELL_SURFACE_POPUP:
2410 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002411 default:
2412 break;
2413 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002414
2415 /* Update the surface’s layer. */
2416 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002417}
2418
Tiago Vignattibe143262012-04-16 17:31:41 +03002419static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002420shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002421{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002422 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002423}
2424
Alex Wu21858432012-04-01 20:13:08 +08002425static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002426black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002427
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002429create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002430 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002431 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002432{
2433 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002435
2436 surface = weston_surface_create(ec);
2437 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002438 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002439 return NULL;
2440 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002441 view = weston_view_create(surface);
2442 if (surface == NULL) {
2443 weston_log("no memory\n");
2444 weston_surface_destroy(surface);
2445 return NULL;
2446 }
Alex Wu4539b082012-03-01 12:57:46 +08002447
Alex Wu21858432012-04-01 20:13:08 +08002448 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002449 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002450 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002451 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002452 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002453 pixman_region32_fini(&surface->input);
2454 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002455
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002456 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002457 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458
2459 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002460}
2461
Philip Withnalled8f1a92013-11-25 18:01:43 +00002462static void
2463shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2464{
2465 struct weston_output *output = shsurf->fullscreen_output;
2466
Rafael Antognolli03b16592013-12-03 15:35:42 -02002467 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002468
2469 if (!shsurf->fullscreen.black_view)
2470 shsurf->fullscreen.black_view =
2471 create_black_surface(shsurf->surface->compositor,
2472 shsurf->surface,
2473 output->x, output->y,
2474 output->width,
2475 output->height);
2476
2477 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2478 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2479 wl_list_insert(&shsurf->view->layer_link,
2480 &shsurf->fullscreen.black_view->layer_link);
2481 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2482 weston_surface_damage(shsurf->surface);
2483}
2484
Alex Wu4539b082012-03-01 12:57:46 +08002485/* Create black surface and append it to the associated fullscreen surface.
2486 * Handle size dismatch and positioning according to the method. */
2487static void
2488shell_configure_fullscreen(struct shell_surface *shsurf)
2489{
2490 struct weston_output *output = shsurf->fullscreen_output;
2491 struct weston_surface *surface = shsurf->surface;
2492 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002493 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002494 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002495
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002496 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2497 restore_output_mode(output);
2498
Philip Withnalled8f1a92013-11-25 18:01:43 +00002499 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002500
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002502 &surf_width, &surf_height);
2503
Alex Wu4539b082012-03-01 12:57:46 +08002504 switch (shsurf->fullscreen.type) {
2505 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002506 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002508 break;
2509 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002510 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002511 if (output->width == surf_width &&
2512 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 weston_view_set_position(shsurf->view,
2514 output->x - surf_x,
2515 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002516 break;
2517 }
2518
Alex Wu4539b082012-03-01 12:57:46 +08002519 matrix = &shsurf->fullscreen.transform.matrix;
2520 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002521
Scott Moreau1bad5db2012-08-18 01:04:05 -06002522 output_aspect = (float) output->width /
2523 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 /* XXX: Use surf_width and surf_height here? */
2525 surface_aspect = (float) surface->width /
2526 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002527 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002528 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002529 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002530 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002531 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002532 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002533
Alex Wu4539b082012-03-01 12:57:46 +08002534 weston_matrix_scale(matrix, scale, scale, 1);
2535 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002536 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002537 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002538 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2539 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002541
Alex Wu4539b082012-03-01 12:57:46 +08002542 break;
2543 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002544 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002545 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002546 surf_width * surface->buffer_viewport.scale,
2547 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002548 shsurf->fullscreen.framerate};
2549
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002550 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002551 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 weston_view_set_position(shsurf->view,
2553 output->x - surf_x,
2554 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002555 shsurf->fullscreen.black_view->surface->width = output->width;
2556 shsurf->fullscreen.black_view->surface->height = output->height;
2557 weston_view_set_position(shsurf->fullscreen.black_view,
2558 output->x - surf_x,
2559 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002560 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002561 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002562 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002564 }
Alex Wubd3354b2012-04-17 17:20:49 +08002565 }
Alex Wu4539b082012-03-01 12:57:46 +08002566 break;
2567 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002569 break;
2570 default:
2571 break;
2572 }
2573}
2574
Alex Wu4539b082012-03-01 12:57:46 +08002575static void
2576shell_map_fullscreen(struct shell_surface *shsurf)
2577{
Alex Wubd3354b2012-04-17 17:20:49 +08002578 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002579}
2580
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002581static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002582set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2583{
2584 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002586 shsurf->transient.x = x;
2587 shsurf->transient.y = y;
2588 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002589
2590 shell_surface_set_parent(shsurf, NULL);
2591
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002592 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002593 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002594}
2595
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002596static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002597
2598static void
2599destroy_shell_seat(struct wl_listener *listener, void *data)
2600{
2601 struct shell_seat *shseat =
2602 container_of(listener,
2603 struct shell_seat, seat_destroy_listener);
2604 struct shell_surface *shsurf, *prev = NULL;
2605
2606 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002607 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002608 shseat->popup_grab.client = NULL;
2609
2610 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2611 shsurf->popup.shseat = NULL;
2612 if (prev) {
2613 wl_list_init(&prev->popup.grab_link);
2614 }
2615 prev = shsurf;
2616 }
2617 wl_list_init(&prev->popup.grab_link);
2618 }
2619
2620 wl_list_remove(&shseat->seat_destroy_listener.link);
2621 free(shseat);
2622}
2623
2624static struct shell_seat *
2625create_shell_seat(struct weston_seat *seat)
2626{
2627 struct shell_seat *shseat;
2628
2629 shseat = calloc(1, sizeof *shseat);
2630 if (!shseat) {
2631 weston_log("no memory to allocate shell seat\n");
2632 return NULL;
2633 }
2634
2635 shseat->seat = seat;
2636 wl_list_init(&shseat->popup_grab.surfaces_list);
2637
2638 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2639 wl_signal_add(&seat->destroy_signal,
2640 &shseat->seat_destroy_listener);
2641
2642 return shseat;
2643}
2644
2645static struct shell_seat *
2646get_shell_seat(struct weston_seat *seat)
2647{
2648 struct wl_listener *listener;
2649
2650 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2651 if (listener == NULL)
2652 return create_shell_seat(seat);
2653
2654 return container_of(listener,
2655 struct shell_seat, seat_destroy_listener);
2656}
2657
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002658static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002659popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002660{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002661 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002663 struct shell_seat *shseat =
2664 container_of(grab, struct shell_seat, popup_grab.grab);
2665 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002666 wl_fixed_t sx, sy;
2667
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668 view = weston_compositor_pick_view(pointer->seat->compositor,
2669 pointer->x, pointer->y,
2670 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 if (view && view->surface->resource &&
2673 wl_resource_get_client(view->surface->resource) == client) {
2674 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002676 weston_pointer_set_focus(pointer, NULL,
2677 wl_fixed_from_int(0),
2678 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679 }
2680}
2681
2682static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002683popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2684 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002685{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002686 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002687 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002688 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002689
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002690 weston_pointer_move(pointer, x, y);
2691
Neil Roberts96d790e2013-09-19 17:32:00 +01002692 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 weston_view_from_global_fixed(pointer->focus,
2694 pointer->x, pointer->y,
2695 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002696 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002697 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002698}
2699
2700static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002701popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002702 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002703{
2704 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002705 struct shell_seat *shseat =
2706 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002707 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002708 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002709 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002710 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002711
Neil Roberts96d790e2013-09-19 17:32:00 +01002712 resource_list = &grab->pointer->focus_resource_list;
2713 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002715 wl_resource_for_each(resource, resource_list) {
2716 wl_pointer_send_button(resource, serial,
2717 time, button, state);
2718 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002719 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002720 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002721 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002722 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002723 }
2724
Daniel Stone4dbadb12012-05-30 16:31:51 +01002725 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002726 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002727}
2728
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002729static void
2730popup_grab_cancel(struct weston_pointer_grab *grab)
2731{
2732 popup_grab_end(grab->pointer);
2733}
2734
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002735static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002736 popup_grab_focus,
2737 popup_grab_motion,
2738 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002739 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002740};
2741
2742static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002743shell_surface_send_popup_done(struct shell_surface *shsurf)
2744{
2745 if (shell_surface_is_wl_shell_surface(shsurf))
2746 wl_shell_surface_send_popup_done(shsurf->resource);
2747 else if (shell_surface_is_xdg_popup(shsurf))
2748 xdg_popup_send_popup_done(shsurf->resource,
2749 shsurf->popup.serial);
2750}
2751
2752static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002753popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002754{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002755 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002756 struct shell_seat *shseat =
2757 container_of(grab, struct shell_seat, popup_grab.grab);
2758 struct shell_surface *shsurf;
2759 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002760
2761 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002762 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002763 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002764 shseat->popup_grab.grab.interface = NULL;
2765 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002766 /* Send the popup_done event to all the popups open */
2767 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002768 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002769 shsurf->popup.shseat = NULL;
2770 if (prev) {
2771 wl_list_init(&prev->popup.grab_link);
2772 }
2773 prev = shsurf;
2774 }
2775 wl_list_init(&prev->popup.grab_link);
2776 wl_list_init(&shseat->popup_grab.surfaces_list);
2777 }
2778}
2779
2780static void
2781add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2782{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002783 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002784
2785 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002786 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002787 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002788 /* We must make sure here that this popup was opened after
2789 * a mouse press, and not just by moving around with other
2790 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002791 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002792 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002793
Rob Bradforddfe31052013-06-26 19:49:11 +01002794 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002795 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002796 } else {
2797 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002798 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002799}
2800
2801static void
2802remove_popup_grab(struct shell_surface *shsurf)
2803{
2804 struct shell_seat *shseat = shsurf->popup.shseat;
2805
2806 wl_list_remove(&shsurf->popup.grab_link);
2807 wl_list_init(&shsurf->popup.grab_link);
2808 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002809 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002810 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002811 }
2812}
2813
2814static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002815shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002816{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002817 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002819
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 shsurf->surface->output = parent_view->output;
2821 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002822
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 weston_view_set_transform_parent(shsurf->view, parent_view);
2824 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2825 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002826
Kristian Høgsberge3148752013-05-06 23:19:49 -04002827 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002828 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002829 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002830 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002831 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002832 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002833}
2834
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002835static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002836 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002837 shell_surface_move,
2838 shell_surface_resize,
2839 shell_surface_set_toplevel,
2840 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002842 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002843 shell_surface_set_maximized,
2844 shell_surface_set_title,
2845 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002846};
2847
2848static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002849destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002850{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002851 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2852
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2854 remove_popup_grab(shsurf);
2855 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856
Alex Wubd3354b2012-04-17 17:20:49 +08002857 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002858 shell_surface_is_top_fullscreen(shsurf))
2859 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002860
Jason Ekstranda7af7042013-10-12 22:38:11 -05002861 if (shsurf->fullscreen.black_view)
2862 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002863
Alex Wubd3354b2012-04-17 17:20:49 +08002864 /* As destroy_resource() use wl_list_for_each_safe(),
2865 * we can always remove the listener.
2866 */
2867 wl_list_remove(&shsurf->surface_destroy_listener.link);
2868 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002869 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002870 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002871
Jason Ekstranda7af7042013-10-12 22:38:11 -05002872 weston_view_destroy(shsurf->view);
2873
Philip Withnall648a4dd2013-11-25 18:01:44 +00002874 wl_list_remove(&shsurf->children_link);
2875
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002876 wl_list_remove(&shsurf->link);
2877 free(shsurf);
2878}
2879
2880static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002881shell_destroy_shell_surface(struct wl_resource *resource)
2882{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002883 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002884
2885 destroy_shell_surface(shsurf);
2886}
2887
2888static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002889shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002890{
2891 struct shell_surface *shsurf = container_of(listener,
2892 struct shell_surface,
2893 surface_destroy_listener);
2894
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002895 if (shsurf->resource)
2896 wl_resource_destroy(shsurf->resource);
2897 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002898 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002899}
2900
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002901static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002902shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002903static void
2904shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002905
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002906struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002907get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002908{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002909 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002910 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002911 else
2912 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002913}
2914
Rafael Antognollie2a34552013-12-03 15:35:45 -02002915static struct shell_surface *
2916create_common_surface(void *shell, struct weston_surface *surface,
2917 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002918{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002919 struct shell_surface *shsurf;
2920
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002921 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002922 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002923 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002924 }
2925
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002926 shsurf = calloc(1, sizeof *shsurf);
2927 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002928 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002929 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002930 }
2931
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932 shsurf->view = weston_view_create(surface);
2933 if (!shsurf->view) {
2934 weston_log("no memory to allocate shell surface\n");
2935 free(shsurf);
2936 return NULL;
2937 }
2938
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002939 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002940 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002941 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002942
Tiago Vignattibc052c92012-04-19 16:18:18 +03002943 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002944 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002945 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002946 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002947 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002948 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002949 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2950 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002952 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002953 wl_list_init(&shsurf->fullscreen.transform.link);
2954
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002955 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002956 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002957 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002958 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002959
2960 /* init link so its safe to always remove it in destroy_shell_surface */
2961 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002962 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963
Pekka Paalanen460099f2012-01-20 16:48:25 +02002964 /* empty when not in use */
2965 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002966 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002967
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002968 wl_list_init(&shsurf->workspace_transform.link);
2969
Philip Withnall648a4dd2013-11-25 18:01:44 +00002970 wl_list_init(&shsurf->children_link);
2971 wl_list_init(&shsurf->children_list);
2972 shsurf->parent = NULL;
2973
Pekka Paalanen98262232011-12-01 10:42:22 +02002974 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002976 shsurf->client = client;
2977
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002978 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002979}
2980
Rafael Antognollie2a34552013-12-03 15:35:45 -02002981static struct shell_surface *
2982create_shell_surface(void *shell, struct weston_surface *surface,
2983 const struct weston_shell_client *client)
2984{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002985 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002986}
2987
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988static struct weston_view *
2989get_primary_view(void *shell, struct shell_surface *shsurf)
2990{
2991 return shsurf->view;
2992}
2993
Tiago Vignattibc052c92012-04-19 16:18:18 +03002994static void
2995shell_get_shell_surface(struct wl_client *client,
2996 struct wl_resource *resource,
2997 uint32_t id,
2998 struct wl_resource *surface_resource)
2999{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003000 struct weston_surface *surface =
3001 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003002 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003003 struct shell_surface *shsurf;
3004
3005 if (get_shell_surface(surface)) {
3006 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003007 WL_DISPLAY_ERROR_INVALID_OBJECT,
3008 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003009 return;
3010 }
3011
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003012 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003013 if (!shsurf) {
3014 wl_resource_post_error(surface_resource,
3015 WL_DISPLAY_ERROR_INVALID_OBJECT,
3016 "surface->configure already set");
3017 return;
3018 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003019
Jason Ekstranda85118c2013-06-27 20:17:02 -05003020 shsurf->resource =
3021 wl_resource_create(client,
3022 &wl_shell_surface_interface, 1, id);
3023 wl_resource_set_implementation(shsurf->resource,
3024 &shell_surface_implementation,
3025 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003026}
3027
Rafael Antognollie2a34552013-12-03 15:35:45 -02003028static bool
3029shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3030{
3031 return wl_resource_instance_of(shsurf->resource,
3032 &wl_shell_surface_interface,
3033 &shell_surface_implementation);
3034}
3035
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003036static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003037 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003038};
3039
Rafael Antognollie2a34552013-12-03 15:35:45 -02003040/****************************
3041 * xdg-shell implementation */
3042
3043static void
3044xdg_surface_destroy(struct wl_client *client,
3045 struct wl_resource *resource)
3046{
3047 wl_resource_destroy(resource);
3048}
3049
3050static void
3051xdg_surface_pong(struct wl_client *client,
3052 struct wl_resource *resource,
3053 uint32_t serial)
3054{
3055 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3056
3057 surface_pong(shsurf, serial);
3058}
3059
3060static void
3061xdg_surface_set_app_id(struct wl_client *client,
3062 struct wl_resource *resource,
3063 const char *app_id)
3064{
3065 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3066
3067 free(shsurf->class);
3068 shsurf->class = strdup(app_id);
3069}
3070
3071static void
3072xdg_surface_set_title(struct wl_client *client,
3073 struct wl_resource *resource, const char *title)
3074{
3075 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3076
3077 set_title(shsurf, title);
3078}
3079
3080static void
3081xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3082 struct wl_resource *seat_resource, uint32_t serial)
3083{
3084 common_surface_move(resource, seat_resource, serial);
3085}
3086
3087static void
3088xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3089 struct wl_resource *seat_resource, uint32_t serial,
3090 uint32_t edges)
3091{
3092 common_surface_resize(resource, seat_resource, serial, edges);
3093}
3094
3095static void
3096xdg_surface_set_output(struct wl_client *client,
3097 struct wl_resource *resource,
3098 struct wl_resource *output_resource)
3099{
3100 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3101 struct weston_output *output;
3102
3103 if (output_resource)
3104 output = wl_resource_get_user_data(output_resource);
3105 else
3106 output = NULL;
3107
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003108 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003109}
3110
3111static void
3112xdg_surface_set_fullscreen(struct wl_client *client,
3113 struct wl_resource *resource)
3114{
3115 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3116
3117 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3118 return;
3119
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003120 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003121 set_fullscreen(shsurf,
3122 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003123 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003124}
3125
3126static void
3127xdg_surface_unset_fullscreen(struct wl_client *client,
3128 struct wl_resource *resource)
3129{
3130 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003131 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003132
3133 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3134 return;
3135
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003136 if (!shsurf->next_state.fullscreen)
3137 return;
3138
Rafael Antognollie2a34552013-12-03 15:35:45 -02003139 shsurf->next_state.fullscreen = false;
3140 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003141
3142 if (shsurf->saved_size_valid) {
3143 width = shsurf->saved_width;
3144 height = shsurf->saved_height;
3145 shsurf->saved_size_valid = false;
3146 } else {
3147 width = shsurf->surface->width;
3148 height = shsurf->surface->height;
3149 }
3150
3151 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003152}
3153
3154static void
3155xdg_surface_set_maximized(struct wl_client *client,
3156 struct wl_resource *resource)
3157{
3158 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3159
3160 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3161 return;
3162
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003163 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003164 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003165}
3166
3167static void
3168xdg_surface_unset_maximized(struct wl_client *client,
3169 struct wl_resource *resource)
3170{
3171 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003172 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003173
3174 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3175 return;
3176
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003177 if (!shsurf->next_state.maximized)
3178 return;
3179
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180 shsurf->next_state.maximized = false;
3181 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003182
3183 if (shsurf->saved_size_valid) {
3184 width = shsurf->saved_width;
3185 height = shsurf->saved_height;
3186 shsurf->saved_size_valid = false;
3187 } else {
3188 width = shsurf->surface->width;
3189 height = shsurf->surface->height;
3190 }
3191
3192 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003193}
3194
3195static const struct xdg_surface_interface xdg_surface_implementation = {
3196 xdg_surface_destroy,
3197 xdg_surface_set_transient_for,
3198 xdg_surface_set_title,
3199 xdg_surface_set_app_id,
3200 xdg_surface_pong,
3201 xdg_surface_move,
3202 xdg_surface_resize,
3203 xdg_surface_set_output,
3204 xdg_surface_set_fullscreen,
3205 xdg_surface_unset_fullscreen,
3206 xdg_surface_set_maximized,
3207 xdg_surface_unset_maximized,
3208 NULL /* set_minimized */
3209};
3210
3211static void
3212xdg_send_configure(struct weston_surface *surface,
3213 uint32_t edges, int32_t width, int32_t height)
3214{
3215 struct shell_surface *shsurf = get_shell_surface(surface);
3216
3217 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3218}
3219
3220static const struct weston_shell_client xdg_client = {
3221 xdg_send_configure
3222};
3223
3224static void
3225xdg_use_unstable_version(struct wl_client *client,
3226 struct wl_resource *resource,
3227 int32_t version)
3228{
3229 if (version > 1) {
3230 wl_resource_post_error(resource,
3231 1,
3232 "xdg-shell:: version not implemented yet.");
3233 return;
3234 }
3235}
3236
3237static struct shell_surface *
3238create_xdg_surface(void *shell, struct weston_surface *surface,
3239 const struct weston_shell_client *client)
3240{
3241 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003242
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003243 shsurf = create_common_surface(shell, surface, client);
3244 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003245
3246 return shsurf;
3247}
3248
3249static void
3250xdg_get_xdg_surface(struct wl_client *client,
3251 struct wl_resource *resource,
3252 uint32_t id,
3253 struct wl_resource *surface_resource)
3254{
3255 struct weston_surface *surface =
3256 wl_resource_get_user_data(surface_resource);
3257 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3258 struct shell_surface *shsurf;
3259
3260 if (get_shell_surface(surface)) {
3261 wl_resource_post_error(surface_resource,
3262 WL_DISPLAY_ERROR_INVALID_OBJECT,
3263 "desktop_shell::get_shell_surface already requested");
3264 return;
3265 }
3266
3267 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3268 if (!shsurf) {
3269 wl_resource_post_error(surface_resource,
3270 WL_DISPLAY_ERROR_INVALID_OBJECT,
3271 "surface->configure already set");
3272 return;
3273 }
3274
3275 shsurf->resource =
3276 wl_resource_create(client,
3277 &xdg_surface_interface, 1, id);
3278 wl_resource_set_implementation(shsurf->resource,
3279 &xdg_surface_implementation,
3280 shsurf, shell_destroy_shell_surface);
3281}
3282
3283static bool
3284shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3285{
3286 return wl_resource_instance_of(shsurf->resource,
3287 &xdg_surface_interface,
3288 &xdg_surface_implementation);
3289}
3290
3291/* xdg-popup implementation */
3292
3293static void
3294xdg_popup_destroy(struct wl_client *client,
3295 struct wl_resource *resource)
3296{
3297 wl_resource_destroy(resource);
3298}
3299
3300static void
3301xdg_popup_pong(struct wl_client *client,
3302 struct wl_resource *resource,
3303 uint32_t serial)
3304{
3305 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3306
3307 surface_pong(shsurf, serial);
3308}
3309
3310static const struct xdg_popup_interface xdg_popup_implementation = {
3311 xdg_popup_destroy,
3312 xdg_popup_pong
3313};
3314
3315static void
3316xdg_popup_send_configure(struct weston_surface *surface,
3317 uint32_t edges, int32_t width, int32_t height)
3318{
3319}
3320
3321static const struct weston_shell_client xdg_popup_client = {
3322 xdg_popup_send_configure
3323};
3324
3325static struct shell_surface *
3326create_xdg_popup(void *shell, struct weston_surface *surface,
3327 const struct weston_shell_client *client,
3328 struct weston_surface *parent,
3329 struct shell_seat *seat,
3330 uint32_t serial,
3331 int32_t x, int32_t y)
3332{
3333 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003334
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003335 shsurf = create_common_surface(shell, surface, client);
3336 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337 shsurf->popup.shseat = seat;
3338 shsurf->popup.serial = serial;
3339 shsurf->popup.x = x;
3340 shsurf->popup.y = y;
3341 shell_surface_set_parent(shsurf, parent);
3342
3343 return shsurf;
3344}
3345
3346static void
3347xdg_get_xdg_popup(struct wl_client *client,
3348 struct wl_resource *resource,
3349 uint32_t id,
3350 struct wl_resource *surface_resource,
3351 struct wl_resource *parent_resource,
3352 struct wl_resource *seat_resource,
3353 uint32_t serial,
3354 int32_t x, int32_t y, uint32_t flags)
3355{
3356 struct weston_surface *surface =
3357 wl_resource_get_user_data(surface_resource);
3358 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3359 struct shell_surface *shsurf;
3360 struct weston_surface *parent;
3361 struct shell_seat *seat;
3362
3363 if (get_shell_surface(surface)) {
3364 wl_resource_post_error(surface_resource,
3365 WL_DISPLAY_ERROR_INVALID_OBJECT,
3366 "desktop_shell::get_shell_surface already requested");
3367 return;
3368 }
3369
3370 if (!parent_resource) {
3371 wl_resource_post_error(surface_resource,
3372 WL_DISPLAY_ERROR_INVALID_OBJECT,
3373 "xdg_shell::get_xdg_popup requires a parent shell surface");
3374 }
3375
3376 parent = wl_resource_get_user_data(parent_resource);
3377 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3378
3379 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3380 parent, seat, serial, x, y);
3381 if (!shsurf) {
3382 wl_resource_post_error(surface_resource,
3383 WL_DISPLAY_ERROR_INVALID_OBJECT,
3384 "surface->configure already set");
3385 return;
3386 }
3387
3388 shsurf->resource =
3389 wl_resource_create(client,
3390 &xdg_popup_interface, 1, id);
3391 wl_resource_set_implementation(shsurf->resource,
3392 &xdg_popup_implementation,
3393 shsurf, shell_destroy_shell_surface);
3394}
3395
3396static bool
3397shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3398{
3399 return wl_resource_instance_of(shsurf->resource,
3400 &xdg_popup_interface,
3401 &xdg_popup_implementation);
3402}
3403
3404static const struct xdg_shell_interface xdg_implementation = {
3405 xdg_use_unstable_version,
3406 xdg_get_xdg_surface,
3407 xdg_get_xdg_popup
3408};
3409
3410static int
3411xdg_shell_unversioned_dispatch(const void *implementation,
3412 void *_target, uint32_t opcode,
3413 const struct wl_message *message,
3414 union wl_argument *args)
3415{
3416 struct wl_resource *resource = _target;
3417 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3418
3419 if (opcode != 0) {
3420 wl_resource_post_error(resource,
3421 WL_DISPLAY_ERROR_INVALID_OBJECT,
3422 "must call use_unstable_version first");
3423 return 0;
3424 }
3425
3426#define XDG_SERVER_VERSION 1
3427
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003428 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3429 "shell implementation doesn't match protocol version");
3430
Rafael Antognollie2a34552013-12-03 15:35:45 -02003431 if (args[0].i != XDG_SERVER_VERSION) {
3432 wl_resource_post_error(resource,
3433 WL_DISPLAY_ERROR_INVALID_OBJECT,
3434 "incompatible version, server is %d "
3435 "client wants %d",
3436 XDG_SERVER_VERSION, args[0].i);
3437 return 0;
3438 }
3439
3440 wl_resource_set_implementation(resource, &xdg_implementation,
3441 shell, NULL);
3442
3443 return 1;
3444}
3445
3446/* end of xdg-shell implementation */
3447/***********************************/
3448
Kristian Høgsberg07937562011-04-12 17:25:42 -04003449static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003450shell_fade(struct desktop_shell *shell, enum fade_type type);
3451
3452static int
3453screensaver_timeout(void *data)
3454{
3455 struct desktop_shell *shell = data;
3456
3457 shell_fade(shell, FADE_OUT);
3458
3459 return 1;
3460}
3461
3462static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003463handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003464{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003465 struct desktop_shell *shell =
3466 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003467
Pekka Paalanen18027e52011-12-02 16:31:49 +02003468 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003469
3470 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003471 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003472}
3473
3474static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003475launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003476{
3477 if (shell->screensaver.binding)
3478 return;
3479
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003480 if (!shell->screensaver.path) {
3481 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003482 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003483 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003484
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003485 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003486 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003487 return;
3488 }
3489
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003490 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003491 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003492 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003493 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003494}
3495
3496static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003497terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003498{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003499 if (shell->screensaver.process.pid == 0)
3500 return;
3501
3502 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003503}
3504
3505static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003506configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003507{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003509
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3511 if (v->output == ev->output && v != ev) {
3512 weston_view_unmap(v);
3513 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003514 }
3515 }
3516
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003517 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003518
Jason Ekstranda7af7042013-10-12 22:38:11 -05003519 if (wl_list_empty(&ev->layer_link)) {
3520 wl_list_insert(&layer->view_list, &ev->layer_link);
3521 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003522 }
3523}
3524
3525static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003526background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003527{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003528 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003530
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 view = container_of(es->views.next, struct weston_view, surface_link);
3532
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003533 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003534}
3535
3536static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003537desktop_shell_set_background(struct wl_client *client,
3538 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003539 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003540 struct wl_resource *surface_resource)
3541{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003542 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003543 struct weston_surface *surface =
3544 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003545 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003546
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003547 if (surface->configure) {
3548 wl_resource_post_error(surface_resource,
3549 WL_DISPLAY_ERROR_INVALID_OBJECT,
3550 "surface role already assigned");
3551 return;
3552 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003553
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3555 weston_view_destroy(view);
3556 view = weston_view_create(surface);
3557
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003558 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003559 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003560 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003561 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003562 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003563 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003564 surface->output->width,
3565 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003566}
3567
3568static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003569panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003570{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003571 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003573
Jason Ekstranda7af7042013-10-12 22:38:11 -05003574 view = container_of(es->views.next, struct weston_view, surface_link);
3575
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003576 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003577}
3578
3579static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003580desktop_shell_set_panel(struct wl_client *client,
3581 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003582 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003583 struct wl_resource *surface_resource)
3584{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003585 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003586 struct weston_surface *surface =
3587 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003588 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003589
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003590 if (surface->configure) {
3591 wl_resource_post_error(surface_resource,
3592 WL_DISPLAY_ERROR_INVALID_OBJECT,
3593 "surface role already assigned");
3594 return;
3595 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003596
Jason Ekstranda7af7042013-10-12 22:38:11 -05003597 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3598 weston_view_destroy(view);
3599 view = weston_view_create(surface);
3600
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003601 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003602 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003603 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003605 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003606 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003607 surface->output->width,
3608 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003609}
3610
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003611static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003612lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003613{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003614 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 struct weston_view *view;
3616
3617 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003618
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003619 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003620 return;
3621
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003623
3624 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003625 wl_list_insert(&shell->lock_layer.view_list,
3626 &view->layer_link);
3627 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003628 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003629 }
3630}
3631
3632static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003633handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003634{
Tiago Vignattibe143262012-04-16 17:31:41 +03003635 struct desktop_shell *shell =
3636 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003637
Martin Minarik6d118362012-06-07 18:01:59 +02003638 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003639 shell->lock_surface = NULL;
3640}
3641
3642static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003643desktop_shell_set_lock_surface(struct wl_client *client,
3644 struct wl_resource *resource,
3645 struct wl_resource *surface_resource)
3646{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003647 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003648 struct weston_surface *surface =
3649 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003650
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003651 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003652
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003653 if (!shell->locked)
3654 return;
3655
Pekka Paalanen98262232011-12-01 10:42:22 +02003656 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003657
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003658 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003659 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003660 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003661
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003662 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003663 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003664 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003665}
3666
3667static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003668resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003669{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003670 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003671
Pekka Paalanen77346a62011-11-30 16:26:35 +02003672 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003673
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003674 wl_list_remove(&shell->lock_layer.link);
3675 wl_list_insert(&shell->compositor->cursor_layer.link,
3676 &shell->fullscreen_layer.link);
3677 wl_list_insert(&shell->fullscreen_layer.link,
3678 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003679 if (shell->showing_input_panels) {
3680 wl_list_insert(&shell->panel_layer.link,
3681 &shell->input_panel_layer.link);
3682 wl_list_insert(&shell->input_panel_layer.link,
3683 &ws->layer.link);
3684 } else {
3685 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3686 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003687
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003688 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003689
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003690 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003691 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003692 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003693}
3694
3695static void
3696desktop_shell_unlock(struct wl_client *client,
3697 struct wl_resource *resource)
3698{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003699 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003700
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003701 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003702
3703 if (shell->locked)
3704 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003705}
3706
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003707static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003708desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003709 struct wl_resource *resource,
3710 struct wl_resource *surface_resource)
3711{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003712 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003713
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003714 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003715 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003716}
3717
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003718static void
3719desktop_shell_desktop_ready(struct wl_client *client,
3720 struct wl_resource *resource)
3721{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003722 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003723
3724 shell_fade_startup(shell);
3725}
3726
Kristian Høgsberg75840622011-09-06 13:48:16 -04003727static const struct desktop_shell_interface desktop_shell_implementation = {
3728 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003729 desktop_shell_set_panel,
3730 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003731 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003732 desktop_shell_set_grab_surface,
3733 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003734};
3735
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003736static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003737get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003738{
3739 struct shell_surface *shsurf;
3740
3741 shsurf = get_shell_surface(surface);
3742 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003743 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003744 return shsurf->type;
3745}
3746
Kristian Høgsberg75840622011-09-06 13:48:16 -04003747static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003748move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003749{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003750 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003751 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003752 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003753
Pekka Paalanen01388e22013-04-25 13:57:44 +03003754 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003755 if (surface == NULL)
3756 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003757
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003758 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003759 if (shsurf == NULL || shsurf->state.fullscreen ||
3760 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003761 return;
3762
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003763 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003764}
3765
3766static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003767maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3768{
3769 struct weston_surface *focus = seat->pointer->focus->surface;
3770 struct weston_surface *surface;
3771 struct shell_surface *shsurf;
3772
3773 surface = weston_surface_get_main_surface(focus);
3774 if (surface == NULL)
3775 return;
3776
3777 shsurf = get_shell_surface(surface);
3778 if (shsurf == NULL)
3779 return;
3780
3781 if (!shell_surface_is_xdg_surface(shsurf))
3782 return;
3783
3784 if (shsurf->state.maximized)
3785 xdg_surface_send_request_unset_maximized(shsurf->resource);
3786 else
3787 xdg_surface_send_request_set_maximized(shsurf->resource);
3788}
3789
3790static void
3791fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3792{
3793 struct weston_surface *focus = seat->pointer->focus->surface;
3794 struct weston_surface *surface;
3795 struct shell_surface *shsurf;
3796
3797 surface = weston_surface_get_main_surface(focus);
3798 if (surface == NULL)
3799 return;
3800
3801 shsurf = get_shell_surface(surface);
3802 if (shsurf == NULL)
3803 return;
3804
3805 if (!shell_surface_is_xdg_surface(shsurf))
3806 return;
3807
3808 if (shsurf->state.fullscreen)
3809 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3810 else
3811 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3812}
3813
3814static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003815touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3816{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003817 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003818 struct weston_surface *surface;
3819 struct shell_surface *shsurf;
3820
3821 surface = weston_surface_get_main_surface(focus);
3822 if (surface == NULL)
3823 return;
3824
3825 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003826 if (shsurf == NULL || shsurf->state.fullscreen ||
3827 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003828 return;
3829
3830 surface_touch_move(shsurf, (struct weston_seat *) seat);
3831}
3832
3833static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003834resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003835{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003836 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003837 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003838 uint32_t edges = 0;
3839 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003840 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003841
Pekka Paalanen01388e22013-04-25 13:57:44 +03003842 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003843 if (surface == NULL)
3844 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003845
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003846 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003847 if (shsurf == NULL || shsurf->state.fullscreen ||
3848 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003849 return;
3850
Jason Ekstranda7af7042013-10-12 22:38:11 -05003851 weston_view_from_global(shsurf->view,
3852 wl_fixed_to_int(seat->pointer->grab_x),
3853 wl_fixed_to_int(seat->pointer->grab_y),
3854 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003855
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003856 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003857 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003858 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003859 edges |= 0;
3860 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003861 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003862
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003863 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003864 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003865 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003866 edges |= 0;
3867 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003868 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003869
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003870 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003871}
3872
3873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003874surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003875 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003876{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003877 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003878 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003879 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003880 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003881
Pekka Paalanen01388e22013-04-25 13:57:44 +03003882 /* XXX: broken for windows containing sub-surfaces */
3883 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003884 if (surface == NULL)
3885 return;
3886
3887 shsurf = get_shell_surface(surface);
3888 if (!shsurf)
3889 return;
3890
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003892
Jason Ekstranda7af7042013-10-12 22:38:11 -05003893 if (shsurf->view->alpha > 1.0)
3894 shsurf->view->alpha = 1.0;
3895 if (shsurf->view->alpha < step)
3896 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003897
Jason Ekstranda7af7042013-10-12 22:38:11 -05003898 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003899 weston_surface_damage(surface);
3900}
3901
3902static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003903do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003904 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003905{
Daniel Stone37816df2012-05-16 18:45:18 +01003906 struct weston_seat *ws = (struct weston_seat *) seat;
3907 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003908 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003909 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003910
3911 wl_list_for_each(output, &compositor->output_list, link) {
3912 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003913 wl_fixed_to_double(seat->pointer->x),
3914 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003915 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003916 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003917 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003918 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003919 increment = -output->zoom.increment;
3920 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003921 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003922 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003923 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003924 else
3925 increment = 0;
3926
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003927 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003928
Scott Moreaue6603982012-06-11 13:07:51 -06003929 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003930 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003931 else if (output->zoom.level > output->zoom.max_level)
3932 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003933 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003934 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003935 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003936
Scott Moreaue6603982012-06-11 13:07:51 -06003937 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003938
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003940 }
3941 }
3942}
3943
Scott Moreauccbf29d2012-02-22 14:21:41 -07003944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003945zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003946 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003947{
3948 do_zoom(seat, time, 0, axis, value);
3949}
3950
3951static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003952zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003953 void *data)
3954{
3955 do_zoom(seat, time, key, 0, 0);
3956}
3957
3958static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003959terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003960 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003961{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003962 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003963
Daniel Stone325fc2d2012-05-30 16:31:58 +01003964 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003965}
3966
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003967static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003968rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3969 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003970{
3971 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003972 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003973 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003974 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003975 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003976
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003977 weston_pointer_move(pointer, x, y);
3978
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003979 if (!shsurf)
3980 return;
3981
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003982 cx = 0.5f * shsurf->surface->width;
3983 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003984
Daniel Stone37816df2012-05-16 18:45:18 +01003985 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3986 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003987 r = sqrtf(dx * dx + dy * dy);
3988
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003989 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003991
3992 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003993 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003994 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003995
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003996 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003997 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003998
3999 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004000 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004001 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004002 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004003 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004004
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004005 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004006 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004007 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004008 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004009 wl_list_init(&shsurf->rotation.transform.link);
4010 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004011 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004012 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004013
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004014 /* We need to adjust the position of the surface
4015 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004016 cposx = shsurf->view->geometry.x + cx;
4017 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004018 dposx = rotate->center.x - cposx;
4019 dposy = rotate->center.y - cposy;
4020 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 weston_view_set_position(shsurf->view,
4022 shsurf->view->geometry.x + dposx,
4023 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004024 }
4025
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004026 /* Repaint implies weston_surface_update_transform(), which
4027 * lazily applies the damage due to rotation update.
4028 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004029 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004030}
4031
4032static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004033rotate_grab_button(struct weston_pointer_grab *grab,
4034 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004035{
4036 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004037 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004038 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004039 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004040 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004041
Daniel Stone4dbadb12012-05-30 16:31:51 +01004042 if (pointer->button_count == 0 &&
4043 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004044 if (shsurf)
4045 weston_matrix_multiply(&shsurf->rotation.rotation,
4046 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004047 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004048 free(rotate);
4049 }
4050}
4051
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004052static void
4053rotate_grab_cancel(struct weston_pointer_grab *grab)
4054{
4055 struct rotate_grab *rotate =
4056 container_of(grab, struct rotate_grab, base.grab);
4057
4058 shell_grab_end(&rotate->base);
4059 free(rotate);
4060}
4061
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004062static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004063 noop_grab_focus,
4064 rotate_grab_motion,
4065 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004066 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004067};
4068
4069static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004070surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004071{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004072 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004073 float dx, dy;
4074 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004075
Pekka Paalanen460099f2012-01-20 16:48:25 +02004076 rotate = malloc(sizeof *rotate);
4077 if (!rotate)
4078 return;
4079
Jason Ekstranda7af7042013-10-12 22:38:11 -05004080 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004081 surface->surface->width * 0.5f,
4082 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004083 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004084
Daniel Stone37816df2012-05-16 18:45:18 +01004085 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4086 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004087 r = sqrtf(dx * dx + dy * dy);
4088 if (r > 20.0f) {
4089 struct weston_matrix inverse;
4090
4091 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004092 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004093 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004094
4095 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004096 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004097 } else {
4098 weston_matrix_init(&surface->rotation.rotation);
4099 weston_matrix_init(&rotate->rotation);
4100 }
4101
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004102 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4103 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004104}
4105
4106static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004107rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004108 void *data)
4109{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004110 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004111 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004112 struct shell_surface *surface;
4113
Pekka Paalanen01388e22013-04-25 13:57:44 +03004114 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004115 if (base_surface == NULL)
4116 return;
4117
4118 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004119 if (surface == NULL || surface->state.fullscreen ||
4120 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004121 return;
4122
4123 surface_rotate(surface, seat);
4124}
4125
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004126/* Move all fullscreen layers down to the current workspace in a non-reversible
4127 * manner. This should be used when implementing shell-wide overlays, such as
4128 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004129void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004130lower_fullscreen_layer(struct desktop_shell *shell)
4131{
4132 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004133 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004134
4135 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004136 wl_list_for_each_reverse_safe(view, prev,
4137 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004138 layer_link) {
4139 wl_list_remove(&view->layer_link);
4140 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4141 weston_view_damage_below(view);
4142 weston_surface_damage(view->surface);
4143 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004144}
4145
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004146void
Tiago Vignattibe143262012-04-16 17:31:41 +03004147activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004148 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004149{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004150 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004151 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004152 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004153 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004154 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004155
Pekka Paalanen01388e22013-04-25 13:57:44 +03004156 main_surface = weston_surface_get_main_surface(es);
4157
Daniel Stone37816df2012-05-16 18:45:18 +01004158 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004159
Jonas Ådahl8538b222012-08-29 22:13:03 +02004160 state = ensure_focus_state(shell, seat);
4161 if (state == NULL)
4162 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004163
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004164 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004165 state->keyboard_focus = es;
4166 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004167 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004168
Rafael Antognolli03b16592013-12-03 15:35:42 -02004169 shsurf = get_shell_surface(main_surface);
4170 if (shsurf->state.fullscreen)
4171 shell_configure_fullscreen(shsurf);
4172 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004173 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004174
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004175 if (shell->focus_animation_type != ANIMATION_NONE) {
4176 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004177 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004178 }
4179
4180 /* Update the surface’s layer. This brings it to the top of the stacking
4181 * order as appropriate. */
4182 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004183}
4184
Alex Wu21858432012-04-01 20:13:08 +08004185/* no-op func for checking black surface */
4186static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004187black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004188{
4189}
4190
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004191static bool
Alex Wu21858432012-04-01 20:13:08 +08004192is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4193{
4194 if (es->configure == black_surface_configure) {
4195 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004196 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004197 return true;
4198 }
4199 return false;
4200}
4201
Kristian Høgsberg75840622011-09-06 13:48:16 -04004202static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004203activate_binding(struct weston_seat *seat,
4204 struct desktop_shell *shell,
4205 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004206{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004207 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004208
Alex Wu9c35e6b2012-03-05 14:13:13 +08004209 if (!focus)
4210 return;
4211
Pekka Paalanen01388e22013-04-25 13:57:44 +03004212 if (is_black_surface(focus, &main_surface))
4213 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004214
Pekka Paalanen01388e22013-04-25 13:57:44 +03004215 main_surface = weston_surface_get_main_surface(focus);
4216 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004217 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004218
Neil Robertsa28c6932013-10-03 16:43:04 +01004219 activate(shell, focus, seat);
4220}
4221
4222static void
4223click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4224 void *data)
4225{
4226 if (seat->pointer->grab != &seat->pointer->default_grab)
4227 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004228 if (seat->pointer->focus == NULL)
4229 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004230
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004231 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004232}
4233
4234static void
4235touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4236{
4237 if (seat->touch->grab != &seat->touch->default_grab)
4238 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004239 if (seat->touch->focus == NULL)
4240 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004241
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004242 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004243}
4244
4245static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004246lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004247{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004248 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004249
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004250 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004251 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004252 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004253 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004254
4255 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004256
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004257 /* Hide all surfaces by removing the fullscreen, panel and
4258 * toplevel layers. This way nothing else can show or receive
4259 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004260
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004261 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004262 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004263 if (shell->showing_input_panels)
4264 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004265 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004266 wl_list_insert(&shell->compositor->cursor_layer.link,
4267 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004268
Pekka Paalanen77346a62011-11-30 16:26:35 +02004269 launch_screensaver(shell);
4270
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004271 /* TODO: disable bindings that should not work while locked. */
4272
4273 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004274}
4275
4276static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004277unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004278{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004279 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004280 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004281 return;
4282 }
4283
4284 /* If desktop-shell client has gone away, unlock immediately. */
4285 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004286 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004287 return;
4288 }
4289
4290 if (shell->prepare_event_sent)
4291 return;
4292
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004293 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004294 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004295}
4296
4297static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004298shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004299{
4300 struct desktop_shell *shell = data;
4301
4302 shell->fade.animation = NULL;
4303
4304 switch (shell->fade.type) {
4305 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004306 weston_surface_destroy(shell->fade.view->surface);
4307 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004308 break;
4309 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004310 lock(shell);
4311 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004312 default:
4313 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004314 }
4315}
4316
Jason Ekstranda7af7042013-10-12 22:38:11 -05004317static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004318shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004319{
4320 struct weston_compositor *compositor = shell->compositor;
4321 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004323
4324 surface = weston_surface_create(compositor);
4325 if (!surface)
4326 return NULL;
4327
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 view = weston_view_create(surface);
4329 if (!view) {
4330 weston_surface_destroy(surface);
4331 return NULL;
4332 }
4333
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004334 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004335 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004336 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004337 wl_list_insert(&compositor->fade_layer.view_list,
4338 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004339 pixman_region32_init(&surface->input);
4340
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004342}
4343
4344static void
4345shell_fade(struct desktop_shell *shell, enum fade_type type)
4346{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004347 float tint;
4348
4349 switch (type) {
4350 case FADE_IN:
4351 tint = 0.0;
4352 break;
4353 case FADE_OUT:
4354 tint = 1.0;
4355 break;
4356 default:
4357 weston_log("shell: invalid fade type\n");
4358 return;
4359 }
4360
4361 shell->fade.type = type;
4362
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 if (shell->fade.view == NULL) {
4364 shell->fade.view = shell_fade_create_surface(shell);
4365 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004366 return;
4367
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 shell->fade.view->alpha = 1.0 - tint;
4369 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004370 }
4371
4372 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004373 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004374 else
4375 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004377 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004378 shell_fade_done, shell);
4379}
4380
4381static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004382do_shell_fade_startup(void *data)
4383{
4384 struct desktop_shell *shell = data;
4385
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004386 if (shell->startup_animation_type == ANIMATION_FADE)
4387 shell_fade(shell, FADE_IN);
4388 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 weston_surface_destroy(shell->fade.view->surface);
4390 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004391 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004392}
4393
4394static void
4395shell_fade_startup(struct desktop_shell *shell)
4396{
4397 struct wl_event_loop *loop;
4398
4399 if (!shell->fade.startup_timer)
4400 return;
4401
4402 wl_event_source_remove(shell->fade.startup_timer);
4403 shell->fade.startup_timer = NULL;
4404
4405 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4406 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4407}
4408
4409static int
4410fade_startup_timeout(void *data)
4411{
4412 struct desktop_shell *shell = data;
4413
4414 shell_fade_startup(shell);
4415 return 0;
4416}
4417
4418static void
4419shell_fade_init(struct desktop_shell *shell)
4420{
4421 /* Make compositor output all black, and wait for the desktop-shell
4422 * client to signal it is ready, then fade in. The timer triggers a
4423 * fade-in, in case the desktop-shell client takes too long.
4424 */
4425
4426 struct wl_event_loop *loop;
4427
Jason Ekstranda7af7042013-10-12 22:38:11 -05004428 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004429 weston_log("%s: warning: fade surface already exists\n",
4430 __func__);
4431 return;
4432 }
4433
Jason Ekstranda7af7042013-10-12 22:38:11 -05004434 shell->fade.view = shell_fade_create_surface(shell);
4435 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004436 return;
4437
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 weston_view_update_transform(shell->fade.view);
4439 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004440
4441 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4442 shell->fade.startup_timer =
4443 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4444 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4445}
4446
4447static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004448idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004449{
4450 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004451 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004452
4453 shell_fade(shell, FADE_OUT);
4454 /* lock() is called from shell_fade_done() */
4455}
4456
4457static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004458wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004459{
4460 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004461 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004462
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004463 unlock(shell);
4464}
4465
4466static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004467center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004468{
Giulio Camuffob8366642013-04-25 13:57:46 +03004469 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004470 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004471
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004473
4474 x = output->x + (output->width - width) / 2 - surf_x / 2;
4475 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004476
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004477 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004478}
4479
4480static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004481weston_view_set_initial_position(struct weston_view *view,
4482 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004483{
4484 struct weston_compositor *compositor = shell->compositor;
4485 int ix = 0, iy = 0;
4486 int range_x, range_y;
4487 int dx, dy, x, y, panel_height;
4488 struct weston_output *output, *target_output = NULL;
4489 struct weston_seat *seat;
4490
4491 /* As a heuristic place the new window on the same output as the
4492 * pointer. Falling back to the output containing 0, 0.
4493 *
4494 * TODO: Do something clever for touch too?
4495 */
4496 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004497 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004498 ix = wl_fixed_to_int(seat->pointer->x);
4499 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004500 break;
4501 }
4502 }
4503
4504 wl_list_for_each(output, &compositor->output_list, link) {
4505 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4506 target_output = output;
4507 break;
4508 }
4509 }
4510
4511 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004512 weston_view_set_position(view, 10 + random() % 400,
4513 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004514 return;
4515 }
4516
4517 /* Valid range within output where the surface will still be onscreen.
4518 * If this is negative it means that the surface is bigger than
4519 * output.
4520 */
4521 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004522 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004523 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004524 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004525
Rob Bradford4cb88c72012-08-13 15:18:44 +01004526 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004527 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004528 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004529 dx = 0;
4530
4531 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004532 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004533 else
4534 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004535
4536 x = target_output->x + dx;
4537 y = target_output->y + dy;
4538
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004540}
4541
4542static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004544 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004545{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004546 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004547 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004548 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004549 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004550
Pekka Paalanen77346a62011-11-30 16:26:35 +02004551 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004553 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004554 if (shsurf->state.fullscreen) {
4555 center_on_output(shsurf->view, shsurf->fullscreen_output);
4556 shell_map_fullscreen(shsurf);
4557 } else if (shsurf->state.maximized) {
4558 /* use surface configure to set the geometry */
4559 panel_height = get_output_panel_height(shell, shsurf->output);
4560 surface_subsurfaces_boundingbox(shsurf->surface,
4561 &surf_x, &surf_y, NULL, NULL);
4562 weston_view_set_position(shsurf->view,
4563 shsurf->output->x - surf_x,
4564 shsurf->output->y +
4565 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004566 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004567 weston_view_set_initial_position(shsurf->view, shell);
4568 }
Juan Zhao96879df2012-02-07 08:45:41 +08004569 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004570 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004571 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004572 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004573 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004574 weston_view_set_position(shsurf->view,
4575 shsurf->view->geometry.x + sx,
4576 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004577 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004578 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004579 default:
4580 ;
4581 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004582
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004583 /* Surface stacking order, see also activate(). */
4584 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004585
Jason Ekstranda7af7042013-10-12 22:38:11 -05004586 if (shsurf->type != SHELL_SURFACE_NONE) {
4587 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004588 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004589 shsurf->surface->output = shsurf->output;
4590 shsurf->view->output = shsurf->output;
4591 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004592 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004593
Rafael Antognollied207b42013-12-03 15:35:43 -02004594 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4595 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4596 }
4597
Jason Ekstranda7af7042013-10-12 22:38:11 -05004598 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004599 /* XXX: xwayland's using the same fields for transient type */
4600 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004601 if (shsurf->transient.flags ==
4602 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4603 break;
4604 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004605 if (shsurf->state.relative &&
4606 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4607 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004608 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004609 break;
4610 wl_list_for_each(seat, &compositor->seat_list, link)
4611 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004612 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004613 case SHELL_SURFACE_POPUP:
4614 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004615 default:
4616 break;
4617 }
4618
Rafael Antognolli03b16592013-12-03 15:35:42 -02004619 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4620 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004621 {
4622 switch (shell->win_animation_type) {
4623 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004625 break;
4626 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004628 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004629 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004630 default:
4631 break;
4632 }
4633 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004634}
4635
4636static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004637configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004638 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004639{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004640 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004642 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004643
Pekka Paalanen77346a62011-11-30 16:26:35 +02004644 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004645
Rafael Antognolli03b16592013-12-03 15:35:42 -02004646 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004647 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004648 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004649 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004650 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4651 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004652 mx = shsurf->output->x - surf_x;
4653 my = shsurf->output->y +
4654 get_output_panel_height(shell,shsurf->output) - surf_y;
4655 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004656 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004657 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004658 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004659
Alex Wu4539b082012-03-01 12:57:46 +08004660 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004661 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 wl_list_for_each(view, &surface->views, surface_link)
4663 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004664
Rafael Antognolli03b16592013-12-03 15:35:42 -02004665 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004666 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004667 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004668}
4669
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004670static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004671shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004672{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004673 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004674 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004675
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004676 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004677
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004678 if (!weston_surface_is_mapped(es) &&
4679 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004680 remove_popup_grab(shsurf);
4681 }
4682
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004683 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004684 return;
4685
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004686 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004687 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004688 type_changed = 1;
4689 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004690
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004691 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004692 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004693 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004694 shsurf->last_width != es->width ||
4695 shsurf->last_height != es->height) {
4696 shsurf->last_width = es->width;
4697 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004698 float from_x, from_y;
4699 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004700
Jason Ekstranda7af7042013-10-12 22:38:11 -05004701 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4702 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004703 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004704 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004705 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004706 }
4707}
4708
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004709static void
4710shell_surface_output_destroyed(struct weston_surface *es)
4711{
4712 struct shell_surface *shsurf = get_shell_surface(es);
4713
4714 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004715 shsurf->next_state.maximized = false;
4716 shsurf->next_state.fullscreen = false;
4717 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004718}
4719
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004720static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004721
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004722static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004723desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004724{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004725 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004726 struct desktop_shell *shell =
4727 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004728
4729 shell->child.process.pid = 0;
4730 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004731
4732 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4733 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004734 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004735 shell->child.deathstamp = time;
4736 shell->child.deathcount = 0;
4737 }
4738
4739 shell->child.deathcount++;
4740 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004741 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004742 return;
4743 }
4744
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004745 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004746 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004747 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004748}
4749
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004750static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004751desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4752{
4753 struct desktop_shell *shell;
4754
4755 shell = container_of(listener, struct desktop_shell,
4756 child.client_destroy_listener);
4757
4758 shell->child.client = NULL;
4759}
4760
4761static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004762launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004763{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004764 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004765
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004766 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004767 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004768 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004769 desktop_shell_sigchld);
4770
4771 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004772 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004773
4774 shell->child.client_destroy_listener.notify =
4775 desktop_shell_client_destroy;
4776 wl_client_add_destroy_listener(shell->child.client,
4777 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004778}
4779
4780static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004781bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4782{
Tiago Vignattibe143262012-04-16 17:31:41 +03004783 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004784 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004785
Jason Ekstranda85118c2013-06-27 20:17:02 -05004786 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4787 if (resource)
4788 wl_resource_set_implementation(resource, &shell_implementation,
4789 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004790}
4791
Kristian Høgsberg75840622011-09-06 13:48:16 -04004792static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004793bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4794{
4795 struct desktop_shell *shell = data;
4796 struct wl_resource *resource;
4797
4798 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4799 if (resource)
4800 wl_resource_set_dispatcher(resource,
4801 xdg_shell_unversioned_dispatch,
4802 NULL, shell, NULL);
4803}
4804
4805static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004806unbind_desktop_shell(struct wl_resource *resource)
4807{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004808 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004809
4810 if (shell->locked)
4811 resume_desktop(shell);
4812
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004813 shell->child.desktop_shell = NULL;
4814 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004815}
4816
4817static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004818bind_desktop_shell(struct wl_client *client,
4819 void *data, uint32_t version, uint32_t id)
4820{
Tiago Vignattibe143262012-04-16 17:31:41 +03004821 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004822 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004823
Jason Ekstranda85118c2013-06-27 20:17:02 -05004824 resource = wl_resource_create(client, &desktop_shell_interface,
4825 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004826
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004827 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004828 wl_resource_set_implementation(resource,
4829 &desktop_shell_implementation,
4830 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004831 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004832
4833 if (version < 2)
4834 shell_fade_startup(shell);
4835
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004836 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004837 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004838
4839 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4840 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004841 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004842}
4843
Pekka Paalanen6e168112011-11-24 11:34:05 +02004844static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004845screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004846{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004847 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004849
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004850 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004851 return;
4852
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004853 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004854 if (!shell->locked)
4855 return;
4856
Jason Ekstranda7af7042013-10-12 22:38:11 -05004857 view = container_of(surface->views.next, struct weston_view, surface_link);
4858 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004859
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860 if (wl_list_empty(&view->layer_link)) {
4861 wl_list_insert(shell->lock_layer.view_list.prev,
4862 &view->layer_link);
4863 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004864 wl_event_source_timer_update(shell->screensaver.timer,
4865 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004866 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004867 }
4868}
4869
4870static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004871screensaver_set_surface(struct wl_client *client,
4872 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004873 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004874 struct wl_resource *output_resource)
4875{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004876 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004877 struct weston_surface *surface =
4878 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004879 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 struct weston_view *view, *next;
4881
4882 /* Make sure we only have one view */
4883 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4884 weston_view_destroy(view);
4885 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004886
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004887 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004888 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004889 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004890}
4891
4892static const struct screensaver_interface screensaver_implementation = {
4893 screensaver_set_surface
4894};
4895
4896static void
4897unbind_screensaver(struct wl_resource *resource)
4898{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004899 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004900
Pekka Paalanen77346a62011-11-30 16:26:35 +02004901 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004902}
4903
4904static void
4905bind_screensaver(struct wl_client *client,
4906 void *data, uint32_t version, uint32_t id)
4907{
Tiago Vignattibe143262012-04-16 17:31:41 +03004908 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004909 struct wl_resource *resource;
4910
Jason Ekstranda85118c2013-06-27 20:17:02 -05004911 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004912
Pekka Paalanen77346a62011-11-30 16:26:35 +02004913 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004914 wl_resource_set_implementation(resource,
4915 &screensaver_implementation,
4916 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004917 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004918 return;
4919 }
4920
4921 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4922 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004923 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004924}
4925
Kristian Høgsberg07045392012-02-19 18:52:44 -05004926struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004927 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004928 struct weston_surface *current;
4929 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004930 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004931};
4932
4933static void
4934switcher_next(struct switcher *switcher)
4935{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004937 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004938 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004939 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004940
Jason Ekstranda7af7042013-10-12 22:38:11 -05004941 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004942 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02004943 if (shsurf &&
4944 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4945 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004946 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004947 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004948 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004949 next = view->surface;
4950 prev = view->surface;
4951 view->alpha = 0.25;
4952 weston_view_geometry_dirty(view);
4953 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004954 }
Alex Wu1659daa2012-04-01 20:13:09 +08004955
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 if (is_black_surface(view->surface, NULL)) {
4957 view->alpha = 0.25;
4958 weston_view_geometry_dirty(view);
4959 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004960 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004961 }
4962
4963 if (next == NULL)
4964 next = first;
4965
Alex Wu07b26062012-03-12 16:06:01 +08004966 if (next == NULL)
4967 return;
4968
Kristian Høgsberg07045392012-02-19 18:52:44 -05004969 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004970 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004971
4972 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004973 wl_list_for_each(view, &next->views, surface_link)
4974 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004975
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004976 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004977 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004978 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004979}
4980
4981static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004982switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004983{
4984 struct switcher *switcher =
4985 container_of(listener, struct switcher, listener);
4986
4987 switcher_next(switcher);
4988}
4989
4990static void
Daniel Stone351eb612012-05-31 15:27:47 -04004991switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004992{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004993 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004994 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004995 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004996
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004998 if (is_focus_view(view))
4999 continue;
5000
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 view->alpha = 1.0;
5002 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005003 }
5004
Alex Wu07b26062012-03-12 16:06:01 +08005005 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005006 activate(switcher->shell, switcher->current,
5007 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005008 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005009 weston_keyboard_end_grab(keyboard);
5010 if (keyboard->input_method_resource)
5011 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005012 free(switcher);
5013}
5014
5015static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005016switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005017 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005018{
5019 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005020 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005021
Daniel Stonec9785ea2012-05-30 16:31:52 +01005022 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005023 switcher_next(switcher);
5024}
5025
5026static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005027switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005028 uint32_t mods_depressed, uint32_t mods_latched,
5029 uint32_t mods_locked, uint32_t group)
5030{
5031 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005032 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005033
Daniel Stone351eb612012-05-31 15:27:47 -04005034 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5035 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005036}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005037
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005038static void
5039switcher_cancel(struct weston_keyboard_grab *grab)
5040{
5041 struct switcher *switcher = container_of(grab, struct switcher, grab);
5042
5043 switcher_destroy(switcher);
5044}
5045
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005046static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005047 switcher_key,
5048 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005049 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005050};
5051
5052static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005053switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005054 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005055{
Tiago Vignattibe143262012-04-16 17:31:41 +03005056 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005057 struct switcher *switcher;
5058
5059 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005060 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005061 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005062 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005063 wl_list_init(&switcher->listener.link);
5064
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005065 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005066 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005067 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005068 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5069 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005070 switcher_next(switcher);
5071}
5072
Pekka Paalanen3c647232011-12-22 13:43:43 +02005073static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005074backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005075 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005076{
5077 struct weston_compositor *compositor = data;
5078 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005079 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005080
5081 /* TODO: we're limiting to simple use cases, where we assume just
5082 * control on the primary display. We'd have to extend later if we
5083 * ever get support for setting backlights on random desktop LCD
5084 * panels though */
5085 output = get_default_output(compositor);
5086 if (!output)
5087 return;
5088
5089 if (!output->set_backlight)
5090 return;
5091
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005092 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5093 backlight_new = output->backlight_current - 25;
5094 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5095 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005096
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005097 if (backlight_new < 5)
5098 backlight_new = 5;
5099 if (backlight_new > 255)
5100 backlight_new = 255;
5101
5102 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005103 output->set_backlight(output, output->backlight_current);
5104}
5105
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005106struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005107 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005108 struct weston_seat *seat;
5109 uint32_t key[2];
5110 int key_released[2];
5111};
5112
5113static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005114debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005115 uint32_t key, uint32_t state)
5116{
5117 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005118 struct weston_compositor *ec = db->seat->compositor;
5119 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005120 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005121 uint32_t serial;
5122 int send = 0, terminate = 0;
5123 int check_binding = 1;
5124 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005125 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005126
5127 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5128 /* Do not run bindings on key releases */
5129 check_binding = 0;
5130
5131 for (i = 0; i < 2; i++)
5132 if (key == db->key[i])
5133 db->key_released[i] = 1;
5134
5135 if (db->key_released[0] && db->key_released[1]) {
5136 /* All key releases been swalled so end the grab */
5137 terminate = 1;
5138 } else if (key != db->key[0] && key != db->key[1]) {
5139 /* Should not swallow release of other keys */
5140 send = 1;
5141 }
5142 } else if (key == db->key[0] && !db->key_released[0]) {
5143 /* Do not check bindings for the first press of the binding
5144 * key. This allows it to be used as a debug shortcut.
5145 * We still need to swallow this event. */
5146 check_binding = 0;
5147 } else if (db->key[1]) {
5148 /* If we already ran a binding don't process another one since
5149 * we can't keep track of all the binding keys that were
5150 * pressed in order to swallow the release events. */
5151 send = 1;
5152 check_binding = 0;
5153 }
5154
5155 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005156 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5157 key, state)) {
5158 /* We ran a binding so swallow the press and keep the
5159 * grab to swallow the released too. */
5160 send = 0;
5161 terminate = 0;
5162 db->key[1] = key;
5163 } else {
5164 /* Terminate the grab since the key pressed is not a
5165 * debug binding key. */
5166 send = 1;
5167 terminate = 1;
5168 }
5169 }
5170
5171 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005172 serial = wl_display_next_serial(display);
5173 resource_list = &grab->keyboard->focus_resource_list;
5174 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005175 wl_keyboard_send_key(resource, serial, time, key, state);
5176 }
5177 }
5178
5179 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005180 weston_keyboard_end_grab(grab->keyboard);
5181 if (grab->keyboard->input_method_resource)
5182 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005183 free(db);
5184 }
5185}
5186
5187static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005188debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005189 uint32_t mods_depressed, uint32_t mods_latched,
5190 uint32_t mods_locked, uint32_t group)
5191{
5192 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005193 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005194
Neil Roberts96d790e2013-09-19 17:32:00 +01005195 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005196
Neil Roberts96d790e2013-09-19 17:32:00 +01005197 wl_resource_for_each(resource, resource_list) {
5198 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5199 mods_latched, mods_locked, group);
5200 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005201}
5202
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005203static void
5204debug_binding_cancel(struct weston_keyboard_grab *grab)
5205{
5206 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5207
5208 weston_keyboard_end_grab(grab->keyboard);
5209 free(db);
5210}
5211
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005212struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005213 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005214 debug_binding_modifiers,
5215 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005216};
5217
5218static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005219debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005220{
5221 struct debug_binding_grab *grab;
5222
5223 grab = calloc(1, sizeof *grab);
5224 if (!grab)
5225 return;
5226
5227 grab->seat = (struct weston_seat *) seat;
5228 grab->key[0] = key;
5229 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005230 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005231}
5232
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005233static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005234force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005235 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005236{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005237 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005238 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005239 struct desktop_shell *shell = data;
5240 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005241 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005242
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005243 focus_surface = seat->keyboard->focus;
5244 if (!focus_surface)
5245 return;
5246
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005247 wl_signal_emit(&compositor->kill_signal, focus_surface);
5248
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005249 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005250 wl_client_get_credentials(client, &pid, NULL, NULL);
5251
5252 /* Skip clients that we launched ourselves (the credentials of
5253 * the socketpair is ours) */
5254 if (pid == getpid())
5255 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005256
Daniel Stone325fc2d2012-05-30 16:31:58 +01005257 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005258}
5259
5260static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005261workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005262 uint32_t key, void *data)
5263{
5264 struct desktop_shell *shell = data;
5265 unsigned int new_index = shell->workspaces.current;
5266
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005267 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005268 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005269 if (new_index != 0)
5270 new_index--;
5271
5272 change_workspace(shell, new_index);
5273}
5274
5275static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005276workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005277 uint32_t key, void *data)
5278{
5279 struct desktop_shell *shell = data;
5280 unsigned int new_index = shell->workspaces.current;
5281
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005282 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005283 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005284 if (new_index < shell->workspaces.num - 1)
5285 new_index++;
5286
5287 change_workspace(shell, new_index);
5288}
5289
5290static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005291workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005292 uint32_t key, void *data)
5293{
5294 struct desktop_shell *shell = data;
5295 unsigned int new_index;
5296
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005297 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005298 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005299 new_index = key - KEY_F1;
5300 if (new_index >= shell->workspaces.num)
5301 new_index = shell->workspaces.num - 1;
5302
5303 change_workspace(shell, new_index);
5304}
5305
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005306static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005307workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005308 uint32_t key, void *data)
5309{
5310 struct desktop_shell *shell = data;
5311 unsigned int new_index = shell->workspaces.current;
5312
5313 if (shell->locked)
5314 return;
5315
5316 if (new_index != 0)
5317 new_index--;
5318
5319 take_surface_to_workspace_by_seat(shell, seat, new_index);
5320}
5321
5322static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005323workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005324 uint32_t key, void *data)
5325{
5326 struct desktop_shell *shell = data;
5327 unsigned int new_index = shell->workspaces.current;
5328
5329 if (shell->locked)
5330 return;
5331
5332 if (new_index < shell->workspaces.num - 1)
5333 new_index++;
5334
5335 take_surface_to_workspace_by_seat(shell, seat, new_index);
5336}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005337
5338static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005339handle_output_destroy(struct wl_listener *listener, void *data)
5340{
5341 struct shell_output *output_listener =
5342 container_of(listener, struct shell_output, destroy_listener);
5343
5344 wl_list_remove(&output_listener->destroy_listener.link);
5345 wl_list_remove(&output_listener->link);
5346 free(output_listener);
5347}
5348
5349static void
5350create_shell_output(struct desktop_shell *shell,
5351 struct weston_output *output)
5352{
5353 struct shell_output *shell_output;
5354
5355 shell_output = zalloc(sizeof *shell_output);
5356 if (shell_output == NULL)
5357 return;
5358
5359 shell_output->output = output;
5360 shell_output->shell = shell;
5361 shell_output->destroy_listener.notify = handle_output_destroy;
5362 wl_signal_add(&output->destroy_signal,
5363 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005364 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005365}
5366
5367static void
5368handle_output_create(struct wl_listener *listener, void *data)
5369{
5370 struct desktop_shell *shell =
5371 container_of(listener, struct desktop_shell, output_create_listener);
5372 struct weston_output *output = (struct weston_output *)data;
5373
5374 create_shell_output(shell, output);
5375}
5376
5377static void
5378setup_output_destroy_handler(struct weston_compositor *ec,
5379 struct desktop_shell *shell)
5380{
5381 struct weston_output *output;
5382
5383 wl_list_init(&shell->output_list);
5384 wl_list_for_each(output, &ec->output_list, link)
5385 create_shell_output(shell, output);
5386
5387 shell->output_create_listener.notify = handle_output_create;
5388 wl_signal_add(&ec->output_created_signal,
5389 &shell->output_create_listener);
5390}
5391
5392static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005393shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005394{
Tiago Vignattibe143262012-04-16 17:31:41 +03005395 struct desktop_shell *shell =
5396 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005397 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005398 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005399
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005400 if (shell->child.client)
5401 wl_client_destroy(shell->child.client);
5402
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005403 wl_list_remove(&shell->idle_listener.link);
5404 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005405
5406 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005407
Xiong Zhang6b481422013-10-23 13:58:32 +08005408 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5409 wl_list_remove(&shell_output->destroy_listener.link);
5410 wl_list_remove(&shell_output->link);
5411 free(shell_output);
5412 }
5413
5414 wl_list_remove(&shell->output_create_listener.link);
5415
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005416 wl_array_for_each(ws, &shell->workspaces.array)
5417 workspace_destroy(*ws);
5418 wl_array_release(&shell->workspaces.array);
5419
Pekka Paalanen3c647232011-12-22 13:43:43 +02005420 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005421 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005422 free(shell);
5423}
5424
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005425static void
5426shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5427{
5428 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005429 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005430
5431 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005432 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5433 MODIFIER_CTRL | MODIFIER_ALT,
5434 terminate_binding, ec);
5435 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5436 click_to_activate_binding,
5437 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005438 weston_compositor_add_touch_binding(ec, 0,
5439 touch_to_activate_binding,
5440 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005441 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5442 MODIFIER_SUPER | MODIFIER_ALT,
5443 surface_opacity_binding, NULL);
5444 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5445 MODIFIER_SUPER, zoom_axis_binding,
5446 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005447
5448 /* configurable bindings */
5449 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005450 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5451 zoom_key_binding, NULL);
5452 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5453 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005454 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5455 NULL);
5456 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5457 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005458 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5459 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005460 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005461 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5462 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005463
5464 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5465 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5466 rotate_binding, NULL);
5467
Daniel Stone325fc2d2012-05-30 16:31:58 +01005468 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5469 shell);
5470 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5471 ec);
5472 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5473 backlight_binding, ec);
5474 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5475 ec);
5476 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5477 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005478 weston_compositor_add_key_binding(ec, KEY_K, mod,
5479 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005480 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5481 workspace_up_binding, shell);
5482 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5483 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005484 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5485 workspace_move_surface_up_binding,
5486 shell);
5487 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5488 workspace_move_surface_down_binding,
5489 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005490
Daniel Stonedf8133b2013-11-19 11:37:14 +01005491 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5492
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005493 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5494 if (shell->workspaces.num > 1) {
5495 num_workspace_bindings = shell->workspaces.num;
5496 if (num_workspace_bindings > 6)
5497 num_workspace_bindings = 6;
5498 for (i = 0; i < num_workspace_bindings; i++)
5499 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5500 workspace_f_binding,
5501 shell);
5502 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005503
5504 /* Debug bindings */
5505 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5506 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005507}
5508
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005509WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005510module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005511 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005512{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005513 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005514 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005515 struct workspace **pws;
5516 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005517 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005518
Peter Huttererf3d62272013-08-08 11:57:05 +10005519 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005520 if (shell == NULL)
5521 return -1;
5522
Kristian Høgsberg75840622011-09-06 13:48:16 -04005523 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005524
5525 shell->destroy_listener.notify = shell_destroy;
5526 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005527 shell->idle_listener.notify = idle_handler;
5528 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5529 shell->wake_listener.notify = wake_handler;
5530 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005531
Scott Moreauff1db4a2012-04-17 19:06:18 -06005532 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005533 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005534 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005535 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005536 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005537 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005538 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005539 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005540 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005541 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005542 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005543
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005544 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5545 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005546 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5547 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005548 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005549
5550 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005551 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005552
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005553 if (input_panel_setup(shell) < 0)
5554 return -1;
5555
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005556 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005557
Daniel Stonedf8133b2013-11-19 11:37:14 +01005558 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5559 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5560
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005561 for (i = 0; i < shell->workspaces.num; i++) {
5562 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5563 if (pws == NULL)
5564 return -1;
5565
5566 *pws = workspace_create();
5567 if (*pws == NULL)
5568 return -1;
5569 }
5570 activate_workspace(shell, 0);
5571
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005572 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005573 wl_list_init(&shell->workspaces.animation.link);
5574 shell->workspaces.animation.frame = animate_workspace_change_frame;
5575
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005576 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005577 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005578 return -1;
5579
Rafael Antognollie2a34552013-12-03 15:35:45 -02005580 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5581 shell, bind_xdg_shell) == NULL)
5582 return -1;
5583
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005584 if (wl_global_create(ec->wl_display,
5585 &desktop_shell_interface, 2,
5586 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005587 return -1;
5588
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005589 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5590 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005591 return -1;
5592
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005593 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5594 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005595 return -1;
5596
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005597 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005598
Xiong Zhang6b481422013-10-23 13:58:32 +08005599 setup_output_destroy_handler(ec, shell);
5600
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005601 loop = wl_display_get_event_loop(ec->wl_display);
5602 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005603
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005604 shell->screensaver.timer =
5605 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5606
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005607 wl_list_for_each(seat, &ec->seat_list, link)
5608 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005609
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005610 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005611
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005612 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005613
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005614 return 0;
5615}