blob: 137ec7ab0b11cd4dd4e1b7c98c2794b8d32c1a6c [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øgsbergd56ab4e2014-01-16 16:51:52 -0800441
442 weston_config_section_get_string(section,
443 "exposay-modifier", &s, "none");
444 if (strcmp(s, "none") == 0)
445 shell->exposay_modifier = 0;
446 else
447 shell->exposay_modifier = get_modifier(s);
448 free(s);
449
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400450 weston_config_section_get_string(section, "animation", &s, "none");
451 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200452 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700453 weston_config_section_get_string(section,
454 "startup-animation", &s, "fade");
455 shell->startup_animation_type = get_animation_type(s);
456 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700457 if (shell->startup_animation_type == ANIMATION_ZOOM)
458 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100459 weston_config_section_get_string(section, "focus-animation", &s, "none");
460 shell->focus_animation_type = get_animation_type(s);
461 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400462 weston_config_section_get_uint(section, "num-workspaces",
463 &shell->workspaces.num,
464 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200465}
466
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800467struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100468get_default_output(struct weston_compositor *compositor)
469{
470 return container_of(compositor->output_list.next,
471 struct weston_output, link);
472}
473
474
475/* no-op func for checking focus surface */
476static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600477focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100478{
479}
480
481static struct focus_surface *
482get_focus_surface(struct weston_surface *surface)
483{
484 if (surface->configure == focus_surface_configure)
485 return surface->configure_private;
486 else
487 return NULL;
488}
489
490static bool
491is_focus_surface (struct weston_surface *es)
492{
493 return (es->configure == focus_surface_configure);
494}
495
496static bool
497is_focus_view (struct weston_view *view)
498{
499 return is_focus_surface (view->surface);
500}
501
502static struct focus_surface *
503create_focus_surface(struct weston_compositor *ec,
504 struct weston_output *output)
505{
506 struct focus_surface *fsurf = NULL;
507 struct weston_surface *surface = NULL;
508
509 fsurf = malloc(sizeof *fsurf);
510 if (!fsurf)
511 return NULL;
512
513 fsurf->surface = weston_surface_create(ec);
514 surface = fsurf->surface;
515 if (surface == NULL) {
516 free(fsurf);
517 return NULL;
518 }
519
520 surface->configure = focus_surface_configure;
521 surface->output = output;
522 surface->configure_private = fsurf;
523
524 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100525 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100526
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600527 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600528 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100529 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
530 pixman_region32_fini(&surface->opaque);
531 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
532 output->width, output->height);
533 pixman_region32_fini(&surface->input);
534 pixman_region32_init(&surface->input);
535
536 wl_list_init(&fsurf->workspace_transform.link);
537
538 return fsurf;
539}
540
541static void
542focus_surface_destroy(struct focus_surface *fsurf)
543{
544 weston_surface_destroy(fsurf->surface);
545 free(fsurf);
546}
547
548static void
549focus_animation_done(struct weston_view_animation *animation, void *data)
550{
551 struct workspace *ws = data;
552
553 ws->focus_animation = NULL;
554}
555
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200556static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200557focus_state_destroy(struct focus_state *state)
558{
559 wl_list_remove(&state->seat_destroy_listener.link);
560 wl_list_remove(&state->surface_destroy_listener.link);
561 free(state);
562}
563
564static void
565focus_state_seat_destroy(struct wl_listener *listener, void *data)
566{
567 struct focus_state *state = container_of(listener,
568 struct focus_state,
569 seat_destroy_listener);
570
571 wl_list_remove(&state->link);
572 focus_state_destroy(state);
573}
574
575static void
576focus_state_surface_destroy(struct wl_listener *listener, void *data)
577{
578 struct focus_state *state = container_of(listener,
579 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400580 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400581 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582 struct weston_surface *main_surface, *next;
583 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200584
Pekka Paalanen01388e22013-04-25 13:57:44 +0300585 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
586
Kristian Høgsberge3778222012-07-31 17:29:30 -0400587 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500588 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
589 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400590 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100591 if (is_focus_view(view))
592 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400593
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400595 break;
596 }
597
Pekka Paalanen01388e22013-04-25 13:57:44 +0300598 /* if the focus was a sub-surface, activate its main surface */
599 if (main_surface != state->keyboard_focus)
600 next = main_surface;
601
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100602 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100604 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400605 activate(shell, next, state->seat);
606 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
608 if (state->ws->focus_animation)
609 weston_view_animation_destroy(state->ws->focus_animation);
610
611 state->ws->focus_animation = weston_fade_run(
612 state->ws->fsurf_front->view,
613 state->ws->fsurf_front->view->alpha, 0.0, 300,
614 focus_animation_done, state->ws);
615 }
616
Kristian Høgsberge3778222012-07-31 17:29:30 -0400617 wl_list_remove(&state->link);
618 focus_state_destroy(state);
619 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200620}
621
622static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400623focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200624{
Jonas Ådahl04769742012-06-13 00:01:24 +0200625 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200626
627 state = malloc(sizeof *state);
628 if (state == NULL)
629 return NULL;
630
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400632 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200633 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400634 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200635
636 state->seat_destroy_listener.notify = focus_state_seat_destroy;
637 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400638 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200639 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400640 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200641
642 return state;
643}
644
Jonas Ådahl8538b222012-08-29 22:13:03 +0200645static struct focus_state *
646ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
647{
648 struct workspace *ws = get_current_workspace(shell);
649 struct focus_state *state;
650
651 wl_list_for_each(state, &ws->focus_list, link)
652 if (state->seat == seat)
653 break;
654
655 if (&state->link == &ws->focus_list)
656 state = focus_state_create(seat, ws);
657
658 return state;
659}
660
Jonas Ådahl04769742012-06-13 00:01:24 +0200661static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400662restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200663{
664 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400665 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200666
667 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800668 if (state->seat->keyboard == NULL)
669 continue;
670
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400671 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200672
Kristian Høgsberge3148752013-05-06 23:19:49 -0400673 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200674 }
675}
676
677static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200678replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
679 struct weston_seat *seat)
680{
681 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400682 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200683
684 wl_list_for_each(state, &ws->focus_list, link) {
685 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400686 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500687 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200688 return;
689 }
690 }
691}
692
693static void
694drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
695 struct weston_surface *surface)
696{
697 struct focus_state *state;
698
699 wl_list_for_each(state, &ws->focus_list, link)
700 if (state->keyboard_focus == surface)
701 state->keyboard_focus = NULL;
702}
703
704static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
706 struct weston_view *from, struct weston_view *to)
707{
708 struct weston_output *output;
709 bool focus_surface_created = false;
710
711 /* FIXME: Only support dim animation using two layers */
712 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
713 return;
714
715 output = get_default_output(shell->compositor);
716 if (ws->fsurf_front == NULL && (from || to)) {
717 ws->fsurf_front = create_focus_surface(shell->compositor, output);
718 ws->fsurf_back = create_focus_surface(shell->compositor, output);
719 ws->fsurf_front->view->alpha = 0.0;
720 ws->fsurf_back->view->alpha = 0.0;
721 focus_surface_created = true;
722 } else {
723 wl_list_remove(&ws->fsurf_front->view->layer_link);
724 wl_list_remove(&ws->fsurf_back->view->layer_link);
725 }
726
727 if (ws->focus_animation) {
728 weston_view_animation_destroy(ws->focus_animation);
729 ws->focus_animation = NULL;
730 }
731
732 if (to)
733 wl_list_insert(&to->layer_link,
734 &ws->fsurf_front->view->layer_link);
735 else if (from)
736 wl_list_insert(&ws->layer.view_list,
737 &ws->fsurf_front->view->layer_link);
738
739 if (focus_surface_created) {
740 ws->focus_animation = weston_fade_run(
741 ws->fsurf_front->view,
742 ws->fsurf_front->view->alpha, 0.6, 300,
743 focus_animation_done, ws);
744 } else if (from) {
745 wl_list_insert(&from->layer_link,
746 &ws->fsurf_back->view->layer_link);
747 ws->focus_animation = weston_stable_fade_run(
748 ws->fsurf_front->view, 0.0,
749 ws->fsurf_back->view, 0.6,
750 focus_animation_done, ws);
751 } else if (to) {
752 wl_list_insert(&ws->layer.view_list,
753 &ws->fsurf_back->view->layer_link);
754 ws->focus_animation = weston_stable_fade_run(
755 ws->fsurf_front->view, 0.0,
756 ws->fsurf_back->view, 0.6,
757 focus_animation_done, ws);
758 }
759}
760
761static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200762workspace_destroy(struct workspace *ws)
763{
Jonas Ådahl04769742012-06-13 00:01:24 +0200764 struct focus_state *state, *next;
765
766 wl_list_for_each_safe(state, next, &ws->focus_list, link)
767 focus_state_destroy(state);
768
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100769 if (ws->fsurf_front)
770 focus_surface_destroy(ws->fsurf_front);
771 if (ws->fsurf_back)
772 focus_surface_destroy(ws->fsurf_back);
773
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200774 free(ws);
775}
776
Jonas Ådahl04769742012-06-13 00:01:24 +0200777static void
778seat_destroyed(struct wl_listener *listener, void *data)
779{
780 struct weston_seat *seat = data;
781 struct focus_state *state, *next;
782 struct workspace *ws = container_of(listener,
783 struct workspace,
784 seat_destroyed_listener);
785
786 wl_list_for_each_safe(state, next, &ws->focus_list, link)
787 if (state->seat == seat)
788 wl_list_remove(&state->link);
789}
790
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200791static struct workspace *
792workspace_create(void)
793{
794 struct workspace *ws = malloc(sizeof *ws);
795 if (ws == NULL)
796 return NULL;
797
798 weston_layer_init(&ws->layer, NULL);
799
Jonas Ådahl04769742012-06-13 00:01:24 +0200800 wl_list_init(&ws->focus_list);
801 wl_list_init(&ws->seat_destroyed_listener.link);
802 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803 ws->fsurf_front = NULL;
804 ws->fsurf_back = NULL;
805 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200806
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200807 return ws;
808}
809
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810static int
811workspace_is_empty(struct workspace *ws)
812{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500813 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200814}
815
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200816static struct workspace *
817get_workspace(struct desktop_shell *shell, unsigned int index)
818{
819 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200820 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200821 pws += index;
822 return *pws;
823}
824
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800825struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200826get_current_workspace(struct desktop_shell *shell)
827{
828 return get_workspace(shell, shell->workspaces.current);
829}
830
831static void
832activate_workspace(struct desktop_shell *shell, unsigned int index)
833{
834 struct workspace *ws;
835
836 ws = get_workspace(shell, index);
837 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
838
839 shell->workspaces.current = index;
840}
841
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200842static unsigned int
843get_output_height(struct weston_output *output)
844{
845 return abs(output->region.extents.y1 - output->region.extents.y2);
846}
847
848static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851 struct weston_transform *transform;
852
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 if (is_focus_view(view)) {
854 struct focus_surface *fsurf = get_focus_surface(view->surface);
855 transform = &fsurf->workspace_transform;
856 } else {
857 struct shell_surface *shsurf = get_shell_surface(view->surface);
858 transform = &shsurf->workspace_transform;
859 }
860
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200864
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 weston_matrix_init(&transform->matrix);
866 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200867 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500868 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200869}
870
871static void
872workspace_translate_out(struct workspace *ws, double fraction)
873{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200875 unsigned int height;
876 double d;
877
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
879 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880 d = height * fraction;
881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200883 }
884}
885
886static void
887workspace_translate_in(struct workspace *ws, double fraction)
888{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200890 unsigned int height;
891 double d;
892
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
894 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200895
896 if (fraction > 0)
897 d = -(height - height * fraction);
898 else
899 d = height + height * fraction;
900
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100901 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200902 }
903}
904
905static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200906broadcast_current_workspace_state(struct desktop_shell *shell)
907{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700908 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200909
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700910 wl_resource_for_each(resource, &shell->workspaces.client_list)
911 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200912 shell->workspaces.current,
913 shell->workspaces.num);
914}
915
916static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917reverse_workspace_change_animation(struct desktop_shell *shell,
918 unsigned int index,
919 struct workspace *from,
920 struct workspace *to)
921{
922 shell->workspaces.current = index;
923
924 shell->workspaces.anim_to = to;
925 shell->workspaces.anim_from = from;
926 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
927 shell->workspaces.anim_timestamp = 0;
928
Scott Moreau4272e632012-08-13 09:58:41 -0600929 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930}
931
932static void
933workspace_deactivate_transforms(struct workspace *ws)
934{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100936 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100939 if (is_focus_view(view)) {
940 struct focus_surface *fsurf = get_focus_surface(view->surface);
941 transform = &fsurf->workspace_transform;
942 } else {
943 struct shell_surface *shsurf = get_shell_surface(view->surface);
944 transform = &shsurf->workspace_transform;
945 }
946
947 if (!wl_list_empty(&transform->link)) {
948 wl_list_remove(&transform->link);
949 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200952 }
953}
954
955static void
956finish_workspace_change_animation(struct desktop_shell *shell,
957 struct workspace *from,
958 struct workspace *to)
959{
Scott Moreau4272e632012-08-13 09:58:41 -0600960 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200961
962 wl_list_remove(&shell->workspaces.animation.link);
963 workspace_deactivate_transforms(from);
964 workspace_deactivate_transforms(to);
965 shell->workspaces.anim_to = NULL;
966
967 wl_list_remove(&shell->workspaces.anim_from->layer.link);
968}
969
970static void
971animate_workspace_change_frame(struct weston_animation *animation,
972 struct weston_output *output, uint32_t msecs)
973{
974 struct desktop_shell *shell =
975 container_of(animation, struct desktop_shell,
976 workspaces.animation);
977 struct workspace *from = shell->workspaces.anim_from;
978 struct workspace *to = shell->workspaces.anim_to;
979 uint32_t t;
980 double x, y;
981
982 if (workspace_is_empty(from) && workspace_is_empty(to)) {
983 finish_workspace_change_animation(shell, from, to);
984 return;
985 }
986
987 if (shell->workspaces.anim_timestamp == 0) {
988 if (shell->workspaces.anim_current == 0.0)
989 shell->workspaces.anim_timestamp = msecs;
990 else
991 shell->workspaces.anim_timestamp =
992 msecs -
993 /* Invers of movement function 'y' below. */
994 (asin(1.0 - shell->workspaces.anim_current) *
995 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
996 M_2_PI);
997 }
998
999 t = msecs - shell->workspaces.anim_timestamp;
1000
1001 /*
1002 * x = [0, π/2]
1003 * y(x) = sin(x)
1004 */
1005 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1006 y = sin(x);
1007
1008 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001009 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010
1011 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1012 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1013 shell->workspaces.anim_current = y;
1014
Scott Moreau4272e632012-08-13 09:58:41 -06001015 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001017 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019}
1020
1021static void
1022animate_workspace_change(struct desktop_shell *shell,
1023 unsigned int index,
1024 struct workspace *from,
1025 struct workspace *to)
1026{
1027 struct weston_output *output;
1028
1029 int dir;
1030
1031 if (index > shell->workspaces.current)
1032 dir = -1;
1033 else
1034 dir = 1;
1035
1036 shell->workspaces.current = index;
1037
1038 shell->workspaces.anim_dir = dir;
1039 shell->workspaces.anim_from = from;
1040 shell->workspaces.anim_to = to;
1041 shell->workspaces.anim_current = 0.0;
1042 shell->workspaces.anim_timestamp = 0;
1043
1044 output = container_of(shell->compositor->output_list.next,
1045 struct weston_output, link);
1046 wl_list_insert(&output->animation_list,
1047 &shell->workspaces.animation.link);
1048
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001049 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050
1051 workspace_translate_in(to, 0);
1052
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001053 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001054
Scott Moreau4272e632012-08-13 09:58:41 -06001055 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056}
1057
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001058static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001059update_workspace(struct desktop_shell *shell, unsigned int index,
1060 struct workspace *from, struct workspace *to)
1061{
1062 shell->workspaces.current = index;
1063 wl_list_insert(&from->layer.link, &to->layer.link);
1064 wl_list_remove(&from->layer.link);
1065}
1066
1067static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001068change_workspace(struct desktop_shell *shell, unsigned int index)
1069{
1070 struct workspace *from;
1071 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001072 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001073
1074 if (index == shell->workspaces.current)
1075 return;
1076
1077 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079 return;
1080
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001081 from = get_current_workspace(shell);
1082 to = get_workspace(shell, index);
1083
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001084 if (shell->workspaces.anim_from == to &&
1085 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001086 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001088 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089 return;
1090 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001091
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001092 if (shell->workspaces.anim_to != NULL)
1093 finish_workspace_change_animation(shell,
1094 shell->workspaces.anim_from,
1095 shell->workspaces.anim_to);
1096
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001097 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001098
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001099 if (shell->focus_animation_type != ANIMATION_NONE) {
1100 wl_list_for_each(state, &from->focus_list, link)
1101 if (state->keyboard_focus)
1102 animate_focus_change(shell, from,
1103 get_default_view(state->keyboard_focus), NULL);
1104
1105 wl_list_for_each(state, &to->focus_list, link)
1106 if (state->keyboard_focus)
1107 animate_focus_change(shell, to,
1108 NULL, get_default_view(state->keyboard_focus));
1109 }
1110
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001111 if (workspace_is_empty(to) && workspace_is_empty(from))
1112 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113 else
1114 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001115
1116 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001117}
1118
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119static bool
1120workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1121{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001123 struct wl_list *e;
1124
1125 if (wl_list_empty(list))
1126 return false;
1127
1128 e = list->next;
1129
1130 if (e->next != list)
1131 return false;
1132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001134}
1135
1136static void
Philip Withnall659163d2013-11-25 18:01:36 +00001137move_surface_to_workspace(struct desktop_shell *shell,
1138 struct shell_surface *shsurf,
1139 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001140{
1141 struct workspace *from;
1142 struct workspace *to;
1143 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001144 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001145 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001146
1147 if (workspace == shell->workspaces.current)
1148 return;
1149
Philip Withnall659163d2013-11-25 18:01:36 +00001150 view = get_default_view(shsurf->surface);
1151 if (!view)
1152 return;
1153
1154 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1155
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001156 if (workspace >= shell->workspaces.num)
1157 workspace = shell->workspaces.num - 1;
1158
Jonas Ådahle9d22502012-08-29 22:13:01 +02001159 from = get_current_workspace(shell);
1160 to = get_workspace(shell, workspace);
1161
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 wl_list_remove(&view->layer_link);
1163 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001164
Philip Withnall648a4dd2013-11-25 18:01:44 +00001165 shell_surface_update_child_surface_layers(shsurf);
1166
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001168 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1169 if (!seat->keyboard)
1170 continue;
1171
1172 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001174 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001175 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001176
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001178}
1179
1180static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001182 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001183 unsigned int index)
1184{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001185 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 struct shell_surface *shsurf;
1188 struct workspace *from;
1189 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001190 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191
Pekka Paalanen01388e22013-04-25 13:57:44 +03001192 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 view = get_default_view(surface);
1194 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001195 index == shell->workspaces.current ||
1196 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 return;
1198
1199 from = get_current_workspace(shell);
1200 to = get_workspace(shell, index);
1201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 wl_list_remove(&view->layer_link);
1203 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204
Philip Withnall659163d2013-11-25 18:01:36 +00001205 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001206 if (shsurf != NULL)
1207 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001208
Jonas Ådahle9d22502012-08-29 22:13:01 +02001209 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001210 drop_focus_state(shell, from, surface);
1211
1212 if (shell->workspaces.anim_from == to &&
1213 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001214 wl_list_remove(&to->layer.link);
1215 wl_list_insert(from->layer.link.prev, &to->layer.link);
1216
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001218 broadcast_current_workspace_state(shell);
1219
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 return;
1221 }
1222
1223 if (shell->workspaces.anim_to != NULL)
1224 finish_workspace_change_animation(shell,
1225 shell->workspaces.anim_from,
1226 shell->workspaces.anim_to);
1227
1228 if (workspace_is_empty(from) &&
1229 workspace_has_only(to, surface))
1230 update_workspace(shell, index, from, to);
1231 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001232 if (shsurf != NULL &&
1233 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 wl_list_insert(&shell->workspaces.anim_sticky_list,
1235 &shsurf->workspace_transform.link);
1236
1237 animate_workspace_change(shell, index, from, to);
1238 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239
1240 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001241
1242 state = ensure_focus_state(shell, seat);
1243 if (state != NULL)
1244 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001245}
1246
1247static void
1248workspace_manager_move_surface(struct wl_client *client,
1249 struct wl_resource *resource,
1250 struct wl_resource *surface_resource,
1251 uint32_t workspace)
1252{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001253 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001254 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001255 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001256 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001257 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001258
Pekka Paalanen01388e22013-04-25 13:57:44 +03001259 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001260 shell_surface = get_shell_surface(main_surface);
1261 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001263
1264 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001265}
1266
1267static const struct workspace_manager_interface workspace_manager_implementation = {
1268 workspace_manager_move_surface,
1269};
1270
1271static void
1272unbind_resource(struct wl_resource *resource)
1273{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001274 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275}
1276
1277static void
1278bind_workspace_manager(struct wl_client *client,
1279 void *data, uint32_t version, uint32_t id)
1280{
1281 struct desktop_shell *shell = data;
1282 struct wl_resource *resource;
1283
Jason Ekstranda85118c2013-06-27 20:17:02 -05001284 resource = wl_resource_create(client,
1285 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001286
1287 if (resource == NULL) {
1288 weston_log("couldn't add workspace manager object");
1289 return;
1290 }
1291
Jason Ekstranda85118c2013-06-27 20:17:02 -05001292 wl_resource_set_implementation(resource,
1293 &workspace_manager_implementation,
1294 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001295 wl_list_insert(&shell->workspaces.client_list,
1296 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297
1298 workspace_manager_send_state(resource,
1299 shell->workspaces.current,
1300 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001301}
1302
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001303static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001304touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1305 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1306{
1307}
1308
1309static void
1310touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1311{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001312 struct weston_touch_move_grab *move =
1313 (struct weston_touch_move_grab *) container_of(
1314 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001315
Jonas Ådahl9484b692013-12-02 22:05:03 +01001316 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001317 shell_touch_grab_end(&move->base);
1318 free(move);
1319 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001320}
1321
1322static void
1323touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1324 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1325{
1326 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1327 struct shell_surface *shsurf = move->base.shsurf;
1328 struct weston_surface *es;
1329 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1330 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1331
1332 if (!shsurf)
1333 return;
1334
1335 es = shsurf->surface;
1336
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001337 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001338
1339 weston_compositor_schedule_repaint(es->compositor);
1340}
1341
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001342static void
1343touch_move_grab_cancel(struct weston_touch_grab *grab)
1344{
1345 struct weston_touch_move_grab *move =
1346 (struct weston_touch_move_grab *) container_of(
1347 grab, struct shell_touch_grab, grab);
1348
1349 shell_touch_grab_end(&move->base);
1350 free(move);
1351}
1352
Rusty Lynch1084da52013-08-15 09:10:08 -07001353static const struct weston_touch_grab_interface touch_move_grab_interface = {
1354 touch_move_grab_down,
1355 touch_move_grab_up,
1356 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001357 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001358};
1359
1360static int
1361surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1362{
1363 struct weston_touch_move_grab *move;
1364
1365 if (!shsurf)
1366 return -1;
1367
Rafael Antognolli03b16592013-12-03 15:35:42 -02001368 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001369 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001370 if (shsurf->grabbed)
1371 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001372
1373 move = malloc(sizeof *move);
1374 if (!move)
1375 return -1;
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001378 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001380 seat->touch->grab_y;
1381
1382 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1383 seat->touch);
1384
1385 return 0;
1386}
1387
1388static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001389noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001390{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001391}
1392
1393static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001394move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1395 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001396{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001397 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001398 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001399 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001400 int dx, dy;
1401
1402 weston_pointer_move(pointer, x, y);
1403 dx = wl_fixed_to_int(pointer->x + move->dx);
1404 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001405
1406 if (!shsurf)
1407 return;
1408
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001409 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001412}
1413
1414static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001415move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001416 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001417{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001418 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1419 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001420 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001421 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001422
Daniel Stone4dbadb12012-05-30 16:31:51 +01001423 if (pointer->button_count == 0 &&
1424 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001425 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001426 free(grab);
1427 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001428}
1429
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001430static void
1431move_grab_cancel(struct weston_pointer_grab *grab)
1432{
1433 struct shell_grab *shell_grab =
1434 container_of(grab, struct shell_grab, grab);
1435
1436 shell_grab_end(shell_grab);
1437 free(grab);
1438}
1439
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001440static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001441 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001442 move_grab_motion,
1443 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001444 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001445};
1446
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001447static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001448surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001449{
1450 struct weston_move_grab *move;
1451
1452 if (!shsurf)
1453 return -1;
1454
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001455 if (shsurf->grabbed)
1456 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001457 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001458 return 0;
1459
1460 move = malloc(sizeof *move);
1461 if (!move)
1462 return -1;
1463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001465 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001467 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001468
1469 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001470 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001471
1472 return 0;
1473}
1474
1475static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001476common_surface_move(struct wl_resource *resource,
1477 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001478{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001479 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001480 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001481 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001482
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001483 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001484 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001485 seat->pointer->button_count > 0 &&
1486 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001488 if ((surface == shsurf->surface) &&
1489 (surface_move(shsurf, seat) < 0))
1490 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001491 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001492 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001493 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001495 if ((surface == shsurf->surface) &&
1496 (surface_touch_move(shsurf, seat) < 0))
1497 wl_resource_post_no_memory(resource);
1498 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499}
1500
Rafael Antognollie2a34552013-12-03 15:35:45 -02001501static void
1502shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1503 struct wl_resource *seat_resource, uint32_t serial)
1504{
1505 common_surface_move(resource, seat_resource, serial);
1506}
1507
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001508struct weston_resize_grab {
1509 struct shell_grab base;
1510 uint32_t edges;
1511 int32_t width, height;
1512};
1513
1514static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001515resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1516 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001517{
1518 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001520 struct shell_surface *shsurf = resize->base.shsurf;
1521 int32_t width, height;
1522 wl_fixed_t from_x, from_y;
1523 wl_fixed_t to_x, to_y;
1524
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001525 weston_pointer_move(pointer, x, y);
1526
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001527 if (!shsurf)
1528 return;
1529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 weston_view_from_global_fixed(shsurf->view,
1531 pointer->grab_x, pointer->grab_y,
1532 &from_x, &from_y);
1533 weston_view_from_global_fixed(shsurf->view,
1534 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001535
1536 width = resize->width;
1537 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1538 width += wl_fixed_to_int(from_x - to_x);
1539 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1540 width += wl_fixed_to_int(to_x - from_x);
1541 }
1542
1543 height = resize->height;
1544 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1545 height += wl_fixed_to_int(from_y - to_y);
1546 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1547 height += wl_fixed_to_int(to_y - from_y);
1548 }
1549
1550 shsurf->client->send_configure(shsurf->surface,
1551 resize->edges, width, height);
1552}
1553
1554static void
1555send_configure(struct weston_surface *surface,
1556 uint32_t edges, int32_t width, int32_t height)
1557{
1558 struct shell_surface *shsurf = get_shell_surface(surface);
1559
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001560 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 edges, width, height);
1562}
1563
1564static const struct weston_shell_client shell_client = {
1565 send_configure
1566};
1567
1568static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001569resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 uint32_t time, uint32_t button, uint32_t state_w)
1571{
1572 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001573 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574 enum wl_pointer_button_state state = state_w;
1575
1576 if (pointer->button_count == 0 &&
1577 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1578 shell_grab_end(&resize->base);
1579 free(grab);
1580 }
1581}
1582
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001583static void
1584resize_grab_cancel(struct weston_pointer_grab *grab)
1585{
1586 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1587
1588 shell_grab_end(&resize->base);
1589 free(grab);
1590}
1591
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001592static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001593 noop_grab_focus,
1594 resize_grab_motion,
1595 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001596 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597};
1598
Giulio Camuffob8366642013-04-25 13:57:46 +03001599/*
1600 * Returns the bounding box of a surface and all its sub-surfaces,
1601 * in the surface coordinates system. */
1602static void
1603surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1604 int32_t *y, int32_t *w, int32_t *h) {
1605 pixman_region32_t region;
1606 pixman_box32_t *box;
1607 struct weston_subsurface *subsurface;
1608
1609 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 surface->width,
1611 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001612
1613 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1614 pixman_region32_union_rect(&region, &region,
1615 subsurface->position.x,
1616 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 subsurface->surface->width,
1618 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001619 }
1620
1621 box = pixman_region32_extents(&region);
1622 if (x)
1623 *x = box->x1;
1624 if (y)
1625 *y = box->y1;
1626 if (w)
1627 *w = box->x2 - box->x1;
1628 if (h)
1629 *h = box->y2 - box->y1;
1630
1631 pixman_region32_fini(&region);
1632}
1633
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634static int
1635surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001636 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637{
1638 struct weston_resize_grab *resize;
1639
Rafael Antognolli03b16592013-12-03 15:35:42 -02001640 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641 return 0;
1642
1643 if (edges == 0 || edges > 15 ||
1644 (edges & 3) == 3 || (edges & 12) == 12)
1645 return 0;
1646
1647 resize = malloc(sizeof *resize);
1648 if (!resize)
1649 return -1;
1650
1651 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001652 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1653 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654
1655 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001656 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657
1658 return 0;
1659}
1660
1661static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001662common_surface_resize(struct wl_resource *resource,
1663 struct wl_resource *seat_resource, uint32_t serial,
1664 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001666 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001667 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001668 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669
Rafael Antognolli03b16592013-12-03 15:35:42 -02001670 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 return;
1672
Kristian Høgsberge3148752013-05-06 23:19:49 -04001673 if (seat->pointer->button_count == 0 ||
1674 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001675 seat->pointer->focus == NULL)
1676 return;
1677
1678 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1679 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680 return;
1681
Kristian Høgsberge3148752013-05-06 23:19:49 -04001682 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001683 wl_resource_post_no_memory(resource);
1684}
1685
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001686static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001687shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1688 struct wl_resource *seat_resource, uint32_t serial,
1689 uint32_t edges)
1690{
1691 common_surface_resize(resource, seat_resource, serial, edges);
1692}
1693
1694static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001695end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1696
1697static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001698busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001699{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001700 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001701 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001703 wl_fixed_t sx, sy;
1704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 view = weston_compositor_pick_view(pointer->seat->compositor,
1706 pointer->x, pointer->y,
1707 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001710 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001711}
1712
1713static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001714busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1715 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001716{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001717 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001718}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001719
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001720static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001721busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001722 uint32_t time, uint32_t button, uint32_t state)
1723{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001724 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001725 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001726 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001727
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001728 if (shsurf && button == BTN_LEFT && state) {
1729 activate(shsurf->shell, shsurf->surface, seat);
1730 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001731 } else if (shsurf && button == BTN_RIGHT && state) {
1732 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001733 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001734 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001735}
1736
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001737static void
1738busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1739{
1740 struct shell_grab *grab = (struct shell_grab *) base;
1741
1742 shell_grab_end(grab);
1743 free(grab);
1744}
1745
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001746static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001747 busy_cursor_grab_focus,
1748 busy_cursor_grab_motion,
1749 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001750 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001751};
1752
1753static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001754set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755{
1756 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757
1758 grab = malloc(sizeof *grab);
1759 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001760 return;
1761
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001762 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1763 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001764}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001766static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001767end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768{
1769 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1770
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001771 if (grab->grab.interface == &busy_cursor_grab_interface &&
1772 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001773 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001775 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001776}
1777
Scott Moreau9521d5e2012-04-19 13:06:17 -06001778static void
1779ping_timer_destroy(struct shell_surface *shsurf)
1780{
1781 if (!shsurf || !shsurf->ping_timer)
1782 return;
1783
1784 if (shsurf->ping_timer->source)
1785 wl_event_source_remove(shsurf->ping_timer->source);
1786
1787 free(shsurf->ping_timer);
1788 shsurf->ping_timer = NULL;
1789}
1790
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001791static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001792ping_timeout_handler(void *data)
1793{
1794 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001796
Scott Moreau9521d5e2012-04-19 13:06:17 -06001797 /* Client is not responding */
1798 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001799
1800 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08001801 if (seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001802 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001803 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001804
1805 return 1;
1806}
1807
1808static void
1809ping_handler(struct weston_surface *surface, uint32_t serial)
1810{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001811 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001812 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001814
1815 if (!shsurf)
1816 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001817 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001818 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001819
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001820 if (shsurf->surface == shsurf->shell->grab_surface)
1821 return;
1822
Scott Moreauff1db4a2012-04-17 19:06:18 -06001823 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001824 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001825 if (!shsurf->ping_timer)
1826 return;
1827
1828 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001829 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1830 shsurf->ping_timer->source =
1831 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1832 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1833
Rafael Antognollie2a34552013-12-03 15:35:45 -02001834 if (shell_surface_is_wl_shell_surface(shsurf))
1835 wl_shell_surface_send_ping(shsurf->resource, serial);
1836 else if (shell_surface_is_xdg_surface(shsurf))
1837 xdg_surface_send_ping(shsurf->resource, serial);
1838 else if (shell_surface_is_xdg_popup(shsurf))
1839 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001840 }
1841}
1842
1843static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001844handle_pointer_focus(struct wl_listener *listener, void *data)
1845{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001846 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848 struct weston_compositor *compositor;
1849 struct shell_surface *shsurf;
1850 uint32_t serial;
1851
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001853 return;
1854
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 compositor = view->surface->compositor;
1856 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001857
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001858 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001859 set_busy_cursor(shsurf, pointer);
1860 } else {
1861 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001863 }
1864}
1865
1866static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001867create_pointer_focus_listener(struct weston_seat *seat)
1868{
1869 struct wl_listener *listener;
1870
Kristian Høgsberge3148752013-05-06 23:19:49 -04001871 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001872 return;
1873
1874 listener = malloc(sizeof *listener);
1875 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001876 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001877}
1878
1879static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001880xdg_surface_set_transient_for(struct wl_client *client,
1881 struct wl_resource *resource,
1882 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001884 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001885 struct weston_surface *parent;
1886
1887 if (parent_resource)
1888 parent = wl_resource_get_user_data(parent_resource);
1889 else
1890 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001891
1892 shell_surface_set_parent(shsurf, parent);
1893}
1894
1895static void
1896surface_pong(struct shell_surface *shsurf, uint32_t serial)
1897{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001898 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001899 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001901 if (shsurf->ping_timer == NULL)
1902 /* Just ignore unsolicited pong. */
1903 return;
1904
Scott Moreauff1db4a2012-04-17 19:06:18 -06001905 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001907 wl_list_for_each(seat, &ec->seat_list, link) {
1908 if(seat->pointer)
1909 end_busy_cursor(shsurf, seat->pointer);
1910 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001911 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001912 }
1913}
1914
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001915static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001916shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1917 uint32_t serial)
1918{
1919 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1920
1921 surface_pong(shsurf, serial);
1922
1923}
1924
1925static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001926set_title(struct shell_surface *shsurf, const char *title)
1927{
1928 free(shsurf->title);
1929 shsurf->title = strdup(title);
1930}
1931
1932static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001933shell_surface_set_title(struct wl_client *client,
1934 struct wl_resource *resource, const char *title)
1935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001936 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001937
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001938 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001939}
1940
1941static void
1942shell_surface_set_class(struct wl_client *client,
1943 struct wl_resource *resource, const char *class)
1944{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001945 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001946
1947 free(shsurf->class);
1948 shsurf->class = strdup(class);
1949}
1950
Alex Wu4539b082012-03-01 12:57:46 +08001951static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001952restore_output_mode(struct weston_output *output)
1953{
Hardening57388e42013-09-18 23:56:36 +02001954 if (output->current_mode != output->original_mode ||
1955 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001956 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001957 output->original_mode,
1958 output->original_scale,
1959 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001960}
1961
1962static void
1963restore_all_output_modes(struct weston_compositor *compositor)
1964{
1965 struct weston_output *output;
1966
1967 wl_list_for_each(output, &compositor->output_list, link)
1968 restore_output_mode(output);
1969}
1970
Philip Withnallbecb77e2013-11-25 18:01:30 +00001971static int
1972get_output_panel_height(struct desktop_shell *shell,
1973 struct weston_output *output)
1974{
1975 struct weston_view *view;
1976 int panel_height = 0;
1977
1978 if (!output)
1979 return 0;
1980
1981 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1982 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001983 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001984 break;
1985 }
1986 }
1987
1988 return panel_height;
1989}
1990
Philip Withnall07926d92013-11-25 18:01:40 +00001991/* The surface will be inserted into the list immediately after the link
1992 * returned by this function (i.e. will be stacked immediately above the
1993 * returned link). */
1994static struct wl_list *
1995shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1996{
1997 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08001998 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00001999
2000 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002001 case SHELL_SURFACE_POPUP:
2002 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002003 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002004 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002005 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002006 /* Move the surface to its parent layer so
2007 * that surfaces which are transient for
2008 * fullscreen surfaces don't get hidden by the
2009 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002010
2011 /* TODO: Handle a parent with multiple views */
2012 parent = get_default_view(shsurf->parent);
2013 if (parent)
2014 return parent->layer_link.prev;
2015 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002016 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002017
2018 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002019 return &shsurf->shell->fullscreen_layer.view_list;
2020
Philip Withnall07926d92013-11-25 18:01:40 +00002021 case SHELL_SURFACE_NONE:
2022 default:
2023 /* Go to the fallback, below. */
2024 break;
2025 }
2026
2027 /* Move the surface to a normal workspace layer so that surfaces
2028 * which were previously fullscreen or transient are no longer
2029 * rendered on top. */
2030 ws = get_current_workspace(shsurf->shell);
2031 return &ws->layer.view_list;
2032}
2033
Philip Withnall648a4dd2013-11-25 18:01:44 +00002034static void
2035shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2036{
2037 struct shell_surface *child;
2038
2039 /* Move the child layers to the same workspace as shsurf. They will be
2040 * stacked above shsurf. */
2041 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2042 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2043 weston_view_geometry_dirty(child->view);
2044 wl_list_remove(&child->view->layer_link);
2045 wl_list_insert(shsurf->view->layer_link.prev,
2046 &child->view->layer_link);
2047 weston_view_geometry_dirty(child->view);
2048 weston_surface_damage(child->surface);
2049
2050 /* Recurse. We don’t expect this to recurse very far (if
2051 * at all) because that would imply we have transient
2052 * (or popup) children of transient surfaces, which
2053 * would be unusual. */
2054 shell_surface_update_child_surface_layers(child);
2055 }
2056 }
2057}
2058
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002059/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002060 * geometry to ensure the changes are redrawn.
2061 *
2062 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2063 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002064static void
2065shell_surface_update_layer(struct shell_surface *shsurf)
2066{
2067 struct wl_list *new_layer_link;
2068
2069 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2070
2071 if (new_layer_link == &shsurf->view->layer_link)
2072 return;
2073
2074 weston_view_geometry_dirty(shsurf->view);
2075 wl_list_remove(&shsurf->view->layer_link);
2076 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2077 weston_view_geometry_dirty(shsurf->view);
2078 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002079
2080 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002081}
2082
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002083static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002084shell_surface_set_parent(struct shell_surface *shsurf,
2085 struct weston_surface *parent)
2086{
2087 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002088
2089 wl_list_remove(&shsurf->children_link);
2090 wl_list_init(&shsurf->children_link);
2091
2092 /* Insert into the parent surface’s child list. */
2093 if (parent != NULL) {
2094 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2095 if (parent_shsurf != NULL)
2096 wl_list_insert(&parent_shsurf->children_list,
2097 &shsurf->children_link);
2098 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002099}
2100
2101static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002102shell_surface_set_output(struct shell_surface *shsurf,
2103 struct weston_output *output)
2104{
2105 struct weston_surface *es = shsurf->surface;
2106
2107 /* get the default output, if the client set it as NULL
2108 check whether the ouput is available */
2109 if (output)
2110 shsurf->output = output;
2111 else if (es->output)
2112 shsurf->output = es->output;
2113 else
2114 shsurf->output = get_default_output(es->compositor);
2115}
2116
2117static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002118surface_clear_next_states(struct shell_surface *shsurf)
2119{
2120 shsurf->next_state.maximized = false;
2121 shsurf->next_state.fullscreen = false;
2122
2123 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2124 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2125 shsurf->state_changed = true;
2126}
2127
2128static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002129set_toplevel(struct shell_surface *shsurf)
2130{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002131 shell_surface_set_parent(shsurf, NULL);
2132 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002133 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002134
2135 /* The layer_link is updated in set_surface_type(),
2136 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002137}
2138
2139static void
2140shell_surface_set_toplevel(struct wl_client *client,
2141 struct wl_resource *resource)
2142{
2143 struct shell_surface *surface = wl_resource_get_user_data(resource);
2144
2145 set_toplevel(surface);
2146}
2147
2148static void
2149set_transient(struct shell_surface *shsurf,
2150 struct weston_surface *parent, int x, int y, uint32_t flags)
2151{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002152 assert(parent != NULL);
2153
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002154 shell_surface_set_parent(shsurf, parent);
2155
2156 surface_clear_next_states(shsurf);
2157
Philip Withnallbecb77e2013-11-25 18:01:30 +00002158 shsurf->transient.x = x;
2159 shsurf->transient.y = y;
2160 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002161
Rafael Antognollied207b42013-12-03 15:35:43 -02002162 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002163 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002164 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002165
2166 /* The layer_link is updated in set_surface_type(),
2167 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002168}
2169
2170static void
2171shell_surface_set_transient(struct wl_client *client,
2172 struct wl_resource *resource,
2173 struct wl_resource *parent_resource,
2174 int x, int y, uint32_t flags)
2175{
2176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2177 struct weston_surface *parent =
2178 wl_resource_get_user_data(parent_resource);
2179
2180 set_transient(shsurf, parent, x, y, flags);
2181}
2182
2183static void
2184set_fullscreen(struct shell_surface *shsurf,
2185 uint32_t method,
2186 uint32_t framerate,
2187 struct weston_output *output)
2188{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002189 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002190
2191 shsurf->fullscreen_output = shsurf->output;
2192 shsurf->fullscreen.type = method;
2193 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002194
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002195 shsurf->next_state.fullscreen = true;
2196 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002197 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002198
2199 shsurf->client->send_configure(shsurf->surface, 0,
2200 shsurf->output->width,
2201 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002202
2203 /* The layer_link is updated in set_surface_type(),
2204 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002205}
2206
2207static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002208weston_view_set_initial_position(struct weston_view *view,
2209 struct desktop_shell *shell);
2210
2211static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002212unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002213{
Philip Withnallf85fe842013-11-25 18:01:37 +00002214 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002215 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2216 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002217 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002218 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002219 shsurf->fullscreen_output = NULL;
2220
Alex Wu4539b082012-03-01 12:57:46 +08002221 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2222 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002223
Alex Wu4539b082012-03-01 12:57:46 +08002224 wl_list_remove(&shsurf->fullscreen.transform.link);
2225 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002226
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227 if (shsurf->fullscreen.black_view)
2228 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2229 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002230
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002231 if (shsurf->saved_position_valid)
2232 weston_view_set_position(shsurf->view,
2233 shsurf->saved_x, shsurf->saved_y);
2234 else
2235 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2236
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002237 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002239 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002240 shsurf->saved_rotation_valid = false;
2241 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002242
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002243 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002244}
2245
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002246static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002247shell_surface_set_fullscreen(struct wl_client *client,
2248 struct wl_resource *resource,
2249 uint32_t method,
2250 uint32_t framerate,
2251 struct wl_resource *output_resource)
2252{
2253 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2254 struct weston_output *output;
2255
2256 if (output_resource)
2257 output = wl_resource_get_user_data(output_resource);
2258 else
2259 output = NULL;
2260
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002261 shell_surface_set_parent(shsurf, NULL);
2262
Rafael Antognolli03b16592013-12-03 15:35:42 -02002263 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002264 set_fullscreen(shsurf, method, framerate, output);
2265}
2266
2267static void
2268set_popup(struct shell_surface *shsurf,
2269 struct weston_surface *parent,
2270 struct weston_seat *seat,
2271 uint32_t serial,
2272 int32_t x,
2273 int32_t y)
2274{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002275 assert(parent != NULL);
2276
Philip Withnallbecb77e2013-11-25 18:01:30 +00002277 shsurf->popup.shseat = get_shell_seat(seat);
2278 shsurf->popup.serial = serial;
2279 shsurf->popup.x = x;
2280 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002281
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002282 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002283}
2284
2285static void
2286shell_surface_set_popup(struct wl_client *client,
2287 struct wl_resource *resource,
2288 struct wl_resource *seat_resource,
2289 uint32_t serial,
2290 struct wl_resource *parent_resource,
2291 int32_t x, int32_t y, uint32_t flags)
2292{
2293 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002294 struct weston_surface *parent =
2295 wl_resource_get_user_data(parent_resource);
2296
2297 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002298
Rafael Antognolli03b16592013-12-03 15:35:42 -02002299 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002301 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302 wl_resource_get_user_data(seat_resource),
2303 serial, x, y);
2304}
2305
2306static void
2307set_maximized(struct shell_surface *shsurf,
2308 struct weston_output *output)
2309{
2310 struct desktop_shell *shell;
2311 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002312
Philip Withnall352e7ed2013-11-25 18:01:35 +00002313 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314
2315 shell = shell_surface_get_shell(shsurf);
2316 panel_height = get_output_panel_height(shell, shsurf->output);
2317 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2318
2319 shsurf->client->send_configure(shsurf->surface, edges,
2320 shsurf->output->width,
2321 shsurf->output->height - panel_height);
2322
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002323 shsurf->next_state.maximized = true;
2324 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002325 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002326}
2327
2328static void
2329unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002330{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002331 /* undo all maximized things here */
2332 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002333
2334 if (shsurf->saved_position_valid)
2335 weston_view_set_position(shsurf->view,
2336 shsurf->saved_x, shsurf->saved_y);
2337 else
2338 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002339
2340 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002341 wl_list_insert(&shsurf->view->geometry.transformation_list,
2342 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002343 shsurf->saved_rotation_valid = false;
2344 }
2345
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002346 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002347}
2348
Philip Withnallbecb77e2013-11-25 18:01:30 +00002349static void
2350shell_surface_set_maximized(struct wl_client *client,
2351 struct wl_resource *resource,
2352 struct wl_resource *output_resource)
2353{
2354 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2355 struct weston_output *output;
2356
2357 if (output_resource)
2358 output = wl_resource_get_user_data(output_resource);
2359 else
2360 output = NULL;
2361
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002362 shell_surface_set_parent(shsurf, NULL);
2363
Rafael Antognolli03b16592013-12-03 15:35:42 -02002364 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002365 set_maximized(shsurf, output);
2366}
2367
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002368/* This is only ever called from set_surface_type(), so there’s no need to
2369 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002370static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002371reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002372{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002373 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002374 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002375 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002376 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002377
Pekka Paalanen98262232011-12-01 10:42:22 +02002378 return 0;
2379}
2380
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002381static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002382set_full_output(struct shell_surface *shsurf)
2383{
2384 shsurf->saved_x = shsurf->view->geometry.x;
2385 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002386 shsurf->saved_width = shsurf->surface->width;
2387 shsurf->saved_height = shsurf->surface->height;
2388 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002389 shsurf->saved_position_valid = true;
2390
2391 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2392 wl_list_remove(&shsurf->rotation.transform.link);
2393 wl_list_init(&shsurf->rotation.transform.link);
2394 weston_view_geometry_dirty(shsurf->view);
2395 shsurf->saved_rotation_valid = true;
2396 }
2397}
2398
2399static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002400set_surface_type(struct shell_surface *shsurf)
2401{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002402 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002404
Philip Withnallbecb77e2013-11-25 18:01:30 +00002405 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002406
Rafael Antognolli03b16592013-12-03 15:35:42 -02002407 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002408 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002409
2410 switch (shsurf->type) {
2411 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002412 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002413 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002414 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002415 weston_view_set_position(shsurf->view,
2416 pev->geometry.x + shsurf->transient.x,
2417 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002418 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002419 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002420
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002421 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422 weston_view_set_position(shsurf->view, shsurf->transient.x,
2423 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002424 break;
2425
Philip Withnall0f640e22013-11-25 18:01:31 +00002426 case SHELL_SURFACE_POPUP:
2427 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002428 default:
2429 break;
2430 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002431
2432 /* Update the surface’s layer. */
2433 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002434}
2435
Tiago Vignattibe143262012-04-16 17:31:41 +03002436static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002437shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002438{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002439 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002440}
2441
Alex Wu21858432012-04-01 20:13:08 +08002442static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002443black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002444
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002446create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002447 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002448 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002449{
2450 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002451 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002452
2453 surface = weston_surface_create(ec);
2454 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002455 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002456 return NULL;
2457 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 view = weston_view_create(surface);
2459 if (surface == NULL) {
2460 weston_log("no memory\n");
2461 weston_surface_destroy(surface);
2462 return NULL;
2463 }
Alex Wu4539b082012-03-01 12:57:46 +08002464
Alex Wu21858432012-04-01 20:13:08 +08002465 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002466 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002467 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002468 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002469 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002470 pixman_region32_fini(&surface->input);
2471 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002472
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002473 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002474 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475
2476 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002477}
2478
Philip Withnalled8f1a92013-11-25 18:01:43 +00002479static void
2480shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2481{
2482 struct weston_output *output = shsurf->fullscreen_output;
2483
Rafael Antognolli03b16592013-12-03 15:35:42 -02002484 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002485
2486 if (!shsurf->fullscreen.black_view)
2487 shsurf->fullscreen.black_view =
2488 create_black_surface(shsurf->surface->compositor,
2489 shsurf->surface,
2490 output->x, output->y,
2491 output->width,
2492 output->height);
2493
2494 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2495 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2496 wl_list_insert(&shsurf->view->layer_link,
2497 &shsurf->fullscreen.black_view->layer_link);
2498 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2499 weston_surface_damage(shsurf->surface);
2500}
2501
Alex Wu4539b082012-03-01 12:57:46 +08002502/* Create black surface and append it to the associated fullscreen surface.
2503 * Handle size dismatch and positioning according to the method. */
2504static void
2505shell_configure_fullscreen(struct shell_surface *shsurf)
2506{
2507 struct weston_output *output = shsurf->fullscreen_output;
2508 struct weston_surface *surface = shsurf->surface;
2509 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002510 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002511 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002512
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002513 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2514 restore_output_mode(output);
2515
Philip Withnalled8f1a92013-11-25 18:01:43 +00002516 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002517
Jason Ekstranda7af7042013-10-12 22:38:11 -05002518 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002519 &surf_width, &surf_height);
2520
Alex Wu4539b082012-03-01 12:57:46 +08002521 switch (shsurf->fullscreen.type) {
2522 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002523 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002525 break;
2526 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002527 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002528 if (output->width == surf_width &&
2529 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002530 weston_view_set_position(shsurf->view,
2531 output->x - surf_x,
2532 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002533 break;
2534 }
2535
Alex Wu4539b082012-03-01 12:57:46 +08002536 matrix = &shsurf->fullscreen.transform.matrix;
2537 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002538
Scott Moreau1bad5db2012-08-18 01:04:05 -06002539 output_aspect = (float) output->width /
2540 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 /* XXX: Use surf_width and surf_height here? */
2542 surface_aspect = (float) surface->width /
2543 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002544 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002545 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002546 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002547 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002548 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002549 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002550
Alex Wu4539b082012-03-01 12:57:46 +08002551 weston_matrix_scale(matrix, scale, scale, 1);
2552 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002553 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002554 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002555 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2556 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002558
Alex Wu4539b082012-03-01 12:57:46 +08002559 break;
2560 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002561 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002562 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002563 surf_width * surface->buffer_viewport.scale,
2564 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002565 shsurf->fullscreen.framerate};
2566
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002567 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002568 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002569 weston_view_set_position(shsurf->view,
2570 output->x - surf_x,
2571 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002572 shsurf->fullscreen.black_view->surface->width = output->width;
2573 shsurf->fullscreen.black_view->surface->height = output->height;
2574 weston_view_set_position(shsurf->fullscreen.black_view,
2575 output->x - surf_x,
2576 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002577 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002578 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002579 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002581 }
Alex Wubd3354b2012-04-17 17:20:49 +08002582 }
Alex Wu4539b082012-03-01 12:57:46 +08002583 break;
2584 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002586 break;
2587 default:
2588 break;
2589 }
2590}
2591
Alex Wu4539b082012-03-01 12:57:46 +08002592static void
2593shell_map_fullscreen(struct shell_surface *shsurf)
2594{
Alex Wubd3354b2012-04-17 17:20:49 +08002595 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002596}
2597
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002598static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002599set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2600{
2601 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002602 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002603 shsurf->transient.x = x;
2604 shsurf->transient.y = y;
2605 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002606
2607 shell_surface_set_parent(shsurf, NULL);
2608
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002609 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002610 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002611}
2612
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002613static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002614
2615static void
2616destroy_shell_seat(struct wl_listener *listener, void *data)
2617{
2618 struct shell_seat *shseat =
2619 container_of(listener,
2620 struct shell_seat, seat_destroy_listener);
2621 struct shell_surface *shsurf, *prev = NULL;
2622
2623 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002624 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002625 shseat->popup_grab.client = NULL;
2626
2627 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2628 shsurf->popup.shseat = NULL;
2629 if (prev) {
2630 wl_list_init(&prev->popup.grab_link);
2631 }
2632 prev = shsurf;
2633 }
2634 wl_list_init(&prev->popup.grab_link);
2635 }
2636
2637 wl_list_remove(&shseat->seat_destroy_listener.link);
2638 free(shseat);
2639}
2640
2641static struct shell_seat *
2642create_shell_seat(struct weston_seat *seat)
2643{
2644 struct shell_seat *shseat;
2645
2646 shseat = calloc(1, sizeof *shseat);
2647 if (!shseat) {
2648 weston_log("no memory to allocate shell seat\n");
2649 return NULL;
2650 }
2651
2652 shseat->seat = seat;
2653 wl_list_init(&shseat->popup_grab.surfaces_list);
2654
2655 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2656 wl_signal_add(&seat->destroy_signal,
2657 &shseat->seat_destroy_listener);
2658
2659 return shseat;
2660}
2661
2662static struct shell_seat *
2663get_shell_seat(struct weston_seat *seat)
2664{
2665 struct wl_listener *listener;
2666
2667 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2668 if (listener == NULL)
2669 return create_shell_seat(seat);
2670
2671 return container_of(listener,
2672 struct shell_seat, seat_destroy_listener);
2673}
2674
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002675static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002676popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002677{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002678 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002680 struct shell_seat *shseat =
2681 container_of(grab, struct shell_seat, popup_grab.grab);
2682 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002683 wl_fixed_t sx, sy;
2684
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685 view = weston_compositor_pick_view(pointer->seat->compositor,
2686 pointer->x, pointer->y,
2687 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002688
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 if (view && view->surface->resource &&
2690 wl_resource_get_client(view->surface->resource) == client) {
2691 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002693 weston_pointer_set_focus(pointer, NULL,
2694 wl_fixed_from_int(0),
2695 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696 }
2697}
2698
2699static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002700popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2701 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002702{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002703 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002704 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002705 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002706
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002707 weston_pointer_move(pointer, x, y);
2708
Neil Roberts96d790e2013-09-19 17:32:00 +01002709 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 weston_view_from_global_fixed(pointer->focus,
2711 pointer->x, pointer->y,
2712 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002713 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002714 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002715}
2716
2717static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002718popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002719 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002720{
2721 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002722 struct shell_seat *shseat =
2723 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002724 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002725 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002726 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002727 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728
Neil Roberts96d790e2013-09-19 17:32:00 +01002729 resource_list = &grab->pointer->focus_resource_list;
2730 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002732 wl_resource_for_each(resource, resource_list) {
2733 wl_pointer_send_button(resource, serial,
2734 time, button, state);
2735 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002736 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002737 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002738 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002739 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002740 }
2741
Daniel Stone4dbadb12012-05-30 16:31:51 +01002742 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002743 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002744}
2745
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002746static void
2747popup_grab_cancel(struct weston_pointer_grab *grab)
2748{
2749 popup_grab_end(grab->pointer);
2750}
2751
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002752static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002753 popup_grab_focus,
2754 popup_grab_motion,
2755 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002756 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002757};
2758
2759static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002760shell_surface_send_popup_done(struct shell_surface *shsurf)
2761{
2762 if (shell_surface_is_wl_shell_surface(shsurf))
2763 wl_shell_surface_send_popup_done(shsurf->resource);
2764 else if (shell_surface_is_xdg_popup(shsurf))
2765 xdg_popup_send_popup_done(shsurf->resource,
2766 shsurf->popup.serial);
2767}
2768
2769static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002770popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002771{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002772 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 struct shell_seat *shseat =
2774 container_of(grab, struct shell_seat, popup_grab.grab);
2775 struct shell_surface *shsurf;
2776 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002777
2778 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002779 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002780 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002781 shseat->popup_grab.grab.interface = NULL;
2782 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002783 /* Send the popup_done event to all the popups open */
2784 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002785 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002786 shsurf->popup.shseat = NULL;
2787 if (prev) {
2788 wl_list_init(&prev->popup.grab_link);
2789 }
2790 prev = shsurf;
2791 }
2792 wl_list_init(&prev->popup.grab_link);
2793 wl_list_init(&shseat->popup_grab.surfaces_list);
2794 }
2795}
2796
2797static void
2798add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2799{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002800 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002801
2802 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002803 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002804 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002805 /* We must make sure here that this popup was opened after
2806 * a mouse press, and not just by moving around with other
2807 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002808 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002809 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002810
Rob Bradforddfe31052013-06-26 19:49:11 +01002811 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002812 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002813 } else {
2814 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002815 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002816}
2817
2818static void
2819remove_popup_grab(struct shell_surface *shsurf)
2820{
2821 struct shell_seat *shseat = shsurf->popup.shseat;
2822
2823 wl_list_remove(&shsurf->popup.grab_link);
2824 wl_list_init(&shsurf->popup.grab_link);
2825 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002827 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002828 }
2829}
2830
2831static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002832shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002833{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002834 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002836
Jason Ekstranda7af7042013-10-12 22:38:11 -05002837 shsurf->surface->output = parent_view->output;
2838 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002839
Jason Ekstranda7af7042013-10-12 22:38:11 -05002840 weston_view_set_transform_parent(shsurf->view, parent_view);
2841 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2842 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002843
Kristian Høgsberge3148752013-05-06 23:19:49 -04002844 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002845 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002846 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002847 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002848 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002849 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850}
2851
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002852static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002853 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002854 shell_surface_move,
2855 shell_surface_resize,
2856 shell_surface_set_toplevel,
2857 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002859 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002860 shell_surface_set_maximized,
2861 shell_surface_set_title,
2862 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002863};
2864
2865static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002866destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002867{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002868 struct shell_surface *child, *next;
2869
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002870 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2871
Giulio Camuffo5085a752013-03-25 21:42:45 +01002872 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2873 remove_popup_grab(shsurf);
2874 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002875
Alex Wubd3354b2012-04-17 17:20:49 +08002876 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002877 shell_surface_is_top_fullscreen(shsurf))
2878 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002879
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 if (shsurf->fullscreen.black_view)
2881 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002882
Alex Wubd3354b2012-04-17 17:20:49 +08002883 /* As destroy_resource() use wl_list_for_each_safe(),
2884 * we can always remove the listener.
2885 */
2886 wl_list_remove(&shsurf->surface_destroy_listener.link);
2887 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002888 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002889 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002890
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 weston_view_destroy(shsurf->view);
2892
Philip Withnall648a4dd2013-11-25 18:01:44 +00002893 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002894 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2895 wl_list_remove(&child->children_link);
2896 child->parent = NULL;
2897 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002898
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002899 wl_list_remove(&shsurf->link);
2900 free(shsurf);
2901}
2902
2903static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002904shell_destroy_shell_surface(struct wl_resource *resource)
2905{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002906 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002907
2908 destroy_shell_surface(shsurf);
2909}
2910
2911static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002912shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002913{
2914 struct shell_surface *shsurf = container_of(listener,
2915 struct shell_surface,
2916 surface_destroy_listener);
2917
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002918 if (shsurf->resource)
2919 wl_resource_destroy(shsurf->resource);
2920 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002921 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002922}
2923
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002924static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002925shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002926static void
2927shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002928
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002929struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002930get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002931{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002932 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002933 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002934 else
2935 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002936}
2937
Rafael Antognollie2a34552013-12-03 15:35:45 -02002938static struct shell_surface *
2939create_common_surface(void *shell, struct weston_surface *surface,
2940 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002941{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002942 struct shell_surface *shsurf;
2943
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002944 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002945 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002946 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002947 }
2948
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002949 shsurf = calloc(1, sizeof *shsurf);
2950 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002951 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002952 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002953 }
2954
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 shsurf->view = weston_view_create(surface);
2956 if (!shsurf->view) {
2957 weston_log("no memory to allocate shell surface\n");
2958 free(shsurf);
2959 return NULL;
2960 }
2961
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002962 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002963 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002964 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002965
Tiago Vignattibc052c92012-04-19 16:18:18 +03002966 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002967 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002968 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002969 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002970 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002971 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002972 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2973 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002974 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002975 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002976 wl_list_init(&shsurf->fullscreen.transform.link);
2977
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002978 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002979 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002980 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002981 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002982
2983 /* init link so its safe to always remove it in destroy_shell_surface */
2984 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002985 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002986
Pekka Paalanen460099f2012-01-20 16:48:25 +02002987 /* empty when not in use */
2988 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002989 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002990
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002991 wl_list_init(&shsurf->workspace_transform.link);
2992
Philip Withnall648a4dd2013-11-25 18:01:44 +00002993 wl_list_init(&shsurf->children_link);
2994 wl_list_init(&shsurf->children_list);
2995 shsurf->parent = NULL;
2996
Pekka Paalanen98262232011-12-01 10:42:22 +02002997 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002998
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002999 shsurf->client = client;
3000
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003001 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003002}
3003
Rafael Antognollie2a34552013-12-03 15:35:45 -02003004static struct shell_surface *
3005create_shell_surface(void *shell, struct weston_surface *surface,
3006 const struct weston_shell_client *client)
3007{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003008 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003009}
3010
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011static struct weston_view *
3012get_primary_view(void *shell, struct shell_surface *shsurf)
3013{
3014 return shsurf->view;
3015}
3016
Tiago Vignattibc052c92012-04-19 16:18:18 +03003017static void
3018shell_get_shell_surface(struct wl_client *client,
3019 struct wl_resource *resource,
3020 uint32_t id,
3021 struct wl_resource *surface_resource)
3022{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003023 struct weston_surface *surface =
3024 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003025 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003026 struct shell_surface *shsurf;
3027
3028 if (get_shell_surface(surface)) {
3029 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003030 WL_DISPLAY_ERROR_INVALID_OBJECT,
3031 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003032 return;
3033 }
3034
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003035 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003036 if (!shsurf) {
3037 wl_resource_post_error(surface_resource,
3038 WL_DISPLAY_ERROR_INVALID_OBJECT,
3039 "surface->configure already set");
3040 return;
3041 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003042
Jason Ekstranda85118c2013-06-27 20:17:02 -05003043 shsurf->resource =
3044 wl_resource_create(client,
3045 &wl_shell_surface_interface, 1, id);
3046 wl_resource_set_implementation(shsurf->resource,
3047 &shell_surface_implementation,
3048 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003049}
3050
Rafael Antognollie2a34552013-12-03 15:35:45 -02003051static bool
3052shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3053{
3054 return wl_resource_instance_of(shsurf->resource,
3055 &wl_shell_surface_interface,
3056 &shell_surface_implementation);
3057}
3058
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003059static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003060 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003061};
3062
Rafael Antognollie2a34552013-12-03 15:35:45 -02003063/****************************
3064 * xdg-shell implementation */
3065
3066static void
3067xdg_surface_destroy(struct wl_client *client,
3068 struct wl_resource *resource)
3069{
3070 wl_resource_destroy(resource);
3071}
3072
3073static void
3074xdg_surface_pong(struct wl_client *client,
3075 struct wl_resource *resource,
3076 uint32_t serial)
3077{
3078 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3079
3080 surface_pong(shsurf, serial);
3081}
3082
3083static void
3084xdg_surface_set_app_id(struct wl_client *client,
3085 struct wl_resource *resource,
3086 const char *app_id)
3087{
3088 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3089
3090 free(shsurf->class);
3091 shsurf->class = strdup(app_id);
3092}
3093
3094static void
3095xdg_surface_set_title(struct wl_client *client,
3096 struct wl_resource *resource, const char *title)
3097{
3098 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3099
3100 set_title(shsurf, title);
3101}
3102
3103static void
3104xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3105 struct wl_resource *seat_resource, uint32_t serial)
3106{
3107 common_surface_move(resource, seat_resource, serial);
3108}
3109
3110static void
3111xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3112 struct wl_resource *seat_resource, uint32_t serial,
3113 uint32_t edges)
3114{
3115 common_surface_resize(resource, seat_resource, serial, edges);
3116}
3117
3118static void
3119xdg_surface_set_output(struct wl_client *client,
3120 struct wl_resource *resource,
3121 struct wl_resource *output_resource)
3122{
3123 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3124 struct weston_output *output;
3125
3126 if (output_resource)
3127 output = wl_resource_get_user_data(output_resource);
3128 else
3129 output = NULL;
3130
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003131 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003132}
3133
3134static void
3135xdg_surface_set_fullscreen(struct wl_client *client,
3136 struct wl_resource *resource)
3137{
3138 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3139
3140 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3141 return;
3142
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003143 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003144 set_fullscreen(shsurf,
3145 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003146 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003147}
3148
3149static void
3150xdg_surface_unset_fullscreen(struct wl_client *client,
3151 struct wl_resource *resource)
3152{
3153 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003154 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003155
3156 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3157 return;
3158
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003159 if (!shsurf->next_state.fullscreen)
3160 return;
3161
Rafael Antognollie2a34552013-12-03 15:35:45 -02003162 shsurf->next_state.fullscreen = false;
3163 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003164
3165 if (shsurf->saved_size_valid) {
3166 width = shsurf->saved_width;
3167 height = shsurf->saved_height;
3168 shsurf->saved_size_valid = false;
3169 } else {
3170 width = shsurf->surface->width;
3171 height = shsurf->surface->height;
3172 }
3173
3174 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175}
3176
3177static void
3178xdg_surface_set_maximized(struct wl_client *client,
3179 struct wl_resource *resource)
3180{
3181 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3182
3183 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3184 return;
3185
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003186 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003187 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003188}
3189
3190static void
3191xdg_surface_unset_maximized(struct wl_client *client,
3192 struct wl_resource *resource)
3193{
3194 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003195 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003196
3197 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3198 return;
3199
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003200 if (!shsurf->next_state.maximized)
3201 return;
3202
Rafael Antognollie2a34552013-12-03 15:35:45 -02003203 shsurf->next_state.maximized = false;
3204 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003205
3206 if (shsurf->saved_size_valid) {
3207 width = shsurf->saved_width;
3208 height = shsurf->saved_height;
3209 shsurf->saved_size_valid = false;
3210 } else {
3211 width = shsurf->surface->width;
3212 height = shsurf->surface->height;
3213 }
3214
3215 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003216}
3217
3218static const struct xdg_surface_interface xdg_surface_implementation = {
3219 xdg_surface_destroy,
3220 xdg_surface_set_transient_for,
3221 xdg_surface_set_title,
3222 xdg_surface_set_app_id,
3223 xdg_surface_pong,
3224 xdg_surface_move,
3225 xdg_surface_resize,
3226 xdg_surface_set_output,
3227 xdg_surface_set_fullscreen,
3228 xdg_surface_unset_fullscreen,
3229 xdg_surface_set_maximized,
3230 xdg_surface_unset_maximized,
3231 NULL /* set_minimized */
3232};
3233
3234static void
3235xdg_send_configure(struct weston_surface *surface,
3236 uint32_t edges, int32_t width, int32_t height)
3237{
3238 struct shell_surface *shsurf = get_shell_surface(surface);
3239
3240 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3241}
3242
3243static const struct weston_shell_client xdg_client = {
3244 xdg_send_configure
3245};
3246
3247static void
3248xdg_use_unstable_version(struct wl_client *client,
3249 struct wl_resource *resource,
3250 int32_t version)
3251{
3252 if (version > 1) {
3253 wl_resource_post_error(resource,
3254 1,
3255 "xdg-shell:: version not implemented yet.");
3256 return;
3257 }
3258}
3259
3260static struct shell_surface *
3261create_xdg_surface(void *shell, struct weston_surface *surface,
3262 const struct weston_shell_client *client)
3263{
3264 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003265
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003266 shsurf = create_common_surface(shell, surface, client);
3267 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003268
3269 return shsurf;
3270}
3271
3272static void
3273xdg_get_xdg_surface(struct wl_client *client,
3274 struct wl_resource *resource,
3275 uint32_t id,
3276 struct wl_resource *surface_resource)
3277{
3278 struct weston_surface *surface =
3279 wl_resource_get_user_data(surface_resource);
3280 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3281 struct shell_surface *shsurf;
3282
3283 if (get_shell_surface(surface)) {
3284 wl_resource_post_error(surface_resource,
3285 WL_DISPLAY_ERROR_INVALID_OBJECT,
3286 "desktop_shell::get_shell_surface already requested");
3287 return;
3288 }
3289
3290 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3291 if (!shsurf) {
3292 wl_resource_post_error(surface_resource,
3293 WL_DISPLAY_ERROR_INVALID_OBJECT,
3294 "surface->configure already set");
3295 return;
3296 }
3297
3298 shsurf->resource =
3299 wl_resource_create(client,
3300 &xdg_surface_interface, 1, id);
3301 wl_resource_set_implementation(shsurf->resource,
3302 &xdg_surface_implementation,
3303 shsurf, shell_destroy_shell_surface);
3304}
3305
3306static bool
3307shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3308{
3309 return wl_resource_instance_of(shsurf->resource,
3310 &xdg_surface_interface,
3311 &xdg_surface_implementation);
3312}
3313
3314/* xdg-popup implementation */
3315
3316static void
3317xdg_popup_destroy(struct wl_client *client,
3318 struct wl_resource *resource)
3319{
3320 wl_resource_destroy(resource);
3321}
3322
3323static void
3324xdg_popup_pong(struct wl_client *client,
3325 struct wl_resource *resource,
3326 uint32_t serial)
3327{
3328 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3329
3330 surface_pong(shsurf, serial);
3331}
3332
3333static const struct xdg_popup_interface xdg_popup_implementation = {
3334 xdg_popup_destroy,
3335 xdg_popup_pong
3336};
3337
3338static void
3339xdg_popup_send_configure(struct weston_surface *surface,
3340 uint32_t edges, int32_t width, int32_t height)
3341{
3342}
3343
3344static const struct weston_shell_client xdg_popup_client = {
3345 xdg_popup_send_configure
3346};
3347
3348static struct shell_surface *
3349create_xdg_popup(void *shell, struct weston_surface *surface,
3350 const struct weston_shell_client *client,
3351 struct weston_surface *parent,
3352 struct shell_seat *seat,
3353 uint32_t serial,
3354 int32_t x, int32_t y)
3355{
3356 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003357
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003358 shsurf = create_common_surface(shell, surface, client);
3359 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003360 shsurf->popup.shseat = seat;
3361 shsurf->popup.serial = serial;
3362 shsurf->popup.x = x;
3363 shsurf->popup.y = y;
3364 shell_surface_set_parent(shsurf, parent);
3365
3366 return shsurf;
3367}
3368
3369static void
3370xdg_get_xdg_popup(struct wl_client *client,
3371 struct wl_resource *resource,
3372 uint32_t id,
3373 struct wl_resource *surface_resource,
3374 struct wl_resource *parent_resource,
3375 struct wl_resource *seat_resource,
3376 uint32_t serial,
3377 int32_t x, int32_t y, uint32_t flags)
3378{
3379 struct weston_surface *surface =
3380 wl_resource_get_user_data(surface_resource);
3381 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3382 struct shell_surface *shsurf;
3383 struct weston_surface *parent;
3384 struct shell_seat *seat;
3385
3386 if (get_shell_surface(surface)) {
3387 wl_resource_post_error(surface_resource,
3388 WL_DISPLAY_ERROR_INVALID_OBJECT,
3389 "desktop_shell::get_shell_surface already requested");
3390 return;
3391 }
3392
3393 if (!parent_resource) {
3394 wl_resource_post_error(surface_resource,
3395 WL_DISPLAY_ERROR_INVALID_OBJECT,
3396 "xdg_shell::get_xdg_popup requires a parent shell surface");
3397 }
3398
3399 parent = wl_resource_get_user_data(parent_resource);
3400 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3401
3402 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3403 parent, seat, serial, x, y);
3404 if (!shsurf) {
3405 wl_resource_post_error(surface_resource,
3406 WL_DISPLAY_ERROR_INVALID_OBJECT,
3407 "surface->configure already set");
3408 return;
3409 }
3410
3411 shsurf->resource =
3412 wl_resource_create(client,
3413 &xdg_popup_interface, 1, id);
3414 wl_resource_set_implementation(shsurf->resource,
3415 &xdg_popup_implementation,
3416 shsurf, shell_destroy_shell_surface);
3417}
3418
3419static bool
3420shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3421{
3422 return wl_resource_instance_of(shsurf->resource,
3423 &xdg_popup_interface,
3424 &xdg_popup_implementation);
3425}
3426
3427static const struct xdg_shell_interface xdg_implementation = {
3428 xdg_use_unstable_version,
3429 xdg_get_xdg_surface,
3430 xdg_get_xdg_popup
3431};
3432
3433static int
3434xdg_shell_unversioned_dispatch(const void *implementation,
3435 void *_target, uint32_t opcode,
3436 const struct wl_message *message,
3437 union wl_argument *args)
3438{
3439 struct wl_resource *resource = _target;
3440 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3441
3442 if (opcode != 0) {
3443 wl_resource_post_error(resource,
3444 WL_DISPLAY_ERROR_INVALID_OBJECT,
3445 "must call use_unstable_version first");
3446 return 0;
3447 }
3448
3449#define XDG_SERVER_VERSION 1
3450
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003451 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3452 "shell implementation doesn't match protocol version");
3453
Rafael Antognollie2a34552013-12-03 15:35:45 -02003454 if (args[0].i != XDG_SERVER_VERSION) {
3455 wl_resource_post_error(resource,
3456 WL_DISPLAY_ERROR_INVALID_OBJECT,
3457 "incompatible version, server is %d "
3458 "client wants %d",
3459 XDG_SERVER_VERSION, args[0].i);
3460 return 0;
3461 }
3462
3463 wl_resource_set_implementation(resource, &xdg_implementation,
3464 shell, NULL);
3465
3466 return 1;
3467}
3468
3469/* end of xdg-shell implementation */
3470/***********************************/
3471
Kristian Høgsberg07937562011-04-12 17:25:42 -04003472static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003473shell_fade(struct desktop_shell *shell, enum fade_type type);
3474
3475static int
3476screensaver_timeout(void *data)
3477{
3478 struct desktop_shell *shell = data;
3479
3480 shell_fade(shell, FADE_OUT);
3481
3482 return 1;
3483}
3484
3485static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003486handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003487{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003488 struct desktop_shell *shell =
3489 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003490
Pekka Paalanen18027e52011-12-02 16:31:49 +02003491 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003492
3493 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003494 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003495}
3496
3497static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003498launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003499{
3500 if (shell->screensaver.binding)
3501 return;
3502
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003503 if (!shell->screensaver.path) {
3504 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003505 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003506 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003507
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003508 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003509 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003510 return;
3511 }
3512
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003513 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003514 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003515 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003516 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003517}
3518
3519static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003520terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003521{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003522 if (shell->screensaver.process.pid == 0)
3523 return;
3524
3525 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003526}
3527
3528static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003529configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003530{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3534 if (v->output == ev->output && v != ev) {
3535 weston_view_unmap(v);
3536 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003537 }
3538 }
3539
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003540 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003541
Jason Ekstranda7af7042013-10-12 22:38:11 -05003542 if (wl_list_empty(&ev->layer_link)) {
3543 wl_list_insert(&layer->view_list, &ev->layer_link);
3544 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003545 }
3546}
3547
3548static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003549background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003550{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003551 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003553
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554 view = container_of(es->views.next, struct weston_view, surface_link);
3555
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003556 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003557}
3558
3559static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003560desktop_shell_set_background(struct wl_client *client,
3561 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003562 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003563 struct wl_resource *surface_resource)
3564{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003565 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003566 struct weston_surface *surface =
3567 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003569
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003570 if (surface->configure) {
3571 wl_resource_post_error(surface_resource,
3572 WL_DISPLAY_ERROR_INVALID_OBJECT,
3573 "surface role already assigned");
3574 return;
3575 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003576
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3578 weston_view_destroy(view);
3579 view = weston_view_create(surface);
3580
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003581 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003582 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003583 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003585 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003586 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003587 surface->output->width,
3588 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003589}
3590
3591static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003592panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003593{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003594 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003595 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003596
Jason Ekstranda7af7042013-10-12 22:38:11 -05003597 view = container_of(es->views.next, struct weston_view, surface_link);
3598
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003599 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003600}
3601
3602static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003603desktop_shell_set_panel(struct wl_client *client,
3604 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003605 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003606 struct wl_resource *surface_resource)
3607{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003608 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003609 struct weston_surface *surface =
3610 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003611 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003612
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003613 if (surface->configure) {
3614 wl_resource_post_error(surface_resource,
3615 WL_DISPLAY_ERROR_INVALID_OBJECT,
3616 "surface role already assigned");
3617 return;
3618 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003619
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3621 weston_view_destroy(view);
3622 view = weston_view_create(surface);
3623
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003624 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003625 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003626 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003627 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003628 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003629 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003630 surface->output->width,
3631 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003632}
3633
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003634static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003635lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003636{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003637 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003638 struct weston_view *view;
3639
3640 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003641
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003642 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003643 return;
3644
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003646
3647 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648 wl_list_insert(&shell->lock_layer.view_list,
3649 &view->layer_link);
3650 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003651 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003652 }
3653}
3654
3655static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003656handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003657{
Tiago Vignattibe143262012-04-16 17:31:41 +03003658 struct desktop_shell *shell =
3659 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003660
Martin Minarik6d118362012-06-07 18:01:59 +02003661 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003662 shell->lock_surface = NULL;
3663}
3664
3665static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003666desktop_shell_set_lock_surface(struct wl_client *client,
3667 struct wl_resource *resource,
3668 struct wl_resource *surface_resource)
3669{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003670 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003671 struct weston_surface *surface =
3672 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003673
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003674 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003675
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003676 if (!shell->locked)
3677 return;
3678
Pekka Paalanen98262232011-12-01 10:42:22 +02003679 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003680
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003681 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003682 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003683 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003684
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003685 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003686 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003687 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003688}
3689
3690static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003691resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003692{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003693 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003694
Pekka Paalanen77346a62011-11-30 16:26:35 +02003695 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003696
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003697 wl_list_remove(&shell->lock_layer.link);
3698 wl_list_insert(&shell->compositor->cursor_layer.link,
3699 &shell->fullscreen_layer.link);
3700 wl_list_insert(&shell->fullscreen_layer.link,
3701 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003702 if (shell->showing_input_panels) {
3703 wl_list_insert(&shell->panel_layer.link,
3704 &shell->input_panel_layer.link);
3705 wl_list_insert(&shell->input_panel_layer.link,
3706 &ws->layer.link);
3707 } else {
3708 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3709 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003710
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003711 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003712
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003713 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003714 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003715 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003716}
3717
3718static void
3719desktop_shell_unlock(struct wl_client *client,
3720 struct wl_resource *resource)
3721{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003722 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003723
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003724 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003725
3726 if (shell->locked)
3727 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003728}
3729
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003730static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003731desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003732 struct wl_resource *resource,
3733 struct wl_resource *surface_resource)
3734{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003735 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003736
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003737 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003738 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003739}
3740
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003741static void
3742desktop_shell_desktop_ready(struct wl_client *client,
3743 struct wl_resource *resource)
3744{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003745 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003746
3747 shell_fade_startup(shell);
3748}
3749
Kristian Høgsberg75840622011-09-06 13:48:16 -04003750static const struct desktop_shell_interface desktop_shell_implementation = {
3751 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003752 desktop_shell_set_panel,
3753 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003754 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003755 desktop_shell_set_grab_surface,
3756 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003757};
3758
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003759static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003760get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003761{
3762 struct shell_surface *shsurf;
3763
3764 shsurf = get_shell_surface(surface);
3765 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003766 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003767 return shsurf->type;
3768}
3769
Kristian Høgsberg75840622011-09-06 13:48:16 -04003770static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003771move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003772{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003773 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003774 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003775 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003776
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003777 if (seat->pointer->focus == NULL)
3778 return;
3779
3780 focus = seat->pointer->focus->surface;
3781
Pekka Paalanen01388e22013-04-25 13:57:44 +03003782 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003783 if (surface == NULL)
3784 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003785
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003786 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003787 if (shsurf == NULL || shsurf->state.fullscreen ||
3788 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003789 return;
3790
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003791 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003792}
3793
3794static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003795maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3796{
3797 struct weston_surface *focus = seat->pointer->focus->surface;
3798 struct weston_surface *surface;
3799 struct shell_surface *shsurf;
3800
3801 surface = weston_surface_get_main_surface(focus);
3802 if (surface == NULL)
3803 return;
3804
3805 shsurf = get_shell_surface(surface);
3806 if (shsurf == NULL)
3807 return;
3808
3809 if (!shell_surface_is_xdg_surface(shsurf))
3810 return;
3811
3812 if (shsurf->state.maximized)
3813 xdg_surface_send_request_unset_maximized(shsurf->resource);
3814 else
3815 xdg_surface_send_request_set_maximized(shsurf->resource);
3816}
3817
3818static void
3819fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3820{
3821 struct weston_surface *focus = seat->pointer->focus->surface;
3822 struct weston_surface *surface;
3823 struct shell_surface *shsurf;
3824
3825 surface = weston_surface_get_main_surface(focus);
3826 if (surface == NULL)
3827 return;
3828
3829 shsurf = get_shell_surface(surface);
3830 if (shsurf == NULL)
3831 return;
3832
3833 if (!shell_surface_is_xdg_surface(shsurf))
3834 return;
3835
3836 if (shsurf->state.fullscreen)
3837 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3838 else
3839 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3840}
3841
3842static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003843touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3844{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003845 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003846 struct weston_surface *surface;
3847 struct shell_surface *shsurf;
3848
3849 surface = weston_surface_get_main_surface(focus);
3850 if (surface == NULL)
3851 return;
3852
3853 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003854 if (shsurf == NULL || shsurf->state.fullscreen ||
3855 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003856 return;
3857
3858 surface_touch_move(shsurf, (struct weston_seat *) seat);
3859}
3860
3861static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003862resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003863{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003864 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003865 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003866 uint32_t edges = 0;
3867 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003868 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003869
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003870 if (seat->pointer->focus == NULL)
3871 return;
3872
3873 focus = seat->pointer->focus->surface;
3874
Pekka Paalanen01388e22013-04-25 13:57:44 +03003875 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003876 if (surface == NULL)
3877 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003878
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003879 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003880 if (shsurf == NULL || shsurf->state.fullscreen ||
3881 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003882 return;
3883
Jason Ekstranda7af7042013-10-12 22:38:11 -05003884 weston_view_from_global(shsurf->view,
3885 wl_fixed_to_int(seat->pointer->grab_x),
3886 wl_fixed_to_int(seat->pointer->grab_y),
3887 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003888
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003889 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003890 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003891 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003892 edges |= 0;
3893 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003894 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003895
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003896 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003897 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003898 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003899 edges |= 0;
3900 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003901 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003902
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003903 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003904}
3905
3906static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003907surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003908 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003909{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003910 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003911 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003912 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003913 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003914
Pekka Paalanen01388e22013-04-25 13:57:44 +03003915 /* XXX: broken for windows containing sub-surfaces */
3916 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003917 if (surface == NULL)
3918 return;
3919
3920 shsurf = get_shell_surface(surface);
3921 if (!shsurf)
3922 return;
3923
Jason Ekstranda7af7042013-10-12 22:38:11 -05003924 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003925
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926 if (shsurf->view->alpha > 1.0)
3927 shsurf->view->alpha = 1.0;
3928 if (shsurf->view->alpha < step)
3929 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003930
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003932 weston_surface_damage(surface);
3933}
3934
3935static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003936do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003937 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003938{
Daniel Stone37816df2012-05-16 18:45:18 +01003939 struct weston_seat *ws = (struct weston_seat *) seat;
3940 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003941 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003942 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003943
3944 wl_list_for_each(output, &compositor->output_list, link) {
3945 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003946 wl_fixed_to_double(seat->pointer->x),
3947 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003948 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003949 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003950 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003951 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003952 increment = -output->zoom.increment;
3953 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003954 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003955 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003956 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003957 else
3958 increment = 0;
3959
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003960 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003961
Scott Moreaue6603982012-06-11 13:07:51 -06003962 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003963 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003964 else if (output->zoom.level > output->zoom.max_level)
3965 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003966 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003967 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003968 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003969
Scott Moreaue6603982012-06-11 13:07:51 -06003970 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003971
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003973 }
3974 }
3975}
3976
Scott Moreauccbf29d2012-02-22 14:21:41 -07003977static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003978zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003979 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003980{
3981 do_zoom(seat, time, 0, axis, value);
3982}
3983
3984static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003985zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003986 void *data)
3987{
3988 do_zoom(seat, time, key, 0, 0);
3989}
3990
3991static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003992terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003993 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003994{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003995 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003996
Daniel Stone325fc2d2012-05-30 16:31:58 +01003997 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003998}
3999
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004000static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004001rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4002 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004003{
4004 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004005 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004006 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004007 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004008 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004009
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004010 weston_pointer_move(pointer, x, y);
4011
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004012 if (!shsurf)
4013 return;
4014
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004015 cx = 0.5f * shsurf->surface->width;
4016 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004017
Daniel Stone37816df2012-05-16 18:45:18 +01004018 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4019 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004020 r = sqrtf(dx * dx + dy * dy);
4021
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004022 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004024
4025 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004026 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004027 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004028
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004029 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004030 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004031
4032 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004033 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004034 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004035 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004036 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004037
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004038 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004040 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004041 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004042 wl_list_init(&shsurf->rotation.transform.link);
4043 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004044 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004045 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004046
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004047 /* We need to adjust the position of the surface
4048 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004049 cposx = shsurf->view->geometry.x + cx;
4050 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004051 dposx = rotate->center.x - cposx;
4052 dposy = rotate->center.y - cposy;
4053 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004054 weston_view_set_position(shsurf->view,
4055 shsurf->view->geometry.x + dposx,
4056 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004057 }
4058
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004059 /* Repaint implies weston_surface_update_transform(), which
4060 * lazily applies the damage due to rotation update.
4061 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004062 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004063}
4064
4065static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004066rotate_grab_button(struct weston_pointer_grab *grab,
4067 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004068{
4069 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004070 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004071 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004072 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004073 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004074
Daniel Stone4dbadb12012-05-30 16:31:51 +01004075 if (pointer->button_count == 0 &&
4076 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004077 if (shsurf)
4078 weston_matrix_multiply(&shsurf->rotation.rotation,
4079 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004080 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004081 free(rotate);
4082 }
4083}
4084
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004085static void
4086rotate_grab_cancel(struct weston_pointer_grab *grab)
4087{
4088 struct rotate_grab *rotate =
4089 container_of(grab, struct rotate_grab, base.grab);
4090
4091 shell_grab_end(&rotate->base);
4092 free(rotate);
4093}
4094
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004095static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004096 noop_grab_focus,
4097 rotate_grab_motion,
4098 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004099 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004100};
4101
4102static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004103surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004104{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004105 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004106 float dx, dy;
4107 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004108
Pekka Paalanen460099f2012-01-20 16:48:25 +02004109 rotate = malloc(sizeof *rotate);
4110 if (!rotate)
4111 return;
4112
Jason Ekstranda7af7042013-10-12 22:38:11 -05004113 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004114 surface->surface->width * 0.5f,
4115 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004116 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004117
Daniel Stone37816df2012-05-16 18:45:18 +01004118 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4119 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004120 r = sqrtf(dx * dx + dy * dy);
4121 if (r > 20.0f) {
4122 struct weston_matrix inverse;
4123
4124 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004125 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004126 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004127
4128 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004129 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004130 } else {
4131 weston_matrix_init(&surface->rotation.rotation);
4132 weston_matrix_init(&rotate->rotation);
4133 }
4134
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004135 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4136 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004137}
4138
4139static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004140rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004141 void *data)
4142{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004143 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004144 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004145 struct shell_surface *surface;
4146
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004147 if (seat->pointer->focus == NULL)
4148 return;
4149
4150 focus = seat->pointer->focus->surface;
4151
Pekka Paalanen01388e22013-04-25 13:57:44 +03004152 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004153 if (base_surface == NULL)
4154 return;
4155
4156 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004157 if (surface == NULL || surface->state.fullscreen ||
4158 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004159 return;
4160
4161 surface_rotate(surface, seat);
4162}
4163
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004164/* Move all fullscreen layers down to the current workspace in a non-reversible
4165 * manner. This should be used when implementing shell-wide overlays, such as
4166 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004167void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004168lower_fullscreen_layer(struct desktop_shell *shell)
4169{
4170 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004171 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004172
4173 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004174 wl_list_for_each_reverse_safe(view, prev,
4175 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004176 layer_link) {
4177 wl_list_remove(&view->layer_link);
4178 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4179 weston_view_damage_below(view);
4180 weston_surface_damage(view->surface);
4181 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004182}
4183
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004184void
Tiago Vignattibe143262012-04-16 17:31:41 +03004185activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004186 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004187{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004188 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004189 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004190 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004191 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004192 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004193
Pekka Paalanen01388e22013-04-25 13:57:44 +03004194 main_surface = weston_surface_get_main_surface(es);
4195
Daniel Stone37816df2012-05-16 18:45:18 +01004196 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004197
Jonas Ådahl8538b222012-08-29 22:13:03 +02004198 state = ensure_focus_state(shell, seat);
4199 if (state == NULL)
4200 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004201
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004202 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004203 state->keyboard_focus = es;
4204 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004205 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004206
Rafael Antognolli03b16592013-12-03 15:35:42 -02004207 shsurf = get_shell_surface(main_surface);
4208 if (shsurf->state.fullscreen)
4209 shell_configure_fullscreen(shsurf);
4210 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004211 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004212
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004213 if (shell->focus_animation_type != ANIMATION_NONE) {
4214 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004215 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004216 }
4217
4218 /* Update the surface’s layer. This brings it to the top of the stacking
4219 * order as appropriate. */
4220 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004221}
4222
Alex Wu21858432012-04-01 20:13:08 +08004223/* no-op func for checking black surface */
4224static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004225black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004226{
4227}
4228
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004229static bool
Alex Wu21858432012-04-01 20:13:08 +08004230is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4231{
4232 if (es->configure == black_surface_configure) {
4233 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004234 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004235 return true;
4236 }
4237 return false;
4238}
4239
Kristian Høgsberg75840622011-09-06 13:48:16 -04004240static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004241activate_binding(struct weston_seat *seat,
4242 struct desktop_shell *shell,
4243 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004244{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004245 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004246
Alex Wu9c35e6b2012-03-05 14:13:13 +08004247 if (!focus)
4248 return;
4249
Pekka Paalanen01388e22013-04-25 13:57:44 +03004250 if (is_black_surface(focus, &main_surface))
4251 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004252
Pekka Paalanen01388e22013-04-25 13:57:44 +03004253 main_surface = weston_surface_get_main_surface(focus);
4254 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004255 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004256
Neil Robertsa28c6932013-10-03 16:43:04 +01004257 activate(shell, focus, seat);
4258}
4259
4260static void
4261click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4262 void *data)
4263{
4264 if (seat->pointer->grab != &seat->pointer->default_grab)
4265 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004266 if (seat->pointer->focus == NULL)
4267 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004268
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004269 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004270}
4271
4272static void
4273touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4274{
4275 if (seat->touch->grab != &seat->touch->default_grab)
4276 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004277 if (seat->touch->focus == NULL)
4278 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004279
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004280 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004281}
4282
4283static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004284lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004285{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004286 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004287
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004288 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004289 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004290 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004291 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004292
4293 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004294
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004295 /* Hide all surfaces by removing the fullscreen, panel and
4296 * toplevel layers. This way nothing else can show or receive
4297 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004298
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004299 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004300 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004301 if (shell->showing_input_panels)
4302 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004303 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004304 wl_list_insert(&shell->compositor->cursor_layer.link,
4305 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004306
Pekka Paalanen77346a62011-11-30 16:26:35 +02004307 launch_screensaver(shell);
4308
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004309 /* TODO: disable bindings that should not work while locked. */
4310
4311 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004312}
4313
4314static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004315unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004316{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004317 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004318 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004319 return;
4320 }
4321
4322 /* If desktop-shell client has gone away, unlock immediately. */
4323 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004324 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004325 return;
4326 }
4327
4328 if (shell->prepare_event_sent)
4329 return;
4330
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004331 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004332 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004333}
4334
4335static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004337{
4338 struct desktop_shell *shell = data;
4339
4340 shell->fade.animation = NULL;
4341
4342 switch (shell->fade.type) {
4343 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 weston_surface_destroy(shell->fade.view->surface);
4345 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004346 break;
4347 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004348 lock(shell);
4349 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004350 default:
4351 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004352 }
4353}
4354
Jason Ekstranda7af7042013-10-12 22:38:11 -05004355static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004356shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004357{
4358 struct weston_compositor *compositor = shell->compositor;
4359 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004361
4362 surface = weston_surface_create(compositor);
4363 if (!surface)
4364 return NULL;
4365
Jason Ekstranda7af7042013-10-12 22:38:11 -05004366 view = weston_view_create(surface);
4367 if (!view) {
4368 weston_surface_destroy(surface);
4369 return NULL;
4370 }
4371
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004372 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004373 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004374 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 wl_list_insert(&compositor->fade_layer.view_list,
4376 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004377 pixman_region32_init(&surface->input);
4378
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004380}
4381
4382static void
4383shell_fade(struct desktop_shell *shell, enum fade_type type)
4384{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004385 float tint;
4386
4387 switch (type) {
4388 case FADE_IN:
4389 tint = 0.0;
4390 break;
4391 case FADE_OUT:
4392 tint = 1.0;
4393 break;
4394 default:
4395 weston_log("shell: invalid fade type\n");
4396 return;
4397 }
4398
4399 shell->fade.type = type;
4400
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 if (shell->fade.view == NULL) {
4402 shell->fade.view = shell_fade_create_surface(shell);
4403 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004404 return;
4405
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 shell->fade.view->alpha = 1.0 - tint;
4407 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004408 }
4409
4410 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004411 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004412 else
4413 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004415 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004416 shell_fade_done, shell);
4417}
4418
4419static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004420do_shell_fade_startup(void *data)
4421{
4422 struct desktop_shell *shell = data;
4423
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004424 if (shell->startup_animation_type == ANIMATION_FADE)
4425 shell_fade(shell, FADE_IN);
4426 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004427 weston_surface_destroy(shell->fade.view->surface);
4428 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004429 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004430}
4431
4432static void
4433shell_fade_startup(struct desktop_shell *shell)
4434{
4435 struct wl_event_loop *loop;
4436
4437 if (!shell->fade.startup_timer)
4438 return;
4439
4440 wl_event_source_remove(shell->fade.startup_timer);
4441 shell->fade.startup_timer = NULL;
4442
4443 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4444 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4445}
4446
4447static int
4448fade_startup_timeout(void *data)
4449{
4450 struct desktop_shell *shell = data;
4451
4452 shell_fade_startup(shell);
4453 return 0;
4454}
4455
4456static void
4457shell_fade_init(struct desktop_shell *shell)
4458{
4459 /* Make compositor output all black, and wait for the desktop-shell
4460 * client to signal it is ready, then fade in. The timer triggers a
4461 * fade-in, in case the desktop-shell client takes too long.
4462 */
4463
4464 struct wl_event_loop *loop;
4465
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004467 weston_log("%s: warning: fade surface already exists\n",
4468 __func__);
4469 return;
4470 }
4471
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 shell->fade.view = shell_fade_create_surface(shell);
4473 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004474 return;
4475
Jason Ekstranda7af7042013-10-12 22:38:11 -05004476 weston_view_update_transform(shell->fade.view);
4477 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004478
4479 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4480 shell->fade.startup_timer =
4481 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4482 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4483}
4484
4485static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004486idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004487{
4488 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004489 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004490
4491 shell_fade(shell, FADE_OUT);
4492 /* lock() is called from shell_fade_done() */
4493}
4494
4495static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004496wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004497{
4498 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004499 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004500
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004501 unlock(shell);
4502}
4503
4504static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004505center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004506{
Giulio Camuffob8366642013-04-25 13:57:46 +03004507 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004508 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004509
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004511
4512 x = output->x + (output->width - width) / 2 - surf_x / 2;
4513 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004514
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004515 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004516}
4517
4518static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004519weston_view_set_initial_position(struct weston_view *view,
4520 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004521{
4522 struct weston_compositor *compositor = shell->compositor;
4523 int ix = 0, iy = 0;
4524 int range_x, range_y;
4525 int dx, dy, x, y, panel_height;
4526 struct weston_output *output, *target_output = NULL;
4527 struct weston_seat *seat;
4528
4529 /* As a heuristic place the new window on the same output as the
4530 * pointer. Falling back to the output containing 0, 0.
4531 *
4532 * TODO: Do something clever for touch too?
4533 */
4534 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004535 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004536 ix = wl_fixed_to_int(seat->pointer->x);
4537 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004538 break;
4539 }
4540 }
4541
4542 wl_list_for_each(output, &compositor->output_list, link) {
4543 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4544 target_output = output;
4545 break;
4546 }
4547 }
4548
4549 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004550 weston_view_set_position(view, 10 + random() % 400,
4551 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004552 return;
4553 }
4554
4555 /* Valid range within output where the surface will still be onscreen.
4556 * If this is negative it means that the surface is bigger than
4557 * output.
4558 */
4559 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004560 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004561 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004562 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004563
Rob Bradford4cb88c72012-08-13 15:18:44 +01004564 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004565 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004566 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004567 dx = 0;
4568
4569 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004570 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004571 else
4572 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004573
4574 x = target_output->x + dx;
4575 y = target_output->y + dy;
4576
Jason Ekstranda7af7042013-10-12 22:38:11 -05004577 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004578}
4579
4580static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004581map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004582 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004583{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004584 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004585 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004586 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004587 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004588
Pekka Paalanen77346a62011-11-30 16:26:35 +02004589 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004591 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004592 if (shsurf->state.fullscreen) {
4593 center_on_output(shsurf->view, shsurf->fullscreen_output);
4594 shell_map_fullscreen(shsurf);
4595 } else if (shsurf->state.maximized) {
4596 /* use surface configure to set the geometry */
4597 panel_height = get_output_panel_height(shell, shsurf->output);
4598 surface_subsurfaces_boundingbox(shsurf->surface,
4599 &surf_x, &surf_y, NULL, NULL);
4600 weston_view_set_position(shsurf->view,
4601 shsurf->output->x - surf_x,
4602 shsurf->output->y +
4603 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004604 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004605 weston_view_set_initial_position(shsurf->view, shell);
4606 }
Juan Zhao96879df2012-02-07 08:45:41 +08004607 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004608 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004609 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004610 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004611 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 weston_view_set_position(shsurf->view,
4613 shsurf->view->geometry.x + sx,
4614 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004615 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004616 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004617 default:
4618 ;
4619 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004620
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004621 /* Surface stacking order, see also activate(). */
4622 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004623
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624 if (shsurf->type != SHELL_SURFACE_NONE) {
4625 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004626 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 shsurf->surface->output = shsurf->output;
4628 shsurf->view->output = shsurf->output;
4629 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004630 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004631
Rafael Antognollied207b42013-12-03 15:35:43 -02004632 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4633 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4634 }
4635
Jason Ekstranda7af7042013-10-12 22:38:11 -05004636 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004637 /* XXX: xwayland's using the same fields for transient type */
4638 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004639 if (shsurf->transient.flags ==
4640 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4641 break;
4642 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004643 if (shsurf->state.relative &&
4644 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4645 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004646 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004647 break;
4648 wl_list_for_each(seat, &compositor->seat_list, link)
4649 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004650 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004651 case SHELL_SURFACE_POPUP:
4652 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004653 default:
4654 break;
4655 }
4656
Rafael Antognolli03b16592013-12-03 15:35:42 -02004657 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4658 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004659 {
4660 switch (shell->win_animation_type) {
4661 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004663 break;
4664 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004665 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004666 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004667 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004668 default:
4669 break;
4670 }
4671 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004672}
4673
4674static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004675configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004676 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004677{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004678 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004679 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004680 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004681
Pekka Paalanen77346a62011-11-30 16:26:35 +02004682 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004683
Rafael Antognolli03b16592013-12-03 15:35:42 -02004684 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004685 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004686 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004687 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004688 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4689 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004690 mx = shsurf->output->x - surf_x;
4691 my = shsurf->output->y +
4692 get_output_panel_height(shell,shsurf->output) - surf_y;
4693 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004694 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004695 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004696 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004697
Alex Wu4539b082012-03-01 12:57:46 +08004698 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004699 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004700 wl_list_for_each(view, &surface->views, surface_link)
4701 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004702
Rafael Antognolli03b16592013-12-03 15:35:42 -02004703 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004704 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004705 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004706}
4707
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004708static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004709shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004710{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004711 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004712 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004713
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004714 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004715
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004716 if (!weston_surface_is_mapped(es) &&
4717 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004718 remove_popup_grab(shsurf);
4719 }
4720
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004721 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004722 return;
4723
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004724 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004725 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004726 type_changed = 1;
4727 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004728
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004729 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004730 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004731 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004732 shsurf->last_width != es->width ||
4733 shsurf->last_height != es->height) {
4734 shsurf->last_width = es->width;
4735 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004736 float from_x, from_y;
4737 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004738
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4740 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004741 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004742 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004743 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004744 }
4745}
4746
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004747static void
4748shell_surface_output_destroyed(struct weston_surface *es)
4749{
4750 struct shell_surface *shsurf = get_shell_surface(es);
4751
4752 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004753 shsurf->next_state.maximized = false;
4754 shsurf->next_state.fullscreen = false;
4755 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004756}
4757
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004758static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004759
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004760static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004761desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004762{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004763 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004764 struct desktop_shell *shell =
4765 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004766
4767 shell->child.process.pid = 0;
4768 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004769
4770 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4771 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004772 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004773 shell->child.deathstamp = time;
4774 shell->child.deathcount = 0;
4775 }
4776
4777 shell->child.deathcount++;
4778 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004779 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004780 return;
4781 }
4782
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004783 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004784 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004785 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004786}
4787
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004788static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004789desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4790{
4791 struct desktop_shell *shell;
4792
4793 shell = container_of(listener, struct desktop_shell,
4794 child.client_destroy_listener);
4795
4796 shell->child.client = NULL;
4797}
4798
4799static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004800launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004801{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004802 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004803
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004804 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004805 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004806 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004807 desktop_shell_sigchld);
4808
4809 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004810 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004811
4812 shell->child.client_destroy_listener.notify =
4813 desktop_shell_client_destroy;
4814 wl_client_add_destroy_listener(shell->child.client,
4815 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004816}
4817
4818static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004819bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4820{
Tiago Vignattibe143262012-04-16 17:31:41 +03004821 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004822 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004823
Jason Ekstranda85118c2013-06-27 20:17:02 -05004824 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4825 if (resource)
4826 wl_resource_set_implementation(resource, &shell_implementation,
4827 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004828}
4829
Kristian Høgsberg75840622011-09-06 13:48:16 -04004830static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004831bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4832{
4833 struct desktop_shell *shell = data;
4834 struct wl_resource *resource;
4835
4836 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4837 if (resource)
4838 wl_resource_set_dispatcher(resource,
4839 xdg_shell_unversioned_dispatch,
4840 NULL, shell, NULL);
4841}
4842
4843static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004844unbind_desktop_shell(struct wl_resource *resource)
4845{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004846 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004847
4848 if (shell->locked)
4849 resume_desktop(shell);
4850
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004851 shell->child.desktop_shell = NULL;
4852 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004853}
4854
4855static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004856bind_desktop_shell(struct wl_client *client,
4857 void *data, uint32_t version, uint32_t id)
4858{
Tiago Vignattibe143262012-04-16 17:31:41 +03004859 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004860 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004861
Jason Ekstranda85118c2013-06-27 20:17:02 -05004862 resource = wl_resource_create(client, &desktop_shell_interface,
4863 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004864
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004865 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004866 wl_resource_set_implementation(resource,
4867 &desktop_shell_implementation,
4868 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004869 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004870
4871 if (version < 2)
4872 shell_fade_startup(shell);
4873
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004874 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004875 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004876
4877 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4878 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004879 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004880}
4881
Pekka Paalanen6e168112011-11-24 11:34:05 +02004882static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004883screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004884{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004885 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004887
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004888 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004889 return;
4890
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004891 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004892 if (!shell->locked)
4893 return;
4894
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 view = container_of(surface->views.next, struct weston_view, surface_link);
4896 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004897
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 if (wl_list_empty(&view->layer_link)) {
4899 wl_list_insert(shell->lock_layer.view_list.prev,
4900 &view->layer_link);
4901 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004902 wl_event_source_timer_update(shell->screensaver.timer,
4903 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004904 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004905 }
4906}
4907
4908static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004909screensaver_set_surface(struct wl_client *client,
4910 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004911 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004912 struct wl_resource *output_resource)
4913{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004914 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004915 struct weston_surface *surface =
4916 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004917 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 struct weston_view *view, *next;
4919
4920 /* Make sure we only have one view */
4921 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4922 weston_view_destroy(view);
4923 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004924
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004925 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004926 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004927 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004928}
4929
4930static const struct screensaver_interface screensaver_implementation = {
4931 screensaver_set_surface
4932};
4933
4934static void
4935unbind_screensaver(struct wl_resource *resource)
4936{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004937 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004938
Pekka Paalanen77346a62011-11-30 16:26:35 +02004939 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004940}
4941
4942static void
4943bind_screensaver(struct wl_client *client,
4944 void *data, uint32_t version, uint32_t id)
4945{
Tiago Vignattibe143262012-04-16 17:31:41 +03004946 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004947 struct wl_resource *resource;
4948
Jason Ekstranda85118c2013-06-27 20:17:02 -05004949 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004950
Pekka Paalanen77346a62011-11-30 16:26:35 +02004951 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004952 wl_resource_set_implementation(resource,
4953 &screensaver_implementation,
4954 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004955 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004956 return;
4957 }
4958
4959 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4960 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004961 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004962}
4963
Kristian Høgsberg07045392012-02-19 18:52:44 -05004964struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004965 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004966 struct weston_surface *current;
4967 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004968 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004969};
4970
4971static void
4972switcher_next(struct switcher *switcher)
4973{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004974 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004975 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004976 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004977 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004978
Jason Ekstranda7af7042013-10-12 22:38:11 -05004979 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004980 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02004981 if (shsurf &&
4982 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4983 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004984 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004985 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004986 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004987 next = view->surface;
4988 prev = view->surface;
4989 view->alpha = 0.25;
4990 weston_view_geometry_dirty(view);
4991 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004992 }
Alex Wu1659daa2012-04-01 20:13:09 +08004993
Jason Ekstranda7af7042013-10-12 22:38:11 -05004994 if (is_black_surface(view->surface, NULL)) {
4995 view->alpha = 0.25;
4996 weston_view_geometry_dirty(view);
4997 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004998 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004999 }
5000
5001 if (next == NULL)
5002 next = first;
5003
Alex Wu07b26062012-03-12 16:06:01 +08005004 if (next == NULL)
5005 return;
5006
Kristian Høgsberg07045392012-02-19 18:52:44 -05005007 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005008 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005009
5010 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005011 wl_list_for_each(view, &next->views, surface_link)
5012 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005013
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005014 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005015 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005016 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005017}
5018
5019static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005020switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005021{
5022 struct switcher *switcher =
5023 container_of(listener, struct switcher, listener);
5024
5025 switcher_next(switcher);
5026}
5027
5028static void
Daniel Stone351eb612012-05-31 15:27:47 -04005029switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005030{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005031 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005032 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005033 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005034
Jason Ekstranda7af7042013-10-12 22:38:11 -05005035 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005036 if (is_focus_view(view))
5037 continue;
5038
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 view->alpha = 1.0;
5040 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005041 }
5042
Alex Wu07b26062012-03-12 16:06:01 +08005043 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005044 activate(switcher->shell, switcher->current,
5045 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005046 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005047 weston_keyboard_end_grab(keyboard);
5048 if (keyboard->input_method_resource)
5049 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005050 free(switcher);
5051}
5052
5053static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005054switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005055 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005056{
5057 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005058 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005059
Daniel Stonec9785ea2012-05-30 16:31:52 +01005060 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005061 switcher_next(switcher);
5062}
5063
5064static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005065switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005066 uint32_t mods_depressed, uint32_t mods_latched,
5067 uint32_t mods_locked, uint32_t group)
5068{
5069 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005070 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005071
Daniel Stone351eb612012-05-31 15:27:47 -04005072 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5073 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005074}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005075
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005076static void
5077switcher_cancel(struct weston_keyboard_grab *grab)
5078{
5079 struct switcher *switcher = container_of(grab, struct switcher, grab);
5080
5081 switcher_destroy(switcher);
5082}
5083
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005084static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005085 switcher_key,
5086 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005087 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005088};
5089
5090static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005091switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005092 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093{
Tiago Vignattibe143262012-04-16 17:31:41 +03005094 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005095 struct switcher *switcher;
5096
5097 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005098 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005099 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005100 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005101 wl_list_init(&switcher->listener.link);
5102
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005103 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005104 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005105 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005106 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5107 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005108 switcher_next(switcher);
5109}
5110
Pekka Paalanen3c647232011-12-22 13:43:43 +02005111static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005112backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005113 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005114{
5115 struct weston_compositor *compositor = data;
5116 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005117 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005118
5119 /* TODO: we're limiting to simple use cases, where we assume just
5120 * control on the primary display. We'd have to extend later if we
5121 * ever get support for setting backlights on random desktop LCD
5122 * panels though */
5123 output = get_default_output(compositor);
5124 if (!output)
5125 return;
5126
5127 if (!output->set_backlight)
5128 return;
5129
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005130 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5131 backlight_new = output->backlight_current - 25;
5132 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5133 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005134
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005135 if (backlight_new < 5)
5136 backlight_new = 5;
5137 if (backlight_new > 255)
5138 backlight_new = 255;
5139
5140 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005141 output->set_backlight(output, output->backlight_current);
5142}
5143
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005144struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005145 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005146 struct weston_seat *seat;
5147 uint32_t key[2];
5148 int key_released[2];
5149};
5150
5151static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005152debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005153 uint32_t key, uint32_t state)
5154{
5155 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005156 struct weston_compositor *ec = db->seat->compositor;
5157 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005158 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005159 uint32_t serial;
5160 int send = 0, terminate = 0;
5161 int check_binding = 1;
5162 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005163 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005164
5165 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5166 /* Do not run bindings on key releases */
5167 check_binding = 0;
5168
5169 for (i = 0; i < 2; i++)
5170 if (key == db->key[i])
5171 db->key_released[i] = 1;
5172
5173 if (db->key_released[0] && db->key_released[1]) {
5174 /* All key releases been swalled so end the grab */
5175 terminate = 1;
5176 } else if (key != db->key[0] && key != db->key[1]) {
5177 /* Should not swallow release of other keys */
5178 send = 1;
5179 }
5180 } else if (key == db->key[0] && !db->key_released[0]) {
5181 /* Do not check bindings for the first press of the binding
5182 * key. This allows it to be used as a debug shortcut.
5183 * We still need to swallow this event. */
5184 check_binding = 0;
5185 } else if (db->key[1]) {
5186 /* If we already ran a binding don't process another one since
5187 * we can't keep track of all the binding keys that were
5188 * pressed in order to swallow the release events. */
5189 send = 1;
5190 check_binding = 0;
5191 }
5192
5193 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005194 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5195 key, state)) {
5196 /* We ran a binding so swallow the press and keep the
5197 * grab to swallow the released too. */
5198 send = 0;
5199 terminate = 0;
5200 db->key[1] = key;
5201 } else {
5202 /* Terminate the grab since the key pressed is not a
5203 * debug binding key. */
5204 send = 1;
5205 terminate = 1;
5206 }
5207 }
5208
5209 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005210 serial = wl_display_next_serial(display);
5211 resource_list = &grab->keyboard->focus_resource_list;
5212 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005213 wl_keyboard_send_key(resource, serial, time, key, state);
5214 }
5215 }
5216
5217 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005218 weston_keyboard_end_grab(grab->keyboard);
5219 if (grab->keyboard->input_method_resource)
5220 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005221 free(db);
5222 }
5223}
5224
5225static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005226debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005227 uint32_t mods_depressed, uint32_t mods_latched,
5228 uint32_t mods_locked, uint32_t group)
5229{
5230 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005231 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005232
Neil Roberts96d790e2013-09-19 17:32:00 +01005233 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005234
Neil Roberts96d790e2013-09-19 17:32:00 +01005235 wl_resource_for_each(resource, resource_list) {
5236 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5237 mods_latched, mods_locked, group);
5238 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005239}
5240
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005241static void
5242debug_binding_cancel(struct weston_keyboard_grab *grab)
5243{
5244 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5245
5246 weston_keyboard_end_grab(grab->keyboard);
5247 free(db);
5248}
5249
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005250struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005251 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005252 debug_binding_modifiers,
5253 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005254};
5255
5256static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005257debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005258{
5259 struct debug_binding_grab *grab;
5260
5261 grab = calloc(1, sizeof *grab);
5262 if (!grab)
5263 return;
5264
5265 grab->seat = (struct weston_seat *) seat;
5266 grab->key[0] = key;
5267 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005268 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005269}
5270
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005271static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005272force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005273 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005274{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005275 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005276 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005277 struct desktop_shell *shell = data;
5278 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005279 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005280
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005281 focus_surface = seat->keyboard->focus;
5282 if (!focus_surface)
5283 return;
5284
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005285 wl_signal_emit(&compositor->kill_signal, focus_surface);
5286
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005287 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005288 wl_client_get_credentials(client, &pid, NULL, NULL);
5289
5290 /* Skip clients that we launched ourselves (the credentials of
5291 * the socketpair is ours) */
5292 if (pid == getpid())
5293 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005294
Daniel Stone325fc2d2012-05-30 16:31:58 +01005295 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005296}
5297
5298static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005299workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005300 uint32_t key, void *data)
5301{
5302 struct desktop_shell *shell = data;
5303 unsigned int new_index = shell->workspaces.current;
5304
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005305 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005306 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005307 if (new_index != 0)
5308 new_index--;
5309
5310 change_workspace(shell, new_index);
5311}
5312
5313static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005314workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005315 uint32_t key, void *data)
5316{
5317 struct desktop_shell *shell = data;
5318 unsigned int new_index = shell->workspaces.current;
5319
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005320 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005321 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005322 if (new_index < shell->workspaces.num - 1)
5323 new_index++;
5324
5325 change_workspace(shell, new_index);
5326}
5327
5328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005329workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005330 uint32_t key, void *data)
5331{
5332 struct desktop_shell *shell = data;
5333 unsigned int new_index;
5334
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005335 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005336 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005337 new_index = key - KEY_F1;
5338 if (new_index >= shell->workspaces.num)
5339 new_index = shell->workspaces.num - 1;
5340
5341 change_workspace(shell, new_index);
5342}
5343
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005344static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005345workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005346 uint32_t key, void *data)
5347{
5348 struct desktop_shell *shell = data;
5349 unsigned int new_index = shell->workspaces.current;
5350
5351 if (shell->locked)
5352 return;
5353
5354 if (new_index != 0)
5355 new_index--;
5356
5357 take_surface_to_workspace_by_seat(shell, seat, new_index);
5358}
5359
5360static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005361workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005362 uint32_t key, void *data)
5363{
5364 struct desktop_shell *shell = data;
5365 unsigned int new_index = shell->workspaces.current;
5366
5367 if (shell->locked)
5368 return;
5369
5370 if (new_index < shell->workspaces.num - 1)
5371 new_index++;
5372
5373 take_surface_to_workspace_by_seat(shell, seat, new_index);
5374}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005375
5376static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005377handle_output_destroy(struct wl_listener *listener, void *data)
5378{
5379 struct shell_output *output_listener =
5380 container_of(listener, struct shell_output, destroy_listener);
5381
5382 wl_list_remove(&output_listener->destroy_listener.link);
5383 wl_list_remove(&output_listener->link);
5384 free(output_listener);
5385}
5386
5387static void
5388create_shell_output(struct desktop_shell *shell,
5389 struct weston_output *output)
5390{
5391 struct shell_output *shell_output;
5392
5393 shell_output = zalloc(sizeof *shell_output);
5394 if (shell_output == NULL)
5395 return;
5396
5397 shell_output->output = output;
5398 shell_output->shell = shell;
5399 shell_output->destroy_listener.notify = handle_output_destroy;
5400 wl_signal_add(&output->destroy_signal,
5401 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005402 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005403}
5404
5405static void
5406handle_output_create(struct wl_listener *listener, void *data)
5407{
5408 struct desktop_shell *shell =
5409 container_of(listener, struct desktop_shell, output_create_listener);
5410 struct weston_output *output = (struct weston_output *)data;
5411
5412 create_shell_output(shell, output);
5413}
5414
5415static void
5416setup_output_destroy_handler(struct weston_compositor *ec,
5417 struct desktop_shell *shell)
5418{
5419 struct weston_output *output;
5420
5421 wl_list_init(&shell->output_list);
5422 wl_list_for_each(output, &ec->output_list, link)
5423 create_shell_output(shell, output);
5424
5425 shell->output_create_listener.notify = handle_output_create;
5426 wl_signal_add(&ec->output_created_signal,
5427 &shell->output_create_listener);
5428}
5429
5430static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005431shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005432{
Tiago Vignattibe143262012-04-16 17:31:41 +03005433 struct desktop_shell *shell =
5434 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005435 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005436 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005437
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005438 /* Force state to unlocked so we don't try to fade */
5439 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005440 if (shell->child.client)
5441 wl_client_destroy(shell->child.client);
5442
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005443 wl_list_remove(&shell->idle_listener.link);
5444 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005445
5446 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005447
Xiong Zhang6b481422013-10-23 13:58:32 +08005448 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5449 wl_list_remove(&shell_output->destroy_listener.link);
5450 wl_list_remove(&shell_output->link);
5451 free(shell_output);
5452 }
5453
5454 wl_list_remove(&shell->output_create_listener.link);
5455
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005456 wl_array_for_each(ws, &shell->workspaces.array)
5457 workspace_destroy(*ws);
5458 wl_array_release(&shell->workspaces.array);
5459
Pekka Paalanen3c647232011-12-22 13:43:43 +02005460 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005461 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005462 free(shell);
5463}
5464
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005465static void
5466shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5467{
5468 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005469 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005470
5471 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005472 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5473 MODIFIER_CTRL | MODIFIER_ALT,
5474 terminate_binding, ec);
5475 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5476 click_to_activate_binding,
5477 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005478 weston_compositor_add_touch_binding(ec, 0,
5479 touch_to_activate_binding,
5480 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005481 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5482 MODIFIER_SUPER | MODIFIER_ALT,
5483 surface_opacity_binding, NULL);
5484 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5485 MODIFIER_SUPER, zoom_axis_binding,
5486 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005487
5488 /* configurable bindings */
5489 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005490 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5491 zoom_key_binding, NULL);
5492 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5493 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005494 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5495 maximize_binding, NULL);
5496 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5497 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005498 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5499 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005500 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005501 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5502 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005503
5504 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5505 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5506 rotate_binding, NULL);
5507
Daniel Stone325fc2d2012-05-30 16:31:58 +01005508 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5509 shell);
5510 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5511 ec);
5512 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5513 backlight_binding, ec);
5514 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5515 ec);
5516 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5517 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005518 weston_compositor_add_key_binding(ec, KEY_K, mod,
5519 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005520 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5521 workspace_up_binding, shell);
5522 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5523 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005524 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5525 workspace_move_surface_up_binding,
5526 shell);
5527 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5528 workspace_move_surface_down_binding,
5529 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005530
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005531 if (shell->exposay_modifier)
5532 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5533 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005534
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005535 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5536 if (shell->workspaces.num > 1) {
5537 num_workspace_bindings = shell->workspaces.num;
5538 if (num_workspace_bindings > 6)
5539 num_workspace_bindings = 6;
5540 for (i = 0; i < num_workspace_bindings; i++)
5541 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5542 workspace_f_binding,
5543 shell);
5544 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005545
5546 /* Debug bindings */
5547 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5548 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005549}
5550
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005551WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005552module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005553 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005554{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005555 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005556 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005557 struct workspace **pws;
5558 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005559 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005560
Peter Huttererf3d62272013-08-08 11:57:05 +10005561 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005562 if (shell == NULL)
5563 return -1;
5564
Kristian Høgsberg75840622011-09-06 13:48:16 -04005565 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005566
5567 shell->destroy_listener.notify = shell_destroy;
5568 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005569 shell->idle_listener.notify = idle_handler;
5570 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5571 shell->wake_listener.notify = wake_handler;
5572 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005573
Scott Moreauff1db4a2012-04-17 19:06:18 -06005574 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005575 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005576 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005577 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005578 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005579 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005580 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005581 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005582 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005583 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005584 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005585
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005586 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5587 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005588 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5589 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005590 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005591
5592 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005593 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005594
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005595 if (input_panel_setup(shell) < 0)
5596 return -1;
5597
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005598 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005599
Daniel Stonedf8133b2013-11-19 11:37:14 +01005600 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5601 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5602
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005603 for (i = 0; i < shell->workspaces.num; i++) {
5604 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5605 if (pws == NULL)
5606 return -1;
5607
5608 *pws = workspace_create();
5609 if (*pws == NULL)
5610 return -1;
5611 }
5612 activate_workspace(shell, 0);
5613
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005614 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005615 wl_list_init(&shell->workspaces.animation.link);
5616 shell->workspaces.animation.frame = animate_workspace_change_frame;
5617
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005618 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005619 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005620 return -1;
5621
Rafael Antognollie2a34552013-12-03 15:35:45 -02005622 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5623 shell, bind_xdg_shell) == NULL)
5624 return -1;
5625
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005626 if (wl_global_create(ec->wl_display,
5627 &desktop_shell_interface, 2,
5628 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005629 return -1;
5630
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005631 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5632 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005633 return -1;
5634
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005635 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5636 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005637 return -1;
5638
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005639 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005640
Xiong Zhang6b481422013-10-23 13:58:32 +08005641 setup_output_destroy_handler(ec, shell);
5642
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005643 loop = wl_display_get_event_loop(ec->wl_display);
5644 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005645
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005646 shell->screensaver.timer =
5647 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5648
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005649 wl_list_for_each(seat, &ec->seat_list, link)
5650 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005651
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005652 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005653
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005654 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005655
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005656 return 0;
5657}