blob: 7d85a7beaa1aa8b954c6d72ad29c63b5e64cccd3 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Scott Moreauff1db4a2012-04-17 19:06:18 -060073struct ping_timer {
74 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060075 uint32_t serial;
76};
77
Philip Withnall648a4dd2013-11-25 18:01:44 +000078/*
79 * Surface stacking and ordering.
80 *
81 * This is handled using several linked lists of surfaces, organised into
82 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
83 * above all of the surfaces in the layer below. The set of layers is static and
84 * in the following order (top-most first):
85 * • Lock layer (only ever displayed on its own)
86 * • Cursor layer
87 * • Fullscreen layer
88 * • Panel layer
89 * • Input panel layer
90 * • Workspace layers
91 * • Background layer
92 *
93 * The list of layers may be manipulated to remove whole layers of surfaces from
94 * display. For example, when locking the screen, all layers except the lock
95 * layer are removed.
96 *
97 * A surface’s layer is modified on configuring the surface, in
98 * set_surface_type() (which is only called when the surface’s type change is
99 * _committed_). If a surface’s type changes (e.g. when making a window
100 * fullscreen) its layer changes too.
101 *
102 * In order to allow popup and transient surfaces to be correctly stacked above
103 * their parent surfaces, each surface tracks both its parent surface, and a
104 * linked list of its children. When a surface’s layer is updated, so are the
105 * layers of its children. Note that child surfaces are *not* the same as
106 * subsurfaces — child/parent surfaces are purely for maintaining stacking
107 * order.
108 *
109 * The children_link list of siblings of a surface (i.e. those surfaces which
110 * have the same parent) only contains weston_surfaces which have a
111 * shell_surface. Stacking is not implemented for non-shell_surface
112 * weston_surfaces. This means that the following implication does *not* hold:
113 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
114 */
115
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200116struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500117 struct wl_resource *resource;
118 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600122 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200123 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400124 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000125 struct wl_list children_list; /* child surfaces of this one */
126 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300127 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800129 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400130 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200132 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800133 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200134 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800135 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700136 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100137
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500140 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 } rotation;
142
143 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100144 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100146 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400147 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 } popup;
149
Alex Wu4539b082012-03-01 12:57:46 +0800150 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300151 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400152 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300153 } transient;
154
155 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800156 enum wl_shell_surface_fullscreen_method type;
157 struct weston_transform transform; /* matrix from x, y */
158 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800160 } fullscreen;
161
Scott Moreauff1db4a2012-04-17 19:06:18 -0600162 struct ping_timer *ping_timer;
163
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200164 struct weston_transform workspace_transform;
165
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500166 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200168 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100169 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400170
171 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172
173 struct {
174 bool maximized;
175 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200176 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200177 } state, next_state; /* surface states */
178 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct weston_touch_move_grab {
200 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800201 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700202 wl_fixed_t dx, dy;
203};
204
Pekka Paalanen460099f2012-01-20 16:48:25 +0200205struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500207 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200209 float x;
210 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 } center;
212};
213
Giulio Camuffo5085a752013-03-25 21:42:45 +0100214struct shell_seat {
215 struct weston_seat *seat;
216 struct wl_listener seat_destroy_listener;
217
218 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400219 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220 struct wl_list surfaces_list;
221 struct wl_client *client;
222 int32_t initial_up;
223 } popup_grab;
224};
225
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100226void
227set_alpha_if_fullscreen(struct shell_surface *shsurf)
228{
229 if (shsurf && shsurf->state.fullscreen)
230 shsurf->fullscreen.black_view->alpha = 0.25;
231}
232
Alex Wubd3354b2012-04-17 17:20:49 +0800233static struct desktop_shell *
234shell_surface_get_shell(struct shell_surface *shsurf);
235
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500236static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400237surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500238
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300239static void
240shell_fade_startup(struct desktop_shell *shell);
241
Philip Withnallbecb77e2013-11-25 18:01:30 +0000242static struct shell_seat *
243get_shell_seat(struct weston_seat *seat);
244
Philip Withnall648a4dd2013-11-25 18:01:44 +0000245static void
246shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
247
Alex Wubd3354b2012-04-17 17:20:49 +0800248static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200249shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
250
251static bool
252shell_surface_is_xdg_surface(struct shell_surface *shsurf);
253
254static bool
255shell_surface_is_xdg_popup(struct shell_surface *shsurf);
256
257static void
258shell_surface_set_parent(struct shell_surface *shsurf,
259 struct weston_surface *parent);
260
261static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800262shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
263{
264 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800266
267 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100268
Jason Ekstranda7af7042013-10-12 22:38:11 -0500269 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800270 return false;
271
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
273 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800274 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800276}
277
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500278static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280{
281 struct shell_grab *grab;
282
283 grab = container_of(listener, struct shell_grab,
284 shsurf_destroy_listener);
285
286 grab->shsurf = NULL;
287}
288
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800289struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500290get_default_view(struct weston_surface *surface)
291{
292 struct shell_surface *shsurf;
293 struct weston_view *view;
294
295 if (!surface || wl_list_empty(&surface->views))
296 return NULL;
297
298 shsurf = get_shell_surface(surface);
299 if (shsurf)
300 return shsurf->view;
301
302 wl_list_for_each(view, &surface->views, surface_link)
303 if (weston_view_is_mapped(view))
304 return view;
305
306 return container_of(surface->views.next, struct weston_view, surface_link);
307}
308
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300309static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400310popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400311
312static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300313shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300315 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct desktop_shell *shell = shsurf->shell;
320
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400321 popup_grab_end(pointer);
322
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323 grab->grab.interface = interface;
324 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400325 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500326 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400327 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700329 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400330 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400331 if (shell->child.desktop_shell) {
332 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
333 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 weston_pointer_set_focus(pointer,
335 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400336 wl_fixed_from_int(0),
337 wl_fixed_from_int(0));
338 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339}
340
341static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300342shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700344 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400345 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700346 grab->shsurf->grabbed = 0;
347 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700349 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350}
351
352static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700353shell_touch_grab_start(struct shell_touch_grab *grab,
354 const struct weston_touch_grab_interface *interface,
355 struct shell_surface *shsurf,
356 struct weston_touch *touch)
357{
358 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800359
Rusty Lynch1084da52013-08-15 09:10:08 -0700360 grab->grab.interface = interface;
361 grab->shsurf = shsurf;
362 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
363 wl_signal_add(&shsurf->destroy_signal,
364 &grab->shsurf_destroy_listener);
365
366 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700367 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700368
369 weston_touch_start_grab(touch, &grab->grab);
370 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371 weston_touch_set_focus(touch->seat,
372 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700373}
374
375static void
376shell_touch_grab_end(struct shell_touch_grab *grab)
377{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700379 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700380 grab->shsurf->grabbed = 0;
381 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700382
383 weston_touch_end_grab(grab->touch);
384}
385
386static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500387center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800388 struct weston_output *output);
389
Daniel Stone496ca172012-05-30 16:31:42 +0100390static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300391get_modifier(char *modifier)
392{
393 if (!modifier)
394 return MODIFIER_SUPER;
395
396 if (!strcmp("ctrl", modifier))
397 return MODIFIER_CTRL;
398 else if (!strcmp("alt", modifier))
399 return MODIFIER_ALT;
400 else if (!strcmp("super", modifier))
401 return MODIFIER_SUPER;
402 else
403 return MODIFIER_SUPER;
404}
405
Juan Zhaoe10d2792012-04-25 19:09:52 +0800406static enum animation_type
407get_animation_type(char *animation)
408{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800409 if (!animation)
410 return ANIMATION_NONE;
411
Juan Zhaoe10d2792012-04-25 19:09:52 +0800412 if (!strcmp("zoom", animation))
413 return ANIMATION_ZOOM;
414 else if (!strcmp("fade", animation))
415 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100416 else if (!strcmp("dim-layer", animation))
417 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800418 else
419 return ANIMATION_NONE;
420}
421
Alex Wu4539b082012-03-01 12:57:46 +0800422static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400423shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200424{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400425 struct weston_config_section *section;
426 int duration;
427 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200428
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400429 section = weston_config_get_section(shell->compositor->config,
430 "screensaver", NULL, NULL);
431 weston_config_section_get_string(section,
432 "path", &shell->screensaver.path, NULL);
433 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200434 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400435
436 section = weston_config_get_section(shell->compositor->config,
437 "shell", NULL, NULL);
438 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100439 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100440 shell->client = s;
441 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 "binding-modifier", &s, "super");
443 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200444 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800445
446 weston_config_section_get_string(section,
447 "exposay-modifier", &s, "none");
448 if (strcmp(s, "none") == 0)
449 shell->exposay_modifier = 0;
450 else
451 shell->exposay_modifier = get_modifier(s);
452 free(s);
453
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400454 weston_config_section_get_string(section, "animation", &s, "none");
455 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200456 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700457 weston_config_section_get_string(section,
458 "startup-animation", &s, "fade");
459 shell->startup_animation_type = get_animation_type(s);
460 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700461 if (shell->startup_animation_type == ANIMATION_ZOOM)
462 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100463 weston_config_section_get_string(section, "focus-animation", &s, "none");
464 shell->focus_animation_type = get_animation_type(s);
465 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466 weston_config_section_get_uint(section, "num-workspaces",
467 &shell->workspaces.num,
468 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200469}
470
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800471struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100472get_default_output(struct weston_compositor *compositor)
473{
474 return container_of(compositor->output_list.next,
475 struct weston_output, link);
476}
477
478
479/* no-op func for checking focus surface */
480static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600481focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100482{
483}
484
485static struct focus_surface *
486get_focus_surface(struct weston_surface *surface)
487{
488 if (surface->configure == focus_surface_configure)
489 return surface->configure_private;
490 else
491 return NULL;
492}
493
494static bool
495is_focus_surface (struct weston_surface *es)
496{
497 return (es->configure == focus_surface_configure);
498}
499
500static bool
501is_focus_view (struct weston_view *view)
502{
503 return is_focus_surface (view->surface);
504}
505
506static struct focus_surface *
507create_focus_surface(struct weston_compositor *ec,
508 struct weston_output *output)
509{
510 struct focus_surface *fsurf = NULL;
511 struct weston_surface *surface = NULL;
512
513 fsurf = malloc(sizeof *fsurf);
514 if (!fsurf)
515 return NULL;
516
517 fsurf->surface = weston_surface_create(ec);
518 surface = fsurf->surface;
519 if (surface == NULL) {
520 free(fsurf);
521 return NULL;
522 }
523
524 surface->configure = focus_surface_configure;
525 surface->output = output;
526 surface->configure_private = fsurf;
527
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800528 fsurf->view = weston_view_create(surface);
529 if (fsurf->view == NULL) {
530 weston_surface_destroy(surface);
531 free(fsurf);
532 return NULL;
533 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100534 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600536 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600537 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
539 pixman_region32_fini(&surface->opaque);
540 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
541 output->width, output->height);
542 pixman_region32_fini(&surface->input);
543 pixman_region32_init(&surface->input);
544
545 wl_list_init(&fsurf->workspace_transform.link);
546
547 return fsurf;
548}
549
550static void
551focus_surface_destroy(struct focus_surface *fsurf)
552{
553 weston_surface_destroy(fsurf->surface);
554 free(fsurf);
555}
556
557static void
558focus_animation_done(struct weston_view_animation *animation, void *data)
559{
560 struct workspace *ws = data;
561
562 ws->focus_animation = NULL;
563}
564
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200565static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200566focus_state_destroy(struct focus_state *state)
567{
568 wl_list_remove(&state->seat_destroy_listener.link);
569 wl_list_remove(&state->surface_destroy_listener.link);
570 free(state);
571}
572
573static void
574focus_state_seat_destroy(struct wl_listener *listener, void *data)
575{
576 struct focus_state *state = container_of(listener,
577 struct focus_state,
578 seat_destroy_listener);
579
580 wl_list_remove(&state->link);
581 focus_state_destroy(state);
582}
583
584static void
585focus_state_surface_destroy(struct wl_listener *listener, void *data)
586{
587 struct focus_state *state = container_of(listener,
588 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400589 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400590 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 struct weston_surface *main_surface, *next;
592 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200593
Pekka Paalanen01388e22013-04-25 13:57:44 +0300594 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
595
Kristian Høgsberge3778222012-07-31 17:29:30 -0400596 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
598 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400599 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100600 if (is_focus_view(view))
601 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400602
Jason Ekstranda7af7042013-10-12 22:38:11 -0500603 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400604 break;
605 }
606
Pekka Paalanen01388e22013-04-25 13:57:44 +0300607 /* if the focus was a sub-surface, activate its main surface */
608 if (main_surface != state->keyboard_focus)
609 next = main_surface;
610
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100611 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400612 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100613 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400614 activate(shell, next, state->seat);
615 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
617 if (state->ws->focus_animation)
618 weston_view_animation_destroy(state->ws->focus_animation);
619
620 state->ws->focus_animation = weston_fade_run(
621 state->ws->fsurf_front->view,
622 state->ws->fsurf_front->view->alpha, 0.0, 300,
623 focus_animation_done, state->ws);
624 }
625
Kristian Høgsberge3778222012-07-31 17:29:30 -0400626 wl_list_remove(&state->link);
627 focus_state_destroy(state);
628 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200629}
630
631static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400632focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200633{
Jonas Ådahl04769742012-06-13 00:01:24 +0200634 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200635
636 state = malloc(sizeof *state);
637 if (state == NULL)
638 return NULL;
639
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100640 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400641 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400643 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200644
645 state->seat_destroy_listener.notify = focus_state_seat_destroy;
646 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400647 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200648 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400649 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200650
651 return state;
652}
653
Jonas Ådahl8538b222012-08-29 22:13:03 +0200654static struct focus_state *
655ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
656{
657 struct workspace *ws = get_current_workspace(shell);
658 struct focus_state *state;
659
660 wl_list_for_each(state, &ws->focus_list, link)
661 if (state->seat == seat)
662 break;
663
664 if (&state->link == &ws->focus_list)
665 state = focus_state_create(seat, ws);
666
667 return state;
668}
669
Jonas Ådahl04769742012-06-13 00:01:24 +0200670static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800671focus_state_set_focus(struct focus_state *state,
672 struct weston_surface *surface)
673{
674 if (state->keyboard_focus) {
675 wl_list_remove(&state->surface_destroy_listener.link);
676 wl_list_init(&state->surface_destroy_listener.link);
677 }
678
679 state->keyboard_focus = surface;
680 if (surface)
681 wl_signal_add(&surface->destroy_signal,
682 &state->surface_destroy_listener);
683}
684
685static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400686restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200687{
688 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400689 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200690
691 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800692 if (state->seat->keyboard == NULL)
693 continue;
694
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400695 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200696
Kristian Høgsberge3148752013-05-06 23:19:49 -0400697 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 }
699}
700
701static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200702replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
703 struct weston_seat *seat)
704{
705 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200706
707 wl_list_for_each(state, &ws->focus_list, link) {
708 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800709 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200710 return;
711 }
712 }
713}
714
715static void
716drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
717 struct weston_surface *surface)
718{
719 struct focus_state *state;
720
721 wl_list_for_each(state, &ws->focus_list, link)
722 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800723 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200724}
725
726static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100727animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
728 struct weston_view *from, struct weston_view *to)
729{
730 struct weston_output *output;
731 bool focus_surface_created = false;
732
733 /* FIXME: Only support dim animation using two layers */
734 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
735 return;
736
737 output = get_default_output(shell->compositor);
738 if (ws->fsurf_front == NULL && (from || to)) {
739 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800740 if (ws->fsurf_front == NULL)
741 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100742 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800743
744 ws->fsurf_back = create_focus_surface(shell->compositor, output);
745 if (ws->fsurf_back == NULL) {
746 focus_surface_destroy(ws->fsurf_front);
747 return;
748 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100751 focus_surface_created = true;
752 } else {
753 wl_list_remove(&ws->fsurf_front->view->layer_link);
754 wl_list_remove(&ws->fsurf_back->view->layer_link);
755 }
756
757 if (ws->focus_animation) {
758 weston_view_animation_destroy(ws->focus_animation);
759 ws->focus_animation = NULL;
760 }
761
762 if (to)
763 wl_list_insert(&to->layer_link,
764 &ws->fsurf_front->view->layer_link);
765 else if (from)
766 wl_list_insert(&ws->layer.view_list,
767 &ws->fsurf_front->view->layer_link);
768
769 if (focus_surface_created) {
770 ws->focus_animation = weston_fade_run(
771 ws->fsurf_front->view,
772 ws->fsurf_front->view->alpha, 0.6, 300,
773 focus_animation_done, ws);
774 } else if (from) {
775 wl_list_insert(&from->layer_link,
776 &ws->fsurf_back->view->layer_link);
777 ws->focus_animation = weston_stable_fade_run(
778 ws->fsurf_front->view, 0.0,
779 ws->fsurf_back->view, 0.6,
780 focus_animation_done, ws);
781 } else if (to) {
782 wl_list_insert(&ws->layer.view_list,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
788 }
789}
790
791static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792workspace_destroy(struct workspace *ws)
793{
Jonas Ådahl04769742012-06-13 00:01:24 +0200794 struct focus_state *state, *next;
795
796 wl_list_for_each_safe(state, next, &ws->focus_list, link)
797 focus_state_destroy(state);
798
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100799 if (ws->fsurf_front)
800 focus_surface_destroy(ws->fsurf_front);
801 if (ws->fsurf_back)
802 focus_surface_destroy(ws->fsurf_back);
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804 free(ws);
805}
806
Jonas Ådahl04769742012-06-13 00:01:24 +0200807static void
808seat_destroyed(struct wl_listener *listener, void *data)
809{
810 struct weston_seat *seat = data;
811 struct focus_state *state, *next;
812 struct workspace *ws = container_of(listener,
813 struct workspace,
814 seat_destroyed_listener);
815
816 wl_list_for_each_safe(state, next, &ws->focus_list, link)
817 if (state->seat == seat)
818 wl_list_remove(&state->link);
819}
820
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200821static struct workspace *
822workspace_create(void)
823{
824 struct workspace *ws = malloc(sizeof *ws);
825 if (ws == NULL)
826 return NULL;
827
828 weston_layer_init(&ws->layer, NULL);
829
Jonas Ådahl04769742012-06-13 00:01:24 +0200830 wl_list_init(&ws->focus_list);
831 wl_list_init(&ws->seat_destroyed_listener.link);
832 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 ws->fsurf_front = NULL;
834 ws->fsurf_back = NULL;
835 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200836
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200837 return ws;
838}
839
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200840static int
841workspace_is_empty(struct workspace *ws)
842{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500843 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200844}
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846static struct workspace *
847get_workspace(struct desktop_shell *shell, unsigned int index)
848{
849 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200850 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851 pws += index;
852 return *pws;
853}
854
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800855struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856get_current_workspace(struct desktop_shell *shell)
857{
858 return get_workspace(shell, shell->workspaces.current);
859}
860
861static void
862activate_workspace(struct desktop_shell *shell, unsigned int index)
863{
864 struct workspace *ws;
865
866 ws = get_workspace(shell, index);
867 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
868
869 shell->workspaces.current = index;
870}
871
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200872static unsigned int
873get_output_height(struct weston_output *output)
874{
875 return abs(output->region.extents.y1 - output->region.extents.y2);
876}
877
878static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100879view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881 struct weston_transform *transform;
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 if (is_focus_view(view)) {
884 struct focus_surface *fsurf = get_focus_surface(view->surface);
885 transform = &fsurf->workspace_transform;
886 } else {
887 struct shell_surface *shsurf = get_shell_surface(view->surface);
888 transform = &shsurf->workspace_transform;
889 }
890
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100893 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100895 weston_matrix_init(&transform->matrix);
896 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899}
900
901static void
902workspace_translate_out(struct workspace *ws, double fraction)
903{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905 unsigned int height;
906 double d;
907
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
909 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200910 d = height * fraction;
911
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913 }
914}
915
916static void
917workspace_translate_in(struct workspace *ws, double fraction)
918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200920 unsigned int height;
921 double d;
922
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
924 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925
926 if (fraction > 0)
927 d = -(height - height * fraction);
928 else
929 d = height + height * fraction;
930
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100931 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932 }
933}
934
935static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200936broadcast_current_workspace_state(struct desktop_shell *shell)
937{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700938 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700940 wl_resource_for_each(resource, &shell->workspaces.client_list)
941 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942 shell->workspaces.current,
943 shell->workspaces.num);
944}
945
946static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947reverse_workspace_change_animation(struct desktop_shell *shell,
948 unsigned int index,
949 struct workspace *from,
950 struct workspace *to)
951{
952 shell->workspaces.current = index;
953
954 shell->workspaces.anim_to = to;
955 shell->workspaces.anim_from = from;
956 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
957 shell->workspaces.anim_timestamp = 0;
958
Scott Moreau4272e632012-08-13 09:58:41 -0600959 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200960}
961
962static void
963workspace_deactivate_transforms(struct workspace *ws)
964{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 if (is_focus_view(view)) {
970 struct focus_surface *fsurf = get_focus_surface(view->surface);
971 transform = &fsurf->workspace_transform;
972 } else {
973 struct shell_surface *shsurf = get_shell_surface(view->surface);
974 transform = &shsurf->workspace_transform;
975 }
976
977 if (!wl_list_empty(&transform->link)) {
978 wl_list_remove(&transform->link);
979 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200980 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982 }
983}
984
985static void
986finish_workspace_change_animation(struct desktop_shell *shell,
987 struct workspace *from,
988 struct workspace *to)
989{
Scott Moreau4272e632012-08-13 09:58:41 -0600990 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991
992 wl_list_remove(&shell->workspaces.animation.link);
993 workspace_deactivate_transforms(from);
994 workspace_deactivate_transforms(to);
995 shell->workspaces.anim_to = NULL;
996
997 wl_list_remove(&shell->workspaces.anim_from->layer.link);
998}
999
1000static void
1001animate_workspace_change_frame(struct weston_animation *animation,
1002 struct weston_output *output, uint32_t msecs)
1003{
1004 struct desktop_shell *shell =
1005 container_of(animation, struct desktop_shell,
1006 workspaces.animation);
1007 struct workspace *from = shell->workspaces.anim_from;
1008 struct workspace *to = shell->workspaces.anim_to;
1009 uint32_t t;
1010 double x, y;
1011
1012 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1013 finish_workspace_change_animation(shell, from, to);
1014 return;
1015 }
1016
1017 if (shell->workspaces.anim_timestamp == 0) {
1018 if (shell->workspaces.anim_current == 0.0)
1019 shell->workspaces.anim_timestamp = msecs;
1020 else
1021 shell->workspaces.anim_timestamp =
1022 msecs -
1023 /* Invers of movement function 'y' below. */
1024 (asin(1.0 - shell->workspaces.anim_current) *
1025 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1026 M_2_PI);
1027 }
1028
1029 t = msecs - shell->workspaces.anim_timestamp;
1030
1031 /*
1032 * x = [0, π/2]
1033 * y(x) = sin(x)
1034 */
1035 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1036 y = sin(x);
1037
1038 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001039 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
1041 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1042 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1043 shell->workspaces.anim_current = y;
1044
Scott Moreau4272e632012-08-13 09:58:41 -06001045 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001047 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001048 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001049}
1050
1051static void
1052animate_workspace_change(struct desktop_shell *shell,
1053 unsigned int index,
1054 struct workspace *from,
1055 struct workspace *to)
1056{
1057 struct weston_output *output;
1058
1059 int dir;
1060
1061 if (index > shell->workspaces.current)
1062 dir = -1;
1063 else
1064 dir = 1;
1065
1066 shell->workspaces.current = index;
1067
1068 shell->workspaces.anim_dir = dir;
1069 shell->workspaces.anim_from = from;
1070 shell->workspaces.anim_to = to;
1071 shell->workspaces.anim_current = 0.0;
1072 shell->workspaces.anim_timestamp = 0;
1073
1074 output = container_of(shell->compositor->output_list.next,
1075 struct weston_output, link);
1076 wl_list_insert(&output->animation_list,
1077 &shell->workspaces.animation.link);
1078
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001079 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
1081 workspace_translate_in(to, 0);
1082
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001083 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001084
Scott Moreau4272e632012-08-13 09:58:41 -06001085 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086}
1087
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001088static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001089update_workspace(struct desktop_shell *shell, unsigned int index,
1090 struct workspace *from, struct workspace *to)
1091{
1092 shell->workspaces.current = index;
1093 wl_list_insert(&from->layer.link, &to->layer.link);
1094 wl_list_remove(&from->layer.link);
1095}
1096
1097static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001098change_workspace(struct desktop_shell *shell, unsigned int index)
1099{
1100 struct workspace *from;
1101 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001102 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001103
1104 if (index == shell->workspaces.current)
1105 return;
1106
1107 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109 return;
1110
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001111 from = get_current_workspace(shell);
1112 to = get_workspace(shell, index);
1113
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 if (shell->workspaces.anim_from == to &&
1115 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001116 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001118 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 return;
1120 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001121
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 if (shell->workspaces.anim_to != NULL)
1123 finish_workspace_change_animation(shell,
1124 shell->workspaces.anim_from,
1125 shell->workspaces.anim_to);
1126
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001127 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001128
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001129 if (shell->focus_animation_type != ANIMATION_NONE) {
1130 wl_list_for_each(state, &from->focus_list, link)
1131 if (state->keyboard_focus)
1132 animate_focus_change(shell, from,
1133 get_default_view(state->keyboard_focus), NULL);
1134
1135 wl_list_for_each(state, &to->focus_list, link)
1136 if (state->keyboard_focus)
1137 animate_focus_change(shell, to,
1138 NULL, get_default_view(state->keyboard_focus));
1139 }
1140
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001141 if (workspace_is_empty(to) && workspace_is_empty(from))
1142 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 else
1144 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001145
1146 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001147}
1148
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001149static bool
1150workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1151{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001153 struct wl_list *e;
1154
1155 if (wl_list_empty(list))
1156 return false;
1157
1158 e = list->next;
1159
1160 if (e->next != list)
1161 return false;
1162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001164}
1165
1166static void
Philip Withnall659163d2013-11-25 18:01:36 +00001167move_surface_to_workspace(struct desktop_shell *shell,
1168 struct shell_surface *shsurf,
1169 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001170{
1171 struct workspace *from;
1172 struct workspace *to;
1173 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001174 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001175 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001176
1177 if (workspace == shell->workspaces.current)
1178 return;
1179
Philip Withnall659163d2013-11-25 18:01:36 +00001180 view = get_default_view(shsurf->surface);
1181 if (!view)
1182 return;
1183
1184 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1185
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001186 if (workspace >= shell->workspaces.num)
1187 workspace = shell->workspaces.num - 1;
1188
Jonas Ådahle9d22502012-08-29 22:13:01 +02001189 from = get_current_workspace(shell);
1190 to = get_workspace(shell, workspace);
1191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 wl_list_remove(&view->layer_link);
1193 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001194
Philip Withnall648a4dd2013-11-25 18:01:44 +00001195 shell_surface_update_child_surface_layers(shsurf);
1196
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001198 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1199 if (!seat->keyboard)
1200 continue;
1201
1202 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001204 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001205 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001208}
1209
1210static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001212 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001213 unsigned int index)
1214{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001215 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217 struct shell_surface *shsurf;
1218 struct workspace *from;
1219 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001220 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221
Pekka Paalanen01388e22013-04-25 13:57:44 +03001222 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view = get_default_view(surface);
1224 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001225 index == shell->workspaces.current ||
1226 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 return;
1228
1229 from = get_current_workspace(shell);
1230 to = get_workspace(shell, index);
1231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 wl_list_remove(&view->layer_link);
1233 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234
Philip Withnall659163d2013-11-25 18:01:36 +00001235 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001236 if (shsurf != NULL)
1237 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001238
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001240 drop_focus_state(shell, from, surface);
1241
1242 if (shell->workspaces.anim_from == to &&
1243 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001244 wl_list_remove(&to->layer.link);
1245 wl_list_insert(from->layer.link.prev, &to->layer.link);
1246
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248 broadcast_current_workspace_state(shell);
1249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250 return;
1251 }
1252
1253 if (shell->workspaces.anim_to != NULL)
1254 finish_workspace_change_animation(shell,
1255 shell->workspaces.anim_from,
1256 shell->workspaces.anim_to);
1257
1258 if (workspace_is_empty(from) &&
1259 workspace_has_only(to, surface))
1260 update_workspace(shell, index, from, to);
1261 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001262 if (shsurf != NULL &&
1263 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264 wl_list_insert(&shell->workspaces.anim_sticky_list,
1265 &shsurf->workspace_transform.link);
1266
1267 animate_workspace_change(shell, index, from, to);
1268 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269
1270 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001271
1272 state = ensure_focus_state(shell, seat);
1273 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001274 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275}
1276
1277static void
1278workspace_manager_move_surface(struct wl_client *client,
1279 struct wl_resource *resource,
1280 struct wl_resource *surface_resource,
1281 uint32_t workspace)
1282{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001283 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001285 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001286 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001287 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288
Pekka Paalanen01388e22013-04-25 13:57:44 +03001289 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001290 shell_surface = get_shell_surface(main_surface);
1291 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001293
1294 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295}
1296
1297static const struct workspace_manager_interface workspace_manager_implementation = {
1298 workspace_manager_move_surface,
1299};
1300
1301static void
1302unbind_resource(struct wl_resource *resource)
1303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001304 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305}
1306
1307static void
1308bind_workspace_manager(struct wl_client *client,
1309 void *data, uint32_t version, uint32_t id)
1310{
1311 struct desktop_shell *shell = data;
1312 struct wl_resource *resource;
1313
Jason Ekstranda85118c2013-06-27 20:17:02 -05001314 resource = wl_resource_create(client,
1315 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001316
1317 if (resource == NULL) {
1318 weston_log("couldn't add workspace manager object");
1319 return;
1320 }
1321
Jason Ekstranda85118c2013-06-27 20:17:02 -05001322 wl_resource_set_implementation(resource,
1323 &workspace_manager_implementation,
1324 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001325 wl_list_insert(&shell->workspaces.client_list,
1326 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
1328 workspace_manager_send_state(resource,
1329 shell->workspaces.current,
1330 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331}
1332
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001333static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001334touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1335 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1336{
1337}
1338
1339static void
1340touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1341{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001342 struct weston_touch_move_grab *move =
1343 (struct weston_touch_move_grab *) container_of(
1344 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001345
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001346 if (touch_id == 0)
1347 move->active = 0;
1348
Jonas Ådahl9484b692013-12-02 22:05:03 +01001349 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001350 shell_touch_grab_end(&move->base);
1351 free(move);
1352 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001353}
1354
1355static void
1356touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1357 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1358{
1359 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1360 struct shell_surface *shsurf = move->base.shsurf;
1361 struct weston_surface *es;
1362 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1363 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1364
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001365 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001366 return;
1367
1368 es = shsurf->surface;
1369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001370 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001371
1372 weston_compositor_schedule_repaint(es->compositor);
1373}
1374
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001375static void
1376touch_move_grab_cancel(struct weston_touch_grab *grab)
1377{
1378 struct weston_touch_move_grab *move =
1379 (struct weston_touch_move_grab *) container_of(
1380 grab, struct shell_touch_grab, grab);
1381
1382 shell_touch_grab_end(&move->base);
1383 free(move);
1384}
1385
Rusty Lynch1084da52013-08-15 09:10:08 -07001386static const struct weston_touch_grab_interface touch_move_grab_interface = {
1387 touch_move_grab_down,
1388 touch_move_grab_up,
1389 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001390 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001391};
1392
1393static int
1394surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1395{
1396 struct weston_touch_move_grab *move;
1397
1398 if (!shsurf)
1399 return -1;
1400
Rafael Antognolli03b16592013-12-03 15:35:42 -02001401 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001402 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001403 if (shsurf->grabbed)
1404 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001405
1406 move = malloc(sizeof *move);
1407 if (!move)
1408 return -1;
1409
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001410 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001412 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001414 seat->touch->grab_y;
1415
1416 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1417 seat->touch);
1418
1419 return 0;
1420}
1421
1422static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001423noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001424{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425}
1426
1427static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001428move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1429 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001431 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001432 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001433 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001434 int dx, dy;
1435
1436 weston_pointer_move(pointer, x, y);
1437 dx = wl_fixed_to_int(pointer->x + move->dx);
1438 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001439
1440 if (!shsurf)
1441 return;
1442
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001443 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001444
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001446}
1447
1448static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001449move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001450 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001452 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1453 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001454 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001455 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001456
Daniel Stone4dbadb12012-05-30 16:31:51 +01001457 if (pointer->button_count == 0 &&
1458 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001459 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001460 free(grab);
1461 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001462}
1463
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001464static void
1465move_grab_cancel(struct weston_pointer_grab *grab)
1466{
1467 struct shell_grab *shell_grab =
1468 container_of(grab, struct shell_grab, grab);
1469
1470 shell_grab_end(shell_grab);
1471 free(grab);
1472}
1473
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001474static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001475 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001476 move_grab_motion,
1477 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001478 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001479};
1480
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001481static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001482surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001483{
1484 struct weston_move_grab *move;
1485
1486 if (!shsurf)
1487 return -1;
1488
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001489 if (shsurf->grabbed)
1490 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001491 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001492 return 0;
1493
1494 move = malloc(sizeof *move);
1495 if (!move)
1496 return -1;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001499 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001501 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001502
1503 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001504 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001505
1506 return 0;
1507}
1508
1509static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001510common_surface_move(struct wl_resource *resource,
1511 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001513 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001514 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001515 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001516
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001517 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001518 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001519 seat->pointer->button_count > 0 &&
1520 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001522 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001523 (surface_move(shsurf, seat) < 0))
1524 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001525 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001526 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001527 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001529 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 (surface_touch_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
1532 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001533}
1534
Rafael Antognollie2a34552013-12-03 15:35:45 -02001535static void
1536shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1537 struct wl_resource *seat_resource, uint32_t serial)
1538{
1539 common_surface_move(resource, seat_resource, serial);
1540}
1541
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542struct weston_resize_grab {
1543 struct shell_grab base;
1544 uint32_t edges;
1545 int32_t width, height;
1546};
1547
1548static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001549resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1550 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551{
1552 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001553 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001554 struct shell_surface *shsurf = resize->base.shsurf;
1555 int32_t width, height;
1556 wl_fixed_t from_x, from_y;
1557 wl_fixed_t to_x, to_y;
1558
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001559 weston_pointer_move(pointer, x, y);
1560
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561 if (!shsurf)
1562 return;
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 weston_view_from_global_fixed(shsurf->view,
1565 pointer->grab_x, pointer->grab_y,
1566 &from_x, &from_y);
1567 weston_view_from_global_fixed(shsurf->view,
1568 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569
1570 width = resize->width;
1571 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1572 width += wl_fixed_to_int(from_x - to_x);
1573 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1574 width += wl_fixed_to_int(to_x - from_x);
1575 }
1576
1577 height = resize->height;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1579 height += wl_fixed_to_int(from_y - to_y);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1581 height += wl_fixed_to_int(to_y - from_y);
1582 }
1583
1584 shsurf->client->send_configure(shsurf->surface,
1585 resize->edges, width, height);
1586}
1587
1588static void
1589send_configure(struct weston_surface *surface,
1590 uint32_t edges, int32_t width, int32_t height)
1591{
1592 struct shell_surface *shsurf = get_shell_surface(surface);
1593
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001594 assert(shsurf);
1595
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001596 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 edges, width, height);
1598}
1599
1600static const struct weston_shell_client shell_client = {
1601 send_configure
1602};
1603
1604static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001605resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001606 uint32_t time, uint32_t button, uint32_t state_w)
1607{
1608 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001609 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 enum wl_pointer_button_state state = state_w;
1611
1612 if (pointer->button_count == 0 &&
1613 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1614 shell_grab_end(&resize->base);
1615 free(grab);
1616 }
1617}
1618
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001619static void
1620resize_grab_cancel(struct weston_pointer_grab *grab)
1621{
1622 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1623
1624 shell_grab_end(&resize->base);
1625 free(grab);
1626}
1627
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001628static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629 noop_grab_focus,
1630 resize_grab_motion,
1631 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001632 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633};
1634
Giulio Camuffob8366642013-04-25 13:57:46 +03001635/*
1636 * Returns the bounding box of a surface and all its sub-surfaces,
1637 * in the surface coordinates system. */
1638static void
1639surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1640 int32_t *y, int32_t *w, int32_t *h) {
1641 pixman_region32_t region;
1642 pixman_box32_t *box;
1643 struct weston_subsurface *subsurface;
1644
1645 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 surface->width,
1647 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001648
1649 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1650 pixman_region32_union_rect(&region, &region,
1651 subsurface->position.x,
1652 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 subsurface->surface->width,
1654 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001655 }
1656
1657 box = pixman_region32_extents(&region);
1658 if (x)
1659 *x = box->x1;
1660 if (y)
1661 *y = box->y1;
1662 if (w)
1663 *w = box->x2 - box->x1;
1664 if (h)
1665 *h = box->y2 - box->y1;
1666
1667 pixman_region32_fini(&region);
1668}
1669
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001670static int
1671surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001672 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673{
1674 struct weston_resize_grab *resize;
1675
Rafael Antognolli03b16592013-12-03 15:35:42 -02001676 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677 return 0;
1678
1679 if (edges == 0 || edges > 15 ||
1680 (edges & 3) == 3 || (edges & 12) == 12)
1681 return 0;
1682
1683 resize = malloc(sizeof *resize);
1684 if (!resize)
1685 return -1;
1686
1687 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001688 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1689 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001690
1691 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001692 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693
1694 return 0;
1695}
1696
1697static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001698common_surface_resize(struct wl_resource *resource,
1699 struct wl_resource *seat_resource, uint32_t serial,
1700 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001702 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001703 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001704 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705
Rafael Antognolli03b16592013-12-03 15:35:42 -02001706 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707 return;
1708
Kristian Høgsberge3148752013-05-06 23:19:49 -04001709 if (seat->pointer->button_count == 0 ||
1710 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001711 seat->pointer->focus == NULL)
1712 return;
1713
1714 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1715 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716 return;
1717
Kristian Høgsberge3148752013-05-06 23:19:49 -04001718 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 wl_resource_post_no_memory(resource);
1720}
1721
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001722static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001723shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1724 struct wl_resource *seat_resource, uint32_t serial,
1725 uint32_t edges)
1726{
1727 common_surface_resize(resource, seat_resource, serial, edges);
1728}
1729
1730static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001731end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1732
1733static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001734busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001735{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001736 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001737 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739 wl_fixed_t sx, sy;
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view = weston_compositor_pick_view(pointer->seat->compositor,
1742 pointer->x, pointer->y,
1743 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001744
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001746 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001747}
1748
1749static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001750busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1751 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001752{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001753 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001754}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001757busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001758 uint32_t time, uint32_t button, uint32_t state)
1759{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001760 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001761 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001762 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001763
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001764 if (shsurf && button == BTN_LEFT && state) {
1765 activate(shsurf->shell, shsurf->surface, seat);
1766 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001767 } else if (shsurf && button == BTN_RIGHT && state) {
1768 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771}
1772
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001773static void
1774busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1775{
1776 struct shell_grab *grab = (struct shell_grab *) base;
1777
1778 shell_grab_end(grab);
1779 free(grab);
1780}
1781
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001782static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783 busy_cursor_grab_focus,
1784 busy_cursor_grab_motion,
1785 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001786 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787};
1788
1789static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001790set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001791{
1792 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793
1794 grab = malloc(sizeof *grab);
1795 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001796 return;
1797
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001798 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1799 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001801
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001803end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804{
1805 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1806
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001807 if (grab->grab.interface == &busy_cursor_grab_interface &&
1808 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001809 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001811 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001812}
1813
Scott Moreau9521d5e2012-04-19 13:06:17 -06001814static void
1815ping_timer_destroy(struct shell_surface *shsurf)
1816{
1817 if (!shsurf || !shsurf->ping_timer)
1818 return;
1819
1820 if (shsurf->ping_timer->source)
1821 wl_event_source_remove(shsurf->ping_timer->source);
1822
1823 free(shsurf->ping_timer);
1824 shsurf->ping_timer = NULL;
1825}
1826
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001827static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001828ping_timeout_handler(void *data)
1829{
1830 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001832
Scott Moreau9521d5e2012-04-19 13:06:17 -06001833 /* Client is not responding */
1834 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835
1836 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001837 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001838 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001839 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001840
1841 return 1;
1842}
1843
1844static void
1845ping_handler(struct weston_surface *surface, uint32_t serial)
1846{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001847 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001848 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001850
1851 if (!shsurf)
1852 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001853 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001854 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001855
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001856 if (shsurf->surface == shsurf->shell->grab_surface)
1857 return;
1858
Scott Moreauff1db4a2012-04-17 19:06:18 -06001859 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001860 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001861 if (!shsurf->ping_timer)
1862 return;
1863
1864 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001865 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1866 shsurf->ping_timer->source =
1867 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1868 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1869
Rafael Antognollie2a34552013-12-03 15:35:45 -02001870 if (shell_surface_is_wl_shell_surface(shsurf))
1871 wl_shell_surface_send_ping(shsurf->resource, serial);
1872 else if (shell_surface_is_xdg_surface(shsurf))
1873 xdg_surface_send_ping(shsurf->resource, serial);
1874 else if (shell_surface_is_xdg_popup(shsurf))
1875 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001876 }
1877}
1878
1879static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880handle_pointer_focus(struct wl_listener *listener, void *data)
1881{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001882 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884 struct weston_compositor *compositor;
1885 struct shell_surface *shsurf;
1886 uint32_t serial;
1887
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001889 return;
1890
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 compositor = view->surface->compositor;
1892 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001893
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001894 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001895 set_busy_cursor(shsurf, pointer);
1896 } else {
1897 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001899 }
1900}
1901
1902static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001903create_pointer_focus_listener(struct weston_seat *seat)
1904{
1905 struct wl_listener *listener;
1906
Kristian Høgsberge3148752013-05-06 23:19:49 -04001907 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001908 return;
1909
1910 listener = malloc(sizeof *listener);
1911 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001912 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001913}
1914
1915static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001916xdg_surface_set_transient_for(struct wl_client *client,
1917 struct wl_resource *resource,
1918 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001919{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001920 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001921 struct weston_surface *parent;
1922
1923 if (parent_resource)
1924 parent = wl_resource_get_user_data(parent_resource);
1925 else
1926 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001927
1928 shell_surface_set_parent(shsurf, parent);
1929}
1930
1931static void
1932surface_pong(struct shell_surface *shsurf, uint32_t serial)
1933{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001934 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001935 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001936
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001937 if (shsurf->ping_timer == NULL)
1938 /* Just ignore unsolicited pong. */
1939 return;
1940
Scott Moreauff1db4a2012-04-17 19:06:18 -06001941 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001942 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001943 wl_list_for_each(seat, &ec->seat_list, link) {
1944 if(seat->pointer)
1945 end_busy_cursor(shsurf, seat->pointer);
1946 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001947 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 }
1949}
1950
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001951static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001952shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1953 uint32_t serial)
1954{
1955 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1956
1957 surface_pong(shsurf, serial);
1958
1959}
1960
1961static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001962set_title(struct shell_surface *shsurf, const char *title)
1963{
1964 free(shsurf->title);
1965 shsurf->title = strdup(title);
1966}
1967
1968static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001969shell_surface_set_title(struct wl_client *client,
1970 struct wl_resource *resource, const char *title)
1971{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001972 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001973
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001974 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001975}
1976
1977static void
1978shell_surface_set_class(struct wl_client *client,
1979 struct wl_resource *resource, const char *class)
1980{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001981 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001982
1983 free(shsurf->class);
1984 shsurf->class = strdup(class);
1985}
1986
Alex Wu4539b082012-03-01 12:57:46 +08001987static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001988restore_output_mode(struct weston_output *output)
1989{
Hardening57388e42013-09-18 23:56:36 +02001990 if (output->current_mode != output->original_mode ||
1991 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001992 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001993 output->original_mode,
1994 output->original_scale,
1995 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001996}
1997
1998static void
1999restore_all_output_modes(struct weston_compositor *compositor)
2000{
2001 struct weston_output *output;
2002
2003 wl_list_for_each(output, &compositor->output_list, link)
2004 restore_output_mode(output);
2005}
2006
Philip Withnallbecb77e2013-11-25 18:01:30 +00002007static int
2008get_output_panel_height(struct desktop_shell *shell,
2009 struct weston_output *output)
2010{
2011 struct weston_view *view;
2012 int panel_height = 0;
2013
2014 if (!output)
2015 return 0;
2016
2017 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2018 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002019 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002020 break;
2021 }
2022 }
2023
2024 return panel_height;
2025}
2026
Philip Withnall07926d92013-11-25 18:01:40 +00002027/* The surface will be inserted into the list immediately after the link
2028 * returned by this function (i.e. will be stacked immediately above the
2029 * returned link). */
2030static struct wl_list *
2031shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2032{
2033 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002034 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002035
2036 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002037 case SHELL_SURFACE_POPUP:
2038 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002039 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002040 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002041 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002042 /* Move the surface to its parent layer so
2043 * that surfaces which are transient for
2044 * fullscreen surfaces don't get hidden by the
2045 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002046
2047 /* TODO: Handle a parent with multiple views */
2048 parent = get_default_view(shsurf->parent);
2049 if (parent)
2050 return parent->layer_link.prev;
2051 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002052 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002053
2054 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002055 return &shsurf->shell->fullscreen_layer.view_list;
2056
Philip Withnall07926d92013-11-25 18:01:40 +00002057 case SHELL_SURFACE_NONE:
2058 default:
2059 /* Go to the fallback, below. */
2060 break;
2061 }
2062
2063 /* Move the surface to a normal workspace layer so that surfaces
2064 * which were previously fullscreen or transient are no longer
2065 * rendered on top. */
2066 ws = get_current_workspace(shsurf->shell);
2067 return &ws->layer.view_list;
2068}
2069
Philip Withnall648a4dd2013-11-25 18:01:44 +00002070static void
2071shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2072{
2073 struct shell_surface *child;
2074
2075 /* Move the child layers to the same workspace as shsurf. They will be
2076 * stacked above shsurf. */
2077 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2078 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2079 weston_view_geometry_dirty(child->view);
2080 wl_list_remove(&child->view->layer_link);
2081 wl_list_insert(shsurf->view->layer_link.prev,
2082 &child->view->layer_link);
2083 weston_view_geometry_dirty(child->view);
2084 weston_surface_damage(child->surface);
2085
2086 /* Recurse. We don’t expect this to recurse very far (if
2087 * at all) because that would imply we have transient
2088 * (or popup) children of transient surfaces, which
2089 * would be unusual. */
2090 shell_surface_update_child_surface_layers(child);
2091 }
2092 }
2093}
2094
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002095/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002096 * geometry to ensure the changes are redrawn.
2097 *
2098 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2099 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002100static void
2101shell_surface_update_layer(struct shell_surface *shsurf)
2102{
2103 struct wl_list *new_layer_link;
2104
2105 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2106
2107 if (new_layer_link == &shsurf->view->layer_link)
2108 return;
2109
2110 weston_view_geometry_dirty(shsurf->view);
2111 wl_list_remove(&shsurf->view->layer_link);
2112 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2113 weston_view_geometry_dirty(shsurf->view);
2114 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002115
2116 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002117}
2118
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002119static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002120shell_surface_set_parent(struct shell_surface *shsurf,
2121 struct weston_surface *parent)
2122{
2123 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002124
2125 wl_list_remove(&shsurf->children_link);
2126 wl_list_init(&shsurf->children_link);
2127
2128 /* Insert into the parent surface’s child list. */
2129 if (parent != NULL) {
2130 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2131 if (parent_shsurf != NULL)
2132 wl_list_insert(&parent_shsurf->children_list,
2133 &shsurf->children_link);
2134 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002135}
2136
2137static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002138shell_surface_set_output(struct shell_surface *shsurf,
2139 struct weston_output *output)
2140{
2141 struct weston_surface *es = shsurf->surface;
2142
2143 /* get the default output, if the client set it as NULL
2144 check whether the ouput is available */
2145 if (output)
2146 shsurf->output = output;
2147 else if (es->output)
2148 shsurf->output = es->output;
2149 else
2150 shsurf->output = get_default_output(es->compositor);
2151}
2152
2153static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002154surface_clear_next_states(struct shell_surface *shsurf)
2155{
2156 shsurf->next_state.maximized = false;
2157 shsurf->next_state.fullscreen = false;
2158
2159 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2160 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2161 shsurf->state_changed = true;
2162}
2163
2164static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002165set_toplevel(struct shell_surface *shsurf)
2166{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002167 shell_surface_set_parent(shsurf, NULL);
2168 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002169 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002170
2171 /* The layer_link is updated in set_surface_type(),
2172 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002173}
2174
2175static void
2176shell_surface_set_toplevel(struct wl_client *client,
2177 struct wl_resource *resource)
2178{
2179 struct shell_surface *surface = wl_resource_get_user_data(resource);
2180
2181 set_toplevel(surface);
2182}
2183
2184static void
2185set_transient(struct shell_surface *shsurf,
2186 struct weston_surface *parent, int x, int y, uint32_t flags)
2187{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002188 assert(parent != NULL);
2189
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002190 shell_surface_set_parent(shsurf, parent);
2191
2192 surface_clear_next_states(shsurf);
2193
Philip Withnallbecb77e2013-11-25 18:01:30 +00002194 shsurf->transient.x = x;
2195 shsurf->transient.y = y;
2196 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002197
Rafael Antognollied207b42013-12-03 15:35:43 -02002198 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002199 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002200 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201
2202 /* The layer_link is updated in set_surface_type(),
2203 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002204}
2205
2206static void
2207shell_surface_set_transient(struct wl_client *client,
2208 struct wl_resource *resource,
2209 struct wl_resource *parent_resource,
2210 int x, int y, uint32_t flags)
2211{
2212 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2213 struct weston_surface *parent =
2214 wl_resource_get_user_data(parent_resource);
2215
2216 set_transient(shsurf, parent, x, y, flags);
2217}
2218
2219static void
2220set_fullscreen(struct shell_surface *shsurf,
2221 uint32_t method,
2222 uint32_t framerate,
2223 struct weston_output *output)
2224{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002225 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002226
2227 shsurf->fullscreen_output = shsurf->output;
2228 shsurf->fullscreen.type = method;
2229 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002230
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002231 shsurf->next_state.fullscreen = true;
2232 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002233 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002234
2235 shsurf->client->send_configure(shsurf->surface, 0,
2236 shsurf->output->width,
2237 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002238
2239 /* The layer_link is updated in set_surface_type(),
2240 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002241}
2242
2243static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002244weston_view_set_initial_position(struct weston_view *view,
2245 struct desktop_shell *shell);
2246
2247static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002249{
Philip Withnallf85fe842013-11-25 18:01:37 +00002250 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002251 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2252 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002253 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002254 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002255 shsurf->fullscreen_output = NULL;
2256
Alex Wu4539b082012-03-01 12:57:46 +08002257 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2258 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002259
Alex Wu4539b082012-03-01 12:57:46 +08002260 wl_list_remove(&shsurf->fullscreen.transform.link);
2261 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002262
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 if (shsurf->fullscreen.black_view)
2264 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2265 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002266
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002267 if (shsurf->saved_position_valid)
2268 weston_view_set_position(shsurf->view,
2269 shsurf->saved_x, shsurf->saved_y);
2270 else
2271 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2272
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002273 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002275 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002276 shsurf->saved_rotation_valid = false;
2277 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002278
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002279 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002280}
2281
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002282static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002283shell_surface_set_fullscreen(struct wl_client *client,
2284 struct wl_resource *resource,
2285 uint32_t method,
2286 uint32_t framerate,
2287 struct wl_resource *output_resource)
2288{
2289 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2290 struct weston_output *output;
2291
2292 if (output_resource)
2293 output = wl_resource_get_user_data(output_resource);
2294 else
2295 output = NULL;
2296
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002297 shell_surface_set_parent(shsurf, NULL);
2298
Rafael Antognolli03b16592013-12-03 15:35:42 -02002299 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300 set_fullscreen(shsurf, method, framerate, output);
2301}
2302
2303static void
2304set_popup(struct shell_surface *shsurf,
2305 struct weston_surface *parent,
2306 struct weston_seat *seat,
2307 uint32_t serial,
2308 int32_t x,
2309 int32_t y)
2310{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002311 assert(parent != NULL);
2312
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313 shsurf->popup.shseat = get_shell_seat(seat);
2314 shsurf->popup.serial = serial;
2315 shsurf->popup.x = x;
2316 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002317
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002318 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002319}
2320
2321static void
2322shell_surface_set_popup(struct wl_client *client,
2323 struct wl_resource *resource,
2324 struct wl_resource *seat_resource,
2325 uint32_t serial,
2326 struct wl_resource *parent_resource,
2327 int32_t x, int32_t y, uint32_t flags)
2328{
2329 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002330 struct weston_surface *parent =
2331 wl_resource_get_user_data(parent_resource);
2332
2333 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002334
Rafael Antognolli03b16592013-12-03 15:35:42 -02002335 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002337 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002338 wl_resource_get_user_data(seat_resource),
2339 serial, x, y);
2340}
2341
2342static void
2343set_maximized(struct shell_surface *shsurf,
2344 struct weston_output *output)
2345{
2346 struct desktop_shell *shell;
2347 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002348
Philip Withnall352e7ed2013-11-25 18:01:35 +00002349 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002350
2351 shell = shell_surface_get_shell(shsurf);
2352 panel_height = get_output_panel_height(shell, shsurf->output);
2353 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2354
2355 shsurf->client->send_configure(shsurf->surface, edges,
2356 shsurf->output->width,
2357 shsurf->output->height - panel_height);
2358
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002359 shsurf->next_state.maximized = true;
2360 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002361 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002362}
2363
2364static void
2365unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002366{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002367 /* undo all maximized things here */
2368 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002369
2370 if (shsurf->saved_position_valid)
2371 weston_view_set_position(shsurf->view,
2372 shsurf->saved_x, shsurf->saved_y);
2373 else
2374 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002375
2376 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 wl_list_insert(&shsurf->view->geometry.transformation_list,
2378 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002379 shsurf->saved_rotation_valid = false;
2380 }
2381
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002382 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002383}
2384
Philip Withnallbecb77e2013-11-25 18:01:30 +00002385static void
2386shell_surface_set_maximized(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *output_resource)
2389{
2390 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2391 struct weston_output *output;
2392
2393 if (output_resource)
2394 output = wl_resource_get_user_data(output_resource);
2395 else
2396 output = NULL;
2397
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002398 shell_surface_set_parent(shsurf, NULL);
2399
Rafael Antognolli03b16592013-12-03 15:35:42 -02002400 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401 set_maximized(shsurf, output);
2402}
2403
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404/* This is only ever called from set_surface_type(), so there’s no need to
2405 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002406static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002407reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002408{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002409 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002410 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002411 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002413
Pekka Paalanen98262232011-12-01 10:42:22 +02002414 return 0;
2415}
2416
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002417static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418set_full_output(struct shell_surface *shsurf)
2419{
2420 shsurf->saved_x = shsurf->view->geometry.x;
2421 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002422 shsurf->saved_width = shsurf->surface->width;
2423 shsurf->saved_height = shsurf->surface->height;
2424 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002425 shsurf->saved_position_valid = true;
2426
2427 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2428 wl_list_remove(&shsurf->rotation.transform.link);
2429 wl_list_init(&shsurf->rotation.transform.link);
2430 weston_view_geometry_dirty(shsurf->view);
2431 shsurf->saved_rotation_valid = true;
2432 }
2433}
2434
2435static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002436set_surface_type(struct shell_surface *shsurf)
2437{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002438 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002440
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002442
Rafael Antognolli03b16592013-12-03 15:35:42 -02002443 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002444 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002445
2446 switch (shsurf->type) {
2447 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002448 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002449 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002450 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002451 weston_view_set_position(shsurf->view,
2452 pev->geometry.x + shsurf->transient.x,
2453 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002454 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002455 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002456
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002457 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 weston_view_set_position(shsurf->view, shsurf->transient.x,
2459 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002460 break;
2461
Philip Withnall0f640e22013-11-25 18:01:31 +00002462 case SHELL_SURFACE_POPUP:
2463 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002464 default:
2465 break;
2466 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002467
2468 /* Update the surface’s layer. */
2469 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002470}
2471
Tiago Vignattibe143262012-04-16 17:31:41 +03002472static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002473shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002474{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002475 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002476}
2477
Alex Wu21858432012-04-01 20:13:08 +08002478static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002479black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002480
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002482create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002483 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002484 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002485{
2486 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002488
2489 surface = weston_surface_create(ec);
2490 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002491 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002492 return NULL;
2493 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494 view = weston_view_create(surface);
2495 if (surface == NULL) {
2496 weston_log("no memory\n");
2497 weston_surface_destroy(surface);
2498 return NULL;
2499 }
Alex Wu4539b082012-03-01 12:57:46 +08002500
Alex Wu21858432012-04-01 20:13:08 +08002501 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002502 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002503 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002504 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002505 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002506 pixman_region32_fini(&surface->input);
2507 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002508
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002509 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002510 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511
2512 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002513}
2514
Philip Withnalled8f1a92013-11-25 18:01:43 +00002515static void
2516shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2517{
2518 struct weston_output *output = shsurf->fullscreen_output;
2519
Rafael Antognolli03b16592013-12-03 15:35:42 -02002520 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002521
2522 if (!shsurf->fullscreen.black_view)
2523 shsurf->fullscreen.black_view =
2524 create_black_surface(shsurf->surface->compositor,
2525 shsurf->surface,
2526 output->x, output->y,
2527 output->width,
2528 output->height);
2529
2530 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2531 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2532 wl_list_insert(&shsurf->view->layer_link,
2533 &shsurf->fullscreen.black_view->layer_link);
2534 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2535 weston_surface_damage(shsurf->surface);
2536}
2537
Alex Wu4539b082012-03-01 12:57:46 +08002538/* Create black surface and append it to the associated fullscreen surface.
2539 * Handle size dismatch and positioning according to the method. */
2540static void
2541shell_configure_fullscreen(struct shell_surface *shsurf)
2542{
2543 struct weston_output *output = shsurf->fullscreen_output;
2544 struct weston_surface *surface = shsurf->surface;
2545 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002547 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002548
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002549 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2550 restore_output_mode(output);
2551
Philip Withnalled8f1a92013-11-25 18:01:43 +00002552 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002553
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002555 &surf_width, &surf_height);
2556
Alex Wu4539b082012-03-01 12:57:46 +08002557 switch (shsurf->fullscreen.type) {
2558 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002559 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002561 break;
2562 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002563 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002564 if (output->width == surf_width &&
2565 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 weston_view_set_position(shsurf->view,
2567 output->x - surf_x,
2568 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002569 break;
2570 }
2571
Alex Wu4539b082012-03-01 12:57:46 +08002572 matrix = &shsurf->fullscreen.transform.matrix;
2573 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002574
Scott Moreau1bad5db2012-08-18 01:04:05 -06002575 output_aspect = (float) output->width /
2576 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 /* XXX: Use surf_width and surf_height here? */
2578 surface_aspect = (float) surface->width /
2579 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002580 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002581 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002582 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002583 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002584 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002585 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002586
Alex Wu4539b082012-03-01 12:57:46 +08002587 weston_matrix_scale(matrix, scale, scale, 1);
2588 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002590 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002591 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2592 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002594
Alex Wu4539b082012-03-01 12:57:46 +08002595 break;
2596 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002597 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002598 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002599 surf_width * surface->buffer_viewport.scale,
2600 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002601 shsurf->fullscreen.framerate};
2602
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002603 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002604 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 weston_view_set_position(shsurf->view,
2606 output->x - surf_x,
2607 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002608 shsurf->fullscreen.black_view->surface->width = output->width;
2609 shsurf->fullscreen.black_view->surface->height = output->height;
2610 weston_view_set_position(shsurf->fullscreen.black_view,
2611 output->x - surf_x,
2612 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002613 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002614 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002615 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002617 }
Alex Wubd3354b2012-04-17 17:20:49 +08002618 }
Alex Wu4539b082012-03-01 12:57:46 +08002619 break;
2620 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002622 break;
2623 default:
2624 break;
2625 }
2626}
2627
Alex Wu4539b082012-03-01 12:57:46 +08002628static void
2629shell_map_fullscreen(struct shell_surface *shsurf)
2630{
Alex Wubd3354b2012-04-17 17:20:49 +08002631 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002632}
2633
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002634static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002635set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2636{
2637 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002638 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002639 shsurf->transient.x = x;
2640 shsurf->transient.y = y;
2641 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002642
2643 shell_surface_set_parent(shsurf, NULL);
2644
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002645 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002646 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002647}
2648
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002649static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002650
2651static void
2652destroy_shell_seat(struct wl_listener *listener, void *data)
2653{
2654 struct shell_seat *shseat =
2655 container_of(listener,
2656 struct shell_seat, seat_destroy_listener);
2657 struct shell_surface *shsurf, *prev = NULL;
2658
2659 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002660 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002661 shseat->popup_grab.client = NULL;
2662
2663 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2664 shsurf->popup.shseat = NULL;
2665 if (prev) {
2666 wl_list_init(&prev->popup.grab_link);
2667 }
2668 prev = shsurf;
2669 }
2670 wl_list_init(&prev->popup.grab_link);
2671 }
2672
2673 wl_list_remove(&shseat->seat_destroy_listener.link);
2674 free(shseat);
2675}
2676
2677static struct shell_seat *
2678create_shell_seat(struct weston_seat *seat)
2679{
2680 struct shell_seat *shseat;
2681
2682 shseat = calloc(1, sizeof *shseat);
2683 if (!shseat) {
2684 weston_log("no memory to allocate shell seat\n");
2685 return NULL;
2686 }
2687
2688 shseat->seat = seat;
2689 wl_list_init(&shseat->popup_grab.surfaces_list);
2690
2691 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2692 wl_signal_add(&seat->destroy_signal,
2693 &shseat->seat_destroy_listener);
2694
2695 return shseat;
2696}
2697
2698static struct shell_seat *
2699get_shell_seat(struct weston_seat *seat)
2700{
2701 struct wl_listener *listener;
2702
2703 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2704 if (listener == NULL)
2705 return create_shell_seat(seat);
2706
2707 return container_of(listener,
2708 struct shell_seat, seat_destroy_listener);
2709}
2710
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002711static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002712popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002714 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002716 struct shell_seat *shseat =
2717 container_of(grab, struct shell_seat, popup_grab.grab);
2718 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002719 wl_fixed_t sx, sy;
2720
Jason Ekstranda7af7042013-10-12 22:38:11 -05002721 view = weston_compositor_pick_view(pointer->seat->compositor,
2722 pointer->x, pointer->y,
2723 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002724
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 if (view && view->surface->resource &&
2726 wl_resource_get_client(view->surface->resource) == client) {
2727 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002729 weston_pointer_set_focus(pointer, NULL,
2730 wl_fixed_from_int(0),
2731 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002732 }
2733}
2734
2735static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002736popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2737 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002739 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002740 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002741 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002742
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002743 weston_pointer_move(pointer, x, y);
2744
Neil Roberts96d790e2013-09-19 17:32:00 +01002745 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 weston_view_from_global_fixed(pointer->focus,
2747 pointer->x, pointer->y,
2748 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002749 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002750 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002751}
2752
2753static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002754popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002755 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002756{
2757 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002758 struct shell_seat *shseat =
2759 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002760 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002762 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002763 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002764
Neil Roberts96d790e2013-09-19 17:32:00 +01002765 resource_list = &grab->pointer->focus_resource_list;
2766 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002767 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002768 wl_resource_for_each(resource, resource_list) {
2769 wl_pointer_send_button(resource, serial,
2770 time, button, state);
2771 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002772 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002774 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002775 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002776 }
2777
Daniel Stone4dbadb12012-05-30 16:31:51 +01002778 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002779 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002780}
2781
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002782static void
2783popup_grab_cancel(struct weston_pointer_grab *grab)
2784{
2785 popup_grab_end(grab->pointer);
2786}
2787
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002788static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789 popup_grab_focus,
2790 popup_grab_motion,
2791 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002792 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002793};
2794
2795static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002796shell_surface_send_popup_done(struct shell_surface *shsurf)
2797{
2798 if (shell_surface_is_wl_shell_surface(shsurf))
2799 wl_shell_surface_send_popup_done(shsurf->resource);
2800 else if (shell_surface_is_xdg_popup(shsurf))
2801 xdg_popup_send_popup_done(shsurf->resource,
2802 shsurf->popup.serial);
2803}
2804
2805static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002806popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002807{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002808 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 struct shell_seat *shseat =
2810 container_of(grab, struct shell_seat, popup_grab.grab);
2811 struct shell_surface *shsurf;
2812 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002813
2814 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002815 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002816 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002817 shseat->popup_grab.grab.interface = NULL;
2818 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002819 /* Send the popup_done event to all the popups open */
2820 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002821 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002822 shsurf->popup.shseat = NULL;
2823 if (prev) {
2824 wl_list_init(&prev->popup.grab_link);
2825 }
2826 prev = shsurf;
2827 }
2828 wl_list_init(&prev->popup.grab_link);
2829 wl_list_init(&shseat->popup_grab.surfaces_list);
2830 }
2831}
2832
2833static void
2834add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2835{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002836 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002837
2838 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002839 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002840 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002841 /* We must make sure here that this popup was opened after
2842 * a mouse press, and not just by moving around with other
2843 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002844 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002845 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002846
Rob Bradforddfe31052013-06-26 19:49:11 +01002847 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002849 } else {
2850 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002851 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002852}
2853
2854static void
2855remove_popup_grab(struct shell_surface *shsurf)
2856{
2857 struct shell_seat *shseat = shsurf->popup.shseat;
2858
2859 wl_list_remove(&shsurf->popup.grab_link);
2860 wl_list_init(&shsurf->popup.grab_link);
2861 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002863 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002864 }
2865}
2866
2867static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002868shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002869{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002870 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872
Jason Ekstranda7af7042013-10-12 22:38:11 -05002873 shsurf->surface->output = parent_view->output;
2874 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002875
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 weston_view_set_transform_parent(shsurf->view, parent_view);
2877 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2878 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879
Kristian Høgsberge3148752013-05-06 23:19:49 -04002880 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002881 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002882 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002883 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002885 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002886}
2887
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002888static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002889 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002890 shell_surface_move,
2891 shell_surface_resize,
2892 shell_surface_set_toplevel,
2893 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002894 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002895 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002896 shell_surface_set_maximized,
2897 shell_surface_set_title,
2898 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002899};
2900
2901static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002902destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002903{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002904 struct shell_surface *child, *next;
2905
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002906 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2907
Giulio Camuffo5085a752013-03-25 21:42:45 +01002908 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2909 remove_popup_grab(shsurf);
2910 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911
Alex Wubd3354b2012-04-17 17:20:49 +08002912 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002913 shell_surface_is_top_fullscreen(shsurf))
2914 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002915
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 if (shsurf->fullscreen.black_view)
2917 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002918
Alex Wubd3354b2012-04-17 17:20:49 +08002919 /* As destroy_resource() use wl_list_for_each_safe(),
2920 * we can always remove the listener.
2921 */
2922 wl_list_remove(&shsurf->surface_destroy_listener.link);
2923 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002924 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002925 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002926
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 weston_view_destroy(shsurf->view);
2928
Philip Withnall648a4dd2013-11-25 18:01:44 +00002929 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01002930 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
2931 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002932
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002933 wl_list_remove(&shsurf->link);
2934 free(shsurf);
2935}
2936
2937static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002938shell_destroy_shell_surface(struct wl_resource *resource)
2939{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002940 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002941
2942 destroy_shell_surface(shsurf);
2943}
2944
2945static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002946shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002947{
2948 struct shell_surface *shsurf = container_of(listener,
2949 struct shell_surface,
2950 surface_destroy_listener);
2951
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002952 if (shsurf->resource)
2953 wl_resource_destroy(shsurf->resource);
2954 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002955 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002956}
2957
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002958static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002959shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002960static void
2961shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002962
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002963struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002964get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002965{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002966 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002967 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002968 else
2969 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002970}
2971
Rafael Antognollie2a34552013-12-03 15:35:45 -02002972static struct shell_surface *
2973create_common_surface(void *shell, struct weston_surface *surface,
2974 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002976 struct shell_surface *shsurf;
2977
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002978 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002979 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002980 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002981 }
2982
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002983 shsurf = calloc(1, sizeof *shsurf);
2984 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002985 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002986 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002987 }
2988
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 shsurf->view = weston_view_create(surface);
2990 if (!shsurf->view) {
2991 weston_log("no memory to allocate shell surface\n");
2992 free(shsurf);
2993 return NULL;
2994 }
2995
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002996 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002997 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002998 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002999
Tiago Vignattibc052c92012-04-19 16:18:18 +03003000 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003001 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003002 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003003 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003004 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003005 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003006 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3007 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003009 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003010 wl_list_init(&shsurf->fullscreen.transform.link);
3011
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003012 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003013 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003014 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003015 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003016
3017 /* init link so its safe to always remove it in destroy_shell_surface */
3018 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003019 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003020
Pekka Paalanen460099f2012-01-20 16:48:25 +02003021 /* empty when not in use */
3022 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003023 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003024
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003025 wl_list_init(&shsurf->workspace_transform.link);
3026
Philip Withnall648a4dd2013-11-25 18:01:44 +00003027 wl_list_init(&shsurf->children_link);
3028 wl_list_init(&shsurf->children_list);
3029 shsurf->parent = NULL;
3030
Pekka Paalanen98262232011-12-01 10:42:22 +02003031 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003032
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003033 shsurf->client = client;
3034
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003035 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003036}
3037
Rafael Antognollie2a34552013-12-03 15:35:45 -02003038static struct shell_surface *
3039create_shell_surface(void *shell, struct weston_surface *surface,
3040 const struct weston_shell_client *client)
3041{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003042 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003043}
3044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045static struct weston_view *
3046get_primary_view(void *shell, struct shell_surface *shsurf)
3047{
3048 return shsurf->view;
3049}
3050
Tiago Vignattibc052c92012-04-19 16:18:18 +03003051static void
3052shell_get_shell_surface(struct wl_client *client,
3053 struct wl_resource *resource,
3054 uint32_t id,
3055 struct wl_resource *surface_resource)
3056{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003057 struct weston_surface *surface =
3058 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003059 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003060 struct shell_surface *shsurf;
3061
3062 if (get_shell_surface(surface)) {
3063 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003064 WL_DISPLAY_ERROR_INVALID_OBJECT,
3065 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003066 return;
3067 }
3068
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003069 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003070 if (!shsurf) {
3071 wl_resource_post_error(surface_resource,
3072 WL_DISPLAY_ERROR_INVALID_OBJECT,
3073 "surface->configure already set");
3074 return;
3075 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003076
Jason Ekstranda85118c2013-06-27 20:17:02 -05003077 shsurf->resource =
3078 wl_resource_create(client,
3079 &wl_shell_surface_interface, 1, id);
3080 wl_resource_set_implementation(shsurf->resource,
3081 &shell_surface_implementation,
3082 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003083}
3084
Rafael Antognollie2a34552013-12-03 15:35:45 -02003085static bool
3086shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3087{
3088 return wl_resource_instance_of(shsurf->resource,
3089 &wl_shell_surface_interface,
3090 &shell_surface_implementation);
3091}
3092
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003093static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003094 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003095};
3096
Rafael Antognollie2a34552013-12-03 15:35:45 -02003097/****************************
3098 * xdg-shell implementation */
3099
3100static void
3101xdg_surface_destroy(struct wl_client *client,
3102 struct wl_resource *resource)
3103{
3104 wl_resource_destroy(resource);
3105}
3106
3107static void
3108xdg_surface_pong(struct wl_client *client,
3109 struct wl_resource *resource,
3110 uint32_t serial)
3111{
3112 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3113
3114 surface_pong(shsurf, serial);
3115}
3116
3117static void
3118xdg_surface_set_app_id(struct wl_client *client,
3119 struct wl_resource *resource,
3120 const char *app_id)
3121{
3122 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3123
3124 free(shsurf->class);
3125 shsurf->class = strdup(app_id);
3126}
3127
3128static void
3129xdg_surface_set_title(struct wl_client *client,
3130 struct wl_resource *resource, const char *title)
3131{
3132 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3133
3134 set_title(shsurf, title);
3135}
3136
3137static void
3138xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3139 struct wl_resource *seat_resource, uint32_t serial)
3140{
3141 common_surface_move(resource, seat_resource, serial);
3142}
3143
3144static void
3145xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3146 struct wl_resource *seat_resource, uint32_t serial,
3147 uint32_t edges)
3148{
3149 common_surface_resize(resource, seat_resource, serial, edges);
3150}
3151
3152static void
3153xdg_surface_set_output(struct wl_client *client,
3154 struct wl_resource *resource,
3155 struct wl_resource *output_resource)
3156{
3157 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3158 struct weston_output *output;
3159
3160 if (output_resource)
3161 output = wl_resource_get_user_data(output_resource);
3162 else
3163 output = NULL;
3164
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003165 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003166}
3167
3168static void
3169xdg_surface_set_fullscreen(struct wl_client *client,
3170 struct wl_resource *resource)
3171{
3172 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3173
3174 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3175 return;
3176
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003177 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003178 set_fullscreen(shsurf,
3179 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003180 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003181}
3182
3183static void
3184xdg_surface_unset_fullscreen(struct wl_client *client,
3185 struct wl_resource *resource)
3186{
3187 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003188 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003189
3190 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3191 return;
3192
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003193 if (!shsurf->next_state.fullscreen)
3194 return;
3195
Rafael Antognollie2a34552013-12-03 15:35:45 -02003196 shsurf->next_state.fullscreen = false;
3197 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003198
3199 if (shsurf->saved_size_valid) {
3200 width = shsurf->saved_width;
3201 height = shsurf->saved_height;
3202 shsurf->saved_size_valid = false;
3203 } else {
3204 width = shsurf->surface->width;
3205 height = shsurf->surface->height;
3206 }
3207
3208 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003209}
3210
3211static void
3212xdg_surface_set_maximized(struct wl_client *client,
3213 struct wl_resource *resource)
3214{
3215 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3216
3217 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3218 return;
3219
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003220 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003221 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003222}
3223
3224static void
3225xdg_surface_unset_maximized(struct wl_client *client,
3226 struct wl_resource *resource)
3227{
3228 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003229 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003230
3231 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3232 return;
3233
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003234 if (!shsurf->next_state.maximized)
3235 return;
3236
Rafael Antognollie2a34552013-12-03 15:35:45 -02003237 shsurf->next_state.maximized = false;
3238 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003239
3240 if (shsurf->saved_size_valid) {
3241 width = shsurf->saved_width;
3242 height = shsurf->saved_height;
3243 shsurf->saved_size_valid = false;
3244 } else {
3245 width = shsurf->surface->width;
3246 height = shsurf->surface->height;
3247 }
3248
3249 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003250}
3251
3252static const struct xdg_surface_interface xdg_surface_implementation = {
3253 xdg_surface_destroy,
3254 xdg_surface_set_transient_for,
3255 xdg_surface_set_title,
3256 xdg_surface_set_app_id,
3257 xdg_surface_pong,
3258 xdg_surface_move,
3259 xdg_surface_resize,
3260 xdg_surface_set_output,
3261 xdg_surface_set_fullscreen,
3262 xdg_surface_unset_fullscreen,
3263 xdg_surface_set_maximized,
3264 xdg_surface_unset_maximized,
3265 NULL /* set_minimized */
3266};
3267
3268static void
3269xdg_send_configure(struct weston_surface *surface,
3270 uint32_t edges, int32_t width, int32_t height)
3271{
3272 struct shell_surface *shsurf = get_shell_surface(surface);
3273
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003274 assert(shsurf);
3275
Rafael Antognollie2a34552013-12-03 15:35:45 -02003276 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3277}
3278
3279static const struct weston_shell_client xdg_client = {
3280 xdg_send_configure
3281};
3282
3283static void
3284xdg_use_unstable_version(struct wl_client *client,
3285 struct wl_resource *resource,
3286 int32_t version)
3287{
3288 if (version > 1) {
3289 wl_resource_post_error(resource,
3290 1,
3291 "xdg-shell:: version not implemented yet.");
3292 return;
3293 }
3294}
3295
3296static struct shell_surface *
3297create_xdg_surface(void *shell, struct weston_surface *surface,
3298 const struct weston_shell_client *client)
3299{
3300 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003301
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003302 shsurf = create_common_surface(shell, surface, client);
3303 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003304
3305 return shsurf;
3306}
3307
3308static void
3309xdg_get_xdg_surface(struct wl_client *client,
3310 struct wl_resource *resource,
3311 uint32_t id,
3312 struct wl_resource *surface_resource)
3313{
3314 struct weston_surface *surface =
3315 wl_resource_get_user_data(surface_resource);
3316 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3317 struct shell_surface *shsurf;
3318
3319 if (get_shell_surface(surface)) {
3320 wl_resource_post_error(surface_resource,
3321 WL_DISPLAY_ERROR_INVALID_OBJECT,
3322 "desktop_shell::get_shell_surface already requested");
3323 return;
3324 }
3325
3326 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3327 if (!shsurf) {
3328 wl_resource_post_error(surface_resource,
3329 WL_DISPLAY_ERROR_INVALID_OBJECT,
3330 "surface->configure already set");
3331 return;
3332 }
3333
3334 shsurf->resource =
3335 wl_resource_create(client,
3336 &xdg_surface_interface, 1, id);
3337 wl_resource_set_implementation(shsurf->resource,
3338 &xdg_surface_implementation,
3339 shsurf, shell_destroy_shell_surface);
3340}
3341
3342static bool
3343shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3344{
3345 return wl_resource_instance_of(shsurf->resource,
3346 &xdg_surface_interface,
3347 &xdg_surface_implementation);
3348}
3349
3350/* xdg-popup implementation */
3351
3352static void
3353xdg_popup_destroy(struct wl_client *client,
3354 struct wl_resource *resource)
3355{
3356 wl_resource_destroy(resource);
3357}
3358
3359static void
3360xdg_popup_pong(struct wl_client *client,
3361 struct wl_resource *resource,
3362 uint32_t serial)
3363{
3364 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3365
3366 surface_pong(shsurf, serial);
3367}
3368
3369static const struct xdg_popup_interface xdg_popup_implementation = {
3370 xdg_popup_destroy,
3371 xdg_popup_pong
3372};
3373
3374static void
3375xdg_popup_send_configure(struct weston_surface *surface,
3376 uint32_t edges, int32_t width, int32_t height)
3377{
3378}
3379
3380static const struct weston_shell_client xdg_popup_client = {
3381 xdg_popup_send_configure
3382};
3383
3384static struct shell_surface *
3385create_xdg_popup(void *shell, struct weston_surface *surface,
3386 const struct weston_shell_client *client,
3387 struct weston_surface *parent,
3388 struct shell_seat *seat,
3389 uint32_t serial,
3390 int32_t x, int32_t y)
3391{
3392 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003393
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003394 shsurf = create_common_surface(shell, surface, client);
3395 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003396 shsurf->popup.shseat = seat;
3397 shsurf->popup.serial = serial;
3398 shsurf->popup.x = x;
3399 shsurf->popup.y = y;
3400 shell_surface_set_parent(shsurf, parent);
3401
3402 return shsurf;
3403}
3404
3405static void
3406xdg_get_xdg_popup(struct wl_client *client,
3407 struct wl_resource *resource,
3408 uint32_t id,
3409 struct wl_resource *surface_resource,
3410 struct wl_resource *parent_resource,
3411 struct wl_resource *seat_resource,
3412 uint32_t serial,
3413 int32_t x, int32_t y, uint32_t flags)
3414{
3415 struct weston_surface *surface =
3416 wl_resource_get_user_data(surface_resource);
3417 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3418 struct shell_surface *shsurf;
3419 struct weston_surface *parent;
3420 struct shell_seat *seat;
3421
3422 if (get_shell_surface(surface)) {
3423 wl_resource_post_error(surface_resource,
3424 WL_DISPLAY_ERROR_INVALID_OBJECT,
3425 "desktop_shell::get_shell_surface already requested");
3426 return;
3427 }
3428
3429 if (!parent_resource) {
3430 wl_resource_post_error(surface_resource,
3431 WL_DISPLAY_ERROR_INVALID_OBJECT,
3432 "xdg_shell::get_xdg_popup requires a parent shell surface");
3433 }
3434
3435 parent = wl_resource_get_user_data(parent_resource);
3436 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3437
3438 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3439 parent, seat, serial, x, y);
3440 if (!shsurf) {
3441 wl_resource_post_error(surface_resource,
3442 WL_DISPLAY_ERROR_INVALID_OBJECT,
3443 "surface->configure already set");
3444 return;
3445 }
3446
3447 shsurf->resource =
3448 wl_resource_create(client,
3449 &xdg_popup_interface, 1, id);
3450 wl_resource_set_implementation(shsurf->resource,
3451 &xdg_popup_implementation,
3452 shsurf, shell_destroy_shell_surface);
3453}
3454
3455static bool
3456shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3457{
3458 return wl_resource_instance_of(shsurf->resource,
3459 &xdg_popup_interface,
3460 &xdg_popup_implementation);
3461}
3462
3463static const struct xdg_shell_interface xdg_implementation = {
3464 xdg_use_unstable_version,
3465 xdg_get_xdg_surface,
3466 xdg_get_xdg_popup
3467};
3468
3469static int
3470xdg_shell_unversioned_dispatch(const void *implementation,
3471 void *_target, uint32_t opcode,
3472 const struct wl_message *message,
3473 union wl_argument *args)
3474{
3475 struct wl_resource *resource = _target;
3476 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3477
3478 if (opcode != 0) {
3479 wl_resource_post_error(resource,
3480 WL_DISPLAY_ERROR_INVALID_OBJECT,
3481 "must call use_unstable_version first");
3482 return 0;
3483 }
3484
3485#define XDG_SERVER_VERSION 1
3486
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003487 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3488 "shell implementation doesn't match protocol version");
3489
Rafael Antognollie2a34552013-12-03 15:35:45 -02003490 if (args[0].i != XDG_SERVER_VERSION) {
3491 wl_resource_post_error(resource,
3492 WL_DISPLAY_ERROR_INVALID_OBJECT,
3493 "incompatible version, server is %d "
3494 "client wants %d",
3495 XDG_SERVER_VERSION, args[0].i);
3496 return 0;
3497 }
3498
3499 wl_resource_set_implementation(resource, &xdg_implementation,
3500 shell, NULL);
3501
3502 return 1;
3503}
3504
3505/* end of xdg-shell implementation */
3506/***********************************/
3507
Kristian Høgsberg07937562011-04-12 17:25:42 -04003508static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003509shell_fade(struct desktop_shell *shell, enum fade_type type);
3510
3511static int
3512screensaver_timeout(void *data)
3513{
3514 struct desktop_shell *shell = data;
3515
3516 shell_fade(shell, FADE_OUT);
3517
3518 return 1;
3519}
3520
3521static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003522handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003523{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003524 struct desktop_shell *shell =
3525 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003526
Pekka Paalanen18027e52011-12-02 16:31:49 +02003527 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003528
3529 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003530 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003531}
3532
3533static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003534launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003535{
3536 if (shell->screensaver.binding)
3537 return;
3538
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003539 if (!shell->screensaver.path) {
3540 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003541 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003542 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003543
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003544 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003545 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003546 return;
3547 }
3548
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003549 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003550 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003551 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003552 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003553}
3554
3555static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003556terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003557{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003558 if (shell->screensaver.process.pid == 0)
3559 return;
3560
3561 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003562}
3563
3564static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003565configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003566{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003568
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3570 if (v->output == ev->output && v != ev) {
3571 weston_view_unmap(v);
3572 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003573 }
3574 }
3575
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003576 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003577
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 if (wl_list_empty(&ev->layer_link)) {
3579 wl_list_insert(&layer->view_list, &ev->layer_link);
3580 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003581 }
3582}
3583
3584static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003585background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003586{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003587 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003588 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003589
Jason Ekstranda7af7042013-10-12 22:38:11 -05003590 view = container_of(es->views.next, struct weston_view, surface_link);
3591
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003592 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003593}
3594
3595static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003596desktop_shell_set_background(struct wl_client *client,
3597 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003598 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003599 struct wl_resource *surface_resource)
3600{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003601 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003602 struct weston_surface *surface =
3603 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003605
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003606 if (surface->configure) {
3607 wl_resource_post_error(surface_resource,
3608 WL_DISPLAY_ERROR_INVALID_OBJECT,
3609 "surface role already assigned");
3610 return;
3611 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003612
Jason Ekstranda7af7042013-10-12 22:38:11 -05003613 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3614 weston_view_destroy(view);
3615 view = weston_view_create(surface);
3616
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003617 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003618 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003619 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003621 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003622 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003623 surface->output->width,
3624 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003625}
3626
3627static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003628panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003629{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003630 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003631 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003632
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 view = container_of(es->views.next, struct weston_view, surface_link);
3634
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003635 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003636}
3637
3638static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003639desktop_shell_set_panel(struct wl_client *client,
3640 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003641 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003642 struct wl_resource *surface_resource)
3643{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003644 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003645 struct weston_surface *surface =
3646 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003648
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003649 if (surface->configure) {
3650 wl_resource_post_error(surface_resource,
3651 WL_DISPLAY_ERROR_INVALID_OBJECT,
3652 "surface role already assigned");
3653 return;
3654 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003655
Jason Ekstranda7af7042013-10-12 22:38:11 -05003656 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3657 weston_view_destroy(view);
3658 view = weston_view_create(surface);
3659
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003660 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003661 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003662 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003663 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003664 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003665 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003666 surface->output->width,
3667 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003668}
3669
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003670static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003671lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003672{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003673 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 struct weston_view *view;
3675
3676 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003677
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003678 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003679 return;
3680
Jason Ekstranda7af7042013-10-12 22:38:11 -05003681 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003682
3683 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 wl_list_insert(&shell->lock_layer.view_list,
3685 &view->layer_link);
3686 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003687 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003688 }
3689}
3690
3691static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003692handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003693{
Tiago Vignattibe143262012-04-16 17:31:41 +03003694 struct desktop_shell *shell =
3695 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003696
Martin Minarik6d118362012-06-07 18:01:59 +02003697 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003698 shell->lock_surface = NULL;
3699}
3700
3701static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003702desktop_shell_set_lock_surface(struct wl_client *client,
3703 struct wl_resource *resource,
3704 struct wl_resource *surface_resource)
3705{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003706 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003707 struct weston_surface *surface =
3708 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003709
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003710 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003711
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003712 if (!shell->locked)
3713 return;
3714
Pekka Paalanen98262232011-12-01 10:42:22 +02003715 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003716
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003717 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003718 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003719 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003720
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003721 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003722 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003723 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003724}
3725
3726static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003727resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003728{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003729 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003730
Pekka Paalanen77346a62011-11-30 16:26:35 +02003731 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003732
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003733 wl_list_remove(&shell->lock_layer.link);
3734 wl_list_insert(&shell->compositor->cursor_layer.link,
3735 &shell->fullscreen_layer.link);
3736 wl_list_insert(&shell->fullscreen_layer.link,
3737 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003738 if (shell->showing_input_panels) {
3739 wl_list_insert(&shell->panel_layer.link,
3740 &shell->input_panel_layer.link);
3741 wl_list_insert(&shell->input_panel_layer.link,
3742 &ws->layer.link);
3743 } else {
3744 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3745 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003746
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003747 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003748
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003749 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003750 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003751 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003752}
3753
3754static void
3755desktop_shell_unlock(struct wl_client *client,
3756 struct wl_resource *resource)
3757{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003758 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003759
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003760 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003761
3762 if (shell->locked)
3763 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003764}
3765
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003766static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003767desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003768 struct wl_resource *resource,
3769 struct wl_resource *surface_resource)
3770{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003771 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003772
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003773 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003774 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003775}
3776
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003777static void
3778desktop_shell_desktop_ready(struct wl_client *client,
3779 struct wl_resource *resource)
3780{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003781 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003782
3783 shell_fade_startup(shell);
3784}
3785
Kristian Høgsberg75840622011-09-06 13:48:16 -04003786static const struct desktop_shell_interface desktop_shell_implementation = {
3787 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003788 desktop_shell_set_panel,
3789 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003790 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003791 desktop_shell_set_grab_surface,
3792 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003793};
3794
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003795static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003796get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003797{
3798 struct shell_surface *shsurf;
3799
3800 shsurf = get_shell_surface(surface);
3801 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003802 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003803 return shsurf->type;
3804}
3805
Kristian Høgsberg75840622011-09-06 13:48:16 -04003806static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003807move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003808{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003809 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003810 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003811 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003812
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003813 if (seat->pointer->focus == NULL)
3814 return;
3815
3816 focus = seat->pointer->focus->surface;
3817
Pekka Paalanen01388e22013-04-25 13:57:44 +03003818 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003819 if (surface == NULL)
3820 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003821
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003822 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003823 if (shsurf == NULL || shsurf->state.fullscreen ||
3824 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003825 return;
3826
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003827 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003828}
3829
3830static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003831maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3832{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003833 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003834 struct weston_surface *surface;
3835 struct shell_surface *shsurf;
3836
3837 surface = weston_surface_get_main_surface(focus);
3838 if (surface == NULL)
3839 return;
3840
3841 shsurf = get_shell_surface(surface);
3842 if (shsurf == NULL)
3843 return;
3844
3845 if (!shell_surface_is_xdg_surface(shsurf))
3846 return;
3847
3848 if (shsurf->state.maximized)
3849 xdg_surface_send_request_unset_maximized(shsurf->resource);
3850 else
3851 xdg_surface_send_request_set_maximized(shsurf->resource);
3852}
3853
3854static void
3855fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3856{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003857 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003858 struct weston_surface *surface;
3859 struct shell_surface *shsurf;
3860
3861 surface = weston_surface_get_main_surface(focus);
3862 if (surface == NULL)
3863 return;
3864
3865 shsurf = get_shell_surface(surface);
3866 if (shsurf == NULL)
3867 return;
3868
3869 if (!shell_surface_is_xdg_surface(shsurf))
3870 return;
3871
3872 if (shsurf->state.fullscreen)
3873 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3874 else
3875 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3876}
3877
3878static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003879touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3880{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003881 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003882 struct weston_surface *surface;
3883 struct shell_surface *shsurf;
3884
3885 surface = weston_surface_get_main_surface(focus);
3886 if (surface == NULL)
3887 return;
3888
3889 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003890 if (shsurf == NULL || shsurf->state.fullscreen ||
3891 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003892 return;
3893
3894 surface_touch_move(shsurf, (struct weston_seat *) seat);
3895}
3896
3897static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003898resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003899{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003900 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003901 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003902 uint32_t edges = 0;
3903 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003904 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003905
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003906 if (seat->pointer->focus == NULL)
3907 return;
3908
3909 focus = seat->pointer->focus->surface;
3910
Pekka Paalanen01388e22013-04-25 13:57:44 +03003911 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003912 if (surface == NULL)
3913 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003914
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003915 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003916 if (shsurf == NULL || shsurf->state.fullscreen ||
3917 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003918 return;
3919
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 weston_view_from_global(shsurf->view,
3921 wl_fixed_to_int(seat->pointer->grab_x),
3922 wl_fixed_to_int(seat->pointer->grab_y),
3923 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003924
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003925 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003926 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003927 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003928 edges |= 0;
3929 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003930 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003931
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003932 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003933 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003934 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003935 edges |= 0;
3936 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003937 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003938
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003939 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003940}
3941
3942static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003943surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003944 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003945{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003946 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003947 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003948 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003949 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003950
Pekka Paalanen01388e22013-04-25 13:57:44 +03003951 /* XXX: broken for windows containing sub-surfaces */
3952 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003953 if (surface == NULL)
3954 return;
3955
3956 shsurf = get_shell_surface(surface);
3957 if (!shsurf)
3958 return;
3959
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003961
Jason Ekstranda7af7042013-10-12 22:38:11 -05003962 if (shsurf->view->alpha > 1.0)
3963 shsurf->view->alpha = 1.0;
3964 if (shsurf->view->alpha < step)
3965 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003966
Jason Ekstranda7af7042013-10-12 22:38:11 -05003967 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003968 weston_surface_damage(surface);
3969}
3970
3971static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003972do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003973 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003974{
Daniel Stone37816df2012-05-16 18:45:18 +01003975 struct weston_seat *ws = (struct weston_seat *) seat;
3976 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003977 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003978 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003979
3980 wl_list_for_each(output, &compositor->output_list, link) {
3981 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003982 wl_fixed_to_double(seat->pointer->x),
3983 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003984 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003985 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003986 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003987 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003988 increment = -output->zoom.increment;
3989 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003990 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003991 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003992 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003993 else
3994 increment = 0;
3995
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003996 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003997
Scott Moreaue6603982012-06-11 13:07:51 -06003998 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003999 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004000 else if (output->zoom.level > output->zoom.max_level)
4001 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004002 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004003 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004004 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004005
Scott Moreaue6603982012-06-11 13:07:51 -06004006 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004007
Jason Ekstranda7af7042013-10-12 22:38:11 -05004008 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004009 }
4010 }
4011}
4012
Scott Moreauccbf29d2012-02-22 14:21:41 -07004013static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004014zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004015 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004016{
4017 do_zoom(seat, time, 0, axis, value);
4018}
4019
4020static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004021zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004022 void *data)
4023{
4024 do_zoom(seat, time, key, 0, 0);
4025}
4026
4027static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004028terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004029 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004030{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004031 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004032
Daniel Stone325fc2d2012-05-30 16:31:58 +01004033 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004034}
4035
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004036static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004037rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4038 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004039{
4040 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004041 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004042 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004043 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004044 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004045
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004046 weston_pointer_move(pointer, x, y);
4047
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004048 if (!shsurf)
4049 return;
4050
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004051 cx = 0.5f * shsurf->surface->width;
4052 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004053
Daniel Stone37816df2012-05-16 18:45:18 +01004054 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4055 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004056 r = sqrtf(dx * dx + dy * dy);
4057
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004058 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004059 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004060
4061 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004062 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004063 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004064
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004065 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004066 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004067
4068 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004069 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004070 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004071 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004072 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004073
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004074 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004075 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004076 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004077 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004078 wl_list_init(&shsurf->rotation.transform.link);
4079 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004080 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004081 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004082
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004083 /* We need to adjust the position of the surface
4084 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004085 cposx = shsurf->view->geometry.x + cx;
4086 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004087 dposx = rotate->center.x - cposx;
4088 dposy = rotate->center.y - cposy;
4089 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004090 weston_view_set_position(shsurf->view,
4091 shsurf->view->geometry.x + dposx,
4092 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004093 }
4094
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004095 /* Repaint implies weston_surface_update_transform(), which
4096 * lazily applies the damage due to rotation update.
4097 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004098 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004099}
4100
4101static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004102rotate_grab_button(struct weston_pointer_grab *grab,
4103 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004104{
4105 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004106 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004107 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004108 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004109 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004110
Daniel Stone4dbadb12012-05-30 16:31:51 +01004111 if (pointer->button_count == 0 &&
4112 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004113 if (shsurf)
4114 weston_matrix_multiply(&shsurf->rotation.rotation,
4115 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004116 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004117 free(rotate);
4118 }
4119}
4120
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004121static void
4122rotate_grab_cancel(struct weston_pointer_grab *grab)
4123{
4124 struct rotate_grab *rotate =
4125 container_of(grab, struct rotate_grab, base.grab);
4126
4127 shell_grab_end(&rotate->base);
4128 free(rotate);
4129}
4130
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004131static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004132 noop_grab_focus,
4133 rotate_grab_motion,
4134 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004135 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004136};
4137
4138static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004139surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004140{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004141 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004142 float dx, dy;
4143 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004144
Pekka Paalanen460099f2012-01-20 16:48:25 +02004145 rotate = malloc(sizeof *rotate);
4146 if (!rotate)
4147 return;
4148
Jason Ekstranda7af7042013-10-12 22:38:11 -05004149 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004150 surface->surface->width * 0.5f,
4151 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004152 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004153
Daniel Stone37816df2012-05-16 18:45:18 +01004154 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4155 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004156 r = sqrtf(dx * dx + dy * dy);
4157 if (r > 20.0f) {
4158 struct weston_matrix inverse;
4159
4160 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004161 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004162 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004163
4164 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004165 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004166 } else {
4167 weston_matrix_init(&surface->rotation.rotation);
4168 weston_matrix_init(&rotate->rotation);
4169 }
4170
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004171 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4172 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004173}
4174
4175static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004176rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004177 void *data)
4178{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004179 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004180 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004181 struct shell_surface *surface;
4182
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004183 if (seat->pointer->focus == NULL)
4184 return;
4185
4186 focus = seat->pointer->focus->surface;
4187
Pekka Paalanen01388e22013-04-25 13:57:44 +03004188 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004189 if (base_surface == NULL)
4190 return;
4191
4192 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004193 if (surface == NULL || surface->state.fullscreen ||
4194 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004195 return;
4196
4197 surface_rotate(surface, seat);
4198}
4199
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004200/* Move all fullscreen layers down to the current workspace in a non-reversible
4201 * manner. This should be used when implementing shell-wide overlays, such as
4202 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004203void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004204lower_fullscreen_layer(struct desktop_shell *shell)
4205{
4206 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004208
4209 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004210 wl_list_for_each_reverse_safe(view, prev,
4211 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004212 layer_link) {
4213 wl_list_remove(&view->layer_link);
4214 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4215 weston_view_damage_below(view);
4216 weston_surface_damage(view->surface);
4217 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004218}
4219
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004220void
Tiago Vignattibe143262012-04-16 17:31:41 +03004221activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004222 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004223{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004224 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004225 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004226 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004227 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004228 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004229
Pekka Paalanen01388e22013-04-25 13:57:44 +03004230 main_surface = weston_surface_get_main_surface(es);
4231
Daniel Stone37816df2012-05-16 18:45:18 +01004232 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004233
Jonas Ådahl8538b222012-08-29 22:13:03 +02004234 state = ensure_focus_state(shell, seat);
4235 if (state == NULL)
4236 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004237
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004238 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004239 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004240
Rafael Antognolli03b16592013-12-03 15:35:42 -02004241 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004242 assert(shsurf);
4243
Rafael Antognolli03b16592013-12-03 15:35:42 -02004244 if (shsurf->state.fullscreen)
4245 shell_configure_fullscreen(shsurf);
4246 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004247 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004248
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004249 if (shell->focus_animation_type != ANIMATION_NONE) {
4250 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004251 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004252 }
4253
4254 /* Update the surface’s layer. This brings it to the top of the stacking
4255 * order as appropriate. */
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004256 shell_surface_update_layer(shsurf);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004257}
4258
Alex Wu21858432012-04-01 20:13:08 +08004259/* no-op func for checking black surface */
4260static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004261black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004262{
4263}
4264
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004265static bool
Alex Wu21858432012-04-01 20:13:08 +08004266is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4267{
4268 if (es->configure == black_surface_configure) {
4269 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004270 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004271 return true;
4272 }
4273 return false;
4274}
4275
Kristian Høgsberg75840622011-09-06 13:48:16 -04004276static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004277activate_binding(struct weston_seat *seat,
4278 struct desktop_shell *shell,
4279 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004280{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004281 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004282
Alex Wu9c35e6b2012-03-05 14:13:13 +08004283 if (!focus)
4284 return;
4285
Pekka Paalanen01388e22013-04-25 13:57:44 +03004286 if (is_black_surface(focus, &main_surface))
4287 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004288
Pekka Paalanen01388e22013-04-25 13:57:44 +03004289 main_surface = weston_surface_get_main_surface(focus);
4290 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004291 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004292
Neil Robertsa28c6932013-10-03 16:43:04 +01004293 activate(shell, focus, seat);
4294}
4295
4296static void
4297click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4298 void *data)
4299{
4300 if (seat->pointer->grab != &seat->pointer->default_grab)
4301 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004302 if (seat->pointer->focus == NULL)
4303 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004304
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004305 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004306}
4307
4308static void
4309touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4310{
4311 if (seat->touch->grab != &seat->touch->default_grab)
4312 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004313 if (seat->touch->focus == NULL)
4314 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004315
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004316 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004317}
4318
4319static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004320lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004321{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004322 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004323
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004324 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004325 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004326 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004327 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004328
4329 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004330
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004331 /* Hide all surfaces by removing the fullscreen, panel and
4332 * toplevel layers. This way nothing else can show or receive
4333 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004334
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004335 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004336 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004337 if (shell->showing_input_panels)
4338 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004339 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004340 wl_list_insert(&shell->compositor->cursor_layer.link,
4341 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004342
Pekka Paalanen77346a62011-11-30 16:26:35 +02004343 launch_screensaver(shell);
4344
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004345 /* TODO: disable bindings that should not work while locked. */
4346
4347 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004348}
4349
4350static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004351unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004352{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004353 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004354 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004355 return;
4356 }
4357
4358 /* If desktop-shell client has gone away, unlock immediately. */
4359 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004360 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004361 return;
4362 }
4363
4364 if (shell->prepare_event_sent)
4365 return;
4366
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004367 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004368 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004369}
4370
4371static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004372shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004373{
4374 struct desktop_shell *shell = data;
4375
4376 shell->fade.animation = NULL;
4377
4378 switch (shell->fade.type) {
4379 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 weston_surface_destroy(shell->fade.view->surface);
4381 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004382 break;
4383 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004384 lock(shell);
4385 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004386 default:
4387 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004388 }
4389}
4390
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004392shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004393{
4394 struct weston_compositor *compositor = shell->compositor;
4395 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004397
4398 surface = weston_surface_create(compositor);
4399 if (!surface)
4400 return NULL;
4401
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 view = weston_view_create(surface);
4403 if (!view) {
4404 weston_surface_destroy(surface);
4405 return NULL;
4406 }
4407
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004408 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004409 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004410 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 wl_list_insert(&compositor->fade_layer.view_list,
4412 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004413 pixman_region32_init(&surface->input);
4414
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004416}
4417
4418static void
4419shell_fade(struct desktop_shell *shell, enum fade_type type)
4420{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004421 float tint;
4422
4423 switch (type) {
4424 case FADE_IN:
4425 tint = 0.0;
4426 break;
4427 case FADE_OUT:
4428 tint = 1.0;
4429 break;
4430 default:
4431 weston_log("shell: invalid fade type\n");
4432 return;
4433 }
4434
4435 shell->fade.type = type;
4436
Jason Ekstranda7af7042013-10-12 22:38:11 -05004437 if (shell->fade.view == NULL) {
4438 shell->fade.view = shell_fade_create_surface(shell);
4439 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004440 return;
4441
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 shell->fade.view->alpha = 1.0 - tint;
4443 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004444 }
4445
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004446 if (shell->fade.view->output == NULL) {
4447 /* If the black view gets a NULL output, we lost the
4448 * last output and we'll just cancel the fade. This
4449 * happens when you close the last window under the
4450 * X11 or Wayland backends. */
4451 shell->locked = false;
4452 weston_surface_destroy(shell->fade.view->surface);
4453 shell->fade.view = NULL;
4454 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004455 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004456 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004457 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004459 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004460 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004461 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004462}
4463
4464static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004465do_shell_fade_startup(void *data)
4466{
4467 struct desktop_shell *shell = data;
4468
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004469 if (shell->startup_animation_type == ANIMATION_FADE)
4470 shell_fade(shell, FADE_IN);
4471 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 weston_surface_destroy(shell->fade.view->surface);
4473 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004474 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004475}
4476
4477static void
4478shell_fade_startup(struct desktop_shell *shell)
4479{
4480 struct wl_event_loop *loop;
4481
4482 if (!shell->fade.startup_timer)
4483 return;
4484
4485 wl_event_source_remove(shell->fade.startup_timer);
4486 shell->fade.startup_timer = NULL;
4487
4488 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4489 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4490}
4491
4492static int
4493fade_startup_timeout(void *data)
4494{
4495 struct desktop_shell *shell = data;
4496
4497 shell_fade_startup(shell);
4498 return 0;
4499}
4500
4501static void
4502shell_fade_init(struct desktop_shell *shell)
4503{
4504 /* Make compositor output all black, and wait for the desktop-shell
4505 * client to signal it is ready, then fade in. The timer triggers a
4506 * fade-in, in case the desktop-shell client takes too long.
4507 */
4508
4509 struct wl_event_loop *loop;
4510
Jason Ekstranda7af7042013-10-12 22:38:11 -05004511 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004512 weston_log("%s: warning: fade surface already exists\n",
4513 __func__);
4514 return;
4515 }
4516
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 shell->fade.view = shell_fade_create_surface(shell);
4518 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004519 return;
4520
Jason Ekstranda7af7042013-10-12 22:38:11 -05004521 weston_view_update_transform(shell->fade.view);
4522 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004523
4524 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4525 shell->fade.startup_timer =
4526 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4527 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4528}
4529
4530static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004531idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004532{
4533 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004534 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004535 struct weston_seat *seat;
4536
4537 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4538 if (seat->pointer)
4539 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004540
4541 shell_fade(shell, FADE_OUT);
4542 /* lock() is called from shell_fade_done() */
4543}
4544
4545static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004546wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004547{
4548 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004549 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004550
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004551 unlock(shell);
4552}
4553
4554static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004555center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004556{
Giulio Camuffob8366642013-04-25 13:57:46 +03004557 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004558 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004559
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004561
4562 x = output->x + (output->width - width) / 2 - surf_x / 2;
4563 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004564
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004565 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004566}
4567
4568static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004569weston_view_set_initial_position(struct weston_view *view,
4570 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004571{
4572 struct weston_compositor *compositor = shell->compositor;
4573 int ix = 0, iy = 0;
4574 int range_x, range_y;
4575 int dx, dy, x, y, panel_height;
4576 struct weston_output *output, *target_output = NULL;
4577 struct weston_seat *seat;
4578
4579 /* As a heuristic place the new window on the same output as the
4580 * pointer. Falling back to the output containing 0, 0.
4581 *
4582 * TODO: Do something clever for touch too?
4583 */
4584 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004585 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004586 ix = wl_fixed_to_int(seat->pointer->x);
4587 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004588 break;
4589 }
4590 }
4591
4592 wl_list_for_each(output, &compositor->output_list, link) {
4593 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4594 target_output = output;
4595 break;
4596 }
4597 }
4598
4599 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600 weston_view_set_position(view, 10 + random() % 400,
4601 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004602 return;
4603 }
4604
4605 /* Valid range within output where the surface will still be onscreen.
4606 * If this is negative it means that the surface is bigger than
4607 * output.
4608 */
4609 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004610 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004611 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004612 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004613
Rob Bradford4cb88c72012-08-13 15:18:44 +01004614 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004615 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004616 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004617 dx = 0;
4618
4619 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004620 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004621 else
4622 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004623
4624 x = target_output->x + dx;
4625 y = target_output->y + dy;
4626
Jason Ekstranda7af7042013-10-12 22:38:11 -05004627 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004628}
4629
4630static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004632 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004633{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004634 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004635 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004636 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004637 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004638
Pekka Paalanen77346a62011-11-30 16:26:35 +02004639 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004640 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004641 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004642 if (shsurf->state.fullscreen) {
4643 center_on_output(shsurf->view, shsurf->fullscreen_output);
4644 shell_map_fullscreen(shsurf);
4645 } else if (shsurf->state.maximized) {
4646 /* use surface configure to set the geometry */
4647 panel_height = get_output_panel_height(shell, shsurf->output);
4648 surface_subsurfaces_boundingbox(shsurf->surface,
4649 &surf_x, &surf_y, NULL, NULL);
4650 weston_view_set_position(shsurf->view,
4651 shsurf->output->x - surf_x,
4652 shsurf->output->y +
4653 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004654 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004655 weston_view_set_initial_position(shsurf->view, shell);
4656 }
Juan Zhao96879df2012-02-07 08:45:41 +08004657 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004658 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004659 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004660 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004661 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 weston_view_set_position(shsurf->view,
4663 shsurf->view->geometry.x + sx,
4664 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004665 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004666 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004667 default:
4668 ;
4669 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004670
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004671 /* Surface stacking order, see also activate(). */
4672 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004673
Jason Ekstranda7af7042013-10-12 22:38:11 -05004674 if (shsurf->type != SHELL_SURFACE_NONE) {
4675 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004676 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004677 shsurf->surface->output = shsurf->output;
4678 shsurf->view->output = shsurf->output;
4679 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004680 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004681
Rafael Antognollied207b42013-12-03 15:35:43 -02004682 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4683 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4684 }
4685
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004687 /* XXX: xwayland's using the same fields for transient type */
4688 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004689 if (shsurf->transient.flags ==
4690 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4691 break;
4692 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004693 if (shsurf->state.relative &&
4694 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4695 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004696 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004697 break;
4698 wl_list_for_each(seat, &compositor->seat_list, link)
4699 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004700 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004701 case SHELL_SURFACE_POPUP:
4702 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004703 default:
4704 break;
4705 }
4706
Rafael Antognolli03b16592013-12-03 15:35:42 -02004707 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4708 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004709 {
4710 switch (shell->win_animation_type) {
4711 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004712 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004713 break;
4714 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004716 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004717 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004718 default:
4719 break;
4720 }
4721 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004722}
4723
4724static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004725configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004727{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004728 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004729 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004730 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004731
Pekka Paalanen77346a62011-11-30 16:26:35 +02004732 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004733
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004734 assert(shsurf);
4735
Rafael Antognolli03b16592013-12-03 15:35:42 -02004736 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004737 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004738 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004739 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004740 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4741 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004742 mx = shsurf->output->x - surf_x;
4743 my = shsurf->output->y +
4744 get_output_panel_height(shell,shsurf->output) - surf_y;
4745 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004746 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004747 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004748 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004749
Alex Wu4539b082012-03-01 12:57:46 +08004750 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004751 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004752 wl_list_for_each(view, &surface->views, surface_link)
4753 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004754
Rafael Antognolli03b16592013-12-03 15:35:42 -02004755 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004756 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004757 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004758}
4759
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004760static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004761shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004762{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004763 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004764 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004765 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004766
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004767 assert(shsurf);
4768
4769 shell = shsurf->shell;
4770
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004771 if (!weston_surface_is_mapped(es) &&
4772 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004773 remove_popup_grab(shsurf);
4774 }
4775
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004776 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004777 return;
4778
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004779 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004780 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004781 type_changed = 1;
4782 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004783
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004784 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004785 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004786 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004787 shsurf->last_width != es->width ||
4788 shsurf->last_height != es->height) {
4789 shsurf->last_width = es->width;
4790 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004791 float from_x, from_y;
4792 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004793
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4795 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004796 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004797 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004798 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004799 }
4800}
4801
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004802static void
4803shell_surface_output_destroyed(struct weston_surface *es)
4804{
4805 struct shell_surface *shsurf = get_shell_surface(es);
4806
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004807 assert(shsurf);
4808
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004809 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004810 shsurf->next_state.maximized = false;
4811 shsurf->next_state.fullscreen = false;
4812 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004813}
4814
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004815static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004816
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004817static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004818desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004819{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004820 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004821 struct desktop_shell *shell =
4822 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004823
4824 shell->child.process.pid = 0;
4825 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004826
4827 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4828 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004829 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004830 shell->child.deathstamp = time;
4831 shell->child.deathcount = 0;
4832 }
4833
4834 shell->child.deathcount++;
4835 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004836 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004837 return;
4838 }
4839
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004840 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004841 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004842 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004843}
4844
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004845static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004846desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4847{
4848 struct desktop_shell *shell;
4849
4850 shell = container_of(listener, struct desktop_shell,
4851 child.client_destroy_listener);
4852
4853 shell->child.client = NULL;
4854}
4855
4856static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004857launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004858{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004859 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004860
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004861 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004862 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004863 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004864 desktop_shell_sigchld);
4865
4866 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004867 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004868
4869 shell->child.client_destroy_listener.notify =
4870 desktop_shell_client_destroy;
4871 wl_client_add_destroy_listener(shell->child.client,
4872 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004873}
4874
4875static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004876bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4877{
Tiago Vignattibe143262012-04-16 17:31:41 +03004878 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004879 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004880
Jason Ekstranda85118c2013-06-27 20:17:02 -05004881 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4882 if (resource)
4883 wl_resource_set_implementation(resource, &shell_implementation,
4884 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004885}
4886
Kristian Høgsberg75840622011-09-06 13:48:16 -04004887static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004888bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4889{
4890 struct desktop_shell *shell = data;
4891 struct wl_resource *resource;
4892
4893 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4894 if (resource)
4895 wl_resource_set_dispatcher(resource,
4896 xdg_shell_unversioned_dispatch,
4897 NULL, shell, NULL);
4898}
4899
4900static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004901unbind_desktop_shell(struct wl_resource *resource)
4902{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004903 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004904
4905 if (shell->locked)
4906 resume_desktop(shell);
4907
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004908 shell->child.desktop_shell = NULL;
4909 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004910}
4911
4912static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004913bind_desktop_shell(struct wl_client *client,
4914 void *data, uint32_t version, uint32_t id)
4915{
Tiago Vignattibe143262012-04-16 17:31:41 +03004916 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004917 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004918
Jason Ekstranda85118c2013-06-27 20:17:02 -05004919 resource = wl_resource_create(client, &desktop_shell_interface,
4920 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004921
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004922 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004923 wl_resource_set_implementation(resource,
4924 &desktop_shell_implementation,
4925 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004926 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004927
4928 if (version < 2)
4929 shell_fade_startup(shell);
4930
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004931 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004932 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004933
4934 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4935 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004936 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004937}
4938
Pekka Paalanen6e168112011-11-24 11:34:05 +02004939static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004940screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004941{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004942 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004944
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004945 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004946 return;
4947
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004948 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004949 if (!shell->locked)
4950 return;
4951
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 view = container_of(surface->views.next, struct weston_view, surface_link);
4953 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004954
Jason Ekstranda7af7042013-10-12 22:38:11 -05004955 if (wl_list_empty(&view->layer_link)) {
4956 wl_list_insert(shell->lock_layer.view_list.prev,
4957 &view->layer_link);
4958 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004959 wl_event_source_timer_update(shell->screensaver.timer,
4960 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004961 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004962 }
4963}
4964
4965static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004966screensaver_set_surface(struct wl_client *client,
4967 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004968 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004969 struct wl_resource *output_resource)
4970{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004971 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004972 struct weston_surface *surface =
4973 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004974 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004975 struct weston_view *view, *next;
4976
4977 /* Make sure we only have one view */
4978 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4979 weston_view_destroy(view);
4980 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004981
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004982 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004983 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004984 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004985}
4986
4987static const struct screensaver_interface screensaver_implementation = {
4988 screensaver_set_surface
4989};
4990
4991static void
4992unbind_screensaver(struct wl_resource *resource)
4993{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004994 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004995
Pekka Paalanen77346a62011-11-30 16:26:35 +02004996 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004997}
4998
4999static void
5000bind_screensaver(struct wl_client *client,
5001 void *data, uint32_t version, uint32_t id)
5002{
Tiago Vignattibe143262012-04-16 17:31:41 +03005003 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005004 struct wl_resource *resource;
5005
Jason Ekstranda85118c2013-06-27 20:17:02 -05005006 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005007
Pekka Paalanen77346a62011-11-30 16:26:35 +02005008 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005009 wl_resource_set_implementation(resource,
5010 &screensaver_implementation,
5011 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005012 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005013 return;
5014 }
5015
5016 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5017 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005018 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005019}
5020
Kristian Høgsberg07045392012-02-19 18:52:44 -05005021struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005022 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005023 struct weston_surface *current;
5024 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005025 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005026};
5027
5028static void
5029switcher_next(struct switcher *switcher)
5030{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005031 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005032 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005033 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005034 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005035
Jason Ekstranda7af7042013-10-12 22:38:11 -05005036 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005037 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005038 if (shsurf &&
5039 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5040 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005041 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005043 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005044 next = view->surface;
5045 prev = view->surface;
5046 view->alpha = 0.25;
5047 weston_view_geometry_dirty(view);
5048 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005049 }
Alex Wu1659daa2012-04-01 20:13:09 +08005050
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 if (is_black_surface(view->surface, NULL)) {
5052 view->alpha = 0.25;
5053 weston_view_geometry_dirty(view);
5054 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005055 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005056 }
5057
5058 if (next == NULL)
5059 next = first;
5060
Alex Wu07b26062012-03-12 16:06:01 +08005061 if (next == NULL)
5062 return;
5063
Kristian Høgsberg07045392012-02-19 18:52:44 -05005064 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005065 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005066
5067 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005068 wl_list_for_each(view, &next->views, surface_link)
5069 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005070
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005071 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005072 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005073 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005074}
5075
5076static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005077switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005078{
5079 struct switcher *switcher =
5080 container_of(listener, struct switcher, listener);
5081
5082 switcher_next(switcher);
5083}
5084
5085static void
Daniel Stone351eb612012-05-31 15:27:47 -04005086switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005088 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005089 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005090 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005091
Jason Ekstranda7af7042013-10-12 22:38:11 -05005092 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005093 if (is_focus_view(view))
5094 continue;
5095
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 view->alpha = 1.0;
5097 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005098 }
5099
Alex Wu07b26062012-03-12 16:06:01 +08005100 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005101 activate(switcher->shell, switcher->current,
5102 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005103 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005104 weston_keyboard_end_grab(keyboard);
5105 if (keyboard->input_method_resource)
5106 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005107 free(switcher);
5108}
5109
5110static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005111switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005112 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005113{
5114 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005115 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005116
Daniel Stonec9785ea2012-05-30 16:31:52 +01005117 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005118 switcher_next(switcher);
5119}
5120
5121static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005122switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005123 uint32_t mods_depressed, uint32_t mods_latched,
5124 uint32_t mods_locked, uint32_t group)
5125{
5126 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005127 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005128
Daniel Stone351eb612012-05-31 15:27:47 -04005129 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5130 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005131}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005132
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005133static void
5134switcher_cancel(struct weston_keyboard_grab *grab)
5135{
5136 struct switcher *switcher = container_of(grab, struct switcher, grab);
5137
5138 switcher_destroy(switcher);
5139}
5140
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005141static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005142 switcher_key,
5143 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005144 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005145};
5146
5147static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005148switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005149 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005150{
Tiago Vignattibe143262012-04-16 17:31:41 +03005151 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005152 struct switcher *switcher;
5153
5154 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005155 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005156 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005157 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005158 wl_list_init(&switcher->listener.link);
5159
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005160 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005161 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005162 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005163 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5164 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005165 switcher_next(switcher);
5166}
5167
Pekka Paalanen3c647232011-12-22 13:43:43 +02005168static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005169backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005170 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005171{
5172 struct weston_compositor *compositor = data;
5173 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005174 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005175
5176 /* TODO: we're limiting to simple use cases, where we assume just
5177 * control on the primary display. We'd have to extend later if we
5178 * ever get support for setting backlights on random desktop LCD
5179 * panels though */
5180 output = get_default_output(compositor);
5181 if (!output)
5182 return;
5183
5184 if (!output->set_backlight)
5185 return;
5186
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005187 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5188 backlight_new = output->backlight_current - 25;
5189 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5190 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005191
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005192 if (backlight_new < 5)
5193 backlight_new = 5;
5194 if (backlight_new > 255)
5195 backlight_new = 255;
5196
5197 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005198 output->set_backlight(output, output->backlight_current);
5199}
5200
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005201struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005202 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005203 struct weston_seat *seat;
5204 uint32_t key[2];
5205 int key_released[2];
5206};
5207
5208static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005209debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005210 uint32_t key, uint32_t state)
5211{
5212 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005213 struct weston_compositor *ec = db->seat->compositor;
5214 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005215 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005216 uint32_t serial;
5217 int send = 0, terminate = 0;
5218 int check_binding = 1;
5219 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005220 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005221
5222 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5223 /* Do not run bindings on key releases */
5224 check_binding = 0;
5225
5226 for (i = 0; i < 2; i++)
5227 if (key == db->key[i])
5228 db->key_released[i] = 1;
5229
5230 if (db->key_released[0] && db->key_released[1]) {
5231 /* All key releases been swalled so end the grab */
5232 terminate = 1;
5233 } else if (key != db->key[0] && key != db->key[1]) {
5234 /* Should not swallow release of other keys */
5235 send = 1;
5236 }
5237 } else if (key == db->key[0] && !db->key_released[0]) {
5238 /* Do not check bindings for the first press of the binding
5239 * key. This allows it to be used as a debug shortcut.
5240 * We still need to swallow this event. */
5241 check_binding = 0;
5242 } else if (db->key[1]) {
5243 /* If we already ran a binding don't process another one since
5244 * we can't keep track of all the binding keys that were
5245 * pressed in order to swallow the release events. */
5246 send = 1;
5247 check_binding = 0;
5248 }
5249
5250 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005251 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5252 key, state)) {
5253 /* We ran a binding so swallow the press and keep the
5254 * grab to swallow the released too. */
5255 send = 0;
5256 terminate = 0;
5257 db->key[1] = key;
5258 } else {
5259 /* Terminate the grab since the key pressed is not a
5260 * debug binding key. */
5261 send = 1;
5262 terminate = 1;
5263 }
5264 }
5265
5266 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005267 serial = wl_display_next_serial(display);
5268 resource_list = &grab->keyboard->focus_resource_list;
5269 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005270 wl_keyboard_send_key(resource, serial, time, key, state);
5271 }
5272 }
5273
5274 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005275 weston_keyboard_end_grab(grab->keyboard);
5276 if (grab->keyboard->input_method_resource)
5277 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005278 free(db);
5279 }
5280}
5281
5282static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005283debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005284 uint32_t mods_depressed, uint32_t mods_latched,
5285 uint32_t mods_locked, uint32_t group)
5286{
5287 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005288 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005289
Neil Roberts96d790e2013-09-19 17:32:00 +01005290 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005291
Neil Roberts96d790e2013-09-19 17:32:00 +01005292 wl_resource_for_each(resource, resource_list) {
5293 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5294 mods_latched, mods_locked, group);
5295 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005296}
5297
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005298static void
5299debug_binding_cancel(struct weston_keyboard_grab *grab)
5300{
5301 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5302
5303 weston_keyboard_end_grab(grab->keyboard);
5304 free(db);
5305}
5306
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005307struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005308 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005309 debug_binding_modifiers,
5310 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005311};
5312
5313static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005314debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005315{
5316 struct debug_binding_grab *grab;
5317
5318 grab = calloc(1, sizeof *grab);
5319 if (!grab)
5320 return;
5321
5322 grab->seat = (struct weston_seat *) seat;
5323 grab->key[0] = key;
5324 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005325 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005326}
5327
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005329force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005330 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005331{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005332 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005333 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005334 struct desktop_shell *shell = data;
5335 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005336 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005337
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005338 focus_surface = seat->keyboard->focus;
5339 if (!focus_surface)
5340 return;
5341
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005342 wl_signal_emit(&compositor->kill_signal, focus_surface);
5343
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005344 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005345 wl_client_get_credentials(client, &pid, NULL, NULL);
5346
5347 /* Skip clients that we launched ourselves (the credentials of
5348 * the socketpair is ours) */
5349 if (pid == getpid())
5350 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005351
Daniel Stone325fc2d2012-05-30 16:31:58 +01005352 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005353}
5354
5355static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005356workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005357 uint32_t key, void *data)
5358{
5359 struct desktop_shell *shell = data;
5360 unsigned int new_index = shell->workspaces.current;
5361
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005362 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005363 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005364 if (new_index != 0)
5365 new_index--;
5366
5367 change_workspace(shell, new_index);
5368}
5369
5370static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005371workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005372 uint32_t key, void *data)
5373{
5374 struct desktop_shell *shell = data;
5375 unsigned int new_index = shell->workspaces.current;
5376
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005377 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005378 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005379 if (new_index < shell->workspaces.num - 1)
5380 new_index++;
5381
5382 change_workspace(shell, new_index);
5383}
5384
5385static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005386workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005387 uint32_t key, void *data)
5388{
5389 struct desktop_shell *shell = data;
5390 unsigned int new_index;
5391
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005392 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005393 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005394 new_index = key - KEY_F1;
5395 if (new_index >= shell->workspaces.num)
5396 new_index = shell->workspaces.num - 1;
5397
5398 change_workspace(shell, new_index);
5399}
5400
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005401static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005402workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005403 uint32_t key, void *data)
5404{
5405 struct desktop_shell *shell = data;
5406 unsigned int new_index = shell->workspaces.current;
5407
5408 if (shell->locked)
5409 return;
5410
5411 if (new_index != 0)
5412 new_index--;
5413
5414 take_surface_to_workspace_by_seat(shell, seat, new_index);
5415}
5416
5417static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005418workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005419 uint32_t key, void *data)
5420{
5421 struct desktop_shell *shell = data;
5422 unsigned int new_index = shell->workspaces.current;
5423
5424 if (shell->locked)
5425 return;
5426
5427 if (new_index < shell->workspaces.num - 1)
5428 new_index++;
5429
5430 take_surface_to_workspace_by_seat(shell, seat, new_index);
5431}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005432
5433static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005434handle_output_destroy(struct wl_listener *listener, void *data)
5435{
5436 struct shell_output *output_listener =
5437 container_of(listener, struct shell_output, destroy_listener);
5438
5439 wl_list_remove(&output_listener->destroy_listener.link);
5440 wl_list_remove(&output_listener->link);
5441 free(output_listener);
5442}
5443
5444static void
5445create_shell_output(struct desktop_shell *shell,
5446 struct weston_output *output)
5447{
5448 struct shell_output *shell_output;
5449
5450 shell_output = zalloc(sizeof *shell_output);
5451 if (shell_output == NULL)
5452 return;
5453
5454 shell_output->output = output;
5455 shell_output->shell = shell;
5456 shell_output->destroy_listener.notify = handle_output_destroy;
5457 wl_signal_add(&output->destroy_signal,
5458 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005459 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005460}
5461
5462static void
5463handle_output_create(struct wl_listener *listener, void *data)
5464{
5465 struct desktop_shell *shell =
5466 container_of(listener, struct desktop_shell, output_create_listener);
5467 struct weston_output *output = (struct weston_output *)data;
5468
5469 create_shell_output(shell, output);
5470}
5471
5472static void
5473setup_output_destroy_handler(struct weston_compositor *ec,
5474 struct desktop_shell *shell)
5475{
5476 struct weston_output *output;
5477
5478 wl_list_init(&shell->output_list);
5479 wl_list_for_each(output, &ec->output_list, link)
5480 create_shell_output(shell, output);
5481
5482 shell->output_create_listener.notify = handle_output_create;
5483 wl_signal_add(&ec->output_created_signal,
5484 &shell->output_create_listener);
5485}
5486
5487static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005488shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005489{
Tiago Vignattibe143262012-04-16 17:31:41 +03005490 struct desktop_shell *shell =
5491 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005492 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005493 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005494
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005495 /* Force state to unlocked so we don't try to fade */
5496 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005497 if (shell->child.client)
5498 wl_client_destroy(shell->child.client);
5499
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005500 wl_list_remove(&shell->idle_listener.link);
5501 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005502
5503 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005504
Xiong Zhang6b481422013-10-23 13:58:32 +08005505 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5506 wl_list_remove(&shell_output->destroy_listener.link);
5507 wl_list_remove(&shell_output->link);
5508 free(shell_output);
5509 }
5510
5511 wl_list_remove(&shell->output_create_listener.link);
5512
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005513 wl_array_for_each(ws, &shell->workspaces.array)
5514 workspace_destroy(*ws);
5515 wl_array_release(&shell->workspaces.array);
5516
Pekka Paalanen3c647232011-12-22 13:43:43 +02005517 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005518 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005519 free(shell);
5520}
5521
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005522static void
5523shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5524{
5525 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005526 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005527
5528 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005529 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5530 MODIFIER_CTRL | MODIFIER_ALT,
5531 terminate_binding, ec);
5532 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5533 click_to_activate_binding,
5534 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005535 weston_compositor_add_touch_binding(ec, 0,
5536 touch_to_activate_binding,
5537 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005538 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5539 MODIFIER_SUPER | MODIFIER_ALT,
5540 surface_opacity_binding, NULL);
5541 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5542 MODIFIER_SUPER, zoom_axis_binding,
5543 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005544
5545 /* configurable bindings */
5546 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005547 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5548 zoom_key_binding, NULL);
5549 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5550 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005551 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5552 maximize_binding, NULL);
5553 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5554 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005555 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5556 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005557 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005558 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5559 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005560 weston_compositor_add_button_binding(ec, BTN_LEFT,
5561 mod | MODIFIER_SHIFT,
5562 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005563
5564 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5565 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5566 rotate_binding, NULL);
5567
Daniel Stone325fc2d2012-05-30 16:31:58 +01005568 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5569 shell);
5570 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5571 ec);
5572 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5573 backlight_binding, ec);
5574 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5575 ec);
5576 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5577 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005578 weston_compositor_add_key_binding(ec, KEY_K, mod,
5579 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005580 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5581 workspace_up_binding, shell);
5582 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5583 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005584 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5585 workspace_move_surface_up_binding,
5586 shell);
5587 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5588 workspace_move_surface_down_binding,
5589 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005590
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005591 if (shell->exposay_modifier)
5592 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5593 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005594
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005595 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5596 if (shell->workspaces.num > 1) {
5597 num_workspace_bindings = shell->workspaces.num;
5598 if (num_workspace_bindings > 6)
5599 num_workspace_bindings = 6;
5600 for (i = 0; i < num_workspace_bindings; i++)
5601 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5602 workspace_f_binding,
5603 shell);
5604 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005605
5606 /* Debug bindings */
5607 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5608 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005609}
5610
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005611WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005612module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005613 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005614{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005615 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005616 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005617 struct workspace **pws;
5618 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005619 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005620
Peter Huttererf3d62272013-08-08 11:57:05 +10005621 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005622 if (shell == NULL)
5623 return -1;
5624
Kristian Høgsberg75840622011-09-06 13:48:16 -04005625 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005626
5627 shell->destroy_listener.notify = shell_destroy;
5628 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005629 shell->idle_listener.notify = idle_handler;
5630 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5631 shell->wake_listener.notify = wake_handler;
5632 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005633
Scott Moreauff1db4a2012-04-17 19:06:18 -06005634 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005635 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005636 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005638 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005639 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005640 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005641 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005642 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005643 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005644 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005645
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005646 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5647 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005648 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5649 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005650 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005651
5652 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005653 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005654
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005655 if (input_panel_setup(shell) < 0)
5656 return -1;
5657
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005658 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005659
Daniel Stonedf8133b2013-11-19 11:37:14 +01005660 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5661 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5662
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005663 for (i = 0; i < shell->workspaces.num; i++) {
5664 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5665 if (pws == NULL)
5666 return -1;
5667
5668 *pws = workspace_create();
5669 if (*pws == NULL)
5670 return -1;
5671 }
5672 activate_workspace(shell, 0);
5673
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005674 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005675 wl_list_init(&shell->workspaces.animation.link);
5676 shell->workspaces.animation.frame = animate_workspace_change_frame;
5677
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005678 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005679 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005680 return -1;
5681
Rafael Antognollie2a34552013-12-03 15:35:45 -02005682 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5683 shell, bind_xdg_shell) == NULL)
5684 return -1;
5685
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005686 if (wl_global_create(ec->wl_display,
5687 &desktop_shell_interface, 2,
5688 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005689 return -1;
5690
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005691 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5692 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005693 return -1;
5694
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005695 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5696 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005697 return -1;
5698
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005699 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005700
Xiong Zhang6b481422013-10-23 13:58:32 +08005701 setup_output_destroy_handler(ec, shell);
5702
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005703 loop = wl_display_get_event_loop(ec->wl_display);
5704 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005705
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005706 shell->screensaver.timer =
5707 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5708
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005709 wl_list_for_each(seat, &ec->seat_list, link)
5710 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005711
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005712 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005713
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005714 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005715
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005716 return 0;
5717}