blob: ad0a055e4b8dfbc4ef5d0f5320eaef9a075fb97b [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Scott Moreauff1db4a2012-04-17 19:06:18 -060073struct ping_timer {
74 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060075 uint32_t serial;
76};
77
Philip Withnall648a4dd2013-11-25 18:01:44 +000078/*
79 * Surface stacking and ordering.
80 *
81 * This is handled using several linked lists of surfaces, organised into
82 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
83 * above all of the surfaces in the layer below. The set of layers is static and
84 * in the following order (top-most first):
85 * • Lock layer (only ever displayed on its own)
86 * • Cursor layer
87 * • Fullscreen layer
88 * • Panel layer
89 * • Input panel layer
90 * • Workspace layers
91 * • Background layer
92 *
93 * The list of layers may be manipulated to remove whole layers of surfaces from
94 * display. For example, when locking the screen, all layers except the lock
95 * layer are removed.
96 *
97 * A surface’s layer is modified on configuring the surface, in
98 * set_surface_type() (which is only called when the surface’s type change is
99 * _committed_). If a surface’s type changes (e.g. when making a window
100 * fullscreen) its layer changes too.
101 *
102 * In order to allow popup and transient surfaces to be correctly stacked above
103 * their parent surfaces, each surface tracks both its parent surface, and a
104 * linked list of its children. When a surface’s layer is updated, so are the
105 * layers of its children. Note that child surfaces are *not* the same as
106 * subsurfaces — child/parent surfaces are purely for maintaining stacking
107 * order.
108 *
109 * The children_link list of siblings of a surface (i.e. those surfaces which
110 * have the same parent) only contains weston_surfaces which have a
111 * shell_surface. Stacking is not implemented for non-shell_surface
112 * weston_surfaces. This means that the following implication does *not* hold:
113 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
114 */
115
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200116struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500117 struct wl_resource *resource;
118 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600122 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200123 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400124 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000125 struct wl_list children_list; /* child surfaces of this one */
126 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300127 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800129 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400130 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200132 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800133 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200134 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800135 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700136 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100137
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500140 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 } rotation;
142
143 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100144 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100146 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400147 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 } popup;
149
Alex Wu4539b082012-03-01 12:57:46 +0800150 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300151 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400152 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300153 } transient;
154
155 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800156 enum wl_shell_surface_fullscreen_method type;
157 struct weston_transform transform; /* matrix from x, y */
158 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800160 } fullscreen;
161
Scott Moreauff1db4a2012-04-17 19:06:18 -0600162 struct ping_timer *ping_timer;
163
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200164 struct weston_transform workspace_transform;
165
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500166 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200168 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100169 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400170
171 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172
173 struct {
174 bool maximized;
175 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200176 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200177 } state, next_state; /* surface states */
178 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct weston_touch_move_grab {
200 struct shell_touch_grab base;
201 wl_fixed_t dx, dy;
202};
203
Pekka Paalanen460099f2012-01-20 16:48:25 +0200204struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500206 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200208 float x;
209 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210 } center;
211};
212
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213struct shell_seat {
214 struct weston_seat *seat;
215 struct wl_listener seat_destroy_listener;
216
217 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400218 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219 struct wl_list surfaces_list;
220 struct wl_client *client;
221 int32_t initial_up;
222 } popup_grab;
223};
224
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100225void
226set_alpha_if_fullscreen(struct shell_surface *shsurf)
227{
228 if (shsurf && shsurf->state.fullscreen)
229 shsurf->fullscreen.black_view->alpha = 0.25;
230}
231
Alex Wubd3354b2012-04-17 17:20:49 +0800232static struct desktop_shell *
233shell_surface_get_shell(struct shell_surface *shsurf);
234
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400236surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500237
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300238static void
239shell_fade_startup(struct desktop_shell *shell);
240
Philip Withnallbecb77e2013-11-25 18:01:30 +0000241static struct shell_seat *
242get_shell_seat(struct weston_seat *seat);
243
Philip Withnall648a4dd2013-11-25 18:01:44 +0000244static void
245shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
246
Alex Wubd3354b2012-04-17 17:20:49 +0800247static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200248shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
249
250static bool
251shell_surface_is_xdg_surface(struct shell_surface *shsurf);
252
253static bool
254shell_surface_is_xdg_popup(struct shell_surface *shsurf);
255
256static void
257shell_surface_set_parent(struct shell_surface *shsurf,
258 struct weston_surface *parent);
259
260static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800261shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
262{
263 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500264 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800265
266 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100267
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800269 return false;
270
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
272 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800273 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800275}
276
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500277static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400278destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279{
280 struct shell_grab *grab;
281
282 grab = container_of(listener, struct shell_grab,
283 shsurf_destroy_listener);
284
285 grab->shsurf = NULL;
286}
287
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800288struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289get_default_view(struct weston_surface *surface)
290{
291 struct shell_surface *shsurf;
292 struct weston_view *view;
293
294 if (!surface || wl_list_empty(&surface->views))
295 return NULL;
296
297 shsurf = get_shell_surface(surface);
298 if (shsurf)
299 return shsurf->view;
300
301 wl_list_for_each(view, &surface->views, surface_link)
302 if (weston_view_is_mapped(view))
303 return view;
304
305 return container_of(surface->views.next, struct weston_view, surface_link);
306}
307
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300308static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400309popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400310
311static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300312shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300314 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300317{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318 struct desktop_shell *shell = shsurf->shell;
319
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400320 popup_grab_end(pointer);
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322 grab->grab.interface = interface;
323 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400324 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500325 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400326 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700328 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400330 if (shell->child.desktop_shell) {
331 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
332 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 weston_pointer_set_focus(pointer,
334 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400335 wl_fixed_from_int(0),
336 wl_fixed_from_int(0));
337 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338}
339
340static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300341shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700343 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400344 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700345 grab->shsurf->grabbed = 0;
346 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300347
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700348 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349}
350
351static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700352shell_touch_grab_start(struct shell_touch_grab *grab,
353 const struct weston_touch_grab_interface *interface,
354 struct shell_surface *shsurf,
355 struct weston_touch *touch)
356{
357 struct desktop_shell *shell = shsurf->shell;
358
359 grab->grab.interface = interface;
360 grab->shsurf = shsurf;
361 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
362 wl_signal_add(&shsurf->destroy_signal,
363 &grab->shsurf_destroy_listener);
364
365 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700367
368 weston_touch_start_grab(touch, &grab->grab);
369 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500370 weston_touch_set_focus(touch->seat,
371 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700372}
373
374static void
375shell_touch_grab_end(struct shell_touch_grab *grab)
376{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700377 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700378 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700379 grab->shsurf->grabbed = 0;
380 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700381
382 weston_touch_end_grab(grab->touch);
383}
384
385static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800387 struct weston_output *output);
388
Daniel Stone496ca172012-05-30 16:31:42 +0100389static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300390get_modifier(char *modifier)
391{
392 if (!modifier)
393 return MODIFIER_SUPER;
394
395 if (!strcmp("ctrl", modifier))
396 return MODIFIER_CTRL;
397 else if (!strcmp("alt", modifier))
398 return MODIFIER_ALT;
399 else if (!strcmp("super", modifier))
400 return MODIFIER_SUPER;
401 else
402 return MODIFIER_SUPER;
403}
404
Juan Zhaoe10d2792012-04-25 19:09:52 +0800405static enum animation_type
406get_animation_type(char *animation)
407{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800408 if (!strcmp("zoom", animation))
409 return ANIMATION_ZOOM;
410 else if (!strcmp("fade", animation))
411 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100412 else if (!strcmp("dim-layer", animation))
413 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800414 else
415 return ANIMATION_NONE;
416}
417
Alex Wu4539b082012-03-01 12:57:46 +0800418static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400419shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200420{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400421 struct weston_config_section *section;
422 int duration;
423 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200424
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400425 section = weston_config_get_section(shell->compositor->config,
426 "screensaver", NULL, NULL);
427 weston_config_section_get_string(section,
428 "path", &shell->screensaver.path, NULL);
429 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200430 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400431
432 section = weston_config_get_section(shell->compositor->config,
433 "shell", NULL, NULL);
434 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100435 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100436 shell->client = s;
437 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400438 "binding-modifier", &s, "super");
439 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200440 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400441 weston_config_section_get_string(section, "animation", &s, "none");
442 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200443 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700444 weston_config_section_get_string(section,
445 "startup-animation", &s, "fade");
446 shell->startup_animation_type = get_animation_type(s);
447 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700448 if (shell->startup_animation_type == ANIMATION_ZOOM)
449 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100450 weston_config_section_get_string(section, "focus-animation", &s, "none");
451 shell->focus_animation_type = get_animation_type(s);
452 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 weston_config_section_get_uint(section, "num-workspaces",
454 &shell->workspaces.num,
455 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200456}
457
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800458struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100459get_default_output(struct weston_compositor *compositor)
460{
461 return container_of(compositor->output_list.next,
462 struct weston_output, link);
463}
464
465
466/* no-op func for checking focus surface */
467static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600468focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100469{
470}
471
472static struct focus_surface *
473get_focus_surface(struct weston_surface *surface)
474{
475 if (surface->configure == focus_surface_configure)
476 return surface->configure_private;
477 else
478 return NULL;
479}
480
481static bool
482is_focus_surface (struct weston_surface *es)
483{
484 return (es->configure == focus_surface_configure);
485}
486
487static bool
488is_focus_view (struct weston_view *view)
489{
490 return is_focus_surface (view->surface);
491}
492
493static struct focus_surface *
494create_focus_surface(struct weston_compositor *ec,
495 struct weston_output *output)
496{
497 struct focus_surface *fsurf = NULL;
498 struct weston_surface *surface = NULL;
499
500 fsurf = malloc(sizeof *fsurf);
501 if (!fsurf)
502 return NULL;
503
504 fsurf->surface = weston_surface_create(ec);
505 surface = fsurf->surface;
506 if (surface == NULL) {
507 free(fsurf);
508 return NULL;
509 }
510
511 surface->configure = focus_surface_configure;
512 surface->output = output;
513 surface->configure_private = fsurf;
514
515 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100516 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100517
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600518 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600519 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100520 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
521 pixman_region32_fini(&surface->opaque);
522 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
523 output->width, output->height);
524 pixman_region32_fini(&surface->input);
525 pixman_region32_init(&surface->input);
526
527 wl_list_init(&fsurf->workspace_transform.link);
528
529 return fsurf;
530}
531
532static void
533focus_surface_destroy(struct focus_surface *fsurf)
534{
535 weston_surface_destroy(fsurf->surface);
536 free(fsurf);
537}
538
539static void
540focus_animation_done(struct weston_view_animation *animation, void *data)
541{
542 struct workspace *ws = data;
543
544 ws->focus_animation = NULL;
545}
546
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200548focus_state_destroy(struct focus_state *state)
549{
550 wl_list_remove(&state->seat_destroy_listener.link);
551 wl_list_remove(&state->surface_destroy_listener.link);
552 free(state);
553}
554
555static void
556focus_state_seat_destroy(struct wl_listener *listener, void *data)
557{
558 struct focus_state *state = container_of(listener,
559 struct focus_state,
560 seat_destroy_listener);
561
562 wl_list_remove(&state->link);
563 focus_state_destroy(state);
564}
565
566static void
567focus_state_surface_destroy(struct wl_listener *listener, void *data)
568{
569 struct focus_state *state = container_of(listener,
570 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400571 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400572 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 struct weston_surface *main_surface, *next;
574 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200575
Pekka Paalanen01388e22013-04-25 13:57:44 +0300576 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
577
Kristian Høgsberge3778222012-07-31 17:29:30 -0400578 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
580 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400581 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100582 if (is_focus_view(view))
583 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400584
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400586 break;
587 }
588
Pekka Paalanen01388e22013-04-25 13:57:44 +0300589 /* if the focus was a sub-surface, activate its main surface */
590 if (main_surface != state->keyboard_focus)
591 next = main_surface;
592
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100593 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400594 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100595 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400596 activate(shell, next, state->seat);
597 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100598 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
599 if (state->ws->focus_animation)
600 weston_view_animation_destroy(state->ws->focus_animation);
601
602 state->ws->focus_animation = weston_fade_run(
603 state->ws->fsurf_front->view,
604 state->ws->fsurf_front->view->alpha, 0.0, 300,
605 focus_animation_done, state->ws);
606 }
607
Kristian Høgsberge3778222012-07-31 17:29:30 -0400608 wl_list_remove(&state->link);
609 focus_state_destroy(state);
610 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200611}
612
613static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400614focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200615{
Jonas Ådahl04769742012-06-13 00:01:24 +0200616 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200617
618 state = malloc(sizeof *state);
619 if (state == NULL)
620 return NULL;
621
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100622 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400623 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200624 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400625 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200626
627 state->seat_destroy_listener.notify = focus_state_seat_destroy;
628 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400629 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200630 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400631 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200632
633 return state;
634}
635
Jonas Ådahl8538b222012-08-29 22:13:03 +0200636static struct focus_state *
637ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
638{
639 struct workspace *ws = get_current_workspace(shell);
640 struct focus_state *state;
641
642 wl_list_for_each(state, &ws->focus_list, link)
643 if (state->seat == seat)
644 break;
645
646 if (&state->link == &ws->focus_list)
647 state = focus_state_create(seat, ws);
648
649 return state;
650}
651
Jonas Ådahl04769742012-06-13 00:01:24 +0200652static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400653restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200654{
655 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400656 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400659 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200660
Kristian Høgsberge3148752013-05-06 23:19:49 -0400661 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200662 }
663}
664
665static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200666replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
667 struct weston_seat *seat)
668{
669 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400670 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200671
672 wl_list_for_each(state, &ws->focus_list, link) {
673 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400674 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500675 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200676 return;
677 }
678 }
679}
680
681static void
682drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
683 struct weston_surface *surface)
684{
685 struct focus_state *state;
686
687 wl_list_for_each(state, &ws->focus_list, link)
688 if (state->keyboard_focus == surface)
689 state->keyboard_focus = NULL;
690}
691
692static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
694 struct weston_view *from, struct weston_view *to)
695{
696 struct weston_output *output;
697 bool focus_surface_created = false;
698
699 /* FIXME: Only support dim animation using two layers */
700 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
701 return;
702
703 output = get_default_output(shell->compositor);
704 if (ws->fsurf_front == NULL && (from || to)) {
705 ws->fsurf_front = create_focus_surface(shell->compositor, output);
706 ws->fsurf_back = create_focus_surface(shell->compositor, output);
707 ws->fsurf_front->view->alpha = 0.0;
708 ws->fsurf_back->view->alpha = 0.0;
709 focus_surface_created = true;
710 } else {
711 wl_list_remove(&ws->fsurf_front->view->layer_link);
712 wl_list_remove(&ws->fsurf_back->view->layer_link);
713 }
714
715 if (ws->focus_animation) {
716 weston_view_animation_destroy(ws->focus_animation);
717 ws->focus_animation = NULL;
718 }
719
720 if (to)
721 wl_list_insert(&to->layer_link,
722 &ws->fsurf_front->view->layer_link);
723 else if (from)
724 wl_list_insert(&ws->layer.view_list,
725 &ws->fsurf_front->view->layer_link);
726
727 if (focus_surface_created) {
728 ws->focus_animation = weston_fade_run(
729 ws->fsurf_front->view,
730 ws->fsurf_front->view->alpha, 0.6, 300,
731 focus_animation_done, ws);
732 } else if (from) {
733 wl_list_insert(&from->layer_link,
734 &ws->fsurf_back->view->layer_link);
735 ws->focus_animation = weston_stable_fade_run(
736 ws->fsurf_front->view, 0.0,
737 ws->fsurf_back->view, 0.6,
738 focus_animation_done, ws);
739 } else if (to) {
740 wl_list_insert(&ws->layer.view_list,
741 &ws->fsurf_back->view->layer_link);
742 ws->focus_animation = weston_stable_fade_run(
743 ws->fsurf_front->view, 0.0,
744 ws->fsurf_back->view, 0.6,
745 focus_animation_done, ws);
746 }
747}
748
749static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200750workspace_destroy(struct workspace *ws)
751{
Jonas Ådahl04769742012-06-13 00:01:24 +0200752 struct focus_state *state, *next;
753
754 wl_list_for_each_safe(state, next, &ws->focus_list, link)
755 focus_state_destroy(state);
756
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 if (ws->fsurf_front)
758 focus_surface_destroy(ws->fsurf_front);
759 if (ws->fsurf_back)
760 focus_surface_destroy(ws->fsurf_back);
761
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200762 free(ws);
763}
764
Jonas Ådahl04769742012-06-13 00:01:24 +0200765static void
766seat_destroyed(struct wl_listener *listener, void *data)
767{
768 struct weston_seat *seat = data;
769 struct focus_state *state, *next;
770 struct workspace *ws = container_of(listener,
771 struct workspace,
772 seat_destroyed_listener);
773
774 wl_list_for_each_safe(state, next, &ws->focus_list, link)
775 if (state->seat == seat)
776 wl_list_remove(&state->link);
777}
778
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200779static struct workspace *
780workspace_create(void)
781{
782 struct workspace *ws = malloc(sizeof *ws);
783 if (ws == NULL)
784 return NULL;
785
786 weston_layer_init(&ws->layer, NULL);
787
Jonas Ådahl04769742012-06-13 00:01:24 +0200788 wl_list_init(&ws->focus_list);
789 wl_list_init(&ws->seat_destroyed_listener.link);
790 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100791 ws->fsurf_front = NULL;
792 ws->fsurf_back = NULL;
793 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200794
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795 return ws;
796}
797
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200798static int
799workspace_is_empty(struct workspace *ws)
800{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200802}
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804static struct workspace *
805get_workspace(struct desktop_shell *shell, unsigned int index)
806{
807 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200808 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200809 pws += index;
810 return *pws;
811}
812
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800813struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200814get_current_workspace(struct desktop_shell *shell)
815{
816 return get_workspace(shell, shell->workspaces.current);
817}
818
819static void
820activate_workspace(struct desktop_shell *shell, unsigned int index)
821{
822 struct workspace *ws;
823
824 ws = get_workspace(shell, index);
825 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
826
827 shell->workspaces.current = index;
828}
829
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200830static unsigned int
831get_output_height(struct weston_output *output)
832{
833 return abs(output->region.extents.y1 - output->region.extents.y2);
834}
835
836static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200838{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200839 struct weston_transform *transform;
840
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 if (is_focus_view(view)) {
842 struct focus_surface *fsurf = get_focus_surface(view->surface);
843 transform = &fsurf->workspace_transform;
844 } else {
845 struct shell_surface *shsurf = get_shell_surface(view->surface);
846 transform = &shsurf->workspace_transform;
847 }
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 weston_matrix_init(&transform->matrix);
854 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857}
858
859static void
860workspace_translate_out(struct workspace *ws, double fraction)
861{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863 unsigned int height;
864 double d;
865
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
867 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 d = height * fraction;
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200871 }
872}
873
874static void
875workspace_translate_in(struct workspace *ws, double fraction)
876{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200878 unsigned int height;
879 double d;
880
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
882 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200883
884 if (fraction > 0)
885 d = -(height - height * fraction);
886 else
887 d = height + height * fraction;
888
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100889 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200890 }
891}
892
893static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894broadcast_current_workspace_state(struct desktop_shell *shell)
895{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700896 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700898 wl_resource_for_each(resource, &shell->workspaces.client_list)
899 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900 shell->workspaces.current,
901 shell->workspaces.num);
902}
903
904static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905reverse_workspace_change_animation(struct desktop_shell *shell,
906 unsigned int index,
907 struct workspace *from,
908 struct workspace *to)
909{
910 shell->workspaces.current = index;
911
912 shell->workspaces.anim_to = to;
913 shell->workspaces.anim_from = from;
914 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
915 shell->workspaces.anim_timestamp = 0;
916
Scott Moreau4272e632012-08-13 09:58:41 -0600917 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200918}
919
920static void
921workspace_deactivate_transforms(struct workspace *ws)
922{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100924 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100927 if (is_focus_view(view)) {
928 struct focus_surface *fsurf = get_focus_surface(view->surface);
929 transform = &fsurf->workspace_transform;
930 } else {
931 struct shell_surface *shsurf = get_shell_surface(view->surface);
932 transform = &shsurf->workspace_transform;
933 }
934
935 if (!wl_list_empty(&transform->link)) {
936 wl_list_remove(&transform->link);
937 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 }
941}
942
943static void
944finish_workspace_change_animation(struct desktop_shell *shell,
945 struct workspace *from,
946 struct workspace *to)
947{
Scott Moreau4272e632012-08-13 09:58:41 -0600948 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949
950 wl_list_remove(&shell->workspaces.animation.link);
951 workspace_deactivate_transforms(from);
952 workspace_deactivate_transforms(to);
953 shell->workspaces.anim_to = NULL;
954
955 wl_list_remove(&shell->workspaces.anim_from->layer.link);
956}
957
958static void
959animate_workspace_change_frame(struct weston_animation *animation,
960 struct weston_output *output, uint32_t msecs)
961{
962 struct desktop_shell *shell =
963 container_of(animation, struct desktop_shell,
964 workspaces.animation);
965 struct workspace *from = shell->workspaces.anim_from;
966 struct workspace *to = shell->workspaces.anim_to;
967 uint32_t t;
968 double x, y;
969
970 if (workspace_is_empty(from) && workspace_is_empty(to)) {
971 finish_workspace_change_animation(shell, from, to);
972 return;
973 }
974
975 if (shell->workspaces.anim_timestamp == 0) {
976 if (shell->workspaces.anim_current == 0.0)
977 shell->workspaces.anim_timestamp = msecs;
978 else
979 shell->workspaces.anim_timestamp =
980 msecs -
981 /* Invers of movement function 'y' below. */
982 (asin(1.0 - shell->workspaces.anim_current) *
983 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
984 M_2_PI);
985 }
986
987 t = msecs - shell->workspaces.anim_timestamp;
988
989 /*
990 * x = [0, π/2]
991 * y(x) = sin(x)
992 */
993 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
994 y = sin(x);
995
996 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1000 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1001 shell->workspaces.anim_current = y;
1002
Scott Moreau4272e632012-08-13 09:58:41 -06001003 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001005 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007}
1008
1009static void
1010animate_workspace_change(struct desktop_shell *shell,
1011 unsigned int index,
1012 struct workspace *from,
1013 struct workspace *to)
1014{
1015 struct weston_output *output;
1016
1017 int dir;
1018
1019 if (index > shell->workspaces.current)
1020 dir = -1;
1021 else
1022 dir = 1;
1023
1024 shell->workspaces.current = index;
1025
1026 shell->workspaces.anim_dir = dir;
1027 shell->workspaces.anim_from = from;
1028 shell->workspaces.anim_to = to;
1029 shell->workspaces.anim_current = 0.0;
1030 shell->workspaces.anim_timestamp = 0;
1031
1032 output = container_of(shell->compositor->output_list.next,
1033 struct weston_output, link);
1034 wl_list_insert(&output->animation_list,
1035 &shell->workspaces.animation.link);
1036
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001037 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038
1039 workspace_translate_in(to, 0);
1040
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001041 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001042
Scott Moreau4272e632012-08-13 09:58:41 -06001043 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044}
1045
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001046static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001047update_workspace(struct desktop_shell *shell, unsigned int index,
1048 struct workspace *from, struct workspace *to)
1049{
1050 shell->workspaces.current = index;
1051 wl_list_insert(&from->layer.link, &to->layer.link);
1052 wl_list_remove(&from->layer.link);
1053}
1054
1055static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001056change_workspace(struct desktop_shell *shell, unsigned int index)
1057{
1058 struct workspace *from;
1059 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001060 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001061
1062 if (index == shell->workspaces.current)
1063 return;
1064
1065 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067 return;
1068
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001069 from = get_current_workspace(shell);
1070 to = get_workspace(shell, index);
1071
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072 if (shell->workspaces.anim_from == to &&
1073 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001074 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001076 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001077 return;
1078 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080 if (shell->workspaces.anim_to != NULL)
1081 finish_workspace_change_animation(shell,
1082 shell->workspaces.anim_from,
1083 shell->workspaces.anim_to);
1084
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001085 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001086
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001087 if (shell->focus_animation_type != ANIMATION_NONE) {
1088 wl_list_for_each(state, &from->focus_list, link)
1089 if (state->keyboard_focus)
1090 animate_focus_change(shell, from,
1091 get_default_view(state->keyboard_focus), NULL);
1092
1093 wl_list_for_each(state, &to->focus_list, link)
1094 if (state->keyboard_focus)
1095 animate_focus_change(shell, to,
1096 NULL, get_default_view(state->keyboard_focus));
1097 }
1098
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001099 if (workspace_is_empty(to) && workspace_is_empty(from))
1100 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 else
1102 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001103
1104 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001105}
1106
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001107static bool
1108workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1109{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001111 struct wl_list *e;
1112
1113 if (wl_list_empty(list))
1114 return false;
1115
1116 e = list->next;
1117
1118 if (e->next != list)
1119 return false;
1120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001122}
1123
1124static void
Philip Withnall659163d2013-11-25 18:01:36 +00001125move_surface_to_workspace(struct desktop_shell *shell,
1126 struct shell_surface *shsurf,
1127 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001128{
1129 struct workspace *from;
1130 struct workspace *to;
1131 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001132 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001133 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001134
1135 if (workspace == shell->workspaces.current)
1136 return;
1137
Philip Withnall659163d2013-11-25 18:01:36 +00001138 view = get_default_view(shsurf->surface);
1139 if (!view)
1140 return;
1141
1142 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1143
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001144 if (workspace >= shell->workspaces.num)
1145 workspace = shell->workspaces.num - 1;
1146
Jonas Ådahle9d22502012-08-29 22:13:01 +02001147 from = get_current_workspace(shell);
1148 to = get_workspace(shell, workspace);
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 wl_list_remove(&view->layer_link);
1151 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001152
Philip Withnall648a4dd2013-11-25 18:01:44 +00001153 shell_surface_update_child_surface_layers(shsurf);
1154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001156 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1157 if (!seat->keyboard)
1158 continue;
1159
1160 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001162 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001163 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001166}
1167
1168static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001169take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001170 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171 unsigned int index)
1172{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001173 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175 struct shell_surface *shsurf;
1176 struct workspace *from;
1177 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001178 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179
Pekka Paalanen01388e22013-04-25 13:57:44 +03001180 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view = get_default_view(surface);
1182 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 index == shell->workspaces.current ||
1184 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185 return;
1186
1187 from = get_current_workspace(shell);
1188 to = get_workspace(shell, index);
1189
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 wl_list_remove(&view->layer_link);
1191 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192
Philip Withnall659163d2013-11-25 18:01:36 +00001193 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001194 if (shsurf != NULL)
1195 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001196
Jonas Ådahle9d22502012-08-29 22:13:01 +02001197 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 drop_focus_state(shell, from, surface);
1199
1200 if (shell->workspaces.anim_from == to &&
1201 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001202 wl_list_remove(&to->layer.link);
1203 wl_list_insert(from->layer.link.prev, &to->layer.link);
1204
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001205 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206 broadcast_current_workspace_state(shell);
1207
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208 return;
1209 }
1210
1211 if (shell->workspaces.anim_to != NULL)
1212 finish_workspace_change_animation(shell,
1213 shell->workspaces.anim_from,
1214 shell->workspaces.anim_to);
1215
1216 if (workspace_is_empty(from) &&
1217 workspace_has_only(to, surface))
1218 update_workspace(shell, index, from, to);
1219 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001220 if (shsurf != NULL &&
1221 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222 wl_list_insert(&shell->workspaces.anim_sticky_list,
1223 &shsurf->workspace_transform.link);
1224
1225 animate_workspace_change(shell, index, from, to);
1226 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001227
1228 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001229
1230 state = ensure_focus_state(shell, seat);
1231 if (state != NULL)
1232 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001233}
1234
1235static void
1236workspace_manager_move_surface(struct wl_client *client,
1237 struct wl_resource *resource,
1238 struct wl_resource *surface_resource,
1239 uint32_t workspace)
1240{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001241 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001242 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001243 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001244 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001245 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246
Pekka Paalanen01388e22013-04-25 13:57:44 +03001247 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001248 shell_surface = get_shell_surface(main_surface);
1249 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001251
1252 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001253}
1254
1255static const struct workspace_manager_interface workspace_manager_implementation = {
1256 workspace_manager_move_surface,
1257};
1258
1259static void
1260unbind_resource(struct wl_resource *resource)
1261{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001262 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001263}
1264
1265static void
1266bind_workspace_manager(struct wl_client *client,
1267 void *data, uint32_t version, uint32_t id)
1268{
1269 struct desktop_shell *shell = data;
1270 struct wl_resource *resource;
1271
Jason Ekstranda85118c2013-06-27 20:17:02 -05001272 resource = wl_resource_create(client,
1273 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001274
1275 if (resource == NULL) {
1276 weston_log("couldn't add workspace manager object");
1277 return;
1278 }
1279
Jason Ekstranda85118c2013-06-27 20:17:02 -05001280 wl_resource_set_implementation(resource,
1281 &workspace_manager_implementation,
1282 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001283 wl_list_insert(&shell->workspaces.client_list,
1284 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001285
1286 workspace_manager_send_state(resource,
1287 shell->workspaces.current,
1288 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289}
1290
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001291static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001292touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1293 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1294{
1295}
1296
1297static void
1298touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1299{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001300 struct weston_touch_move_grab *move =
1301 (struct weston_touch_move_grab *) container_of(
1302 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001303
Jonas Ådahl9484b692013-12-02 22:05:03 +01001304 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001305 shell_touch_grab_end(&move->base);
1306 free(move);
1307 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001308}
1309
1310static void
1311touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1312 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1313{
1314 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1315 struct shell_surface *shsurf = move->base.shsurf;
1316 struct weston_surface *es;
1317 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1318 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1319
1320 if (!shsurf)
1321 return;
1322
1323 es = shsurf->surface;
1324
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001325 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001326
1327 weston_compositor_schedule_repaint(es->compositor);
1328}
1329
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001330static void
1331touch_move_grab_cancel(struct weston_touch_grab *grab)
1332{
1333 struct weston_touch_move_grab *move =
1334 (struct weston_touch_move_grab *) container_of(
1335 grab, struct shell_touch_grab, grab);
1336
1337 shell_touch_grab_end(&move->base);
1338 free(move);
1339}
1340
Rusty Lynch1084da52013-08-15 09:10:08 -07001341static const struct weston_touch_grab_interface touch_move_grab_interface = {
1342 touch_move_grab_down,
1343 touch_move_grab_up,
1344 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001345 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001346};
1347
1348static int
1349surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1350{
1351 struct weston_touch_move_grab *move;
1352
1353 if (!shsurf)
1354 return -1;
1355
Rafael Antognolli03b16592013-12-03 15:35:42 -02001356 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001357 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001358 if (shsurf->grabbed)
1359 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001360
1361 move = malloc(sizeof *move);
1362 if (!move)
1363 return -1;
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001366 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001368 seat->touch->grab_y;
1369
1370 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1371 seat->touch);
1372
1373 return 0;
1374}
1375
1376static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001377noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001378{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001379}
1380
1381static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001382move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1383 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001386 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001387 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001388 int dx, dy;
1389
1390 weston_pointer_move(pointer, x, y);
1391 dx = wl_fixed_to_int(pointer->x + move->dx);
1392 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001393
1394 if (!shsurf)
1395 return;
1396
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001397 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001400}
1401
1402static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001403move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001404 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001405{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001406 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1407 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001408 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001409 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001410
Daniel Stone4dbadb12012-05-30 16:31:51 +01001411 if (pointer->button_count == 0 &&
1412 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001413 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001414 free(grab);
1415 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001416}
1417
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001418static void
1419move_grab_cancel(struct weston_pointer_grab *grab)
1420{
1421 struct shell_grab *shell_grab =
1422 container_of(grab, struct shell_grab, grab);
1423
1424 shell_grab_end(shell_grab);
1425 free(grab);
1426}
1427
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001428static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001429 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001430 move_grab_motion,
1431 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001432 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433};
1434
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001435static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001436surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001437{
1438 struct weston_move_grab *move;
1439
1440 if (!shsurf)
1441 return -1;
1442
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001443 if (shsurf->grabbed)
1444 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001445 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001446 return 0;
1447
1448 move = malloc(sizeof *move);
1449 if (!move)
1450 return -1;
1451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001453 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001456
1457 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001458 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001459
1460 return 0;
1461}
1462
1463static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001464common_surface_move(struct wl_resource *resource,
1465 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001466{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001467 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001468 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001469 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001470
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001471 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001472 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001473 seat->pointer->button_count > 0 &&
1474 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001476 if ((surface == shsurf->surface) &&
1477 (surface_move(shsurf, seat) < 0))
1478 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001479 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001480 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001481 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001483 if ((surface == shsurf->surface) &&
1484 (surface_touch_move(shsurf, seat) < 0))
1485 wl_resource_post_no_memory(resource);
1486 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001487}
1488
Rafael Antognollie2a34552013-12-03 15:35:45 -02001489static void
1490shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1491 struct wl_resource *seat_resource, uint32_t serial)
1492{
1493 common_surface_move(resource, seat_resource, serial);
1494}
1495
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001496struct weston_resize_grab {
1497 struct shell_grab base;
1498 uint32_t edges;
1499 int32_t width, height;
1500};
1501
1502static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001503resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1504 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001505{
1506 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001507 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001508 struct shell_surface *shsurf = resize->base.shsurf;
1509 int32_t width, height;
1510 wl_fixed_t from_x, from_y;
1511 wl_fixed_t to_x, to_y;
1512
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001513 weston_pointer_move(pointer, x, y);
1514
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001515 if (!shsurf)
1516 return;
1517
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 weston_view_from_global_fixed(shsurf->view,
1519 pointer->grab_x, pointer->grab_y,
1520 &from_x, &from_y);
1521 weston_view_from_global_fixed(shsurf->view,
1522 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523
1524 width = resize->width;
1525 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1526 width += wl_fixed_to_int(from_x - to_x);
1527 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1528 width += wl_fixed_to_int(to_x - from_x);
1529 }
1530
1531 height = resize->height;
1532 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1533 height += wl_fixed_to_int(from_y - to_y);
1534 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1535 height += wl_fixed_to_int(to_y - from_y);
1536 }
1537
1538 shsurf->client->send_configure(shsurf->surface,
1539 resize->edges, width, height);
1540}
1541
1542static void
1543send_configure(struct weston_surface *surface,
1544 uint32_t edges, int32_t width, int32_t height)
1545{
1546 struct shell_surface *shsurf = get_shell_surface(surface);
1547
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001548 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549 edges, width, height);
1550}
1551
1552static const struct weston_shell_client shell_client = {
1553 send_configure
1554};
1555
1556static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001557resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558 uint32_t time, uint32_t button, uint32_t state_w)
1559{
1560 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001561 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562 enum wl_pointer_button_state state = state_w;
1563
1564 if (pointer->button_count == 0 &&
1565 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1566 shell_grab_end(&resize->base);
1567 free(grab);
1568 }
1569}
1570
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001571static void
1572resize_grab_cancel(struct weston_pointer_grab *grab)
1573{
1574 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1575
1576 shell_grab_end(&resize->base);
1577 free(grab);
1578}
1579
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001580static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 noop_grab_focus,
1582 resize_grab_motion,
1583 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001584 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585};
1586
Giulio Camuffob8366642013-04-25 13:57:46 +03001587/*
1588 * Returns the bounding box of a surface and all its sub-surfaces,
1589 * in the surface coordinates system. */
1590static void
1591surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1592 int32_t *y, int32_t *w, int32_t *h) {
1593 pixman_region32_t region;
1594 pixman_box32_t *box;
1595 struct weston_subsurface *subsurface;
1596
1597 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 surface->width,
1599 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001600
1601 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1602 pixman_region32_union_rect(&region, &region,
1603 subsurface->position.x,
1604 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 subsurface->surface->width,
1606 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001607 }
1608
1609 box = pixman_region32_extents(&region);
1610 if (x)
1611 *x = box->x1;
1612 if (y)
1613 *y = box->y1;
1614 if (w)
1615 *w = box->x2 - box->x1;
1616 if (h)
1617 *h = box->y2 - box->y1;
1618
1619 pixman_region32_fini(&region);
1620}
1621
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622static int
1623surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001624 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625{
1626 struct weston_resize_grab *resize;
1627
Rafael Antognolli03b16592013-12-03 15:35:42 -02001628 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629 return 0;
1630
1631 if (edges == 0 || edges > 15 ||
1632 (edges & 3) == 3 || (edges & 12) == 12)
1633 return 0;
1634
1635 resize = malloc(sizeof *resize);
1636 if (!resize)
1637 return -1;
1638
1639 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001640 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1641 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642
1643 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001644 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645
1646 return 0;
1647}
1648
1649static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001650common_surface_resize(struct wl_resource *resource,
1651 struct wl_resource *seat_resource, uint32_t serial,
1652 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001654 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001655 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001656 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657
Rafael Antognolli03b16592013-12-03 15:35:42 -02001658 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659 return;
1660
Kristian Høgsberge3148752013-05-06 23:19:49 -04001661 if (seat->pointer->button_count == 0 ||
1662 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001663 seat->pointer->focus == NULL)
1664 return;
1665
1666 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1667 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668 return;
1669
Kristian Høgsberge3148752013-05-06 23:19:49 -04001670 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 wl_resource_post_no_memory(resource);
1672}
1673
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001674static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001675shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1676 struct wl_resource *seat_resource, uint32_t serial,
1677 uint32_t edges)
1678{
1679 common_surface_resize(resource, seat_resource, serial, edges);
1680}
1681
1682static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001683end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1684
1685static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001686busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001687{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001688 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001689 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001691 wl_fixed_t sx, sy;
1692
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 view = weston_compositor_pick_view(pointer->seat->compositor,
1694 pointer->x, pointer->y,
1695 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001696
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001698 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001699}
1700
1701static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001702busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1703 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001704{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001705 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001706}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001707
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001708static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001709busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001710 uint32_t time, uint32_t button, uint32_t state)
1711{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001712 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001713 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001714 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001715
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001716 if (shsurf && button == BTN_LEFT && state) {
1717 activate(shsurf->shell, shsurf->surface, seat);
1718 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001719 } else if (shsurf && button == BTN_RIGHT && state) {
1720 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001721 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001722 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001723}
1724
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001725static void
1726busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1727{
1728 struct shell_grab *grab = (struct shell_grab *) base;
1729
1730 shell_grab_end(grab);
1731 free(grab);
1732}
1733
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001734static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001735 busy_cursor_grab_focus,
1736 busy_cursor_grab_motion,
1737 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001738 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739};
1740
1741static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001742set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743{
1744 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001745
1746 grab = malloc(sizeof *grab);
1747 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748 return;
1749
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001750 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1751 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001752}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001755end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756{
1757 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1758
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001759 if (grab->grab.interface == &busy_cursor_grab_interface &&
1760 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001761 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764}
1765
Scott Moreau9521d5e2012-04-19 13:06:17 -06001766static void
1767ping_timer_destroy(struct shell_surface *shsurf)
1768{
1769 if (!shsurf || !shsurf->ping_timer)
1770 return;
1771
1772 if (shsurf->ping_timer->source)
1773 wl_event_source_remove(shsurf->ping_timer->source);
1774
1775 free(shsurf->ping_timer);
1776 shsurf->ping_timer = NULL;
1777}
1778
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001779static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001780ping_timeout_handler(void *data)
1781{
1782 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001784
Scott Moreau9521d5e2012-04-19 13:06:17 -06001785 /* Client is not responding */
1786 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787
1788 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001789 if (seat->pointer->focus &&
1790 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001791 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001792
1793 return 1;
1794}
1795
1796static void
1797ping_handler(struct weston_surface *surface, uint32_t serial)
1798{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001799 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001800 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001802
1803 if (!shsurf)
1804 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001805 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001806 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001807
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001808 if (shsurf->surface == shsurf->shell->grab_surface)
1809 return;
1810
Scott Moreauff1db4a2012-04-17 19:06:18 -06001811 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001812 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001813 if (!shsurf->ping_timer)
1814 return;
1815
1816 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001817 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1818 shsurf->ping_timer->source =
1819 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1820 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1821
Rafael Antognollie2a34552013-12-03 15:35:45 -02001822 if (shell_surface_is_wl_shell_surface(shsurf))
1823 wl_shell_surface_send_ping(shsurf->resource, serial);
1824 else if (shell_surface_is_xdg_surface(shsurf))
1825 xdg_surface_send_ping(shsurf->resource, serial);
1826 else if (shell_surface_is_xdg_popup(shsurf))
1827 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001828 }
1829}
1830
1831static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832handle_pointer_focus(struct wl_listener *listener, void *data)
1833{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001834 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 struct weston_compositor *compositor;
1837 struct shell_surface *shsurf;
1838 uint32_t serial;
1839
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001841 return;
1842
Jason Ekstranda7af7042013-10-12 22:38:11 -05001843 compositor = view->surface->compositor;
1844 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001846 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001847 set_busy_cursor(shsurf, pointer);
1848 } else {
1849 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851 }
1852}
1853
1854static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001855create_pointer_focus_listener(struct weston_seat *seat)
1856{
1857 struct wl_listener *listener;
1858
Kristian Høgsberge3148752013-05-06 23:19:49 -04001859 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001860 return;
1861
1862 listener = malloc(sizeof *listener);
1863 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001864 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001865}
1866
1867static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001868xdg_surface_set_transient_for(struct wl_client *client,
1869 struct wl_resource *resource,
1870 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001871{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001872 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001873 struct weston_surface *parent;
1874
1875 if (parent_resource)
1876 parent = wl_resource_get_user_data(parent_resource);
1877 else
1878 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001879
1880 shell_surface_set_parent(shsurf, parent);
1881}
1882
1883static void
1884surface_pong(struct shell_surface *shsurf, uint32_t serial)
1885{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001886 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001887 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001889 if (shsurf->ping_timer == NULL)
1890 /* Just ignore unsolicited pong. */
1891 return;
1892
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001895 wl_list_for_each(seat, &ec->seat_list, link) {
1896 if(seat->pointer)
1897 end_busy_cursor(shsurf, seat->pointer);
1898 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001899 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900 }
1901}
1902
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001903static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001904shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1905 uint32_t serial)
1906{
1907 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1908
1909 surface_pong(shsurf, serial);
1910
1911}
1912
1913static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001914set_title(struct shell_surface *shsurf, const char *title)
1915{
1916 free(shsurf->title);
1917 shsurf->title = strdup(title);
1918}
1919
1920static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001921shell_surface_set_title(struct wl_client *client,
1922 struct wl_resource *resource, const char *title)
1923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001925
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001926 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001927}
1928
1929static void
1930shell_surface_set_class(struct wl_client *client,
1931 struct wl_resource *resource, const char *class)
1932{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001934
1935 free(shsurf->class);
1936 shsurf->class = strdup(class);
1937}
1938
Alex Wu4539b082012-03-01 12:57:46 +08001939static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001940restore_output_mode(struct weston_output *output)
1941{
Hardening57388e42013-09-18 23:56:36 +02001942 if (output->current_mode != output->original_mode ||
1943 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001944 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001945 output->original_mode,
1946 output->original_scale,
1947 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001948}
1949
1950static void
1951restore_all_output_modes(struct weston_compositor *compositor)
1952{
1953 struct weston_output *output;
1954
1955 wl_list_for_each(output, &compositor->output_list, link)
1956 restore_output_mode(output);
1957}
1958
Philip Withnallbecb77e2013-11-25 18:01:30 +00001959static int
1960get_output_panel_height(struct desktop_shell *shell,
1961 struct weston_output *output)
1962{
1963 struct weston_view *view;
1964 int panel_height = 0;
1965
1966 if (!output)
1967 return 0;
1968
1969 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1970 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001971 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001972 break;
1973 }
1974 }
1975
1976 return panel_height;
1977}
1978
Philip Withnall07926d92013-11-25 18:01:40 +00001979/* The surface will be inserted into the list immediately after the link
1980 * returned by this function (i.e. will be stacked immediately above the
1981 * returned link). */
1982static struct wl_list *
1983shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1984{
1985 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08001986 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00001987
1988 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08001989 case SHELL_SURFACE_POPUP:
1990 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02001991 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02001992 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02001993 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08001994 /* Move the surface to its parent layer so
1995 * that surfaces which are transient for
1996 * fullscreen surfaces don't get hidden by the
1997 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02001998
1999 /* TODO: Handle a parent with multiple views */
2000 parent = get_default_view(shsurf->parent);
2001 if (parent)
2002 return parent->layer_link.prev;
2003 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002004 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002005
2006 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002007 return &shsurf->shell->fullscreen_layer.view_list;
2008
Philip Withnall07926d92013-11-25 18:01:40 +00002009 case SHELL_SURFACE_NONE:
2010 default:
2011 /* Go to the fallback, below. */
2012 break;
2013 }
2014
2015 /* Move the surface to a normal workspace layer so that surfaces
2016 * which were previously fullscreen or transient are no longer
2017 * rendered on top. */
2018 ws = get_current_workspace(shsurf->shell);
2019 return &ws->layer.view_list;
2020}
2021
Philip Withnall648a4dd2013-11-25 18:01:44 +00002022static void
2023shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2024{
2025 struct shell_surface *child;
2026
2027 /* Move the child layers to the same workspace as shsurf. They will be
2028 * stacked above shsurf. */
2029 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2030 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2031 weston_view_geometry_dirty(child->view);
2032 wl_list_remove(&child->view->layer_link);
2033 wl_list_insert(shsurf->view->layer_link.prev,
2034 &child->view->layer_link);
2035 weston_view_geometry_dirty(child->view);
2036 weston_surface_damage(child->surface);
2037
2038 /* Recurse. We don’t expect this to recurse very far (if
2039 * at all) because that would imply we have transient
2040 * (or popup) children of transient surfaces, which
2041 * would be unusual. */
2042 shell_surface_update_child_surface_layers(child);
2043 }
2044 }
2045}
2046
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002047/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002048 * geometry to ensure the changes are redrawn.
2049 *
2050 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2051 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002052static void
2053shell_surface_update_layer(struct shell_surface *shsurf)
2054{
2055 struct wl_list *new_layer_link;
2056
2057 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2058
2059 if (new_layer_link == &shsurf->view->layer_link)
2060 return;
2061
2062 weston_view_geometry_dirty(shsurf->view);
2063 wl_list_remove(&shsurf->view->layer_link);
2064 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2065 weston_view_geometry_dirty(shsurf->view);
2066 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002067
2068 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002069}
2070
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002071static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002072shell_surface_set_parent(struct shell_surface *shsurf,
2073 struct weston_surface *parent)
2074{
2075 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002076
2077 wl_list_remove(&shsurf->children_link);
2078 wl_list_init(&shsurf->children_link);
2079
2080 /* Insert into the parent surface’s child list. */
2081 if (parent != NULL) {
2082 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2083 if (parent_shsurf != NULL)
2084 wl_list_insert(&parent_shsurf->children_list,
2085 &shsurf->children_link);
2086 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002087}
2088
2089static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002090shell_surface_set_output(struct shell_surface *shsurf,
2091 struct weston_output *output)
2092{
2093 struct weston_surface *es = shsurf->surface;
2094
2095 /* get the default output, if the client set it as NULL
2096 check whether the ouput is available */
2097 if (output)
2098 shsurf->output = output;
2099 else if (es->output)
2100 shsurf->output = es->output;
2101 else
2102 shsurf->output = get_default_output(es->compositor);
2103}
2104
2105static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002106surface_clear_next_states(struct shell_surface *shsurf)
2107{
2108 shsurf->next_state.maximized = false;
2109 shsurf->next_state.fullscreen = false;
2110
2111 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2112 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2113 shsurf->state_changed = true;
2114}
2115
2116static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002117set_toplevel(struct shell_surface *shsurf)
2118{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002119 shell_surface_set_parent(shsurf, NULL);
2120 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002121 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002122
2123 /* The layer_link is updated in set_surface_type(),
2124 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125}
2126
2127static void
2128shell_surface_set_toplevel(struct wl_client *client,
2129 struct wl_resource *resource)
2130{
2131 struct shell_surface *surface = wl_resource_get_user_data(resource);
2132
2133 set_toplevel(surface);
2134}
2135
2136static void
2137set_transient(struct shell_surface *shsurf,
2138 struct weston_surface *parent, int x, int y, uint32_t flags)
2139{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002140 assert(parent != NULL);
2141
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002142 shell_surface_set_parent(shsurf, parent);
2143
2144 surface_clear_next_states(shsurf);
2145
Philip Withnallbecb77e2013-11-25 18:01:30 +00002146 shsurf->transient.x = x;
2147 shsurf->transient.y = y;
2148 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002149
Rafael Antognollied207b42013-12-03 15:35:43 -02002150 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002151 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002152 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002153
2154 /* The layer_link is updated in set_surface_type(),
2155 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002156}
2157
2158static void
2159shell_surface_set_transient(struct wl_client *client,
2160 struct wl_resource *resource,
2161 struct wl_resource *parent_resource,
2162 int x, int y, uint32_t flags)
2163{
2164 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2165 struct weston_surface *parent =
2166 wl_resource_get_user_data(parent_resource);
2167
2168 set_transient(shsurf, parent, x, y, flags);
2169}
2170
2171static void
2172set_fullscreen(struct shell_surface *shsurf,
2173 uint32_t method,
2174 uint32_t framerate,
2175 struct weston_output *output)
2176{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002177 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002178
2179 shsurf->fullscreen_output = shsurf->output;
2180 shsurf->fullscreen.type = method;
2181 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002182
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002183 shsurf->next_state.fullscreen = true;
2184 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002185 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002186
2187 shsurf->client->send_configure(shsurf->surface, 0,
2188 shsurf->output->width,
2189 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002190
2191 /* The layer_link is updated in set_surface_type(),
2192 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002193}
2194
2195static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002196weston_view_set_initial_position(struct weston_view *view,
2197 struct desktop_shell *shell);
2198
2199static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002200unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002201{
Philip Withnallf85fe842013-11-25 18:01:37 +00002202 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002203 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2204 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002205 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002206 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002207 shsurf->fullscreen_output = NULL;
2208
Alex Wu4539b082012-03-01 12:57:46 +08002209 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2210 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002211
Alex Wu4539b082012-03-01 12:57:46 +08002212 wl_list_remove(&shsurf->fullscreen.transform.link);
2213 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 if (shsurf->fullscreen.black_view)
2216 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2217 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002218
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002219 if (shsurf->saved_position_valid)
2220 weston_view_set_position(shsurf->view,
2221 shsurf->saved_x, shsurf->saved_y);
2222 else
2223 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2224
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002225 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002227 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002228 shsurf->saved_rotation_valid = false;
2229 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002230
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002231 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002232}
2233
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002234static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002235shell_surface_set_fullscreen(struct wl_client *client,
2236 struct wl_resource *resource,
2237 uint32_t method,
2238 uint32_t framerate,
2239 struct wl_resource *output_resource)
2240{
2241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2242 struct weston_output *output;
2243
2244 if (output_resource)
2245 output = wl_resource_get_user_data(output_resource);
2246 else
2247 output = NULL;
2248
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002249 shell_surface_set_parent(shsurf, NULL);
2250
Rafael Antognolli03b16592013-12-03 15:35:42 -02002251 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002252 set_fullscreen(shsurf, method, framerate, output);
2253}
2254
2255static void
2256set_popup(struct shell_surface *shsurf,
2257 struct weston_surface *parent,
2258 struct weston_seat *seat,
2259 uint32_t serial,
2260 int32_t x,
2261 int32_t y)
2262{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002263 assert(parent != NULL);
2264
Philip Withnallbecb77e2013-11-25 18:01:30 +00002265 shsurf->popup.shseat = get_shell_seat(seat);
2266 shsurf->popup.serial = serial;
2267 shsurf->popup.x = x;
2268 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002269
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002270 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002271}
2272
2273static void
2274shell_surface_set_popup(struct wl_client *client,
2275 struct wl_resource *resource,
2276 struct wl_resource *seat_resource,
2277 uint32_t serial,
2278 struct wl_resource *parent_resource,
2279 int32_t x, int32_t y, uint32_t flags)
2280{
2281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002282 struct weston_surface *parent =
2283 wl_resource_get_user_data(parent_resource);
2284
2285 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002286
Rafael Antognolli03b16592013-12-03 15:35:42 -02002287 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002288 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002289 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002290 wl_resource_get_user_data(seat_resource),
2291 serial, x, y);
2292}
2293
2294static void
2295set_maximized(struct shell_surface *shsurf,
2296 struct weston_output *output)
2297{
2298 struct desktop_shell *shell;
2299 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300
Philip Withnall352e7ed2013-11-25 18:01:35 +00002301 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302
2303 shell = shell_surface_get_shell(shsurf);
2304 panel_height = get_output_panel_height(shell, shsurf->output);
2305 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2306
2307 shsurf->client->send_configure(shsurf->surface, edges,
2308 shsurf->output->width,
2309 shsurf->output->height - panel_height);
2310
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002311 shsurf->next_state.maximized = true;
2312 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002313 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314}
2315
2316static void
2317unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002318{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002319 /* undo all maximized things here */
2320 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002321
2322 if (shsurf->saved_position_valid)
2323 weston_view_set_position(shsurf->view,
2324 shsurf->saved_x, shsurf->saved_y);
2325 else
2326 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002327
2328 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 wl_list_insert(&shsurf->view->geometry.transformation_list,
2330 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002331 shsurf->saved_rotation_valid = false;
2332 }
2333
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002334 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002335}
2336
Philip Withnallbecb77e2013-11-25 18:01:30 +00002337static void
2338shell_surface_set_maximized(struct wl_client *client,
2339 struct wl_resource *resource,
2340 struct wl_resource *output_resource)
2341{
2342 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2343 struct weston_output *output;
2344
2345 if (output_resource)
2346 output = wl_resource_get_user_data(output_resource);
2347 else
2348 output = NULL;
2349
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002350 shell_surface_set_parent(shsurf, NULL);
2351
Rafael Antognolli03b16592013-12-03 15:35:42 -02002352 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 set_maximized(shsurf, output);
2354}
2355
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002356/* This is only ever called from set_surface_type(), so there’s no need to
2357 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002358static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002360{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002361 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002362 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002363 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002364 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002365
Pekka Paalanen98262232011-12-01 10:42:22 +02002366 return 0;
2367}
2368
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002369static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002370set_full_output(struct shell_surface *shsurf)
2371{
2372 shsurf->saved_x = shsurf->view->geometry.x;
2373 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002374 shsurf->saved_width = shsurf->surface->width;
2375 shsurf->saved_height = shsurf->surface->height;
2376 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002377 shsurf->saved_position_valid = true;
2378
2379 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2380 wl_list_remove(&shsurf->rotation.transform.link);
2381 wl_list_init(&shsurf->rotation.transform.link);
2382 weston_view_geometry_dirty(shsurf->view);
2383 shsurf->saved_rotation_valid = true;
2384 }
2385}
2386
2387static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002388set_surface_type(struct shell_surface *shsurf)
2389{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002390 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002392
Philip Withnallbecb77e2013-11-25 18:01:30 +00002393 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002394
Rafael Antognolli03b16592013-12-03 15:35:42 -02002395 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002396 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002397
2398 switch (shsurf->type) {
2399 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002400 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002401 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002402 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 weston_view_set_position(shsurf->view,
2404 pev->geometry.x + shsurf->transient.x,
2405 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002406 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002407 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002408
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002409 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 weston_view_set_position(shsurf->view, shsurf->transient.x,
2411 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002412 break;
2413
Philip Withnall0f640e22013-11-25 18:01:31 +00002414 case SHELL_SURFACE_POPUP:
2415 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002416 default:
2417 break;
2418 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419
2420 /* Update the surface’s layer. */
2421 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002422}
2423
Tiago Vignattibe143262012-04-16 17:31:41 +03002424static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002425shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002426{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002427 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002428}
2429
Alex Wu21858432012-04-01 20:13:08 +08002430static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002431black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002432
Jason Ekstranda7af7042013-10-12 22:38:11 -05002433static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002434create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002435 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002436 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002437{
2438 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002440
2441 surface = weston_surface_create(ec);
2442 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002443 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002444 return NULL;
2445 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446 view = weston_view_create(surface);
2447 if (surface == NULL) {
2448 weston_log("no memory\n");
2449 weston_surface_destroy(surface);
2450 return NULL;
2451 }
Alex Wu4539b082012-03-01 12:57:46 +08002452
Alex Wu21858432012-04-01 20:13:08 +08002453 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002454 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002455 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002456 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002457 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002458 pixman_region32_fini(&surface->input);
2459 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002460
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002461 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002462 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463
2464 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002465}
2466
Philip Withnalled8f1a92013-11-25 18:01:43 +00002467static void
2468shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2469{
2470 struct weston_output *output = shsurf->fullscreen_output;
2471
Rafael Antognolli03b16592013-12-03 15:35:42 -02002472 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002473
2474 if (!shsurf->fullscreen.black_view)
2475 shsurf->fullscreen.black_view =
2476 create_black_surface(shsurf->surface->compositor,
2477 shsurf->surface,
2478 output->x, output->y,
2479 output->width,
2480 output->height);
2481
2482 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2483 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2484 wl_list_insert(&shsurf->view->layer_link,
2485 &shsurf->fullscreen.black_view->layer_link);
2486 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2487 weston_surface_damage(shsurf->surface);
2488}
2489
Alex Wu4539b082012-03-01 12:57:46 +08002490/* Create black surface and append it to the associated fullscreen surface.
2491 * Handle size dismatch and positioning according to the method. */
2492static void
2493shell_configure_fullscreen(struct shell_surface *shsurf)
2494{
2495 struct weston_output *output = shsurf->fullscreen_output;
2496 struct weston_surface *surface = shsurf->surface;
2497 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002498 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002499 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002500
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002501 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2502 restore_output_mode(output);
2503
Philip Withnalled8f1a92013-11-25 18:01:43 +00002504 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002505
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002507 &surf_width, &surf_height);
2508
Alex Wu4539b082012-03-01 12:57:46 +08002509 switch (shsurf->fullscreen.type) {
2510 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002511 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002513 break;
2514 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002515 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002516 if (output->width == surf_width &&
2517 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002518 weston_view_set_position(shsurf->view,
2519 output->x - surf_x,
2520 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002521 break;
2522 }
2523
Alex Wu4539b082012-03-01 12:57:46 +08002524 matrix = &shsurf->fullscreen.transform.matrix;
2525 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002526
Scott Moreau1bad5db2012-08-18 01:04:05 -06002527 output_aspect = (float) output->width /
2528 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 /* XXX: Use surf_width and surf_height here? */
2530 surface_aspect = (float) surface->width /
2531 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002532 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002533 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002534 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002535 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002536 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002537 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002538
Alex Wu4539b082012-03-01 12:57:46 +08002539 weston_matrix_scale(matrix, scale, scale, 1);
2540 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002542 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002543 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2544 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546
Alex Wu4539b082012-03-01 12:57:46 +08002547 break;
2548 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002549 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002550 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002551 surf_width * surface->buffer_viewport.scale,
2552 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002553 shsurf->fullscreen.framerate};
2554
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002555 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002556 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 weston_view_set_position(shsurf->view,
2558 output->x - surf_x,
2559 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002560 shsurf->fullscreen.black_view->surface->width = output->width;
2561 shsurf->fullscreen.black_view->surface->height = output->height;
2562 weston_view_set_position(shsurf->fullscreen.black_view,
2563 output->x - surf_x,
2564 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002565 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002566 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002567 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002569 }
Alex Wubd3354b2012-04-17 17:20:49 +08002570 }
Alex Wu4539b082012-03-01 12:57:46 +08002571 break;
2572 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002574 break;
2575 default:
2576 break;
2577 }
2578}
2579
Alex Wu4539b082012-03-01 12:57:46 +08002580static void
2581shell_map_fullscreen(struct shell_surface *shsurf)
2582{
Alex Wubd3354b2012-04-17 17:20:49 +08002583 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002584}
2585
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002586static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002587set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2588{
2589 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002590 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002591 shsurf->transient.x = x;
2592 shsurf->transient.y = y;
2593 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002594
2595 shell_surface_set_parent(shsurf, NULL);
2596
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002597 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002598 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002599}
2600
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002601static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002602
2603static void
2604destroy_shell_seat(struct wl_listener *listener, void *data)
2605{
2606 struct shell_seat *shseat =
2607 container_of(listener,
2608 struct shell_seat, seat_destroy_listener);
2609 struct shell_surface *shsurf, *prev = NULL;
2610
2611 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002612 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002613 shseat->popup_grab.client = NULL;
2614
2615 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2616 shsurf->popup.shseat = NULL;
2617 if (prev) {
2618 wl_list_init(&prev->popup.grab_link);
2619 }
2620 prev = shsurf;
2621 }
2622 wl_list_init(&prev->popup.grab_link);
2623 }
2624
2625 wl_list_remove(&shseat->seat_destroy_listener.link);
2626 free(shseat);
2627}
2628
2629static struct shell_seat *
2630create_shell_seat(struct weston_seat *seat)
2631{
2632 struct shell_seat *shseat;
2633
2634 shseat = calloc(1, sizeof *shseat);
2635 if (!shseat) {
2636 weston_log("no memory to allocate shell seat\n");
2637 return NULL;
2638 }
2639
2640 shseat->seat = seat;
2641 wl_list_init(&shseat->popup_grab.surfaces_list);
2642
2643 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2644 wl_signal_add(&seat->destroy_signal,
2645 &shseat->seat_destroy_listener);
2646
2647 return shseat;
2648}
2649
2650static struct shell_seat *
2651get_shell_seat(struct weston_seat *seat)
2652{
2653 struct wl_listener *listener;
2654
2655 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2656 if (listener == NULL)
2657 return create_shell_seat(seat);
2658
2659 return container_of(listener,
2660 struct shell_seat, seat_destroy_listener);
2661}
2662
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002663static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002664popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002665{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002666 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002668 struct shell_seat *shseat =
2669 container_of(grab, struct shell_seat, popup_grab.grab);
2670 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002671 wl_fixed_t sx, sy;
2672
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 view = weston_compositor_pick_view(pointer->seat->compositor,
2674 pointer->x, pointer->y,
2675 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002676
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 if (view && view->surface->resource &&
2678 wl_resource_get_client(view->surface->resource) == client) {
2679 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002680 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002681 weston_pointer_set_focus(pointer, NULL,
2682 wl_fixed_from_int(0),
2683 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684 }
2685}
2686
2687static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002688popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2689 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002690{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002691 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002692 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002693 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002695 weston_pointer_move(pointer, x, y);
2696
Neil Roberts96d790e2013-09-19 17:32:00 +01002697 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 weston_view_from_global_fixed(pointer->focus,
2699 pointer->x, pointer->y,
2700 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002701 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002702 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002703}
2704
2705static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002706popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002707 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002708{
2709 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002710 struct shell_seat *shseat =
2711 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002712 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002713 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002715 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002716
Neil Roberts96d790e2013-09-19 17:32:00 +01002717 resource_list = &grab->pointer->focus_resource_list;
2718 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002719 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002720 wl_resource_for_each(resource, resource_list) {
2721 wl_pointer_send_button(resource, serial,
2722 time, button, state);
2723 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002724 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002725 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002726 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002727 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728 }
2729
Daniel Stone4dbadb12012-05-30 16:31:51 +01002730 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002731 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002732}
2733
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002734static void
2735popup_grab_cancel(struct weston_pointer_grab *grab)
2736{
2737 popup_grab_end(grab->pointer);
2738}
2739
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002740static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002741 popup_grab_focus,
2742 popup_grab_motion,
2743 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002744 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002745};
2746
2747static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748shell_surface_send_popup_done(struct shell_surface *shsurf)
2749{
2750 if (shell_surface_is_wl_shell_surface(shsurf))
2751 wl_shell_surface_send_popup_done(shsurf->resource);
2752 else if (shell_surface_is_xdg_popup(shsurf))
2753 xdg_popup_send_popup_done(shsurf->resource,
2754 shsurf->popup.serial);
2755}
2756
2757static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002758popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002759{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002760 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002761 struct shell_seat *shseat =
2762 container_of(grab, struct shell_seat, popup_grab.grab);
2763 struct shell_surface *shsurf;
2764 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002765
2766 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002767 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002768 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002769 shseat->popup_grab.grab.interface = NULL;
2770 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002771 /* Send the popup_done event to all the popups open */
2772 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002773 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002774 shsurf->popup.shseat = NULL;
2775 if (prev) {
2776 wl_list_init(&prev->popup.grab_link);
2777 }
2778 prev = shsurf;
2779 }
2780 wl_list_init(&prev->popup.grab_link);
2781 wl_list_init(&shseat->popup_grab.surfaces_list);
2782 }
2783}
2784
2785static void
2786add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2787{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002788 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002789
2790 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002791 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002792 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002793 /* We must make sure here that this popup was opened after
2794 * a mouse press, and not just by moving around with other
2795 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002796 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002797 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002798
Rob Bradforddfe31052013-06-26 19:49:11 +01002799 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002800 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002801 } else {
2802 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002803 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002804}
2805
2806static void
2807remove_popup_grab(struct shell_surface *shsurf)
2808{
2809 struct shell_seat *shseat = shsurf->popup.shseat;
2810
2811 wl_list_remove(&shsurf->popup.grab_link);
2812 wl_list_init(&shsurf->popup.grab_link);
2813 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002814 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002815 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002816 }
2817}
2818
2819static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002820shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002822 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 shsurf->surface->output = parent_view->output;
2826 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002827
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 weston_view_set_transform_parent(shsurf->view, parent_view);
2829 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2830 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002831
Kristian Høgsberge3148752013-05-06 23:19:49 -04002832 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002833 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002834 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002835 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002836 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002837 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002838}
2839
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002840static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002841 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002842 shell_surface_move,
2843 shell_surface_resize,
2844 shell_surface_set_toplevel,
2845 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002847 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002848 shell_surface_set_maximized,
2849 shell_surface_set_title,
2850 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002851};
2852
2853static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002854destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002855{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002856 struct shell_surface *child, *next;
2857
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002858 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2859
Giulio Camuffo5085a752013-03-25 21:42:45 +01002860 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2861 remove_popup_grab(shsurf);
2862 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863
Alex Wubd3354b2012-04-17 17:20:49 +08002864 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002865 shell_surface_is_top_fullscreen(shsurf))
2866 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002867
Jason Ekstranda7af7042013-10-12 22:38:11 -05002868 if (shsurf->fullscreen.black_view)
2869 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002870
Alex Wubd3354b2012-04-17 17:20:49 +08002871 /* As destroy_resource() use wl_list_for_each_safe(),
2872 * we can always remove the listener.
2873 */
2874 wl_list_remove(&shsurf->surface_destroy_listener.link);
2875 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002876 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002877 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002878
Jason Ekstranda7af7042013-10-12 22:38:11 -05002879 weston_view_destroy(shsurf->view);
2880
Philip Withnall648a4dd2013-11-25 18:01:44 +00002881 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002882 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2883 wl_list_remove(&child->children_link);
2884 child->parent = NULL;
2885 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002886
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002887 wl_list_remove(&shsurf->link);
2888 free(shsurf);
2889}
2890
2891static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002892shell_destroy_shell_surface(struct wl_resource *resource)
2893{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002894 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002895
2896 destroy_shell_surface(shsurf);
2897}
2898
2899static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002900shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002901{
2902 struct shell_surface *shsurf = container_of(listener,
2903 struct shell_surface,
2904 surface_destroy_listener);
2905
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002906 if (shsurf->resource)
2907 wl_resource_destroy(shsurf->resource);
2908 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002909 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002910}
2911
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002912static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002913shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002914static void
2915shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002916
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002917struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002918get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002919{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002920 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002921 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002922 else
2923 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002924}
2925
Rafael Antognollie2a34552013-12-03 15:35:45 -02002926static struct shell_surface *
2927create_common_surface(void *shell, struct weston_surface *surface,
2928 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002929{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002930 struct shell_surface *shsurf;
2931
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002932 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002933 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002934 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002935 }
2936
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002937 shsurf = calloc(1, sizeof *shsurf);
2938 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002939 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002940 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002941 }
2942
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 shsurf->view = weston_view_create(surface);
2944 if (!shsurf->view) {
2945 weston_log("no memory to allocate shell surface\n");
2946 free(shsurf);
2947 return NULL;
2948 }
2949
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002950 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002951 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002952 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002953
Tiago Vignattibc052c92012-04-19 16:18:18 +03002954 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002955 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002956 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002957 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002958 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002959 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002960 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2961 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002962 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002963 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002964 wl_list_init(&shsurf->fullscreen.transform.link);
2965
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002966 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002967 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002968 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002969 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002970
2971 /* init link so its safe to always remove it in destroy_shell_surface */
2972 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002973 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002974
Pekka Paalanen460099f2012-01-20 16:48:25 +02002975 /* empty when not in use */
2976 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002977 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002978
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002979 wl_list_init(&shsurf->workspace_transform.link);
2980
Philip Withnall648a4dd2013-11-25 18:01:44 +00002981 wl_list_init(&shsurf->children_link);
2982 wl_list_init(&shsurf->children_list);
2983 shsurf->parent = NULL;
2984
Pekka Paalanen98262232011-12-01 10:42:22 +02002985 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002986
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002987 shsurf->client = client;
2988
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002989 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002990}
2991
Rafael Antognollie2a34552013-12-03 15:35:45 -02002992static struct shell_surface *
2993create_shell_surface(void *shell, struct weston_surface *surface,
2994 const struct weston_shell_client *client)
2995{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002996 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002997}
2998
Jason Ekstranda7af7042013-10-12 22:38:11 -05002999static struct weston_view *
3000get_primary_view(void *shell, struct shell_surface *shsurf)
3001{
3002 return shsurf->view;
3003}
3004
Tiago Vignattibc052c92012-04-19 16:18:18 +03003005static void
3006shell_get_shell_surface(struct wl_client *client,
3007 struct wl_resource *resource,
3008 uint32_t id,
3009 struct wl_resource *surface_resource)
3010{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003011 struct weston_surface *surface =
3012 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003013 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003014 struct shell_surface *shsurf;
3015
3016 if (get_shell_surface(surface)) {
3017 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003018 WL_DISPLAY_ERROR_INVALID_OBJECT,
3019 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003020 return;
3021 }
3022
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003023 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003024 if (!shsurf) {
3025 wl_resource_post_error(surface_resource,
3026 WL_DISPLAY_ERROR_INVALID_OBJECT,
3027 "surface->configure already set");
3028 return;
3029 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003030
Jason Ekstranda85118c2013-06-27 20:17:02 -05003031 shsurf->resource =
3032 wl_resource_create(client,
3033 &wl_shell_surface_interface, 1, id);
3034 wl_resource_set_implementation(shsurf->resource,
3035 &shell_surface_implementation,
3036 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003037}
3038
Rafael Antognollie2a34552013-12-03 15:35:45 -02003039static bool
3040shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3041{
3042 return wl_resource_instance_of(shsurf->resource,
3043 &wl_shell_surface_interface,
3044 &shell_surface_implementation);
3045}
3046
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003047static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003048 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003049};
3050
Rafael Antognollie2a34552013-12-03 15:35:45 -02003051/****************************
3052 * xdg-shell implementation */
3053
3054static void
3055xdg_surface_destroy(struct wl_client *client,
3056 struct wl_resource *resource)
3057{
3058 wl_resource_destroy(resource);
3059}
3060
3061static void
3062xdg_surface_pong(struct wl_client *client,
3063 struct wl_resource *resource,
3064 uint32_t serial)
3065{
3066 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3067
3068 surface_pong(shsurf, serial);
3069}
3070
3071static void
3072xdg_surface_set_app_id(struct wl_client *client,
3073 struct wl_resource *resource,
3074 const char *app_id)
3075{
3076 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3077
3078 free(shsurf->class);
3079 shsurf->class = strdup(app_id);
3080}
3081
3082static void
3083xdg_surface_set_title(struct wl_client *client,
3084 struct wl_resource *resource, const char *title)
3085{
3086 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3087
3088 set_title(shsurf, title);
3089}
3090
3091static void
3092xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3093 struct wl_resource *seat_resource, uint32_t serial)
3094{
3095 common_surface_move(resource, seat_resource, serial);
3096}
3097
3098static void
3099xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3100 struct wl_resource *seat_resource, uint32_t serial,
3101 uint32_t edges)
3102{
3103 common_surface_resize(resource, seat_resource, serial, edges);
3104}
3105
3106static void
3107xdg_surface_set_output(struct wl_client *client,
3108 struct wl_resource *resource,
3109 struct wl_resource *output_resource)
3110{
3111 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3112 struct weston_output *output;
3113
3114 if (output_resource)
3115 output = wl_resource_get_user_data(output_resource);
3116 else
3117 output = NULL;
3118
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003119 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003120}
3121
3122static void
3123xdg_surface_set_fullscreen(struct wl_client *client,
3124 struct wl_resource *resource)
3125{
3126 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3127
3128 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3129 return;
3130
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003131 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003132 set_fullscreen(shsurf,
3133 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003134 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003135}
3136
3137static void
3138xdg_surface_unset_fullscreen(struct wl_client *client,
3139 struct wl_resource *resource)
3140{
3141 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003142 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003143
3144 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3145 return;
3146
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003147 if (!shsurf->next_state.fullscreen)
3148 return;
3149
Rafael Antognollie2a34552013-12-03 15:35:45 -02003150 shsurf->next_state.fullscreen = false;
3151 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003152
3153 if (shsurf->saved_size_valid) {
3154 width = shsurf->saved_width;
3155 height = shsurf->saved_height;
3156 shsurf->saved_size_valid = false;
3157 } else {
3158 width = shsurf->surface->width;
3159 height = shsurf->surface->height;
3160 }
3161
3162 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003163}
3164
3165static void
3166xdg_surface_set_maximized(struct wl_client *client,
3167 struct wl_resource *resource)
3168{
3169 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3170
3171 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3172 return;
3173
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003174 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003175 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003176}
3177
3178static void
3179xdg_surface_unset_maximized(struct wl_client *client,
3180 struct wl_resource *resource)
3181{
3182 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003183 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003184
3185 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3186 return;
3187
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003188 if (!shsurf->next_state.maximized)
3189 return;
3190
Rafael Antognollie2a34552013-12-03 15:35:45 -02003191 shsurf->next_state.maximized = false;
3192 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003193
3194 if (shsurf->saved_size_valid) {
3195 width = shsurf->saved_width;
3196 height = shsurf->saved_height;
3197 shsurf->saved_size_valid = false;
3198 } else {
3199 width = shsurf->surface->width;
3200 height = shsurf->surface->height;
3201 }
3202
3203 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003204}
3205
3206static const struct xdg_surface_interface xdg_surface_implementation = {
3207 xdg_surface_destroy,
3208 xdg_surface_set_transient_for,
3209 xdg_surface_set_title,
3210 xdg_surface_set_app_id,
3211 xdg_surface_pong,
3212 xdg_surface_move,
3213 xdg_surface_resize,
3214 xdg_surface_set_output,
3215 xdg_surface_set_fullscreen,
3216 xdg_surface_unset_fullscreen,
3217 xdg_surface_set_maximized,
3218 xdg_surface_unset_maximized,
3219 NULL /* set_minimized */
3220};
3221
3222static void
3223xdg_send_configure(struct weston_surface *surface,
3224 uint32_t edges, int32_t width, int32_t height)
3225{
3226 struct shell_surface *shsurf = get_shell_surface(surface);
3227
3228 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3229}
3230
3231static const struct weston_shell_client xdg_client = {
3232 xdg_send_configure
3233};
3234
3235static void
3236xdg_use_unstable_version(struct wl_client *client,
3237 struct wl_resource *resource,
3238 int32_t version)
3239{
3240 if (version > 1) {
3241 wl_resource_post_error(resource,
3242 1,
3243 "xdg-shell:: version not implemented yet.");
3244 return;
3245 }
3246}
3247
3248static struct shell_surface *
3249create_xdg_surface(void *shell, struct weston_surface *surface,
3250 const struct weston_shell_client *client)
3251{
3252 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003253
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003254 shsurf = create_common_surface(shell, surface, client);
3255 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003256
3257 return shsurf;
3258}
3259
3260static void
3261xdg_get_xdg_surface(struct wl_client *client,
3262 struct wl_resource *resource,
3263 uint32_t id,
3264 struct wl_resource *surface_resource)
3265{
3266 struct weston_surface *surface =
3267 wl_resource_get_user_data(surface_resource);
3268 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3269 struct shell_surface *shsurf;
3270
3271 if (get_shell_surface(surface)) {
3272 wl_resource_post_error(surface_resource,
3273 WL_DISPLAY_ERROR_INVALID_OBJECT,
3274 "desktop_shell::get_shell_surface already requested");
3275 return;
3276 }
3277
3278 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3279 if (!shsurf) {
3280 wl_resource_post_error(surface_resource,
3281 WL_DISPLAY_ERROR_INVALID_OBJECT,
3282 "surface->configure already set");
3283 return;
3284 }
3285
3286 shsurf->resource =
3287 wl_resource_create(client,
3288 &xdg_surface_interface, 1, id);
3289 wl_resource_set_implementation(shsurf->resource,
3290 &xdg_surface_implementation,
3291 shsurf, shell_destroy_shell_surface);
3292}
3293
3294static bool
3295shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3296{
3297 return wl_resource_instance_of(shsurf->resource,
3298 &xdg_surface_interface,
3299 &xdg_surface_implementation);
3300}
3301
3302/* xdg-popup implementation */
3303
3304static void
3305xdg_popup_destroy(struct wl_client *client,
3306 struct wl_resource *resource)
3307{
3308 wl_resource_destroy(resource);
3309}
3310
3311static void
3312xdg_popup_pong(struct wl_client *client,
3313 struct wl_resource *resource,
3314 uint32_t serial)
3315{
3316 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3317
3318 surface_pong(shsurf, serial);
3319}
3320
3321static const struct xdg_popup_interface xdg_popup_implementation = {
3322 xdg_popup_destroy,
3323 xdg_popup_pong
3324};
3325
3326static void
3327xdg_popup_send_configure(struct weston_surface *surface,
3328 uint32_t edges, int32_t width, int32_t height)
3329{
3330}
3331
3332static const struct weston_shell_client xdg_popup_client = {
3333 xdg_popup_send_configure
3334};
3335
3336static struct shell_surface *
3337create_xdg_popup(void *shell, struct weston_surface *surface,
3338 const struct weston_shell_client *client,
3339 struct weston_surface *parent,
3340 struct shell_seat *seat,
3341 uint32_t serial,
3342 int32_t x, int32_t y)
3343{
3344 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003345
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003346 shsurf = create_common_surface(shell, surface, client);
3347 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003348 shsurf->popup.shseat = seat;
3349 shsurf->popup.serial = serial;
3350 shsurf->popup.x = x;
3351 shsurf->popup.y = y;
3352 shell_surface_set_parent(shsurf, parent);
3353
3354 return shsurf;
3355}
3356
3357static void
3358xdg_get_xdg_popup(struct wl_client *client,
3359 struct wl_resource *resource,
3360 uint32_t id,
3361 struct wl_resource *surface_resource,
3362 struct wl_resource *parent_resource,
3363 struct wl_resource *seat_resource,
3364 uint32_t serial,
3365 int32_t x, int32_t y, uint32_t flags)
3366{
3367 struct weston_surface *surface =
3368 wl_resource_get_user_data(surface_resource);
3369 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3370 struct shell_surface *shsurf;
3371 struct weston_surface *parent;
3372 struct shell_seat *seat;
3373
3374 if (get_shell_surface(surface)) {
3375 wl_resource_post_error(surface_resource,
3376 WL_DISPLAY_ERROR_INVALID_OBJECT,
3377 "desktop_shell::get_shell_surface already requested");
3378 return;
3379 }
3380
3381 if (!parent_resource) {
3382 wl_resource_post_error(surface_resource,
3383 WL_DISPLAY_ERROR_INVALID_OBJECT,
3384 "xdg_shell::get_xdg_popup requires a parent shell surface");
3385 }
3386
3387 parent = wl_resource_get_user_data(parent_resource);
3388 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3389
3390 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3391 parent, seat, serial, x, y);
3392 if (!shsurf) {
3393 wl_resource_post_error(surface_resource,
3394 WL_DISPLAY_ERROR_INVALID_OBJECT,
3395 "surface->configure already set");
3396 return;
3397 }
3398
3399 shsurf->resource =
3400 wl_resource_create(client,
3401 &xdg_popup_interface, 1, id);
3402 wl_resource_set_implementation(shsurf->resource,
3403 &xdg_popup_implementation,
3404 shsurf, shell_destroy_shell_surface);
3405}
3406
3407static bool
3408shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3409{
3410 return wl_resource_instance_of(shsurf->resource,
3411 &xdg_popup_interface,
3412 &xdg_popup_implementation);
3413}
3414
3415static const struct xdg_shell_interface xdg_implementation = {
3416 xdg_use_unstable_version,
3417 xdg_get_xdg_surface,
3418 xdg_get_xdg_popup
3419};
3420
3421static int
3422xdg_shell_unversioned_dispatch(const void *implementation,
3423 void *_target, uint32_t opcode,
3424 const struct wl_message *message,
3425 union wl_argument *args)
3426{
3427 struct wl_resource *resource = _target;
3428 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3429
3430 if (opcode != 0) {
3431 wl_resource_post_error(resource,
3432 WL_DISPLAY_ERROR_INVALID_OBJECT,
3433 "must call use_unstable_version first");
3434 return 0;
3435 }
3436
3437#define XDG_SERVER_VERSION 1
3438
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003439 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3440 "shell implementation doesn't match protocol version");
3441
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442 if (args[0].i != XDG_SERVER_VERSION) {
3443 wl_resource_post_error(resource,
3444 WL_DISPLAY_ERROR_INVALID_OBJECT,
3445 "incompatible version, server is %d "
3446 "client wants %d",
3447 XDG_SERVER_VERSION, args[0].i);
3448 return 0;
3449 }
3450
3451 wl_resource_set_implementation(resource, &xdg_implementation,
3452 shell, NULL);
3453
3454 return 1;
3455}
3456
3457/* end of xdg-shell implementation */
3458/***********************************/
3459
Kristian Høgsberg07937562011-04-12 17:25:42 -04003460static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003461shell_fade(struct desktop_shell *shell, enum fade_type type);
3462
3463static int
3464screensaver_timeout(void *data)
3465{
3466 struct desktop_shell *shell = data;
3467
3468 shell_fade(shell, FADE_OUT);
3469
3470 return 1;
3471}
3472
3473static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003474handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003475{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003476 struct desktop_shell *shell =
3477 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003478
Pekka Paalanen18027e52011-12-02 16:31:49 +02003479 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003480
3481 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003482 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003483}
3484
3485static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003486launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003487{
3488 if (shell->screensaver.binding)
3489 return;
3490
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003491 if (!shell->screensaver.path) {
3492 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003493 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003494 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003495
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003496 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003497 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003498 return;
3499 }
3500
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003501 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003502 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003503 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003504 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003505}
3506
3507static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003508terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003509{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003510 if (shell->screensaver.process.pid == 0)
3511 return;
3512
3513 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003514}
3515
3516static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003517configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003518{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003519 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003520
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3522 if (v->output == ev->output && v != ev) {
3523 weston_view_unmap(v);
3524 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003525 }
3526 }
3527
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003528 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003529
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 if (wl_list_empty(&ev->layer_link)) {
3531 wl_list_insert(&layer->view_list, &ev->layer_link);
3532 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003533 }
3534}
3535
3536static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003537background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003538{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003539 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003541
Jason Ekstranda7af7042013-10-12 22:38:11 -05003542 view = container_of(es->views.next, struct weston_view, surface_link);
3543
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003544 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003545}
3546
3547static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003548desktop_shell_set_background(struct wl_client *client,
3549 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003550 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003551 struct wl_resource *surface_resource)
3552{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003553 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003554 struct weston_surface *surface =
3555 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003556 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003557
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003558 if (surface->configure) {
3559 wl_resource_post_error(surface_resource,
3560 WL_DISPLAY_ERROR_INVALID_OBJECT,
3561 "surface role already assigned");
3562 return;
3563 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003564
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3566 weston_view_destroy(view);
3567 view = weston_view_create(surface);
3568
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003569 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003570 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003571 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003573 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003574 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003575 surface->output->width,
3576 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003577}
3578
3579static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003580panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003581{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003582 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003583 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003584
Jason Ekstranda7af7042013-10-12 22:38:11 -05003585 view = container_of(es->views.next, struct weston_view, surface_link);
3586
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003587 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003588}
3589
3590static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003591desktop_shell_set_panel(struct wl_client *client,
3592 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003593 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003594 struct wl_resource *surface_resource)
3595{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003596 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003597 struct weston_surface *surface =
3598 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003599 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003600
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003601 if (surface->configure) {
3602 wl_resource_post_error(surface_resource,
3603 WL_DISPLAY_ERROR_INVALID_OBJECT,
3604 "surface role already assigned");
3605 return;
3606 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003607
Jason Ekstranda7af7042013-10-12 22:38:11 -05003608 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3609 weston_view_destroy(view);
3610 view = weston_view_create(surface);
3611
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003612 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003613 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003614 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003616 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003617 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003618 surface->output->width,
3619 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003620}
3621
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003622static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003623lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003624{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003625 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 struct weston_view *view;
3627
3628 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003629
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003630 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003631 return;
3632
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003634
3635 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 wl_list_insert(&shell->lock_layer.view_list,
3637 &view->layer_link);
3638 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003639 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003640 }
3641}
3642
3643static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003644handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003645{
Tiago Vignattibe143262012-04-16 17:31:41 +03003646 struct desktop_shell *shell =
3647 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003648
Martin Minarik6d118362012-06-07 18:01:59 +02003649 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003650 shell->lock_surface = NULL;
3651}
3652
3653static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003654desktop_shell_set_lock_surface(struct wl_client *client,
3655 struct wl_resource *resource,
3656 struct wl_resource *surface_resource)
3657{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003658 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003659 struct weston_surface *surface =
3660 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003661
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003662 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003663
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003664 if (!shell->locked)
3665 return;
3666
Pekka Paalanen98262232011-12-01 10:42:22 +02003667 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003668
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003669 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003670 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003671 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003672
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003673 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003674 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003675 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003676}
3677
3678static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003679resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003680{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003681 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003682
Pekka Paalanen77346a62011-11-30 16:26:35 +02003683 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003684
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003685 wl_list_remove(&shell->lock_layer.link);
3686 wl_list_insert(&shell->compositor->cursor_layer.link,
3687 &shell->fullscreen_layer.link);
3688 wl_list_insert(&shell->fullscreen_layer.link,
3689 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003690 if (shell->showing_input_panels) {
3691 wl_list_insert(&shell->panel_layer.link,
3692 &shell->input_panel_layer.link);
3693 wl_list_insert(&shell->input_panel_layer.link,
3694 &ws->layer.link);
3695 } else {
3696 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3697 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003698
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003699 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003700
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003701 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003702 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003703 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003704}
3705
3706static void
3707desktop_shell_unlock(struct wl_client *client,
3708 struct wl_resource *resource)
3709{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003710 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003711
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003712 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003713
3714 if (shell->locked)
3715 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003716}
3717
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003718static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003719desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003720 struct wl_resource *resource,
3721 struct wl_resource *surface_resource)
3722{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003723 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003724
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003725 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003726 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003727}
3728
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003729static void
3730desktop_shell_desktop_ready(struct wl_client *client,
3731 struct wl_resource *resource)
3732{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003733 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003734
3735 shell_fade_startup(shell);
3736}
3737
Kristian Høgsberg75840622011-09-06 13:48:16 -04003738static const struct desktop_shell_interface desktop_shell_implementation = {
3739 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003740 desktop_shell_set_panel,
3741 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003742 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003743 desktop_shell_set_grab_surface,
3744 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003745};
3746
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003747static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003748get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003749{
3750 struct shell_surface *shsurf;
3751
3752 shsurf = get_shell_surface(surface);
3753 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003754 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003755 return shsurf->type;
3756}
3757
Kristian Høgsberg75840622011-09-06 13:48:16 -04003758static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003759move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003760{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003761 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003762 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003763 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003764
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003765 if (seat->pointer->focus == NULL)
3766 return;
3767
3768 focus = seat->pointer->focus->surface;
3769
Pekka Paalanen01388e22013-04-25 13:57:44 +03003770 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003771 if (surface == NULL)
3772 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003773
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003774 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003775 if (shsurf == NULL || shsurf->state.fullscreen ||
3776 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003777 return;
3778
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003779 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003780}
3781
3782static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003783maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3784{
3785 struct weston_surface *focus = seat->pointer->focus->surface;
3786 struct weston_surface *surface;
3787 struct shell_surface *shsurf;
3788
3789 surface = weston_surface_get_main_surface(focus);
3790 if (surface == NULL)
3791 return;
3792
3793 shsurf = get_shell_surface(surface);
3794 if (shsurf == NULL)
3795 return;
3796
3797 if (!shell_surface_is_xdg_surface(shsurf))
3798 return;
3799
3800 if (shsurf->state.maximized)
3801 xdg_surface_send_request_unset_maximized(shsurf->resource);
3802 else
3803 xdg_surface_send_request_set_maximized(shsurf->resource);
3804}
3805
3806static void
3807fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3808{
3809 struct weston_surface *focus = seat->pointer->focus->surface;
3810 struct weston_surface *surface;
3811 struct shell_surface *shsurf;
3812
3813 surface = weston_surface_get_main_surface(focus);
3814 if (surface == NULL)
3815 return;
3816
3817 shsurf = get_shell_surface(surface);
3818 if (shsurf == NULL)
3819 return;
3820
3821 if (!shell_surface_is_xdg_surface(shsurf))
3822 return;
3823
3824 if (shsurf->state.fullscreen)
3825 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3826 else
3827 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3828}
3829
3830static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003831touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3832{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003833 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003834 struct weston_surface *surface;
3835 struct shell_surface *shsurf;
3836
3837 surface = weston_surface_get_main_surface(focus);
3838 if (surface == NULL)
3839 return;
3840
3841 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003842 if (shsurf == NULL || shsurf->state.fullscreen ||
3843 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003844 return;
3845
3846 surface_touch_move(shsurf, (struct weston_seat *) seat);
3847}
3848
3849static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003850resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003851{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003852 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003853 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003854 uint32_t edges = 0;
3855 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003856 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003857
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003858 if (seat->pointer->focus == NULL)
3859 return;
3860
3861 focus = seat->pointer->focus->surface;
3862
Pekka Paalanen01388e22013-04-25 13:57:44 +03003863 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003864 if (surface == NULL)
3865 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003866
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003867 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003868 if (shsurf == NULL || shsurf->state.fullscreen ||
3869 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003870 return;
3871
Jason Ekstranda7af7042013-10-12 22:38:11 -05003872 weston_view_from_global(shsurf->view,
3873 wl_fixed_to_int(seat->pointer->grab_x),
3874 wl_fixed_to_int(seat->pointer->grab_y),
3875 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003876
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003877 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003878 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003879 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003880 edges |= 0;
3881 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003882 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003883
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003884 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003885 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003886 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003887 edges |= 0;
3888 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003889 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003890
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003891 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003892}
3893
3894static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003895surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003896 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003897{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003898 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003899 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003900 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003901 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003902
Pekka Paalanen01388e22013-04-25 13:57:44 +03003903 /* XXX: broken for windows containing sub-surfaces */
3904 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003905 if (surface == NULL)
3906 return;
3907
3908 shsurf = get_shell_surface(surface);
3909 if (!shsurf)
3910 return;
3911
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003913
Jason Ekstranda7af7042013-10-12 22:38:11 -05003914 if (shsurf->view->alpha > 1.0)
3915 shsurf->view->alpha = 1.0;
3916 if (shsurf->view->alpha < step)
3917 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003918
Jason Ekstranda7af7042013-10-12 22:38:11 -05003919 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003920 weston_surface_damage(surface);
3921}
3922
3923static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003924do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003925 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003926{
Daniel Stone37816df2012-05-16 18:45:18 +01003927 struct weston_seat *ws = (struct weston_seat *) seat;
3928 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003929 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003930 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003931
3932 wl_list_for_each(output, &compositor->output_list, link) {
3933 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003934 wl_fixed_to_double(seat->pointer->x),
3935 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003936 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003937 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003938 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003939 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003940 increment = -output->zoom.increment;
3941 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003942 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003943 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003944 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003945 else
3946 increment = 0;
3947
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003948 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003949
Scott Moreaue6603982012-06-11 13:07:51 -06003950 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003951 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003952 else if (output->zoom.level > output->zoom.max_level)
3953 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003954 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003955 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003956 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003957
Scott Moreaue6603982012-06-11 13:07:51 -06003958 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003959
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003961 }
3962 }
3963}
3964
Scott Moreauccbf29d2012-02-22 14:21:41 -07003965static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003966zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003967 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003968{
3969 do_zoom(seat, time, 0, axis, value);
3970}
3971
3972static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003973zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003974 void *data)
3975{
3976 do_zoom(seat, time, key, 0, 0);
3977}
3978
3979static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003980terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003981 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003983 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003984
Daniel Stone325fc2d2012-05-30 16:31:58 +01003985 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003986}
3987
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003988static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003989rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3990 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003991{
3992 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003993 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003994 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003995 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003996 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003997
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003998 weston_pointer_move(pointer, x, y);
3999
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004000 if (!shsurf)
4001 return;
4002
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004003 cx = 0.5f * shsurf->surface->width;
4004 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004005
Daniel Stone37816df2012-05-16 18:45:18 +01004006 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4007 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004008 r = sqrtf(dx * dx + dy * dy);
4009
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004010 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004012
4013 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004014 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004015 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004016
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004017 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004018 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004019
4020 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004021 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004022 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004023 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004024 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004025
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004026 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004028 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004029 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004030 wl_list_init(&shsurf->rotation.transform.link);
4031 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004032 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004033 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004034
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004035 /* We need to adjust the position of the surface
4036 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004037 cposx = shsurf->view->geometry.x + cx;
4038 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004039 dposx = rotate->center.x - cposx;
4040 dposy = rotate->center.y - cposy;
4041 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004042 weston_view_set_position(shsurf->view,
4043 shsurf->view->geometry.x + dposx,
4044 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004045 }
4046
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004047 /* Repaint implies weston_surface_update_transform(), which
4048 * lazily applies the damage due to rotation update.
4049 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004050 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004051}
4052
4053static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004054rotate_grab_button(struct weston_pointer_grab *grab,
4055 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004056{
4057 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004058 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004059 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004060 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004061 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004062
Daniel Stone4dbadb12012-05-30 16:31:51 +01004063 if (pointer->button_count == 0 &&
4064 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004065 if (shsurf)
4066 weston_matrix_multiply(&shsurf->rotation.rotation,
4067 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004068 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004069 free(rotate);
4070 }
4071}
4072
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004073static void
4074rotate_grab_cancel(struct weston_pointer_grab *grab)
4075{
4076 struct rotate_grab *rotate =
4077 container_of(grab, struct rotate_grab, base.grab);
4078
4079 shell_grab_end(&rotate->base);
4080 free(rotate);
4081}
4082
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004083static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004084 noop_grab_focus,
4085 rotate_grab_motion,
4086 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004087 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004088};
4089
4090static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004091surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004092{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004093 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004094 float dx, dy;
4095 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004096
Pekka Paalanen460099f2012-01-20 16:48:25 +02004097 rotate = malloc(sizeof *rotate);
4098 if (!rotate)
4099 return;
4100
Jason Ekstranda7af7042013-10-12 22:38:11 -05004101 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004102 surface->surface->width * 0.5f,
4103 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004104 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004105
Daniel Stone37816df2012-05-16 18:45:18 +01004106 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4107 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004108 r = sqrtf(dx * dx + dy * dy);
4109 if (r > 20.0f) {
4110 struct weston_matrix inverse;
4111
4112 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004113 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004114 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004115
4116 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004117 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004118 } else {
4119 weston_matrix_init(&surface->rotation.rotation);
4120 weston_matrix_init(&rotate->rotation);
4121 }
4122
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004123 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4124 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004125}
4126
4127static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004128rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004129 void *data)
4130{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004131 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004132 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004133 struct shell_surface *surface;
4134
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004135 if (seat->pointer->focus == NULL)
4136 return;
4137
4138 focus = seat->pointer->focus->surface;
4139
Pekka Paalanen01388e22013-04-25 13:57:44 +03004140 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004141 if (base_surface == NULL)
4142 return;
4143
4144 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004145 if (surface == NULL || surface->state.fullscreen ||
4146 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004147 return;
4148
4149 surface_rotate(surface, seat);
4150}
4151
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004152/* Move all fullscreen layers down to the current workspace in a non-reversible
4153 * manner. This should be used when implementing shell-wide overlays, such as
4154 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004155void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004156lower_fullscreen_layer(struct desktop_shell *shell)
4157{
4158 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004159 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004160
4161 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004162 wl_list_for_each_reverse_safe(view, prev,
4163 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004164 layer_link) {
4165 wl_list_remove(&view->layer_link);
4166 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4167 weston_view_damage_below(view);
4168 weston_surface_damage(view->surface);
4169 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004170}
4171
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004172void
Tiago Vignattibe143262012-04-16 17:31:41 +03004173activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004174 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004175{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004176 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004177 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004178 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004179 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004180 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004181
Pekka Paalanen01388e22013-04-25 13:57:44 +03004182 main_surface = weston_surface_get_main_surface(es);
4183
Daniel Stone37816df2012-05-16 18:45:18 +01004184 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004185
Jonas Ådahl8538b222012-08-29 22:13:03 +02004186 state = ensure_focus_state(shell, seat);
4187 if (state == NULL)
4188 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004189
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004190 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004191 state->keyboard_focus = es;
4192 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004193 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004194
Rafael Antognolli03b16592013-12-03 15:35:42 -02004195 shsurf = get_shell_surface(main_surface);
4196 if (shsurf->state.fullscreen)
4197 shell_configure_fullscreen(shsurf);
4198 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004199 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004200
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004201 if (shell->focus_animation_type != ANIMATION_NONE) {
4202 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004203 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004204 }
4205
4206 /* Update the surface’s layer. This brings it to the top of the stacking
4207 * order as appropriate. */
4208 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004209}
4210
Alex Wu21858432012-04-01 20:13:08 +08004211/* no-op func for checking black surface */
4212static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004213black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004214{
4215}
4216
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004217static bool
Alex Wu21858432012-04-01 20:13:08 +08004218is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4219{
4220 if (es->configure == black_surface_configure) {
4221 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004222 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004223 return true;
4224 }
4225 return false;
4226}
4227
Kristian Høgsberg75840622011-09-06 13:48:16 -04004228static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004229activate_binding(struct weston_seat *seat,
4230 struct desktop_shell *shell,
4231 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004232{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004233 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004234
Alex Wu9c35e6b2012-03-05 14:13:13 +08004235 if (!focus)
4236 return;
4237
Pekka Paalanen01388e22013-04-25 13:57:44 +03004238 if (is_black_surface(focus, &main_surface))
4239 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004240
Pekka Paalanen01388e22013-04-25 13:57:44 +03004241 main_surface = weston_surface_get_main_surface(focus);
4242 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004243 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004244
Neil Robertsa28c6932013-10-03 16:43:04 +01004245 activate(shell, focus, seat);
4246}
4247
4248static void
4249click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4250 void *data)
4251{
4252 if (seat->pointer->grab != &seat->pointer->default_grab)
4253 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004254 if (seat->pointer->focus == NULL)
4255 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004256
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004257 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004258}
4259
4260static void
4261touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4262{
4263 if (seat->touch->grab != &seat->touch->default_grab)
4264 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004265 if (seat->touch->focus == NULL)
4266 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004267
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004268 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004269}
4270
4271static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004272lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004273{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004274 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004275
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004276 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004277 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004278 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004279 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004280
4281 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004282
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004283 /* Hide all surfaces by removing the fullscreen, panel and
4284 * toplevel layers. This way nothing else can show or receive
4285 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004286
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004287 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004288 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004289 if (shell->showing_input_panels)
4290 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004291 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004292 wl_list_insert(&shell->compositor->cursor_layer.link,
4293 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004294
Pekka Paalanen77346a62011-11-30 16:26:35 +02004295 launch_screensaver(shell);
4296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004297 /* TODO: disable bindings that should not work while locked. */
4298
4299 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004300}
4301
4302static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004303unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004304{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004305 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004306 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004307 return;
4308 }
4309
4310 /* If desktop-shell client has gone away, unlock immediately. */
4311 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004312 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004313 return;
4314 }
4315
4316 if (shell->prepare_event_sent)
4317 return;
4318
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004319 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004320 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004321}
4322
4323static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004325{
4326 struct desktop_shell *shell = data;
4327
4328 shell->fade.animation = NULL;
4329
4330 switch (shell->fade.type) {
4331 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004332 weston_surface_destroy(shell->fade.view->surface);
4333 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004334 break;
4335 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004336 lock(shell);
4337 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004338 default:
4339 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004340 }
4341}
4342
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004344shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004345{
4346 struct weston_compositor *compositor = shell->compositor;
4347 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004349
4350 surface = weston_surface_create(compositor);
4351 if (!surface)
4352 return NULL;
4353
Jason Ekstranda7af7042013-10-12 22:38:11 -05004354 view = weston_view_create(surface);
4355 if (!view) {
4356 weston_surface_destroy(surface);
4357 return NULL;
4358 }
4359
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004360 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004361 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004362 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 wl_list_insert(&compositor->fade_layer.view_list,
4364 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004365 pixman_region32_init(&surface->input);
4366
Jason Ekstranda7af7042013-10-12 22:38:11 -05004367 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004368}
4369
4370static void
4371shell_fade(struct desktop_shell *shell, enum fade_type type)
4372{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004373 float tint;
4374
4375 switch (type) {
4376 case FADE_IN:
4377 tint = 0.0;
4378 break;
4379 case FADE_OUT:
4380 tint = 1.0;
4381 break;
4382 default:
4383 weston_log("shell: invalid fade type\n");
4384 return;
4385 }
4386
4387 shell->fade.type = type;
4388
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 if (shell->fade.view == NULL) {
4390 shell->fade.view = shell_fade_create_surface(shell);
4391 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004392 return;
4393
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 shell->fade.view->alpha = 1.0 - tint;
4395 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004396 }
4397
4398 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004399 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004400 else
4401 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004403 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004404 shell_fade_done, shell);
4405}
4406
4407static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004408do_shell_fade_startup(void *data)
4409{
4410 struct desktop_shell *shell = data;
4411
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004412 if (shell->startup_animation_type == ANIMATION_FADE)
4413 shell_fade(shell, FADE_IN);
4414 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 weston_surface_destroy(shell->fade.view->surface);
4416 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004417 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004418}
4419
4420static void
4421shell_fade_startup(struct desktop_shell *shell)
4422{
4423 struct wl_event_loop *loop;
4424
4425 if (!shell->fade.startup_timer)
4426 return;
4427
4428 wl_event_source_remove(shell->fade.startup_timer);
4429 shell->fade.startup_timer = NULL;
4430
4431 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4432 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4433}
4434
4435static int
4436fade_startup_timeout(void *data)
4437{
4438 struct desktop_shell *shell = data;
4439
4440 shell_fade_startup(shell);
4441 return 0;
4442}
4443
4444static void
4445shell_fade_init(struct desktop_shell *shell)
4446{
4447 /* Make compositor output all black, and wait for the desktop-shell
4448 * client to signal it is ready, then fade in. The timer triggers a
4449 * fade-in, in case the desktop-shell client takes too long.
4450 */
4451
4452 struct wl_event_loop *loop;
4453
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004455 weston_log("%s: warning: fade surface already exists\n",
4456 __func__);
4457 return;
4458 }
4459
Jason Ekstranda7af7042013-10-12 22:38:11 -05004460 shell->fade.view = shell_fade_create_surface(shell);
4461 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004462 return;
4463
Jason Ekstranda7af7042013-10-12 22:38:11 -05004464 weston_view_update_transform(shell->fade.view);
4465 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004466
4467 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4468 shell->fade.startup_timer =
4469 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4470 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4471}
4472
4473static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004474idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004475{
4476 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004477 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004478
4479 shell_fade(shell, FADE_OUT);
4480 /* lock() is called from shell_fade_done() */
4481}
4482
4483static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004484wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004485{
4486 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004487 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004488
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004489 unlock(shell);
4490}
4491
4492static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004494{
Giulio Camuffob8366642013-04-25 13:57:46 +03004495 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004496 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004497
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004499
4500 x = output->x + (output->width - width) / 2 - surf_x / 2;
4501 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004502
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004503 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004504}
4505
4506static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004507weston_view_set_initial_position(struct weston_view *view,
4508 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004509{
4510 struct weston_compositor *compositor = shell->compositor;
4511 int ix = 0, iy = 0;
4512 int range_x, range_y;
4513 int dx, dy, x, y, panel_height;
4514 struct weston_output *output, *target_output = NULL;
4515 struct weston_seat *seat;
4516
4517 /* As a heuristic place the new window on the same output as the
4518 * pointer. Falling back to the output containing 0, 0.
4519 *
4520 * TODO: Do something clever for touch too?
4521 */
4522 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004523 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004524 ix = wl_fixed_to_int(seat->pointer->x);
4525 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004526 break;
4527 }
4528 }
4529
4530 wl_list_for_each(output, &compositor->output_list, link) {
4531 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4532 target_output = output;
4533 break;
4534 }
4535 }
4536
4537 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004538 weston_view_set_position(view, 10 + random() % 400,
4539 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004540 return;
4541 }
4542
4543 /* Valid range within output where the surface will still be onscreen.
4544 * If this is negative it means that the surface is bigger than
4545 * output.
4546 */
4547 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004548 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004549 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004550 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004551
Rob Bradford4cb88c72012-08-13 15:18:44 +01004552 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004553 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004554 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004555 dx = 0;
4556
4557 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004558 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004559 else
4560 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004561
4562 x = target_output->x + dx;
4563 y = target_output->y + dy;
4564
Jason Ekstranda7af7042013-10-12 22:38:11 -05004565 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004566}
4567
4568static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004569map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004570 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004572 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004573 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004574 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004575 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004576
Pekka Paalanen77346a62011-11-30 16:26:35 +02004577 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004578 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004579 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004580 if (shsurf->state.fullscreen) {
4581 center_on_output(shsurf->view, shsurf->fullscreen_output);
4582 shell_map_fullscreen(shsurf);
4583 } else if (shsurf->state.maximized) {
4584 /* use surface configure to set the geometry */
4585 panel_height = get_output_panel_height(shell, shsurf->output);
4586 surface_subsurfaces_boundingbox(shsurf->surface,
4587 &surf_x, &surf_y, NULL, NULL);
4588 weston_view_set_position(shsurf->view,
4589 shsurf->output->x - surf_x,
4590 shsurf->output->y +
4591 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004592 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004593 weston_view_set_initial_position(shsurf->view, shell);
4594 }
Juan Zhao96879df2012-02-07 08:45:41 +08004595 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004596 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004597 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004598 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004599 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600 weston_view_set_position(shsurf->view,
4601 shsurf->view->geometry.x + sx,
4602 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004603 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004604 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004605 default:
4606 ;
4607 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004608
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004609 /* Surface stacking order, see also activate(). */
4610 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004611
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 if (shsurf->type != SHELL_SURFACE_NONE) {
4613 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004614 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 shsurf->surface->output = shsurf->output;
4616 shsurf->view->output = shsurf->output;
4617 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004618 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004619
Rafael Antognollied207b42013-12-03 15:35:43 -02004620 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4621 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4622 }
4623
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004625 /* XXX: xwayland's using the same fields for transient type */
4626 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004627 if (shsurf->transient.flags ==
4628 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4629 break;
4630 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004631 if (shsurf->state.relative &&
4632 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4633 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004634 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004635 break;
4636 wl_list_for_each(seat, &compositor->seat_list, link)
4637 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004638 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004639 case SHELL_SURFACE_POPUP:
4640 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004641 default:
4642 break;
4643 }
4644
Rafael Antognolli03b16592013-12-03 15:35:42 -02004645 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4646 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004647 {
4648 switch (shell->win_animation_type) {
4649 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004650 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004651 break;
4652 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004653 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004654 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004655 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004656 default:
4657 break;
4658 }
4659 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004660}
4661
4662static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004663configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004664 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004665{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004666 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004667 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004668 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004669
Pekka Paalanen77346a62011-11-30 16:26:35 +02004670 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004671
Rafael Antognolli03b16592013-12-03 15:35:42 -02004672 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004673 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004674 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004675 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004676 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4677 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004678 mx = shsurf->output->x - surf_x;
4679 my = shsurf->output->y +
4680 get_output_panel_height(shell,shsurf->output) - surf_y;
4681 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004682 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004683 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004684 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004685
Alex Wu4539b082012-03-01 12:57:46 +08004686 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004687 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004688 wl_list_for_each(view, &surface->views, surface_link)
4689 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004690
Rafael Antognolli03b16592013-12-03 15:35:42 -02004691 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004692 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004693 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004694}
4695
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004696static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004697shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004698{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004699 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004700 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004701
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004702 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004703
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004704 if (!weston_surface_is_mapped(es) &&
4705 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004706 remove_popup_grab(shsurf);
4707 }
4708
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004709 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004710 return;
4711
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004712 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004713 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004714 type_changed = 1;
4715 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004716
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004717 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004718 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004719 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004720 shsurf->last_width != es->width ||
4721 shsurf->last_height != es->height) {
4722 shsurf->last_width = es->width;
4723 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004724 float from_x, from_y;
4725 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004726
Jason Ekstranda7af7042013-10-12 22:38:11 -05004727 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4728 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004729 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004731 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004732 }
4733}
4734
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004735static void
4736shell_surface_output_destroyed(struct weston_surface *es)
4737{
4738 struct shell_surface *shsurf = get_shell_surface(es);
4739
4740 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004741 shsurf->next_state.maximized = false;
4742 shsurf->next_state.fullscreen = false;
4743 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004744}
4745
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004746static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004747
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004748static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004749desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004750{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004751 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004752 struct desktop_shell *shell =
4753 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004754
4755 shell->child.process.pid = 0;
4756 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004757
4758 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4759 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004760 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004761 shell->child.deathstamp = time;
4762 shell->child.deathcount = 0;
4763 }
4764
4765 shell->child.deathcount++;
4766 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004767 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004768 return;
4769 }
4770
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004771 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004772 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004773 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004774}
4775
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004776static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004777desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4778{
4779 struct desktop_shell *shell;
4780
4781 shell = container_of(listener, struct desktop_shell,
4782 child.client_destroy_listener);
4783
4784 shell->child.client = NULL;
4785}
4786
4787static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004788launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004789{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004790 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004791
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004792 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004793 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004794 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004795 desktop_shell_sigchld);
4796
4797 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004798 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004799
4800 shell->child.client_destroy_listener.notify =
4801 desktop_shell_client_destroy;
4802 wl_client_add_destroy_listener(shell->child.client,
4803 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004804}
4805
4806static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004807bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4808{
Tiago Vignattibe143262012-04-16 17:31:41 +03004809 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004810 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004811
Jason Ekstranda85118c2013-06-27 20:17:02 -05004812 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4813 if (resource)
4814 wl_resource_set_implementation(resource, &shell_implementation,
4815 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004816}
4817
Kristian Høgsberg75840622011-09-06 13:48:16 -04004818static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004819bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4820{
4821 struct desktop_shell *shell = data;
4822 struct wl_resource *resource;
4823
4824 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4825 if (resource)
4826 wl_resource_set_dispatcher(resource,
4827 xdg_shell_unversioned_dispatch,
4828 NULL, shell, NULL);
4829}
4830
4831static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004832unbind_desktop_shell(struct wl_resource *resource)
4833{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004834 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004835
4836 if (shell->locked)
4837 resume_desktop(shell);
4838
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004839 shell->child.desktop_shell = NULL;
4840 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004841}
4842
4843static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004844bind_desktop_shell(struct wl_client *client,
4845 void *data, uint32_t version, uint32_t id)
4846{
Tiago Vignattibe143262012-04-16 17:31:41 +03004847 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004848 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004849
Jason Ekstranda85118c2013-06-27 20:17:02 -05004850 resource = wl_resource_create(client, &desktop_shell_interface,
4851 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004852
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004853 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004854 wl_resource_set_implementation(resource,
4855 &desktop_shell_implementation,
4856 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004857 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004858
4859 if (version < 2)
4860 shell_fade_startup(shell);
4861
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004862 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004863 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004864
4865 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4866 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004867 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004868}
4869
Pekka Paalanen6e168112011-11-24 11:34:05 +02004870static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004871screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004872{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004873 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004875
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004876 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004877 return;
4878
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004879 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004880 if (!shell->locked)
4881 return;
4882
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 view = container_of(surface->views.next, struct weston_view, surface_link);
4884 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004885
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 if (wl_list_empty(&view->layer_link)) {
4887 wl_list_insert(shell->lock_layer.view_list.prev,
4888 &view->layer_link);
4889 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004890 wl_event_source_timer_update(shell->screensaver.timer,
4891 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004892 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004893 }
4894}
4895
4896static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004897screensaver_set_surface(struct wl_client *client,
4898 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004899 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004900 struct wl_resource *output_resource)
4901{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004902 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004903 struct weston_surface *surface =
4904 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004905 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004906 struct weston_view *view, *next;
4907
4908 /* Make sure we only have one view */
4909 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4910 weston_view_destroy(view);
4911 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004912
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004913 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004914 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004915 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004916}
4917
4918static const struct screensaver_interface screensaver_implementation = {
4919 screensaver_set_surface
4920};
4921
4922static void
4923unbind_screensaver(struct wl_resource *resource)
4924{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004925 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004926
Pekka Paalanen77346a62011-11-30 16:26:35 +02004927 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004928}
4929
4930static void
4931bind_screensaver(struct wl_client *client,
4932 void *data, uint32_t version, uint32_t id)
4933{
Tiago Vignattibe143262012-04-16 17:31:41 +03004934 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004935 struct wl_resource *resource;
4936
Jason Ekstranda85118c2013-06-27 20:17:02 -05004937 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004938
Pekka Paalanen77346a62011-11-30 16:26:35 +02004939 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004940 wl_resource_set_implementation(resource,
4941 &screensaver_implementation,
4942 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004943 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004944 return;
4945 }
4946
4947 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4948 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004949 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004950}
4951
Kristian Høgsberg07045392012-02-19 18:52:44 -05004952struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004953 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004954 struct weston_surface *current;
4955 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004956 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004957};
4958
4959static void
4960switcher_next(struct switcher *switcher)
4961{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004962 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004963 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004964 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004965 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004966
Jason Ekstranda7af7042013-10-12 22:38:11 -05004967 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004968 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02004969 if (shsurf &&
4970 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4971 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004972 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004973 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004974 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004975 next = view->surface;
4976 prev = view->surface;
4977 view->alpha = 0.25;
4978 weston_view_geometry_dirty(view);
4979 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004980 }
Alex Wu1659daa2012-04-01 20:13:09 +08004981
Jason Ekstranda7af7042013-10-12 22:38:11 -05004982 if (is_black_surface(view->surface, NULL)) {
4983 view->alpha = 0.25;
4984 weston_view_geometry_dirty(view);
4985 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004986 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004987 }
4988
4989 if (next == NULL)
4990 next = first;
4991
Alex Wu07b26062012-03-12 16:06:01 +08004992 if (next == NULL)
4993 return;
4994
Kristian Høgsberg07045392012-02-19 18:52:44 -05004995 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004996 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004997
4998 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999 wl_list_for_each(view, &next->views, surface_link)
5000 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005001
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005002 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005003 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005004 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005005}
5006
5007static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005008switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005009{
5010 struct switcher *switcher =
5011 container_of(listener, struct switcher, listener);
5012
5013 switcher_next(switcher);
5014}
5015
5016static void
Daniel Stone351eb612012-05-31 15:27:47 -04005017switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005019 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005020 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005021 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005022
Jason Ekstranda7af7042013-10-12 22:38:11 -05005023 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005024 if (is_focus_view(view))
5025 continue;
5026
Jason Ekstranda7af7042013-10-12 22:38:11 -05005027 view->alpha = 1.0;
5028 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005029 }
5030
Alex Wu07b26062012-03-12 16:06:01 +08005031 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005032 activate(switcher->shell, switcher->current,
5033 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005034 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005035 weston_keyboard_end_grab(keyboard);
5036 if (keyboard->input_method_resource)
5037 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005038 free(switcher);
5039}
5040
5041static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005042switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005043 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005044{
5045 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005046 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005047
Daniel Stonec9785ea2012-05-30 16:31:52 +01005048 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005049 switcher_next(switcher);
5050}
5051
5052static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005053switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005054 uint32_t mods_depressed, uint32_t mods_latched,
5055 uint32_t mods_locked, uint32_t group)
5056{
5057 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005058 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005059
Daniel Stone351eb612012-05-31 15:27:47 -04005060 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5061 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005062}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005063
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005064static void
5065switcher_cancel(struct weston_keyboard_grab *grab)
5066{
5067 struct switcher *switcher = container_of(grab, struct switcher, grab);
5068
5069 switcher_destroy(switcher);
5070}
5071
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005072static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005073 switcher_key,
5074 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005075 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076};
5077
5078static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005079switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005080 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081{
Tiago Vignattibe143262012-04-16 17:31:41 +03005082 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005083 struct switcher *switcher;
5084
5085 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005086 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005087 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005088 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005089 wl_list_init(&switcher->listener.link);
5090
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005091 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005092 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005094 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5095 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005096 switcher_next(switcher);
5097}
5098
Pekka Paalanen3c647232011-12-22 13:43:43 +02005099static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005100backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005101 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005102{
5103 struct weston_compositor *compositor = data;
5104 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005105 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005106
5107 /* TODO: we're limiting to simple use cases, where we assume just
5108 * control on the primary display. We'd have to extend later if we
5109 * ever get support for setting backlights on random desktop LCD
5110 * panels though */
5111 output = get_default_output(compositor);
5112 if (!output)
5113 return;
5114
5115 if (!output->set_backlight)
5116 return;
5117
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005118 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5119 backlight_new = output->backlight_current - 25;
5120 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5121 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005122
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005123 if (backlight_new < 5)
5124 backlight_new = 5;
5125 if (backlight_new > 255)
5126 backlight_new = 255;
5127
5128 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005129 output->set_backlight(output, output->backlight_current);
5130}
5131
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005132struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005133 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005134 struct weston_seat *seat;
5135 uint32_t key[2];
5136 int key_released[2];
5137};
5138
5139static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005140debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005141 uint32_t key, uint32_t state)
5142{
5143 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005144 struct weston_compositor *ec = db->seat->compositor;
5145 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005146 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005147 uint32_t serial;
5148 int send = 0, terminate = 0;
5149 int check_binding = 1;
5150 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005151 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005152
5153 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5154 /* Do not run bindings on key releases */
5155 check_binding = 0;
5156
5157 for (i = 0; i < 2; i++)
5158 if (key == db->key[i])
5159 db->key_released[i] = 1;
5160
5161 if (db->key_released[0] && db->key_released[1]) {
5162 /* All key releases been swalled so end the grab */
5163 terminate = 1;
5164 } else if (key != db->key[0] && key != db->key[1]) {
5165 /* Should not swallow release of other keys */
5166 send = 1;
5167 }
5168 } else if (key == db->key[0] && !db->key_released[0]) {
5169 /* Do not check bindings for the first press of the binding
5170 * key. This allows it to be used as a debug shortcut.
5171 * We still need to swallow this event. */
5172 check_binding = 0;
5173 } else if (db->key[1]) {
5174 /* If we already ran a binding don't process another one since
5175 * we can't keep track of all the binding keys that were
5176 * pressed in order to swallow the release events. */
5177 send = 1;
5178 check_binding = 0;
5179 }
5180
5181 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005182 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5183 key, state)) {
5184 /* We ran a binding so swallow the press and keep the
5185 * grab to swallow the released too. */
5186 send = 0;
5187 terminate = 0;
5188 db->key[1] = key;
5189 } else {
5190 /* Terminate the grab since the key pressed is not a
5191 * debug binding key. */
5192 send = 1;
5193 terminate = 1;
5194 }
5195 }
5196
5197 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005198 serial = wl_display_next_serial(display);
5199 resource_list = &grab->keyboard->focus_resource_list;
5200 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005201 wl_keyboard_send_key(resource, serial, time, key, state);
5202 }
5203 }
5204
5205 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005206 weston_keyboard_end_grab(grab->keyboard);
5207 if (grab->keyboard->input_method_resource)
5208 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005209 free(db);
5210 }
5211}
5212
5213static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005214debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005215 uint32_t mods_depressed, uint32_t mods_latched,
5216 uint32_t mods_locked, uint32_t group)
5217{
5218 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005219 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005220
Neil Roberts96d790e2013-09-19 17:32:00 +01005221 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005222
Neil Roberts96d790e2013-09-19 17:32:00 +01005223 wl_resource_for_each(resource, resource_list) {
5224 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5225 mods_latched, mods_locked, group);
5226 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005227}
5228
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005229static void
5230debug_binding_cancel(struct weston_keyboard_grab *grab)
5231{
5232 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5233
5234 weston_keyboard_end_grab(grab->keyboard);
5235 free(db);
5236}
5237
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005238struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005239 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005240 debug_binding_modifiers,
5241 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005242};
5243
5244static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005245debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005246{
5247 struct debug_binding_grab *grab;
5248
5249 grab = calloc(1, sizeof *grab);
5250 if (!grab)
5251 return;
5252
5253 grab->seat = (struct weston_seat *) seat;
5254 grab->key[0] = key;
5255 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005256 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005257}
5258
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005259static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005260force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005261 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005262{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005263 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005264 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005265 struct desktop_shell *shell = data;
5266 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005267 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005268
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005269 focus_surface = seat->keyboard->focus;
5270 if (!focus_surface)
5271 return;
5272
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005273 wl_signal_emit(&compositor->kill_signal, focus_surface);
5274
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005275 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005276 wl_client_get_credentials(client, &pid, NULL, NULL);
5277
5278 /* Skip clients that we launched ourselves (the credentials of
5279 * the socketpair is ours) */
5280 if (pid == getpid())
5281 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005282
Daniel Stone325fc2d2012-05-30 16:31:58 +01005283 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005284}
5285
5286static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005287workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005288 uint32_t key, void *data)
5289{
5290 struct desktop_shell *shell = data;
5291 unsigned int new_index = shell->workspaces.current;
5292
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005293 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005294 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005295 if (new_index != 0)
5296 new_index--;
5297
5298 change_workspace(shell, new_index);
5299}
5300
5301static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005302workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005303 uint32_t key, void *data)
5304{
5305 struct desktop_shell *shell = data;
5306 unsigned int new_index = shell->workspaces.current;
5307
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005308 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005309 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005310 if (new_index < shell->workspaces.num - 1)
5311 new_index++;
5312
5313 change_workspace(shell, new_index);
5314}
5315
5316static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005317workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005318 uint32_t key, void *data)
5319{
5320 struct desktop_shell *shell = data;
5321 unsigned int new_index;
5322
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005323 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005324 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005325 new_index = key - KEY_F1;
5326 if (new_index >= shell->workspaces.num)
5327 new_index = shell->workspaces.num - 1;
5328
5329 change_workspace(shell, new_index);
5330}
5331
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005332static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005333workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005334 uint32_t key, void *data)
5335{
5336 struct desktop_shell *shell = data;
5337 unsigned int new_index = shell->workspaces.current;
5338
5339 if (shell->locked)
5340 return;
5341
5342 if (new_index != 0)
5343 new_index--;
5344
5345 take_surface_to_workspace_by_seat(shell, seat, new_index);
5346}
5347
5348static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005349workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005350 uint32_t key, void *data)
5351{
5352 struct desktop_shell *shell = data;
5353 unsigned int new_index = shell->workspaces.current;
5354
5355 if (shell->locked)
5356 return;
5357
5358 if (new_index < shell->workspaces.num - 1)
5359 new_index++;
5360
5361 take_surface_to_workspace_by_seat(shell, seat, new_index);
5362}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005363
5364static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005365handle_output_destroy(struct wl_listener *listener, void *data)
5366{
5367 struct shell_output *output_listener =
5368 container_of(listener, struct shell_output, destroy_listener);
5369
5370 wl_list_remove(&output_listener->destroy_listener.link);
5371 wl_list_remove(&output_listener->link);
5372 free(output_listener);
5373}
5374
5375static void
5376create_shell_output(struct desktop_shell *shell,
5377 struct weston_output *output)
5378{
5379 struct shell_output *shell_output;
5380
5381 shell_output = zalloc(sizeof *shell_output);
5382 if (shell_output == NULL)
5383 return;
5384
5385 shell_output->output = output;
5386 shell_output->shell = shell;
5387 shell_output->destroy_listener.notify = handle_output_destroy;
5388 wl_signal_add(&output->destroy_signal,
5389 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005390 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005391}
5392
5393static void
5394handle_output_create(struct wl_listener *listener, void *data)
5395{
5396 struct desktop_shell *shell =
5397 container_of(listener, struct desktop_shell, output_create_listener);
5398 struct weston_output *output = (struct weston_output *)data;
5399
5400 create_shell_output(shell, output);
5401}
5402
5403static void
5404setup_output_destroy_handler(struct weston_compositor *ec,
5405 struct desktop_shell *shell)
5406{
5407 struct weston_output *output;
5408
5409 wl_list_init(&shell->output_list);
5410 wl_list_for_each(output, &ec->output_list, link)
5411 create_shell_output(shell, output);
5412
5413 shell->output_create_listener.notify = handle_output_create;
5414 wl_signal_add(&ec->output_created_signal,
5415 &shell->output_create_listener);
5416}
5417
5418static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005419shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005420{
Tiago Vignattibe143262012-04-16 17:31:41 +03005421 struct desktop_shell *shell =
5422 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005423 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005424 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005425
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005426 if (shell->child.client)
5427 wl_client_destroy(shell->child.client);
5428
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005429 wl_list_remove(&shell->idle_listener.link);
5430 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005431
5432 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005433
Xiong Zhang6b481422013-10-23 13:58:32 +08005434 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5435 wl_list_remove(&shell_output->destroy_listener.link);
5436 wl_list_remove(&shell_output->link);
5437 free(shell_output);
5438 }
5439
5440 wl_list_remove(&shell->output_create_listener.link);
5441
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005442 wl_array_for_each(ws, &shell->workspaces.array)
5443 workspace_destroy(*ws);
5444 wl_array_release(&shell->workspaces.array);
5445
Pekka Paalanen3c647232011-12-22 13:43:43 +02005446 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005447 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005448 free(shell);
5449}
5450
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005451static void
5452shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5453{
5454 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005455 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005456
5457 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005458 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5459 MODIFIER_CTRL | MODIFIER_ALT,
5460 terminate_binding, ec);
5461 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5462 click_to_activate_binding,
5463 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005464 weston_compositor_add_touch_binding(ec, 0,
5465 touch_to_activate_binding,
5466 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005467 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5468 MODIFIER_SUPER | MODIFIER_ALT,
5469 surface_opacity_binding, NULL);
5470 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5471 MODIFIER_SUPER, zoom_axis_binding,
5472 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005473
5474 /* configurable bindings */
5475 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005476 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5477 zoom_key_binding, NULL);
5478 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5479 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005480 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5481 maximize_binding, NULL);
5482 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5483 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005484 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5485 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005486 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005487 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5488 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005489
5490 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5491 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5492 rotate_binding, NULL);
5493
Daniel Stone325fc2d2012-05-30 16:31:58 +01005494 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5495 shell);
5496 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5497 ec);
5498 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5499 backlight_binding, ec);
5500 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5501 ec);
5502 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5503 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005504 weston_compositor_add_key_binding(ec, KEY_K, mod,
5505 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005506 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5507 workspace_up_binding, shell);
5508 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5509 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005510 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5511 workspace_move_surface_up_binding,
5512 shell);
5513 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5514 workspace_move_surface_down_binding,
5515 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005516
Daniel Stonedf8133b2013-11-19 11:37:14 +01005517 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5518
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005519 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5520 if (shell->workspaces.num > 1) {
5521 num_workspace_bindings = shell->workspaces.num;
5522 if (num_workspace_bindings > 6)
5523 num_workspace_bindings = 6;
5524 for (i = 0; i < num_workspace_bindings; i++)
5525 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5526 workspace_f_binding,
5527 shell);
5528 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005529
5530 /* Debug bindings */
5531 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5532 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005533}
5534
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005535WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005536module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005537 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005538{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005539 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005540 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005541 struct workspace **pws;
5542 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005543 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005544
Peter Huttererf3d62272013-08-08 11:57:05 +10005545 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005546 if (shell == NULL)
5547 return -1;
5548
Kristian Høgsberg75840622011-09-06 13:48:16 -04005549 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005550
5551 shell->destroy_listener.notify = shell_destroy;
5552 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005553 shell->idle_listener.notify = idle_handler;
5554 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5555 shell->wake_listener.notify = wake_handler;
5556 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005557
Scott Moreauff1db4a2012-04-17 19:06:18 -06005558 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005559 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005560 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005561 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005562 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005563 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005564 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005565 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005566 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005567 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005568 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005569
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005570 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5571 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005572 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5573 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005574 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005575
5576 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005577 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005578
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005579 if (input_panel_setup(shell) < 0)
5580 return -1;
5581
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005582 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005583
Daniel Stonedf8133b2013-11-19 11:37:14 +01005584 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5585 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5586
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005587 for (i = 0; i < shell->workspaces.num; i++) {
5588 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5589 if (pws == NULL)
5590 return -1;
5591
5592 *pws = workspace_create();
5593 if (*pws == NULL)
5594 return -1;
5595 }
5596 activate_workspace(shell, 0);
5597
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005598 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005599 wl_list_init(&shell->workspaces.animation.link);
5600 shell->workspaces.animation.frame = animate_workspace_change_frame;
5601
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005602 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005603 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005604 return -1;
5605
Rafael Antognollie2a34552013-12-03 15:35:45 -02005606 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5607 shell, bind_xdg_shell) == NULL)
5608 return -1;
5609
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005610 if (wl_global_create(ec->wl_display,
5611 &desktop_shell_interface, 2,
5612 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005613 return -1;
5614
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005615 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5616 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005617 return -1;
5618
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005619 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5620 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005621 return -1;
5622
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005623 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005624
Xiong Zhang6b481422013-10-23 13:58:32 +08005625 setup_output_destroy_handler(ec, shell);
5626
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005627 loop = wl_display_get_event_loop(ec->wl_display);
5628 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005629
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005630 shell->screensaver.timer =
5631 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5632
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005633 wl_list_for_each(seat, &ec->seat_list, link)
5634 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005635
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005636 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005637
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005638 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005639
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005640 return 0;
5641}