blob: 7d7563feb69d3a6fa9d4e7dfeb8a0865ce9ecbf0 [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;
359
360 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øgsberg2f5faff2012-07-31 16:36:34 -0400671restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200672{
673 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400674 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200675
676 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800677 if (state->seat->keyboard == NULL)
678 continue;
679
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400680 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200681
Kristian Høgsberge3148752013-05-06 23:19:49 -0400682 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200683 }
684}
685
686static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200687replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
688 struct weston_seat *seat)
689{
690 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400691 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200692
693 wl_list_for_each(state, &ws->focus_list, link) {
694 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400695 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500696 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200697 return;
698 }
699 }
700}
701
702static void
703drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
704 struct weston_surface *surface)
705{
706 struct focus_state *state;
707
708 wl_list_for_each(state, &ws->focus_list, link)
709 if (state->keyboard_focus == surface)
710 state->keyboard_focus = NULL;
711}
712
713static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100714animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
715 struct weston_view *from, struct weston_view *to)
716{
717 struct weston_output *output;
718 bool focus_surface_created = false;
719
720 /* FIXME: Only support dim animation using two layers */
721 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
722 return;
723
724 output = get_default_output(shell->compositor);
725 if (ws->fsurf_front == NULL && (from || to)) {
726 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800727 if (ws->fsurf_front == NULL)
728 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800730
731 ws->fsurf_back = create_focus_surface(shell->compositor, output);
732 if (ws->fsurf_back == NULL) {
733 focus_surface_destroy(ws->fsurf_front);
734 return;
735 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100736 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800737
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738 focus_surface_created = true;
739 } else {
740 wl_list_remove(&ws->fsurf_front->view->layer_link);
741 wl_list_remove(&ws->fsurf_back->view->layer_link);
742 }
743
744 if (ws->focus_animation) {
745 weston_view_animation_destroy(ws->focus_animation);
746 ws->focus_animation = NULL;
747 }
748
749 if (to)
750 wl_list_insert(&to->layer_link,
751 &ws->fsurf_front->view->layer_link);
752 else if (from)
753 wl_list_insert(&ws->layer.view_list,
754 &ws->fsurf_front->view->layer_link);
755
756 if (focus_surface_created) {
757 ws->focus_animation = weston_fade_run(
758 ws->fsurf_front->view,
759 ws->fsurf_front->view->alpha, 0.6, 300,
760 focus_animation_done, ws);
761 } else if (from) {
762 wl_list_insert(&from->layer_link,
763 &ws->fsurf_back->view->layer_link);
764 ws->focus_animation = weston_stable_fade_run(
765 ws->fsurf_front->view, 0.0,
766 ws->fsurf_back->view, 0.6,
767 focus_animation_done, ws);
768 } else if (to) {
769 wl_list_insert(&ws->layer.view_list,
770 &ws->fsurf_back->view->layer_link);
771 ws->focus_animation = weston_stable_fade_run(
772 ws->fsurf_front->view, 0.0,
773 ws->fsurf_back->view, 0.6,
774 focus_animation_done, ws);
775 }
776}
777
778static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200779workspace_destroy(struct workspace *ws)
780{
Jonas Ådahl04769742012-06-13 00:01:24 +0200781 struct focus_state *state, *next;
782
783 wl_list_for_each_safe(state, next, &ws->focus_list, link)
784 focus_state_destroy(state);
785
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100786 if (ws->fsurf_front)
787 focus_surface_destroy(ws->fsurf_front);
788 if (ws->fsurf_back)
789 focus_surface_destroy(ws->fsurf_back);
790
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200791 free(ws);
792}
793
Jonas Ådahl04769742012-06-13 00:01:24 +0200794static void
795seat_destroyed(struct wl_listener *listener, void *data)
796{
797 struct weston_seat *seat = data;
798 struct focus_state *state, *next;
799 struct workspace *ws = container_of(listener,
800 struct workspace,
801 seat_destroyed_listener);
802
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 if (state->seat == seat)
805 wl_list_remove(&state->link);
806}
807
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200808static struct workspace *
809workspace_create(void)
810{
811 struct workspace *ws = malloc(sizeof *ws);
812 if (ws == NULL)
813 return NULL;
814
815 weston_layer_init(&ws->layer, NULL);
816
Jonas Ådahl04769742012-06-13 00:01:24 +0200817 wl_list_init(&ws->focus_list);
818 wl_list_init(&ws->seat_destroyed_listener.link);
819 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100820 ws->fsurf_front = NULL;
821 ws->fsurf_back = NULL;
822 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824 return ws;
825}
826
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200827static int
828workspace_is_empty(struct workspace *ws)
829{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500830 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200831}
832
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200833static struct workspace *
834get_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200837 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838 pws += index;
839 return *pws;
840}
841
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800842struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200843get_current_workspace(struct desktop_shell *shell)
844{
845 return get_workspace(shell, shell->workspaces.current);
846}
847
848static void
849activate_workspace(struct desktop_shell *shell, unsigned int index)
850{
851 struct workspace *ws;
852
853 ws = get_workspace(shell, index);
854 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
855
856 shell->workspaces.current = index;
857}
858
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200859static unsigned int
860get_output_height(struct weston_output *output)
861{
862 return abs(output->region.extents.y1 - output->region.extents.y2);
863}
864
865static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200867{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 struct weston_transform *transform;
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 if (is_focus_view(view)) {
871 struct focus_surface *fsurf = get_focus_surface(view->surface);
872 transform = &fsurf->workspace_transform;
873 } else {
874 struct shell_surface *shsurf = get_shell_surface(view->surface);
875 transform = &shsurf->workspace_transform;
876 }
877
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200878 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100880 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 weston_matrix_init(&transform->matrix);
883 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200886}
887
888static void
889workspace_translate_out(struct workspace *ws, double fraction)
890{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200892 unsigned int height;
893 double d;
894
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
896 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897 d = height * fraction;
898
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100899 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200900 }
901}
902
903static void
904workspace_translate_in(struct workspace *ws, double fraction)
905{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200907 unsigned int height;
908 double d;
909
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
911 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912
913 if (fraction > 0)
914 d = -(height - height * fraction);
915 else
916 d = height + height * fraction;
917
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200919 }
920}
921
922static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200923broadcast_current_workspace_state(struct desktop_shell *shell)
924{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700925 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200926
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700927 wl_resource_for_each(resource, &shell->workspaces.client_list)
928 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200929 shell->workspaces.current,
930 shell->workspaces.num);
931}
932
933static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934reverse_workspace_change_animation(struct desktop_shell *shell,
935 unsigned int index,
936 struct workspace *from,
937 struct workspace *to)
938{
939 shell->workspaces.current = index;
940
941 shell->workspaces.anim_to = to;
942 shell->workspaces.anim_from = from;
943 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
944 shell->workspaces.anim_timestamp = 0;
945
Scott Moreau4272e632012-08-13 09:58:41 -0600946 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947}
948
949static void
950workspace_deactivate_transforms(struct workspace *ws)
951{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100956 if (is_focus_view(view)) {
957 struct focus_surface *fsurf = get_focus_surface(view->surface);
958 transform = &fsurf->workspace_transform;
959 } else {
960 struct shell_surface *shsurf = get_shell_surface(view->surface);
961 transform = &shsurf->workspace_transform;
962 }
963
964 if (!wl_list_empty(&transform->link)) {
965 wl_list_remove(&transform->link);
966 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200967 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200969 }
970}
971
972static void
973finish_workspace_change_animation(struct desktop_shell *shell,
974 struct workspace *from,
975 struct workspace *to)
976{
Scott Moreau4272e632012-08-13 09:58:41 -0600977 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
979 wl_list_remove(&shell->workspaces.animation.link);
980 workspace_deactivate_transforms(from);
981 workspace_deactivate_transforms(to);
982 shell->workspaces.anim_to = NULL;
983
984 wl_list_remove(&shell->workspaces.anim_from->layer.link);
985}
986
987static void
988animate_workspace_change_frame(struct weston_animation *animation,
989 struct weston_output *output, uint32_t msecs)
990{
991 struct desktop_shell *shell =
992 container_of(animation, struct desktop_shell,
993 workspaces.animation);
994 struct workspace *from = shell->workspaces.anim_from;
995 struct workspace *to = shell->workspaces.anim_to;
996 uint32_t t;
997 double x, y;
998
999 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1000 finish_workspace_change_animation(shell, from, to);
1001 return;
1002 }
1003
1004 if (shell->workspaces.anim_timestamp == 0) {
1005 if (shell->workspaces.anim_current == 0.0)
1006 shell->workspaces.anim_timestamp = msecs;
1007 else
1008 shell->workspaces.anim_timestamp =
1009 msecs -
1010 /* Invers of movement function 'y' below. */
1011 (asin(1.0 - shell->workspaces.anim_current) *
1012 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1013 M_2_PI);
1014 }
1015
1016 t = msecs - shell->workspaces.anim_timestamp;
1017
1018 /*
1019 * x = [0, π/2]
1020 * y(x) = sin(x)
1021 */
1022 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1023 y = sin(x);
1024
1025 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001026 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027
1028 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1029 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1030 shell->workspaces.anim_current = y;
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001034 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036}
1037
1038static void
1039animate_workspace_change(struct desktop_shell *shell,
1040 unsigned int index,
1041 struct workspace *from,
1042 struct workspace *to)
1043{
1044 struct weston_output *output;
1045
1046 int dir;
1047
1048 if (index > shell->workspaces.current)
1049 dir = -1;
1050 else
1051 dir = 1;
1052
1053 shell->workspaces.current = index;
1054
1055 shell->workspaces.anim_dir = dir;
1056 shell->workspaces.anim_from = from;
1057 shell->workspaces.anim_to = to;
1058 shell->workspaces.anim_current = 0.0;
1059 shell->workspaces.anim_timestamp = 0;
1060
1061 output = container_of(shell->compositor->output_list.next,
1062 struct weston_output, link);
1063 wl_list_insert(&output->animation_list,
1064 &shell->workspaces.animation.link);
1065
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001066 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067
1068 workspace_translate_in(to, 0);
1069
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001070 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001071
Scott Moreau4272e632012-08-13 09:58:41 -06001072 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073}
1074
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001075static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001076update_workspace(struct desktop_shell *shell, unsigned int index,
1077 struct workspace *from, struct workspace *to)
1078{
1079 shell->workspaces.current = index;
1080 wl_list_insert(&from->layer.link, &to->layer.link);
1081 wl_list_remove(&from->layer.link);
1082}
1083
1084static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001085change_workspace(struct desktop_shell *shell, unsigned int index)
1086{
1087 struct workspace *from;
1088 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001089 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001090
1091 if (index == shell->workspaces.current)
1092 return;
1093
1094 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001096 return;
1097
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001098 from = get_current_workspace(shell);
1099 to = get_workspace(shell, index);
1100
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 if (shell->workspaces.anim_from == to &&
1102 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001103 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001105 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 return;
1107 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001108
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109 if (shell->workspaces.anim_to != NULL)
1110 finish_workspace_change_animation(shell,
1111 shell->workspaces.anim_from,
1112 shell->workspaces.anim_to);
1113
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001114 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001115
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001116 if (shell->focus_animation_type != ANIMATION_NONE) {
1117 wl_list_for_each(state, &from->focus_list, link)
1118 if (state->keyboard_focus)
1119 animate_focus_change(shell, from,
1120 get_default_view(state->keyboard_focus), NULL);
1121
1122 wl_list_for_each(state, &to->focus_list, link)
1123 if (state->keyboard_focus)
1124 animate_focus_change(shell, to,
1125 NULL, get_default_view(state->keyboard_focus));
1126 }
1127
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001128 if (workspace_is_empty(to) && workspace_is_empty(from))
1129 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 else
1131 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001132
1133 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001134}
1135
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001136static bool
1137workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1138{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001140 struct wl_list *e;
1141
1142 if (wl_list_empty(list))
1143 return false;
1144
1145 e = list->next;
1146
1147 if (e->next != list)
1148 return false;
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001151}
1152
1153static void
Philip Withnall659163d2013-11-25 18:01:36 +00001154move_surface_to_workspace(struct desktop_shell *shell,
1155 struct shell_surface *shsurf,
1156 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001157{
1158 struct workspace *from;
1159 struct workspace *to;
1160 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001161 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001162 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001163
1164 if (workspace == shell->workspaces.current)
1165 return;
1166
Philip Withnall659163d2013-11-25 18:01:36 +00001167 view = get_default_view(shsurf->surface);
1168 if (!view)
1169 return;
1170
1171 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1172
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001173 if (workspace >= shell->workspaces.num)
1174 workspace = shell->workspaces.num - 1;
1175
Jonas Ådahle9d22502012-08-29 22:13:01 +02001176 from = get_current_workspace(shell);
1177 to = get_workspace(shell, workspace);
1178
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 wl_list_remove(&view->layer_link);
1180 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001181
Philip Withnall648a4dd2013-11-25 18:01:44 +00001182 shell_surface_update_child_surface_layers(shsurf);
1183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001185 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1186 if (!seat->keyboard)
1187 continue;
1188
1189 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001191 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001192 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001193
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001195}
1196
1197static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001199 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200 unsigned int index)
1201{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001202 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 struct shell_surface *shsurf;
1205 struct workspace *from;
1206 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001207 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208
Pekka Paalanen01388e22013-04-25 13:57:44 +03001209 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 view = get_default_view(surface);
1211 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001212 index == shell->workspaces.current ||
1213 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 return;
1215
1216 from = get_current_workspace(shell);
1217 to = get_workspace(shell, index);
1218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 wl_list_remove(&view->layer_link);
1220 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221
Philip Withnall659163d2013-11-25 18:01:36 +00001222 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001223 if (shsurf != NULL)
1224 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001225
Jonas Ådahle9d22502012-08-29 22:13:01 +02001226 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227 drop_focus_state(shell, from, surface);
1228
1229 if (shell->workspaces.anim_from == to &&
1230 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001231 wl_list_remove(&to->layer.link);
1232 wl_list_insert(from->layer.link.prev, &to->layer.link);
1233
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001235 broadcast_current_workspace_state(shell);
1236
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001237 return;
1238 }
1239
1240 if (shell->workspaces.anim_to != NULL)
1241 finish_workspace_change_animation(shell,
1242 shell->workspaces.anim_from,
1243 shell->workspaces.anim_to);
1244
1245 if (workspace_is_empty(from) &&
1246 workspace_has_only(to, surface))
1247 update_workspace(shell, index, from, to);
1248 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001249 if (shsurf != NULL &&
1250 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251 wl_list_insert(&shell->workspaces.anim_sticky_list,
1252 &shsurf->workspace_transform.link);
1253
1254 animate_workspace_change(shell, index, from, to);
1255 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001256
1257 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001258
1259 state = ensure_focus_state(shell, seat);
1260 if (state != NULL)
1261 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001262}
1263
1264static void
1265workspace_manager_move_surface(struct wl_client *client,
1266 struct wl_resource *resource,
1267 struct wl_resource *surface_resource,
1268 uint32_t workspace)
1269{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001270 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001271 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001272 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001273 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001274 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275
Pekka Paalanen01388e22013-04-25 13:57:44 +03001276 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001277 shell_surface = get_shell_surface(main_surface);
1278 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001280
1281 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282}
1283
1284static const struct workspace_manager_interface workspace_manager_implementation = {
1285 workspace_manager_move_surface,
1286};
1287
1288static void
1289unbind_resource(struct wl_resource *resource)
1290{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001291 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001292}
1293
1294static void
1295bind_workspace_manager(struct wl_client *client,
1296 void *data, uint32_t version, uint32_t id)
1297{
1298 struct desktop_shell *shell = data;
1299 struct wl_resource *resource;
1300
Jason Ekstranda85118c2013-06-27 20:17:02 -05001301 resource = wl_resource_create(client,
1302 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303
1304 if (resource == NULL) {
1305 weston_log("couldn't add workspace manager object");
1306 return;
1307 }
1308
Jason Ekstranda85118c2013-06-27 20:17:02 -05001309 wl_resource_set_implementation(resource,
1310 &workspace_manager_implementation,
1311 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001312 wl_list_insert(&shell->workspaces.client_list,
1313 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001314
1315 workspace_manager_send_state(resource,
1316 shell->workspaces.current,
1317 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318}
1319
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001320static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001321touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1322 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1323{
1324}
1325
1326static void
1327touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1328{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001329 struct weston_touch_move_grab *move =
1330 (struct weston_touch_move_grab *) container_of(
1331 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001332
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001333 if (touch_id == 0)
1334 move->active = 0;
1335
Jonas Ådahl9484b692013-12-02 22:05:03 +01001336 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001337 shell_touch_grab_end(&move->base);
1338 free(move);
1339 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001340}
1341
1342static void
1343touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1344 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1345{
1346 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1347 struct shell_surface *shsurf = move->base.shsurf;
1348 struct weston_surface *es;
1349 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1350 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1351
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001352 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353 return;
1354
1355 es = shsurf->surface;
1356
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001357 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001358
1359 weston_compositor_schedule_repaint(es->compositor);
1360}
1361
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001362static void
1363touch_move_grab_cancel(struct weston_touch_grab *grab)
1364{
1365 struct weston_touch_move_grab *move =
1366 (struct weston_touch_move_grab *) container_of(
1367 grab, struct shell_touch_grab, grab);
1368
1369 shell_touch_grab_end(&move->base);
1370 free(move);
1371}
1372
Rusty Lynch1084da52013-08-15 09:10:08 -07001373static const struct weston_touch_grab_interface touch_move_grab_interface = {
1374 touch_move_grab_down,
1375 touch_move_grab_up,
1376 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001377 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001378};
1379
1380static int
1381surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1382{
1383 struct weston_touch_move_grab *move;
1384
1385 if (!shsurf)
1386 return -1;
1387
Rafael Antognolli03b16592013-12-03 15:35:42 -02001388 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001389 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001390 if (shsurf->grabbed)
1391 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001392
1393 move = malloc(sizeof *move);
1394 if (!move)
1395 return -1;
1396
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001397 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001399 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001401 seat->touch->grab_y;
1402
1403 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1404 seat->touch);
1405
1406 return 0;
1407}
1408
1409static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001410noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001411{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001412}
1413
1414static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001415move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1416 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001417{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001418 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001419 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001420 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001421 int dx, dy;
1422
1423 weston_pointer_move(pointer, x, y);
1424 dx = wl_fixed_to_int(pointer->x + move->dx);
1425 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001426
1427 if (!shsurf)
1428 return;
1429
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001430 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433}
1434
1435static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001436move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001437 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001438{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001439 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1440 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001441 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001442 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001443
Daniel Stone4dbadb12012-05-30 16:31:51 +01001444 if (pointer->button_count == 0 &&
1445 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001446 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001447 free(grab);
1448 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001449}
1450
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001451static void
1452move_grab_cancel(struct weston_pointer_grab *grab)
1453{
1454 struct shell_grab *shell_grab =
1455 container_of(grab, struct shell_grab, grab);
1456
1457 shell_grab_end(shell_grab);
1458 free(grab);
1459}
1460
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001461static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001462 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001463 move_grab_motion,
1464 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001465 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001466};
1467
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001468static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001469surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001470{
1471 struct weston_move_grab *move;
1472
1473 if (!shsurf)
1474 return -1;
1475
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001476 if (shsurf->grabbed)
1477 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001478 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001479 return 0;
1480
1481 move = malloc(sizeof *move);
1482 if (!move)
1483 return -1;
1484
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001486 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001488 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001489
1490 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001491 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001492
1493 return 0;
1494}
1495
1496static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001497common_surface_move(struct wl_resource *resource,
1498 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001499{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001500 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001501 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001502 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001503
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001504 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001505 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001506 seat->pointer->button_count > 0 &&
1507 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001509 if ((surface == shsurf->surface) &&
1510 (surface_move(shsurf, seat) < 0))
1511 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001512 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001513 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001514 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001516 if ((surface == shsurf->surface) &&
1517 (surface_touch_move(shsurf, seat) < 0))
1518 wl_resource_post_no_memory(resource);
1519 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001520}
1521
Rafael Antognollie2a34552013-12-03 15:35:45 -02001522static void
1523shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1524 struct wl_resource *seat_resource, uint32_t serial)
1525{
1526 common_surface_move(resource, seat_resource, serial);
1527}
1528
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001529struct weston_resize_grab {
1530 struct shell_grab base;
1531 uint32_t edges;
1532 int32_t width, height;
1533};
1534
1535static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001536resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1537 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538{
1539 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001540 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541 struct shell_surface *shsurf = resize->base.shsurf;
1542 int32_t width, height;
1543 wl_fixed_t from_x, from_y;
1544 wl_fixed_t to_x, to_y;
1545
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001546 weston_pointer_move(pointer, x, y);
1547
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548 if (!shsurf)
1549 return;
1550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 weston_view_from_global_fixed(shsurf->view,
1552 pointer->grab_x, pointer->grab_y,
1553 &from_x, &from_y);
1554 weston_view_from_global_fixed(shsurf->view,
1555 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556
1557 width = resize->width;
1558 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1559 width += wl_fixed_to_int(from_x - to_x);
1560 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1561 width += wl_fixed_to_int(to_x - from_x);
1562 }
1563
1564 height = resize->height;
1565 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1566 height += wl_fixed_to_int(from_y - to_y);
1567 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1568 height += wl_fixed_to_int(to_y - from_y);
1569 }
1570
1571 shsurf->client->send_configure(shsurf->surface,
1572 resize->edges, width, height);
1573}
1574
1575static void
1576send_configure(struct weston_surface *surface,
1577 uint32_t edges, int32_t width, int32_t height)
1578{
1579 struct shell_surface *shsurf = get_shell_surface(surface);
1580
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001581 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582 edges, width, height);
1583}
1584
1585static const struct weston_shell_client shell_client = {
1586 send_configure
1587};
1588
1589static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001590resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591 uint32_t time, uint32_t button, uint32_t state_w)
1592{
1593 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001594 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595 enum wl_pointer_button_state state = state_w;
1596
1597 if (pointer->button_count == 0 &&
1598 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1599 shell_grab_end(&resize->base);
1600 free(grab);
1601 }
1602}
1603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604static void
1605resize_grab_cancel(struct weston_pointer_grab *grab)
1606{
1607 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1608
1609 shell_grab_end(&resize->base);
1610 free(grab);
1611}
1612
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001613static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001614 noop_grab_focus,
1615 resize_grab_motion,
1616 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001617 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001618};
1619
Giulio Camuffob8366642013-04-25 13:57:46 +03001620/*
1621 * Returns the bounding box of a surface and all its sub-surfaces,
1622 * in the surface coordinates system. */
1623static void
1624surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1625 int32_t *y, int32_t *w, int32_t *h) {
1626 pixman_region32_t region;
1627 pixman_box32_t *box;
1628 struct weston_subsurface *subsurface;
1629
1630 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 surface->width,
1632 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001633
1634 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1635 pixman_region32_union_rect(&region, &region,
1636 subsurface->position.x,
1637 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 subsurface->surface->width,
1639 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001640 }
1641
1642 box = pixman_region32_extents(&region);
1643 if (x)
1644 *x = box->x1;
1645 if (y)
1646 *y = box->y1;
1647 if (w)
1648 *w = box->x2 - box->x1;
1649 if (h)
1650 *h = box->y2 - box->y1;
1651
1652 pixman_region32_fini(&region);
1653}
1654
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655static int
1656surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001657 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658{
1659 struct weston_resize_grab *resize;
1660
Rafael Antognolli03b16592013-12-03 15:35:42 -02001661 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662 return 0;
1663
1664 if (edges == 0 || edges > 15 ||
1665 (edges & 3) == 3 || (edges & 12) == 12)
1666 return 0;
1667
1668 resize = malloc(sizeof *resize);
1669 if (!resize)
1670 return -1;
1671
1672 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001673 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1674 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675
1676 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001677 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678
1679 return 0;
1680}
1681
1682static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001683common_surface_resize(struct wl_resource *resource,
1684 struct wl_resource *seat_resource, uint32_t serial,
1685 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001686{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001687 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001688 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001689 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001690
Rafael Antognolli03b16592013-12-03 15:35:42 -02001691 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692 return;
1693
Kristian Høgsberge3148752013-05-06 23:19:49 -04001694 if (seat->pointer->button_count == 0 ||
1695 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001696 seat->pointer->focus == NULL)
1697 return;
1698
1699 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1700 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701 return;
1702
Kristian Høgsberge3148752013-05-06 23:19:49 -04001703 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704 wl_resource_post_no_memory(resource);
1705}
1706
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001707static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001708shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1709 struct wl_resource *seat_resource, uint32_t serial,
1710 uint32_t edges)
1711{
1712 common_surface_resize(resource, seat_resource, serial, edges);
1713}
1714
1715static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001716end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1717
1718static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001719busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001720{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001721 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001722 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001724 wl_fixed_t sx, sy;
1725
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 view = weston_compositor_pick_view(pointer->seat->compositor,
1727 pointer->x, pointer->y,
1728 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001731 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001732}
1733
1734static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001735busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1736 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001737{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001738 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001742busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743 uint32_t time, uint32_t button, uint32_t state)
1744{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001745 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001746 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001747 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001748
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001749 if (shsurf && button == BTN_LEFT && state) {
1750 activate(shsurf->shell, shsurf->surface, seat);
1751 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001752 } else if (shsurf && button == BTN_RIGHT && state) {
1753 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001754 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001755 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756}
1757
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001758static void
1759busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1760{
1761 struct shell_grab *grab = (struct shell_grab *) base;
1762
1763 shell_grab_end(grab);
1764 free(grab);
1765}
1766
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001767static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768 busy_cursor_grab_focus,
1769 busy_cursor_grab_motion,
1770 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001771 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772};
1773
1774static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001775set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001776{
1777 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001778
1779 grab = malloc(sizeof *grab);
1780 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001781 return;
1782
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001783 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1784 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001786
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001788end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001789{
1790 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1791
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001792 if (grab->grab.interface == &busy_cursor_grab_interface &&
1793 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001794 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001796 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001797}
1798
Scott Moreau9521d5e2012-04-19 13:06:17 -06001799static void
1800ping_timer_destroy(struct shell_surface *shsurf)
1801{
1802 if (!shsurf || !shsurf->ping_timer)
1803 return;
1804
1805 if (shsurf->ping_timer->source)
1806 wl_event_source_remove(shsurf->ping_timer->source);
1807
1808 free(shsurf->ping_timer);
1809 shsurf->ping_timer = NULL;
1810}
1811
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001812static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001813ping_timeout_handler(void *data)
1814{
1815 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001817
Scott Moreau9521d5e2012-04-19 13:06:17 -06001818 /* Client is not responding */
1819 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820
1821 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001822 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001823 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001824 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001825
1826 return 1;
1827}
1828
1829static void
1830ping_handler(struct weston_surface *surface, uint32_t serial)
1831{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001832 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001833 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001835
1836 if (!shsurf)
1837 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001838 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001839 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001840
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001841 if (shsurf->surface == shsurf->shell->grab_surface)
1842 return;
1843
Scott Moreauff1db4a2012-04-17 19:06:18 -06001844 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001845 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001846 if (!shsurf->ping_timer)
1847 return;
1848
1849 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001850 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1851 shsurf->ping_timer->source =
1852 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1853 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1854
Rafael Antognollie2a34552013-12-03 15:35:45 -02001855 if (shell_surface_is_wl_shell_surface(shsurf))
1856 wl_shell_surface_send_ping(shsurf->resource, serial);
1857 else if (shell_surface_is_xdg_surface(shsurf))
1858 xdg_surface_send_ping(shsurf->resource, serial);
1859 else if (shell_surface_is_xdg_popup(shsurf))
1860 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001861 }
1862}
1863
1864static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001865handle_pointer_focus(struct wl_listener *listener, void *data)
1866{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001867 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001869 struct weston_compositor *compositor;
1870 struct shell_surface *shsurf;
1871 uint32_t serial;
1872
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001874 return;
1875
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 compositor = view->surface->compositor;
1877 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001878
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001879 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880 set_busy_cursor(shsurf, pointer);
1881 } else {
1882 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884 }
1885}
1886
1887static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001888create_pointer_focus_listener(struct weston_seat *seat)
1889{
1890 struct wl_listener *listener;
1891
Kristian Høgsberge3148752013-05-06 23:19:49 -04001892 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001893 return;
1894
1895 listener = malloc(sizeof *listener);
1896 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001897 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001898}
1899
1900static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001901xdg_surface_set_transient_for(struct wl_client *client,
1902 struct wl_resource *resource,
1903 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001904{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001905 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001906 struct weston_surface *parent;
1907
1908 if (parent_resource)
1909 parent = wl_resource_get_user_data(parent_resource);
1910 else
1911 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001912
1913 shell_surface_set_parent(shsurf, parent);
1914}
1915
1916static void
1917surface_pong(struct shell_surface *shsurf, uint32_t serial)
1918{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001920 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001921
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001922 if (shsurf->ping_timer == NULL)
1923 /* Just ignore unsolicited pong. */
1924 return;
1925
Scott Moreauff1db4a2012-04-17 19:06:18 -06001926 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001927 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001928 wl_list_for_each(seat, &ec->seat_list, link) {
1929 if(seat->pointer)
1930 end_busy_cursor(shsurf, seat->pointer);
1931 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001932 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001933 }
1934}
1935
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001936static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001937shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1938 uint32_t serial)
1939{
1940 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1941
1942 surface_pong(shsurf, serial);
1943
1944}
1945
1946static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001947set_title(struct shell_surface *shsurf, const char *title)
1948{
1949 free(shsurf->title);
1950 shsurf->title = strdup(title);
1951}
1952
1953static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001954shell_surface_set_title(struct wl_client *client,
1955 struct wl_resource *resource, const char *title)
1956{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001957 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001958
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001959 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001960}
1961
1962static void
1963shell_surface_set_class(struct wl_client *client,
1964 struct wl_resource *resource, const char *class)
1965{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001966 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001967
1968 free(shsurf->class);
1969 shsurf->class = strdup(class);
1970}
1971
Alex Wu4539b082012-03-01 12:57:46 +08001972static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001973restore_output_mode(struct weston_output *output)
1974{
Hardening57388e42013-09-18 23:56:36 +02001975 if (output->current_mode != output->original_mode ||
1976 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001977 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001978 output->original_mode,
1979 output->original_scale,
1980 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001981}
1982
1983static void
1984restore_all_output_modes(struct weston_compositor *compositor)
1985{
1986 struct weston_output *output;
1987
1988 wl_list_for_each(output, &compositor->output_list, link)
1989 restore_output_mode(output);
1990}
1991
Philip Withnallbecb77e2013-11-25 18:01:30 +00001992static int
1993get_output_panel_height(struct desktop_shell *shell,
1994 struct weston_output *output)
1995{
1996 struct weston_view *view;
1997 int panel_height = 0;
1998
1999 if (!output)
2000 return 0;
2001
2002 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2003 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002004 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002005 break;
2006 }
2007 }
2008
2009 return panel_height;
2010}
2011
Philip Withnall07926d92013-11-25 18:01:40 +00002012/* The surface will be inserted into the list immediately after the link
2013 * returned by this function (i.e. will be stacked immediately above the
2014 * returned link). */
2015static struct wl_list *
2016shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2017{
2018 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002019 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002020
2021 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002022 case SHELL_SURFACE_POPUP:
2023 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002024 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002025 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002026 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002027 /* Move the surface to its parent layer so
2028 * that surfaces which are transient for
2029 * fullscreen surfaces don't get hidden by the
2030 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002031
2032 /* TODO: Handle a parent with multiple views */
2033 parent = get_default_view(shsurf->parent);
2034 if (parent)
2035 return parent->layer_link.prev;
2036 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002037 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002038
2039 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002040 return &shsurf->shell->fullscreen_layer.view_list;
2041
Philip Withnall07926d92013-11-25 18:01:40 +00002042 case SHELL_SURFACE_NONE:
2043 default:
2044 /* Go to the fallback, below. */
2045 break;
2046 }
2047
2048 /* Move the surface to a normal workspace layer so that surfaces
2049 * which were previously fullscreen or transient are no longer
2050 * rendered on top. */
2051 ws = get_current_workspace(shsurf->shell);
2052 return &ws->layer.view_list;
2053}
2054
Philip Withnall648a4dd2013-11-25 18:01:44 +00002055static void
2056shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2057{
2058 struct shell_surface *child;
2059
2060 /* Move the child layers to the same workspace as shsurf. They will be
2061 * stacked above shsurf. */
2062 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2063 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2064 weston_view_geometry_dirty(child->view);
2065 wl_list_remove(&child->view->layer_link);
2066 wl_list_insert(shsurf->view->layer_link.prev,
2067 &child->view->layer_link);
2068 weston_view_geometry_dirty(child->view);
2069 weston_surface_damage(child->surface);
2070
2071 /* Recurse. We don’t expect this to recurse very far (if
2072 * at all) because that would imply we have transient
2073 * (or popup) children of transient surfaces, which
2074 * would be unusual. */
2075 shell_surface_update_child_surface_layers(child);
2076 }
2077 }
2078}
2079
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002080/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002081 * geometry to ensure the changes are redrawn.
2082 *
2083 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2084 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002085static void
2086shell_surface_update_layer(struct shell_surface *shsurf)
2087{
2088 struct wl_list *new_layer_link;
2089
2090 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2091
2092 if (new_layer_link == &shsurf->view->layer_link)
2093 return;
2094
2095 weston_view_geometry_dirty(shsurf->view);
2096 wl_list_remove(&shsurf->view->layer_link);
2097 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2098 weston_view_geometry_dirty(shsurf->view);
2099 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002100
2101 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002102}
2103
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002104static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002105shell_surface_set_parent(struct shell_surface *shsurf,
2106 struct weston_surface *parent)
2107{
2108 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002109
2110 wl_list_remove(&shsurf->children_link);
2111 wl_list_init(&shsurf->children_link);
2112
2113 /* Insert into the parent surface’s child list. */
2114 if (parent != NULL) {
2115 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2116 if (parent_shsurf != NULL)
2117 wl_list_insert(&parent_shsurf->children_list,
2118 &shsurf->children_link);
2119 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002120}
2121
2122static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002123shell_surface_set_output(struct shell_surface *shsurf,
2124 struct weston_output *output)
2125{
2126 struct weston_surface *es = shsurf->surface;
2127
2128 /* get the default output, if the client set it as NULL
2129 check whether the ouput is available */
2130 if (output)
2131 shsurf->output = output;
2132 else if (es->output)
2133 shsurf->output = es->output;
2134 else
2135 shsurf->output = get_default_output(es->compositor);
2136}
2137
2138static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002139surface_clear_next_states(struct shell_surface *shsurf)
2140{
2141 shsurf->next_state.maximized = false;
2142 shsurf->next_state.fullscreen = false;
2143
2144 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2145 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2146 shsurf->state_changed = true;
2147}
2148
2149static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002150set_toplevel(struct shell_surface *shsurf)
2151{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002152 shell_surface_set_parent(shsurf, NULL);
2153 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002154 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002155
2156 /* The layer_link is updated in set_surface_type(),
2157 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002158}
2159
2160static void
2161shell_surface_set_toplevel(struct wl_client *client,
2162 struct wl_resource *resource)
2163{
2164 struct shell_surface *surface = wl_resource_get_user_data(resource);
2165
2166 set_toplevel(surface);
2167}
2168
2169static void
2170set_transient(struct shell_surface *shsurf,
2171 struct weston_surface *parent, int x, int y, uint32_t flags)
2172{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002173 assert(parent != NULL);
2174
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002175 shell_surface_set_parent(shsurf, parent);
2176
2177 surface_clear_next_states(shsurf);
2178
Philip Withnallbecb77e2013-11-25 18:01:30 +00002179 shsurf->transient.x = x;
2180 shsurf->transient.y = y;
2181 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002182
Rafael Antognollied207b42013-12-03 15:35:43 -02002183 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002184 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002185 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002186
2187 /* The layer_link is updated in set_surface_type(),
2188 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002189}
2190
2191static void
2192shell_surface_set_transient(struct wl_client *client,
2193 struct wl_resource *resource,
2194 struct wl_resource *parent_resource,
2195 int x, int y, uint32_t flags)
2196{
2197 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2198 struct weston_surface *parent =
2199 wl_resource_get_user_data(parent_resource);
2200
2201 set_transient(shsurf, parent, x, y, flags);
2202}
2203
2204static void
2205set_fullscreen(struct shell_surface *shsurf,
2206 uint32_t method,
2207 uint32_t framerate,
2208 struct weston_output *output)
2209{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002210 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002211
2212 shsurf->fullscreen_output = shsurf->output;
2213 shsurf->fullscreen.type = method;
2214 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002215
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002216 shsurf->next_state.fullscreen = true;
2217 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002218 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002219
2220 shsurf->client->send_configure(shsurf->surface, 0,
2221 shsurf->output->width,
2222 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002223
2224 /* The layer_link is updated in set_surface_type(),
2225 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002226}
2227
2228static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002229weston_view_set_initial_position(struct weston_view *view,
2230 struct desktop_shell *shell);
2231
2232static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002233unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002234{
Philip Withnallf85fe842013-11-25 18:01:37 +00002235 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002236 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2237 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002238 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002239 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002240 shsurf->fullscreen_output = NULL;
2241
Alex Wu4539b082012-03-01 12:57:46 +08002242 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2243 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002244
Alex Wu4539b082012-03-01 12:57:46 +08002245 wl_list_remove(&shsurf->fullscreen.transform.link);
2246 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002247
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 if (shsurf->fullscreen.black_view)
2249 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2250 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002251
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002252 if (shsurf->saved_position_valid)
2253 weston_view_set_position(shsurf->view,
2254 shsurf->saved_x, shsurf->saved_y);
2255 else
2256 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2257
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002258 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002260 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002261 shsurf->saved_rotation_valid = false;
2262 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002263
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002264 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002265}
2266
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002267static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002268shell_surface_set_fullscreen(struct wl_client *client,
2269 struct wl_resource *resource,
2270 uint32_t method,
2271 uint32_t framerate,
2272 struct wl_resource *output_resource)
2273{
2274 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2275 struct weston_output *output;
2276
2277 if (output_resource)
2278 output = wl_resource_get_user_data(output_resource);
2279 else
2280 output = NULL;
2281
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002282 shell_surface_set_parent(shsurf, NULL);
2283
Rafael Antognolli03b16592013-12-03 15:35:42 -02002284 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285 set_fullscreen(shsurf, method, framerate, output);
2286}
2287
2288static void
2289set_popup(struct shell_surface *shsurf,
2290 struct weston_surface *parent,
2291 struct weston_seat *seat,
2292 uint32_t serial,
2293 int32_t x,
2294 int32_t y)
2295{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002296 assert(parent != NULL);
2297
Philip Withnallbecb77e2013-11-25 18:01:30 +00002298 shsurf->popup.shseat = get_shell_seat(seat);
2299 shsurf->popup.serial = serial;
2300 shsurf->popup.x = x;
2301 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002302
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002303 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002304}
2305
2306static void
2307shell_surface_set_popup(struct wl_client *client,
2308 struct wl_resource *resource,
2309 struct wl_resource *seat_resource,
2310 uint32_t serial,
2311 struct wl_resource *parent_resource,
2312 int32_t x, int32_t y, uint32_t flags)
2313{
2314 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002315 struct weston_surface *parent =
2316 wl_resource_get_user_data(parent_resource);
2317
2318 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002319
Rafael Antognolli03b16592013-12-03 15:35:42 -02002320 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002321 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002322 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002323 wl_resource_get_user_data(seat_resource),
2324 serial, x, y);
2325}
2326
2327static void
2328set_maximized(struct shell_surface *shsurf,
2329 struct weston_output *output)
2330{
2331 struct desktop_shell *shell;
2332 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002333
Philip Withnall352e7ed2013-11-25 18:01:35 +00002334 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002335
2336 shell = shell_surface_get_shell(shsurf);
2337 panel_height = get_output_panel_height(shell, shsurf->output);
2338 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2339
2340 shsurf->client->send_configure(shsurf->surface, edges,
2341 shsurf->output->width,
2342 shsurf->output->height - panel_height);
2343
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002344 shsurf->next_state.maximized = true;
2345 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002346 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002347}
2348
2349static void
2350unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002351{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002352 /* undo all maximized things here */
2353 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002354
2355 if (shsurf->saved_position_valid)
2356 weston_view_set_position(shsurf->view,
2357 shsurf->saved_x, shsurf->saved_y);
2358 else
2359 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002360
2361 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 wl_list_insert(&shsurf->view->geometry.transformation_list,
2363 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002364 shsurf->saved_rotation_valid = false;
2365 }
2366
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002367 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002368}
2369
Philip Withnallbecb77e2013-11-25 18:01:30 +00002370static void
2371shell_surface_set_maximized(struct wl_client *client,
2372 struct wl_resource *resource,
2373 struct wl_resource *output_resource)
2374{
2375 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2376 struct weston_output *output;
2377
2378 if (output_resource)
2379 output = wl_resource_get_user_data(output_resource);
2380 else
2381 output = NULL;
2382
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002383 shell_surface_set_parent(shsurf, NULL);
2384
Rafael Antognolli03b16592013-12-03 15:35:42 -02002385 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002386 set_maximized(shsurf, output);
2387}
2388
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002389/* This is only ever called from set_surface_type(), so there’s no need to
2390 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002391static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002392reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002393{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002394 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002395 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002396 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002397 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002398
Pekka Paalanen98262232011-12-01 10:42:22 +02002399 return 0;
2400}
2401
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002402static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002403set_full_output(struct shell_surface *shsurf)
2404{
2405 shsurf->saved_x = shsurf->view->geometry.x;
2406 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002407 shsurf->saved_width = shsurf->surface->width;
2408 shsurf->saved_height = shsurf->surface->height;
2409 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002410 shsurf->saved_position_valid = true;
2411
2412 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2413 wl_list_remove(&shsurf->rotation.transform.link);
2414 wl_list_init(&shsurf->rotation.transform.link);
2415 weston_view_geometry_dirty(shsurf->view);
2416 shsurf->saved_rotation_valid = true;
2417 }
2418}
2419
2420static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002421set_surface_type(struct shell_surface *shsurf)
2422{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002423 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002424 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002425
Philip Withnallbecb77e2013-11-25 18:01:30 +00002426 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002427
Rafael Antognolli03b16592013-12-03 15:35:42 -02002428 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002429 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002430
2431 switch (shsurf->type) {
2432 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002433 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002434 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002435 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436 weston_view_set_position(shsurf->view,
2437 pev->geometry.x + shsurf->transient.x,
2438 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002439 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002440 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002441
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002442 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002443 weston_view_set_position(shsurf->view, shsurf->transient.x,
2444 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002445 break;
2446
Philip Withnall0f640e22013-11-25 18:01:31 +00002447 case SHELL_SURFACE_POPUP:
2448 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002449 default:
2450 break;
2451 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002452
2453 /* Update the surface’s layer. */
2454 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002455}
2456
Tiago Vignattibe143262012-04-16 17:31:41 +03002457static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002458shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002459{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002460 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002461}
2462
Alex Wu21858432012-04-01 20:13:08 +08002463static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002464black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002465
Jason Ekstranda7af7042013-10-12 22:38:11 -05002466static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002467create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002468 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002469 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002470{
2471 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002473
2474 surface = weston_surface_create(ec);
2475 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002476 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002477 return NULL;
2478 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479 view = weston_view_create(surface);
2480 if (surface == NULL) {
2481 weston_log("no memory\n");
2482 weston_surface_destroy(surface);
2483 return NULL;
2484 }
Alex Wu4539b082012-03-01 12:57:46 +08002485
Alex Wu21858432012-04-01 20:13:08 +08002486 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002487 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002488 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002489 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002490 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002491 pixman_region32_fini(&surface->input);
2492 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002493
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002494 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002495 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002496
2497 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002498}
2499
Philip Withnalled8f1a92013-11-25 18:01:43 +00002500static void
2501shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2502{
2503 struct weston_output *output = shsurf->fullscreen_output;
2504
Rafael Antognolli03b16592013-12-03 15:35:42 -02002505 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002506
2507 if (!shsurf->fullscreen.black_view)
2508 shsurf->fullscreen.black_view =
2509 create_black_surface(shsurf->surface->compositor,
2510 shsurf->surface,
2511 output->x, output->y,
2512 output->width,
2513 output->height);
2514
2515 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2516 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2517 wl_list_insert(&shsurf->view->layer_link,
2518 &shsurf->fullscreen.black_view->layer_link);
2519 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2520 weston_surface_damage(shsurf->surface);
2521}
2522
Alex Wu4539b082012-03-01 12:57:46 +08002523/* Create black surface and append it to the associated fullscreen surface.
2524 * Handle size dismatch and positioning according to the method. */
2525static void
2526shell_configure_fullscreen(struct shell_surface *shsurf)
2527{
2528 struct weston_output *output = shsurf->fullscreen_output;
2529 struct weston_surface *surface = shsurf->surface;
2530 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002531 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002532 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002533
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002534 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2535 restore_output_mode(output);
2536
Philip Withnalled8f1a92013-11-25 18:01:43 +00002537 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002538
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002540 &surf_width, &surf_height);
2541
Alex Wu4539b082012-03-01 12:57:46 +08002542 switch (shsurf->fullscreen.type) {
2543 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002544 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002546 break;
2547 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002548 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002549 if (output->width == surf_width &&
2550 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551 weston_view_set_position(shsurf->view,
2552 output->x - surf_x,
2553 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002554 break;
2555 }
2556
Alex Wu4539b082012-03-01 12:57:46 +08002557 matrix = &shsurf->fullscreen.transform.matrix;
2558 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002559
Scott Moreau1bad5db2012-08-18 01:04:05 -06002560 output_aspect = (float) output->width /
2561 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002562 /* XXX: Use surf_width and surf_height here? */
2563 surface_aspect = (float) surface->width /
2564 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002565 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002566 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002567 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002568 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002569 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002570 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002571
Alex Wu4539b082012-03-01 12:57:46 +08002572 weston_matrix_scale(matrix, scale, scale, 1);
2573 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002575 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002576 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2577 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002578 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002579
Alex Wu4539b082012-03-01 12:57:46 +08002580 break;
2581 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002582 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002583 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002584 surf_width * surface->buffer_viewport.scale,
2585 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002586 shsurf->fullscreen.framerate};
2587
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002588 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002589 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 weston_view_set_position(shsurf->view,
2591 output->x - surf_x,
2592 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002593 shsurf->fullscreen.black_view->surface->width = output->width;
2594 shsurf->fullscreen.black_view->surface->height = output->height;
2595 weston_view_set_position(shsurf->fullscreen.black_view,
2596 output->x - surf_x,
2597 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002598 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002599 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002600 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002601 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002602 }
Alex Wubd3354b2012-04-17 17:20:49 +08002603 }
Alex Wu4539b082012-03-01 12:57:46 +08002604 break;
2605 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002606 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002607 break;
2608 default:
2609 break;
2610 }
2611}
2612
Alex Wu4539b082012-03-01 12:57:46 +08002613static void
2614shell_map_fullscreen(struct shell_surface *shsurf)
2615{
Alex Wubd3354b2012-04-17 17:20:49 +08002616 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002617}
2618
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002619static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002620set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2621{
2622 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002623 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002624 shsurf->transient.x = x;
2625 shsurf->transient.y = y;
2626 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002627
2628 shell_surface_set_parent(shsurf, NULL);
2629
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002630 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002631 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002632}
2633
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002634static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002635
2636static void
2637destroy_shell_seat(struct wl_listener *listener, void *data)
2638{
2639 struct shell_seat *shseat =
2640 container_of(listener,
2641 struct shell_seat, seat_destroy_listener);
2642 struct shell_surface *shsurf, *prev = NULL;
2643
2644 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002645 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002646 shseat->popup_grab.client = NULL;
2647
2648 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2649 shsurf->popup.shseat = NULL;
2650 if (prev) {
2651 wl_list_init(&prev->popup.grab_link);
2652 }
2653 prev = shsurf;
2654 }
2655 wl_list_init(&prev->popup.grab_link);
2656 }
2657
2658 wl_list_remove(&shseat->seat_destroy_listener.link);
2659 free(shseat);
2660}
2661
2662static struct shell_seat *
2663create_shell_seat(struct weston_seat *seat)
2664{
2665 struct shell_seat *shseat;
2666
2667 shseat = calloc(1, sizeof *shseat);
2668 if (!shseat) {
2669 weston_log("no memory to allocate shell seat\n");
2670 return NULL;
2671 }
2672
2673 shseat->seat = seat;
2674 wl_list_init(&shseat->popup_grab.surfaces_list);
2675
2676 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2677 wl_signal_add(&seat->destroy_signal,
2678 &shseat->seat_destroy_listener);
2679
2680 return shseat;
2681}
2682
2683static struct shell_seat *
2684get_shell_seat(struct weston_seat *seat)
2685{
2686 struct wl_listener *listener;
2687
2688 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2689 if (listener == NULL)
2690 return create_shell_seat(seat);
2691
2692 return container_of(listener,
2693 struct shell_seat, seat_destroy_listener);
2694}
2695
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002696static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002697popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002698{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002699 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002700 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002701 struct shell_seat *shseat =
2702 container_of(grab, struct shell_seat, popup_grab.grab);
2703 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002704 wl_fixed_t sx, sy;
2705
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 view = weston_compositor_pick_view(pointer->seat->compositor,
2707 pointer->x, pointer->y,
2708 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002709
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 if (view && view->surface->resource &&
2711 wl_resource_get_client(view->surface->resource) == client) {
2712 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002714 weston_pointer_set_focus(pointer, NULL,
2715 wl_fixed_from_int(0),
2716 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002717 }
2718}
2719
2720static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002721popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2722 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002723{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002724 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002725 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002726 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002727
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002728 weston_pointer_move(pointer, x, y);
2729
Neil Roberts96d790e2013-09-19 17:32:00 +01002730 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 weston_view_from_global_fixed(pointer->focus,
2732 pointer->x, pointer->y,
2733 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002734 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002735 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002736}
2737
2738static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002739popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002741{
2742 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002743 struct shell_seat *shseat =
2744 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002745 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002746 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002747 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002748 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002749
Neil Roberts96d790e2013-09-19 17:32:00 +01002750 resource_list = &grab->pointer->focus_resource_list;
2751 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002752 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002753 wl_resource_for_each(resource, resource_list) {
2754 wl_pointer_send_button(resource, serial,
2755 time, button, state);
2756 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002757 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002758 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002759 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002760 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002761 }
2762
Daniel Stone4dbadb12012-05-30 16:31:51 +01002763 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002764 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002765}
2766
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002767static void
2768popup_grab_cancel(struct weston_pointer_grab *grab)
2769{
2770 popup_grab_end(grab->pointer);
2771}
2772
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002773static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002774 popup_grab_focus,
2775 popup_grab_motion,
2776 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002777 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002778};
2779
2780static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002781shell_surface_send_popup_done(struct shell_surface *shsurf)
2782{
2783 if (shell_surface_is_wl_shell_surface(shsurf))
2784 wl_shell_surface_send_popup_done(shsurf->resource);
2785 else if (shell_surface_is_xdg_popup(shsurf))
2786 xdg_popup_send_popup_done(shsurf->resource,
2787 shsurf->popup.serial);
2788}
2789
2790static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002791popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002792{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002793 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002794 struct shell_seat *shseat =
2795 container_of(grab, struct shell_seat, popup_grab.grab);
2796 struct shell_surface *shsurf;
2797 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002798
2799 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002800 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002801 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002802 shseat->popup_grab.grab.interface = NULL;
2803 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002804 /* Send the popup_done event to all the popups open */
2805 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002806 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002807 shsurf->popup.shseat = NULL;
2808 if (prev) {
2809 wl_list_init(&prev->popup.grab_link);
2810 }
2811 prev = shsurf;
2812 }
2813 wl_list_init(&prev->popup.grab_link);
2814 wl_list_init(&shseat->popup_grab.surfaces_list);
2815 }
2816}
2817
2818static void
2819add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2820{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002821 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002822
2823 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002824 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002825 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002826 /* We must make sure here that this popup was opened after
2827 * a mouse press, and not just by moving around with other
2828 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002829 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002830 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002831
Rob Bradforddfe31052013-06-26 19:49:11 +01002832 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002833 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002834 } else {
2835 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002836 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002837}
2838
2839static void
2840remove_popup_grab(struct shell_surface *shsurf)
2841{
2842 struct shell_seat *shseat = shsurf->popup.shseat;
2843
2844 wl_list_remove(&shsurf->popup.grab_link);
2845 wl_list_init(&shsurf->popup.grab_link);
2846 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002847 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002848 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002849 }
2850}
2851
2852static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002853shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002857
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 shsurf->surface->output = parent_view->output;
2859 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002860
Jason Ekstranda7af7042013-10-12 22:38:11 -05002861 weston_view_set_transform_parent(shsurf->view, parent_view);
2862 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2863 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002864
Kristian Høgsberge3148752013-05-06 23:19:49 -04002865 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002866 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002867 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002868 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002869 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002870 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002871}
2872
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002873static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002874 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002875 shell_surface_move,
2876 shell_surface_resize,
2877 shell_surface_set_toplevel,
2878 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002880 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002881 shell_surface_set_maximized,
2882 shell_surface_set_title,
2883 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002884};
2885
2886static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002887destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002888{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002889 struct shell_surface *child, *next;
2890
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002891 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2892
Giulio Camuffo5085a752013-03-25 21:42:45 +01002893 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2894 remove_popup_grab(shsurf);
2895 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002896
Alex Wubd3354b2012-04-17 17:20:49 +08002897 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002898 shell_surface_is_top_fullscreen(shsurf))
2899 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002900
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 if (shsurf->fullscreen.black_view)
2902 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002903
Alex Wubd3354b2012-04-17 17:20:49 +08002904 /* As destroy_resource() use wl_list_for_each_safe(),
2905 * we can always remove the listener.
2906 */
2907 wl_list_remove(&shsurf->surface_destroy_listener.link);
2908 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002909 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002910 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002911
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 weston_view_destroy(shsurf->view);
2913
Philip Withnall648a4dd2013-11-25 18:01:44 +00002914 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002915 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2916 wl_list_remove(&child->children_link);
2917 child->parent = NULL;
2918 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002919
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002920 wl_list_remove(&shsurf->link);
2921 free(shsurf);
2922}
2923
2924static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002925shell_destroy_shell_surface(struct wl_resource *resource)
2926{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002927 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002928
2929 destroy_shell_surface(shsurf);
2930}
2931
2932static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002933shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002934{
2935 struct shell_surface *shsurf = container_of(listener,
2936 struct shell_surface,
2937 surface_destroy_listener);
2938
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002939 if (shsurf->resource)
2940 wl_resource_destroy(shsurf->resource);
2941 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002942 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002943}
2944
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002945static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002946shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002947static void
2948shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002949
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002950struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002951get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002952{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002953 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002954 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002955 else
2956 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002957}
2958
Rafael Antognollie2a34552013-12-03 15:35:45 -02002959static struct shell_surface *
2960create_common_surface(void *shell, struct weston_surface *surface,
2961 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002962{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963 struct shell_surface *shsurf;
2964
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002965 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002966 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002967 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002968 }
2969
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002970 shsurf = calloc(1, sizeof *shsurf);
2971 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002972 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002973 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002974 }
2975
Jason Ekstranda7af7042013-10-12 22:38:11 -05002976 shsurf->view = weston_view_create(surface);
2977 if (!shsurf->view) {
2978 weston_log("no memory to allocate shell surface\n");
2979 free(shsurf);
2980 return NULL;
2981 }
2982
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002983 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002984 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002985 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002986
Tiago Vignattibc052c92012-04-19 16:18:18 +03002987 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002988 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002989 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002990 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002991 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002992 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002993 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2994 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002996 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002997 wl_list_init(&shsurf->fullscreen.transform.link);
2998
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002999 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003000 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003001 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003002 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003003
3004 /* init link so its safe to always remove it in destroy_shell_surface */
3005 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003006 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003007
Pekka Paalanen460099f2012-01-20 16:48:25 +02003008 /* empty when not in use */
3009 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003010 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003011
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003012 wl_list_init(&shsurf->workspace_transform.link);
3013
Philip Withnall648a4dd2013-11-25 18:01:44 +00003014 wl_list_init(&shsurf->children_link);
3015 wl_list_init(&shsurf->children_list);
3016 shsurf->parent = NULL;
3017
Pekka Paalanen98262232011-12-01 10:42:22 +02003018 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003019
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003020 shsurf->client = client;
3021
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003022 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003023}
3024
Rafael Antognollie2a34552013-12-03 15:35:45 -02003025static struct shell_surface *
3026create_shell_surface(void *shell, struct weston_surface *surface,
3027 const struct weston_shell_client *client)
3028{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003029 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003030}
3031
Jason Ekstranda7af7042013-10-12 22:38:11 -05003032static struct weston_view *
3033get_primary_view(void *shell, struct shell_surface *shsurf)
3034{
3035 return shsurf->view;
3036}
3037
Tiago Vignattibc052c92012-04-19 16:18:18 +03003038static void
3039shell_get_shell_surface(struct wl_client *client,
3040 struct wl_resource *resource,
3041 uint32_t id,
3042 struct wl_resource *surface_resource)
3043{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003044 struct weston_surface *surface =
3045 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003046 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003047 struct shell_surface *shsurf;
3048
3049 if (get_shell_surface(surface)) {
3050 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003051 WL_DISPLAY_ERROR_INVALID_OBJECT,
3052 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003053 return;
3054 }
3055
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003056 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003057 if (!shsurf) {
3058 wl_resource_post_error(surface_resource,
3059 WL_DISPLAY_ERROR_INVALID_OBJECT,
3060 "surface->configure already set");
3061 return;
3062 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003063
Jason Ekstranda85118c2013-06-27 20:17:02 -05003064 shsurf->resource =
3065 wl_resource_create(client,
3066 &wl_shell_surface_interface, 1, id);
3067 wl_resource_set_implementation(shsurf->resource,
3068 &shell_surface_implementation,
3069 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003070}
3071
Rafael Antognollie2a34552013-12-03 15:35:45 -02003072static bool
3073shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3074{
3075 return wl_resource_instance_of(shsurf->resource,
3076 &wl_shell_surface_interface,
3077 &shell_surface_implementation);
3078}
3079
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003080static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003081 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003082};
3083
Rafael Antognollie2a34552013-12-03 15:35:45 -02003084/****************************
3085 * xdg-shell implementation */
3086
3087static void
3088xdg_surface_destroy(struct wl_client *client,
3089 struct wl_resource *resource)
3090{
3091 wl_resource_destroy(resource);
3092}
3093
3094static void
3095xdg_surface_pong(struct wl_client *client,
3096 struct wl_resource *resource,
3097 uint32_t serial)
3098{
3099 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3100
3101 surface_pong(shsurf, serial);
3102}
3103
3104static void
3105xdg_surface_set_app_id(struct wl_client *client,
3106 struct wl_resource *resource,
3107 const char *app_id)
3108{
3109 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3110
3111 free(shsurf->class);
3112 shsurf->class = strdup(app_id);
3113}
3114
3115static void
3116xdg_surface_set_title(struct wl_client *client,
3117 struct wl_resource *resource, const char *title)
3118{
3119 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3120
3121 set_title(shsurf, title);
3122}
3123
3124static void
3125xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3126 struct wl_resource *seat_resource, uint32_t serial)
3127{
3128 common_surface_move(resource, seat_resource, serial);
3129}
3130
3131static void
3132xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3133 struct wl_resource *seat_resource, uint32_t serial,
3134 uint32_t edges)
3135{
3136 common_surface_resize(resource, seat_resource, serial, edges);
3137}
3138
3139static void
3140xdg_surface_set_output(struct wl_client *client,
3141 struct wl_resource *resource,
3142 struct wl_resource *output_resource)
3143{
3144 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3145 struct weston_output *output;
3146
3147 if (output_resource)
3148 output = wl_resource_get_user_data(output_resource);
3149 else
3150 output = NULL;
3151
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003152 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003153}
3154
3155static void
3156xdg_surface_set_fullscreen(struct wl_client *client,
3157 struct wl_resource *resource)
3158{
3159 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3160
3161 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3162 return;
3163
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003164 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003165 set_fullscreen(shsurf,
3166 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003167 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003168}
3169
3170static void
3171xdg_surface_unset_fullscreen(struct wl_client *client,
3172 struct wl_resource *resource)
3173{
3174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003175 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003176
3177 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3178 return;
3179
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003180 if (!shsurf->next_state.fullscreen)
3181 return;
3182
Rafael Antognollie2a34552013-12-03 15:35:45 -02003183 shsurf->next_state.fullscreen = false;
3184 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003185
3186 if (shsurf->saved_size_valid) {
3187 width = shsurf->saved_width;
3188 height = shsurf->saved_height;
3189 shsurf->saved_size_valid = false;
3190 } else {
3191 width = shsurf->surface->width;
3192 height = shsurf->surface->height;
3193 }
3194
3195 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003196}
3197
3198static void
3199xdg_surface_set_maximized(struct wl_client *client,
3200 struct wl_resource *resource)
3201{
3202 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3203
3204 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3205 return;
3206
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003207 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003208 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003209}
3210
3211static void
3212xdg_surface_unset_maximized(struct wl_client *client,
3213 struct wl_resource *resource)
3214{
3215 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003216 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003217
3218 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3219 return;
3220
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003221 if (!shsurf->next_state.maximized)
3222 return;
3223
Rafael Antognollie2a34552013-12-03 15:35:45 -02003224 shsurf->next_state.maximized = false;
3225 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003226
3227 if (shsurf->saved_size_valid) {
3228 width = shsurf->saved_width;
3229 height = shsurf->saved_height;
3230 shsurf->saved_size_valid = false;
3231 } else {
3232 width = shsurf->surface->width;
3233 height = shsurf->surface->height;
3234 }
3235
3236 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003237}
3238
3239static const struct xdg_surface_interface xdg_surface_implementation = {
3240 xdg_surface_destroy,
3241 xdg_surface_set_transient_for,
3242 xdg_surface_set_title,
3243 xdg_surface_set_app_id,
3244 xdg_surface_pong,
3245 xdg_surface_move,
3246 xdg_surface_resize,
3247 xdg_surface_set_output,
3248 xdg_surface_set_fullscreen,
3249 xdg_surface_unset_fullscreen,
3250 xdg_surface_set_maximized,
3251 xdg_surface_unset_maximized,
3252 NULL /* set_minimized */
3253};
3254
3255static void
3256xdg_send_configure(struct weston_surface *surface,
3257 uint32_t edges, int32_t width, int32_t height)
3258{
3259 struct shell_surface *shsurf = get_shell_surface(surface);
3260
3261 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3262}
3263
3264static const struct weston_shell_client xdg_client = {
3265 xdg_send_configure
3266};
3267
3268static void
3269xdg_use_unstable_version(struct wl_client *client,
3270 struct wl_resource *resource,
3271 int32_t version)
3272{
3273 if (version > 1) {
3274 wl_resource_post_error(resource,
3275 1,
3276 "xdg-shell:: version not implemented yet.");
3277 return;
3278 }
3279}
3280
3281static struct shell_surface *
3282create_xdg_surface(void *shell, struct weston_surface *surface,
3283 const struct weston_shell_client *client)
3284{
3285 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003286
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003287 shsurf = create_common_surface(shell, surface, client);
3288 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003289
3290 return shsurf;
3291}
3292
3293static void
3294xdg_get_xdg_surface(struct wl_client *client,
3295 struct wl_resource *resource,
3296 uint32_t id,
3297 struct wl_resource *surface_resource)
3298{
3299 struct weston_surface *surface =
3300 wl_resource_get_user_data(surface_resource);
3301 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3302 struct shell_surface *shsurf;
3303
3304 if (get_shell_surface(surface)) {
3305 wl_resource_post_error(surface_resource,
3306 WL_DISPLAY_ERROR_INVALID_OBJECT,
3307 "desktop_shell::get_shell_surface already requested");
3308 return;
3309 }
3310
3311 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3312 if (!shsurf) {
3313 wl_resource_post_error(surface_resource,
3314 WL_DISPLAY_ERROR_INVALID_OBJECT,
3315 "surface->configure already set");
3316 return;
3317 }
3318
3319 shsurf->resource =
3320 wl_resource_create(client,
3321 &xdg_surface_interface, 1, id);
3322 wl_resource_set_implementation(shsurf->resource,
3323 &xdg_surface_implementation,
3324 shsurf, shell_destroy_shell_surface);
3325}
3326
3327static bool
3328shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3329{
3330 return wl_resource_instance_of(shsurf->resource,
3331 &xdg_surface_interface,
3332 &xdg_surface_implementation);
3333}
3334
3335/* xdg-popup implementation */
3336
3337static void
3338xdg_popup_destroy(struct wl_client *client,
3339 struct wl_resource *resource)
3340{
3341 wl_resource_destroy(resource);
3342}
3343
3344static void
3345xdg_popup_pong(struct wl_client *client,
3346 struct wl_resource *resource,
3347 uint32_t serial)
3348{
3349 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3350
3351 surface_pong(shsurf, serial);
3352}
3353
3354static const struct xdg_popup_interface xdg_popup_implementation = {
3355 xdg_popup_destroy,
3356 xdg_popup_pong
3357};
3358
3359static void
3360xdg_popup_send_configure(struct weston_surface *surface,
3361 uint32_t edges, int32_t width, int32_t height)
3362{
3363}
3364
3365static const struct weston_shell_client xdg_popup_client = {
3366 xdg_popup_send_configure
3367};
3368
3369static struct shell_surface *
3370create_xdg_popup(void *shell, struct weston_surface *surface,
3371 const struct weston_shell_client *client,
3372 struct weston_surface *parent,
3373 struct shell_seat *seat,
3374 uint32_t serial,
3375 int32_t x, int32_t y)
3376{
3377 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003378
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003379 shsurf = create_common_surface(shell, surface, client);
3380 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003381 shsurf->popup.shseat = seat;
3382 shsurf->popup.serial = serial;
3383 shsurf->popup.x = x;
3384 shsurf->popup.y = y;
3385 shell_surface_set_parent(shsurf, parent);
3386
3387 return shsurf;
3388}
3389
3390static void
3391xdg_get_xdg_popup(struct wl_client *client,
3392 struct wl_resource *resource,
3393 uint32_t id,
3394 struct wl_resource *surface_resource,
3395 struct wl_resource *parent_resource,
3396 struct wl_resource *seat_resource,
3397 uint32_t serial,
3398 int32_t x, int32_t y, uint32_t flags)
3399{
3400 struct weston_surface *surface =
3401 wl_resource_get_user_data(surface_resource);
3402 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3403 struct shell_surface *shsurf;
3404 struct weston_surface *parent;
3405 struct shell_seat *seat;
3406
3407 if (get_shell_surface(surface)) {
3408 wl_resource_post_error(surface_resource,
3409 WL_DISPLAY_ERROR_INVALID_OBJECT,
3410 "desktop_shell::get_shell_surface already requested");
3411 return;
3412 }
3413
3414 if (!parent_resource) {
3415 wl_resource_post_error(surface_resource,
3416 WL_DISPLAY_ERROR_INVALID_OBJECT,
3417 "xdg_shell::get_xdg_popup requires a parent shell surface");
3418 }
3419
3420 parent = wl_resource_get_user_data(parent_resource);
3421 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3422
3423 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3424 parent, seat, serial, x, y);
3425 if (!shsurf) {
3426 wl_resource_post_error(surface_resource,
3427 WL_DISPLAY_ERROR_INVALID_OBJECT,
3428 "surface->configure already set");
3429 return;
3430 }
3431
3432 shsurf->resource =
3433 wl_resource_create(client,
3434 &xdg_popup_interface, 1, id);
3435 wl_resource_set_implementation(shsurf->resource,
3436 &xdg_popup_implementation,
3437 shsurf, shell_destroy_shell_surface);
3438}
3439
3440static bool
3441shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3442{
3443 return wl_resource_instance_of(shsurf->resource,
3444 &xdg_popup_interface,
3445 &xdg_popup_implementation);
3446}
3447
3448static const struct xdg_shell_interface xdg_implementation = {
3449 xdg_use_unstable_version,
3450 xdg_get_xdg_surface,
3451 xdg_get_xdg_popup
3452};
3453
3454static int
3455xdg_shell_unversioned_dispatch(const void *implementation,
3456 void *_target, uint32_t opcode,
3457 const struct wl_message *message,
3458 union wl_argument *args)
3459{
3460 struct wl_resource *resource = _target;
3461 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3462
3463 if (opcode != 0) {
3464 wl_resource_post_error(resource,
3465 WL_DISPLAY_ERROR_INVALID_OBJECT,
3466 "must call use_unstable_version first");
3467 return 0;
3468 }
3469
3470#define XDG_SERVER_VERSION 1
3471
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003472 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3473 "shell implementation doesn't match protocol version");
3474
Rafael Antognollie2a34552013-12-03 15:35:45 -02003475 if (args[0].i != XDG_SERVER_VERSION) {
3476 wl_resource_post_error(resource,
3477 WL_DISPLAY_ERROR_INVALID_OBJECT,
3478 "incompatible version, server is %d "
3479 "client wants %d",
3480 XDG_SERVER_VERSION, args[0].i);
3481 return 0;
3482 }
3483
3484 wl_resource_set_implementation(resource, &xdg_implementation,
3485 shell, NULL);
3486
3487 return 1;
3488}
3489
3490/* end of xdg-shell implementation */
3491/***********************************/
3492
Kristian Høgsberg07937562011-04-12 17:25:42 -04003493static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003494shell_fade(struct desktop_shell *shell, enum fade_type type);
3495
3496static int
3497screensaver_timeout(void *data)
3498{
3499 struct desktop_shell *shell = data;
3500
3501 shell_fade(shell, FADE_OUT);
3502
3503 return 1;
3504}
3505
3506static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003507handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003508{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003509 struct desktop_shell *shell =
3510 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003511
Pekka Paalanen18027e52011-12-02 16:31:49 +02003512 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003513
3514 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003515 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003516}
3517
3518static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003519launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003520{
3521 if (shell->screensaver.binding)
3522 return;
3523
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003524 if (!shell->screensaver.path) {
3525 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003526 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003527 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003528
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003529 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003530 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003531 return;
3532 }
3533
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003534 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003535 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003536 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003537 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003538}
3539
3540static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003541terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003542{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003543 if (shell->screensaver.process.pid == 0)
3544 return;
3545
3546 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003547}
3548
3549static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003550configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003551{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003553
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3555 if (v->output == ev->output && v != ev) {
3556 weston_view_unmap(v);
3557 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003558 }
3559 }
3560
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003561 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003562
Jason Ekstranda7af7042013-10-12 22:38:11 -05003563 if (wl_list_empty(&ev->layer_link)) {
3564 wl_list_insert(&layer->view_list, &ev->layer_link);
3565 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003566 }
3567}
3568
3569static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003570background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003571{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003572 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003573 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003574
Jason Ekstranda7af7042013-10-12 22:38:11 -05003575 view = container_of(es->views.next, struct weston_view, surface_link);
3576
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003577 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003578}
3579
3580static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003581desktop_shell_set_background(struct wl_client *client,
3582 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003583 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003584 struct wl_resource *surface_resource)
3585{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003586 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003587 struct weston_surface *surface =
3588 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003589 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003590
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003591 if (surface->configure) {
3592 wl_resource_post_error(surface_resource,
3593 WL_DISPLAY_ERROR_INVALID_OBJECT,
3594 "surface role already assigned");
3595 return;
3596 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003597
Jason Ekstranda7af7042013-10-12 22:38:11 -05003598 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3599 weston_view_destroy(view);
3600 view = weston_view_create(surface);
3601
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003602 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003603 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003604 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003605 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003606 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003607 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003608 surface->output->width,
3609 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003610}
3611
3612static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003613panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003614{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003615 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003616 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003617
Jason Ekstranda7af7042013-10-12 22:38:11 -05003618 view = container_of(es->views.next, struct weston_view, surface_link);
3619
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003620 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003621}
3622
3623static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003624desktop_shell_set_panel(struct wl_client *client,
3625 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003626 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003627 struct wl_resource *surface_resource)
3628{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003629 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003630 struct weston_surface *surface =
3631 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003632 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003633
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003634 if (surface->configure) {
3635 wl_resource_post_error(surface_resource,
3636 WL_DISPLAY_ERROR_INVALID_OBJECT,
3637 "surface role already assigned");
3638 return;
3639 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003640
Jason Ekstranda7af7042013-10-12 22:38:11 -05003641 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3642 weston_view_destroy(view);
3643 view = weston_view_create(surface);
3644
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003645 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003646 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003647 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003649 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003650 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003651 surface->output->width,
3652 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003653}
3654
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003655static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003656lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003657{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003658 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003659 struct weston_view *view;
3660
3661 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003662
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003663 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003664 return;
3665
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003667
3668 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003669 wl_list_insert(&shell->lock_layer.view_list,
3670 &view->layer_link);
3671 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003672 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003673 }
3674}
3675
3676static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003677handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003678{
Tiago Vignattibe143262012-04-16 17:31:41 +03003679 struct desktop_shell *shell =
3680 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003681
Martin Minarik6d118362012-06-07 18:01:59 +02003682 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003683 shell->lock_surface = NULL;
3684}
3685
3686static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003687desktop_shell_set_lock_surface(struct wl_client *client,
3688 struct wl_resource *resource,
3689 struct wl_resource *surface_resource)
3690{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003691 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003692 struct weston_surface *surface =
3693 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003694
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003695 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003696
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003697 if (!shell->locked)
3698 return;
3699
Pekka Paalanen98262232011-12-01 10:42:22 +02003700 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003701
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003702 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003703 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003704 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003705
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003706 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003707 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003708 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003709}
3710
3711static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003712resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003713{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003714 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003715
Pekka Paalanen77346a62011-11-30 16:26:35 +02003716 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003717
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003718 wl_list_remove(&shell->lock_layer.link);
3719 wl_list_insert(&shell->compositor->cursor_layer.link,
3720 &shell->fullscreen_layer.link);
3721 wl_list_insert(&shell->fullscreen_layer.link,
3722 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003723 if (shell->showing_input_panels) {
3724 wl_list_insert(&shell->panel_layer.link,
3725 &shell->input_panel_layer.link);
3726 wl_list_insert(&shell->input_panel_layer.link,
3727 &ws->layer.link);
3728 } else {
3729 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3730 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003731
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003732 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003733
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003734 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003735 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003736 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003737}
3738
3739static void
3740desktop_shell_unlock(struct wl_client *client,
3741 struct wl_resource *resource)
3742{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003743 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003744
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003745 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003746
3747 if (shell->locked)
3748 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003749}
3750
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003751static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003752desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003753 struct wl_resource *resource,
3754 struct wl_resource *surface_resource)
3755{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003756 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003757
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003758 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003759 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003760}
3761
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003762static void
3763desktop_shell_desktop_ready(struct wl_client *client,
3764 struct wl_resource *resource)
3765{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003766 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003767
3768 shell_fade_startup(shell);
3769}
3770
Kristian Høgsberg75840622011-09-06 13:48:16 -04003771static const struct desktop_shell_interface desktop_shell_implementation = {
3772 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003773 desktop_shell_set_panel,
3774 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003775 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003776 desktop_shell_set_grab_surface,
3777 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778};
3779
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003780static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003781get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003782{
3783 struct shell_surface *shsurf;
3784
3785 shsurf = get_shell_surface(surface);
3786 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003787 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003788 return shsurf->type;
3789}
3790
Kristian Høgsberg75840622011-09-06 13:48:16 -04003791static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003792move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003793{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003794 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003795 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003796 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003797
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003798 if (seat->pointer->focus == NULL)
3799 return;
3800
3801 focus = seat->pointer->focus->surface;
3802
Pekka Paalanen01388e22013-04-25 13:57:44 +03003803 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003804 if (surface == NULL)
3805 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003806
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003807 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003808 if (shsurf == NULL || shsurf->state.fullscreen ||
3809 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003810 return;
3811
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003812 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003813}
3814
3815static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003816maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3817{
3818 struct weston_surface *focus = seat->pointer->focus->surface;
3819 struct weston_surface *surface;
3820 struct shell_surface *shsurf;
3821
3822 surface = weston_surface_get_main_surface(focus);
3823 if (surface == NULL)
3824 return;
3825
3826 shsurf = get_shell_surface(surface);
3827 if (shsurf == NULL)
3828 return;
3829
3830 if (!shell_surface_is_xdg_surface(shsurf))
3831 return;
3832
3833 if (shsurf->state.maximized)
3834 xdg_surface_send_request_unset_maximized(shsurf->resource);
3835 else
3836 xdg_surface_send_request_set_maximized(shsurf->resource);
3837}
3838
3839static void
3840fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3841{
3842 struct weston_surface *focus = seat->pointer->focus->surface;
3843 struct weston_surface *surface;
3844 struct shell_surface *shsurf;
3845
3846 surface = weston_surface_get_main_surface(focus);
3847 if (surface == NULL)
3848 return;
3849
3850 shsurf = get_shell_surface(surface);
3851 if (shsurf == NULL)
3852 return;
3853
3854 if (!shell_surface_is_xdg_surface(shsurf))
3855 return;
3856
3857 if (shsurf->state.fullscreen)
3858 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3859 else
3860 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3861}
3862
3863static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003864touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3865{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003866 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003867 struct weston_surface *surface;
3868 struct shell_surface *shsurf;
3869
3870 surface = weston_surface_get_main_surface(focus);
3871 if (surface == NULL)
3872 return;
3873
3874 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003875 if (shsurf == NULL || shsurf->state.fullscreen ||
3876 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003877 return;
3878
3879 surface_touch_move(shsurf, (struct weston_seat *) seat);
3880}
3881
3882static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003883resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003884{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003885 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003886 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003887 uint32_t edges = 0;
3888 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003889 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003890
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003891 if (seat->pointer->focus == NULL)
3892 return;
3893
3894 focus = seat->pointer->focus->surface;
3895
Pekka Paalanen01388e22013-04-25 13:57:44 +03003896 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003897 if (surface == NULL)
3898 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003899
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003900 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003901 if (shsurf == NULL || shsurf->state.fullscreen ||
3902 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003903 return;
3904
Jason Ekstranda7af7042013-10-12 22:38:11 -05003905 weston_view_from_global(shsurf->view,
3906 wl_fixed_to_int(seat->pointer->grab_x),
3907 wl_fixed_to_int(seat->pointer->grab_y),
3908 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003909
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003910 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003911 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003912 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003913 edges |= 0;
3914 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003915 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003916
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003917 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003918 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003919 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003920 edges |= 0;
3921 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003922 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003923
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003924 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003925}
3926
3927static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003928surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003929 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003930{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003931 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003932 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003933 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003934 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003935
Pekka Paalanen01388e22013-04-25 13:57:44 +03003936 /* XXX: broken for windows containing sub-surfaces */
3937 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003938 if (surface == NULL)
3939 return;
3940
3941 shsurf = get_shell_surface(surface);
3942 if (!shsurf)
3943 return;
3944
Jason Ekstranda7af7042013-10-12 22:38:11 -05003945 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003946
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 if (shsurf->view->alpha > 1.0)
3948 shsurf->view->alpha = 1.0;
3949 if (shsurf->view->alpha < step)
3950 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003951
Jason Ekstranda7af7042013-10-12 22:38:11 -05003952 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003953 weston_surface_damage(surface);
3954}
3955
3956static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003957do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003958 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003959{
Daniel Stone37816df2012-05-16 18:45:18 +01003960 struct weston_seat *ws = (struct weston_seat *) seat;
3961 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003962 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003963 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003964
3965 wl_list_for_each(output, &compositor->output_list, link) {
3966 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003967 wl_fixed_to_double(seat->pointer->x),
3968 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003969 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003970 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003971 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003972 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003973 increment = -output->zoom.increment;
3974 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003975 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003976 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003977 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003978 else
3979 increment = 0;
3980
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003981 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003982
Scott Moreaue6603982012-06-11 13:07:51 -06003983 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003984 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003985 else if (output->zoom.level > output->zoom.max_level)
3986 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003987 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003988 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003989 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003990
Scott Moreaue6603982012-06-11 13:07:51 -06003991 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003992
Jason Ekstranda7af7042013-10-12 22:38:11 -05003993 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003994 }
3995 }
3996}
3997
Scott Moreauccbf29d2012-02-22 14:21:41 -07003998static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003999zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004000 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004001{
4002 do_zoom(seat, time, 0, axis, value);
4003}
4004
4005static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004006zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004007 void *data)
4008{
4009 do_zoom(seat, time, key, 0, 0);
4010}
4011
4012static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004013terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004014 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004015{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004016 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004017
Daniel Stone325fc2d2012-05-30 16:31:58 +01004018 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004019}
4020
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004021static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004022rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4023 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004024{
4025 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004026 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004027 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004028 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004029 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004030
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004031 weston_pointer_move(pointer, x, y);
4032
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004033 if (!shsurf)
4034 return;
4035
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004036 cx = 0.5f * shsurf->surface->width;
4037 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004038
Daniel Stone37816df2012-05-16 18:45:18 +01004039 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4040 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004041 r = sqrtf(dx * dx + dy * dy);
4042
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004043 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004044 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004045
4046 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004047 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004048 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004049
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004050 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004051 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004052
4053 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004054 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004055 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004056 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004057 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004058
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004059 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004060 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004061 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004062 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004063 wl_list_init(&shsurf->rotation.transform.link);
4064 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004065 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004066 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004067
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004068 /* We need to adjust the position of the surface
4069 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004070 cposx = shsurf->view->geometry.x + cx;
4071 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004072 dposx = rotate->center.x - cposx;
4073 dposy = rotate->center.y - cposy;
4074 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004075 weston_view_set_position(shsurf->view,
4076 shsurf->view->geometry.x + dposx,
4077 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004078 }
4079
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004080 /* Repaint implies weston_surface_update_transform(), which
4081 * lazily applies the damage due to rotation update.
4082 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004083 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004084}
4085
4086static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004087rotate_grab_button(struct weston_pointer_grab *grab,
4088 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004089{
4090 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004091 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004092 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004093 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004094 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004095
Daniel Stone4dbadb12012-05-30 16:31:51 +01004096 if (pointer->button_count == 0 &&
4097 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004098 if (shsurf)
4099 weston_matrix_multiply(&shsurf->rotation.rotation,
4100 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004101 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004102 free(rotate);
4103 }
4104}
4105
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004106static void
4107rotate_grab_cancel(struct weston_pointer_grab *grab)
4108{
4109 struct rotate_grab *rotate =
4110 container_of(grab, struct rotate_grab, base.grab);
4111
4112 shell_grab_end(&rotate->base);
4113 free(rotate);
4114}
4115
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004116static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004117 noop_grab_focus,
4118 rotate_grab_motion,
4119 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004120 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004121};
4122
4123static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004124surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004125{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004126 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004127 float dx, dy;
4128 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004129
Pekka Paalanen460099f2012-01-20 16:48:25 +02004130 rotate = malloc(sizeof *rotate);
4131 if (!rotate)
4132 return;
4133
Jason Ekstranda7af7042013-10-12 22:38:11 -05004134 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004135 surface->surface->width * 0.5f,
4136 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004137 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004138
Daniel Stone37816df2012-05-16 18:45:18 +01004139 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4140 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004141 r = sqrtf(dx * dx + dy * dy);
4142 if (r > 20.0f) {
4143 struct weston_matrix inverse;
4144
4145 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004146 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004147 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004148
4149 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004150 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004151 } else {
4152 weston_matrix_init(&surface->rotation.rotation);
4153 weston_matrix_init(&rotate->rotation);
4154 }
4155
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004156 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4157 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004158}
4159
4160static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004161rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004162 void *data)
4163{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004164 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004165 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004166 struct shell_surface *surface;
4167
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004168 if (seat->pointer->focus == NULL)
4169 return;
4170
4171 focus = seat->pointer->focus->surface;
4172
Pekka Paalanen01388e22013-04-25 13:57:44 +03004173 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004174 if (base_surface == NULL)
4175 return;
4176
4177 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004178 if (surface == NULL || surface->state.fullscreen ||
4179 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004180 return;
4181
4182 surface_rotate(surface, seat);
4183}
4184
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004185/* Move all fullscreen layers down to the current workspace in a non-reversible
4186 * manner. This should be used when implementing shell-wide overlays, such as
4187 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004188void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004189lower_fullscreen_layer(struct desktop_shell *shell)
4190{
4191 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004192 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004193
4194 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004195 wl_list_for_each_reverse_safe(view, prev,
4196 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004197 layer_link) {
4198 wl_list_remove(&view->layer_link);
4199 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4200 weston_view_damage_below(view);
4201 weston_surface_damage(view->surface);
4202 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004203}
4204
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004205void
Tiago Vignattibe143262012-04-16 17:31:41 +03004206activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004207 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004208{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004209 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004210 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004211 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004212 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004213 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004214
Pekka Paalanen01388e22013-04-25 13:57:44 +03004215 main_surface = weston_surface_get_main_surface(es);
4216
Daniel Stone37816df2012-05-16 18:45:18 +01004217 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004218
Jonas Ådahl8538b222012-08-29 22:13:03 +02004219 state = ensure_focus_state(shell, seat);
4220 if (state == NULL)
4221 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004222
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004223 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004224 state->keyboard_focus = es;
4225 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004226 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004227
Rafael Antognolli03b16592013-12-03 15:35:42 -02004228 shsurf = get_shell_surface(main_surface);
4229 if (shsurf->state.fullscreen)
4230 shell_configure_fullscreen(shsurf);
4231 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004232 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004233
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004234 if (shell->focus_animation_type != ANIMATION_NONE) {
4235 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004236 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004237 }
4238
4239 /* Update the surface’s layer. This brings it to the top of the stacking
4240 * order as appropriate. */
4241 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004242}
4243
Alex Wu21858432012-04-01 20:13:08 +08004244/* no-op func for checking black surface */
4245static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004246black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004247{
4248}
4249
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004250static bool
Alex Wu21858432012-04-01 20:13:08 +08004251is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4252{
4253 if (es->configure == black_surface_configure) {
4254 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004255 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004256 return true;
4257 }
4258 return false;
4259}
4260
Kristian Høgsberg75840622011-09-06 13:48:16 -04004261static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004262activate_binding(struct weston_seat *seat,
4263 struct desktop_shell *shell,
4264 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004265{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004266 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004267
Alex Wu9c35e6b2012-03-05 14:13:13 +08004268 if (!focus)
4269 return;
4270
Pekka Paalanen01388e22013-04-25 13:57:44 +03004271 if (is_black_surface(focus, &main_surface))
4272 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004273
Pekka Paalanen01388e22013-04-25 13:57:44 +03004274 main_surface = weston_surface_get_main_surface(focus);
4275 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004276 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004277
Neil Robertsa28c6932013-10-03 16:43:04 +01004278 activate(shell, focus, seat);
4279}
4280
4281static void
4282click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4283 void *data)
4284{
4285 if (seat->pointer->grab != &seat->pointer->default_grab)
4286 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004287 if (seat->pointer->focus == NULL)
4288 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004289
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004290 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004291}
4292
4293static void
4294touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4295{
4296 if (seat->touch->grab != &seat->touch->default_grab)
4297 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004298 if (seat->touch->focus == NULL)
4299 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004300
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004301 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004302}
4303
4304static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004305lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004306{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004307 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004308
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004309 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004310 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004311 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004312 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004313
4314 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004315
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004316 /* Hide all surfaces by removing the fullscreen, panel and
4317 * toplevel layers. This way nothing else can show or receive
4318 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004319
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004320 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004321 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004322 if (shell->showing_input_panels)
4323 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004324 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004325 wl_list_insert(&shell->compositor->cursor_layer.link,
4326 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004327
Pekka Paalanen77346a62011-11-30 16:26:35 +02004328 launch_screensaver(shell);
4329
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004330 /* TODO: disable bindings that should not work while locked. */
4331
4332 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004333}
4334
4335static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004336unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004337{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004338 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004339 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004340 return;
4341 }
4342
4343 /* If desktop-shell client has gone away, unlock immediately. */
4344 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004345 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004346 return;
4347 }
4348
4349 if (shell->prepare_event_sent)
4350 return;
4351
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004352 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004353 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004354}
4355
4356static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004358{
4359 struct desktop_shell *shell = data;
4360
4361 shell->fade.animation = NULL;
4362
4363 switch (shell->fade.type) {
4364 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 weston_surface_destroy(shell->fade.view->surface);
4366 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004367 break;
4368 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004369 lock(shell);
4370 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004371 default:
4372 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004373 }
4374}
4375
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004377shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004378{
4379 struct weston_compositor *compositor = shell->compositor;
4380 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004381 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004382
4383 surface = weston_surface_create(compositor);
4384 if (!surface)
4385 return NULL;
4386
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 view = weston_view_create(surface);
4388 if (!view) {
4389 weston_surface_destroy(surface);
4390 return NULL;
4391 }
4392
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004393 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004394 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004395 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 wl_list_insert(&compositor->fade_layer.view_list,
4397 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004398 pixman_region32_init(&surface->input);
4399
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004401}
4402
4403static void
4404shell_fade(struct desktop_shell *shell, enum fade_type type)
4405{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004406 float tint;
4407
4408 switch (type) {
4409 case FADE_IN:
4410 tint = 0.0;
4411 break;
4412 case FADE_OUT:
4413 tint = 1.0;
4414 break;
4415 default:
4416 weston_log("shell: invalid fade type\n");
4417 return;
4418 }
4419
4420 shell->fade.type = type;
4421
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 if (shell->fade.view == NULL) {
4423 shell->fade.view = shell_fade_create_surface(shell);
4424 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004425 return;
4426
Jason Ekstranda7af7042013-10-12 22:38:11 -05004427 shell->fade.view->alpha = 1.0 - tint;
4428 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004429 }
4430
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004431 if (shell->fade.view->output == NULL) {
4432 /* If the black view gets a NULL output, we lost the
4433 * last output and we'll just cancel the fade. This
4434 * happens when you close the last window under the
4435 * X11 or Wayland backends. */
4436 shell->locked = false;
4437 weston_surface_destroy(shell->fade.view->surface);
4438 shell->fade.view = NULL;
4439 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004440 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004441 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004442 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004444 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004445 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004446 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004447}
4448
4449static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004450do_shell_fade_startup(void *data)
4451{
4452 struct desktop_shell *shell = data;
4453
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004454 if (shell->startup_animation_type == ANIMATION_FADE)
4455 shell_fade(shell, FADE_IN);
4456 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 weston_surface_destroy(shell->fade.view->surface);
4458 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004459 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004460}
4461
4462static void
4463shell_fade_startup(struct desktop_shell *shell)
4464{
4465 struct wl_event_loop *loop;
4466
4467 if (!shell->fade.startup_timer)
4468 return;
4469
4470 wl_event_source_remove(shell->fade.startup_timer);
4471 shell->fade.startup_timer = NULL;
4472
4473 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4474 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4475}
4476
4477static int
4478fade_startup_timeout(void *data)
4479{
4480 struct desktop_shell *shell = data;
4481
4482 shell_fade_startup(shell);
4483 return 0;
4484}
4485
4486static void
4487shell_fade_init(struct desktop_shell *shell)
4488{
4489 /* Make compositor output all black, and wait for the desktop-shell
4490 * client to signal it is ready, then fade in. The timer triggers a
4491 * fade-in, in case the desktop-shell client takes too long.
4492 */
4493
4494 struct wl_event_loop *loop;
4495
Jason Ekstranda7af7042013-10-12 22:38:11 -05004496 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004497 weston_log("%s: warning: fade surface already exists\n",
4498 __func__);
4499 return;
4500 }
4501
Jason Ekstranda7af7042013-10-12 22:38:11 -05004502 shell->fade.view = shell_fade_create_surface(shell);
4503 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004504 return;
4505
Jason Ekstranda7af7042013-10-12 22:38:11 -05004506 weston_view_update_transform(shell->fade.view);
4507 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004508
4509 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4510 shell->fade.startup_timer =
4511 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4512 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4513}
4514
4515static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004516idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004517{
4518 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004519 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004520 struct weston_seat *seat;
4521
4522 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4523 if (seat->pointer)
4524 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004525
4526 shell_fade(shell, FADE_OUT);
4527 /* lock() is called from shell_fade_done() */
4528}
4529
4530static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004531wake_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, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004535
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004536 unlock(shell);
4537}
4538
4539static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004541{
Giulio Camuffob8366642013-04-25 13:57:46 +03004542 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004543 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004544
Jason Ekstranda7af7042013-10-12 22:38:11 -05004545 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004546
4547 x = output->x + (output->width - width) / 2 - surf_x / 2;
4548 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004549
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004550 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004551}
4552
4553static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004554weston_view_set_initial_position(struct weston_view *view,
4555 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004556{
4557 struct weston_compositor *compositor = shell->compositor;
4558 int ix = 0, iy = 0;
4559 int range_x, range_y;
4560 int dx, dy, x, y, panel_height;
4561 struct weston_output *output, *target_output = NULL;
4562 struct weston_seat *seat;
4563
4564 /* As a heuristic place the new window on the same output as the
4565 * pointer. Falling back to the output containing 0, 0.
4566 *
4567 * TODO: Do something clever for touch too?
4568 */
4569 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004570 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004571 ix = wl_fixed_to_int(seat->pointer->x);
4572 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004573 break;
4574 }
4575 }
4576
4577 wl_list_for_each(output, &compositor->output_list, link) {
4578 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4579 target_output = output;
4580 break;
4581 }
4582 }
4583
4584 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585 weston_view_set_position(view, 10 + random() % 400,
4586 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004587 return;
4588 }
4589
4590 /* Valid range within output where the surface will still be onscreen.
4591 * If this is negative it means that the surface is bigger than
4592 * output.
4593 */
4594 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004595 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004596 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004597 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004598
Rob Bradford4cb88c72012-08-13 15:18:44 +01004599 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004600 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004601 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004602 dx = 0;
4603
4604 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004605 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004606 else
4607 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004608
4609 x = target_output->x + dx;
4610 y = target_output->y + dy;
4611
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004613}
4614
4615static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004617 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004618{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004619 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004620 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004621 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004622 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004623
Pekka Paalanen77346a62011-11-30 16:26:35 +02004624 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004626 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004627 if (shsurf->state.fullscreen) {
4628 center_on_output(shsurf->view, shsurf->fullscreen_output);
4629 shell_map_fullscreen(shsurf);
4630 } else if (shsurf->state.maximized) {
4631 /* use surface configure to set the geometry */
4632 panel_height = get_output_panel_height(shell, shsurf->output);
4633 surface_subsurfaces_boundingbox(shsurf->surface,
4634 &surf_x, &surf_y, NULL, NULL);
4635 weston_view_set_position(shsurf->view,
4636 shsurf->output->x - surf_x,
4637 shsurf->output->y +
4638 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004639 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004640 weston_view_set_initial_position(shsurf->view, shell);
4641 }
Juan Zhao96879df2012-02-07 08:45:41 +08004642 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004643 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004644 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004645 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004646 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004647 weston_view_set_position(shsurf->view,
4648 shsurf->view->geometry.x + sx,
4649 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004650 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004651 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004652 default:
4653 ;
4654 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004655
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004656 /* Surface stacking order, see also activate(). */
4657 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004658
Jason Ekstranda7af7042013-10-12 22:38:11 -05004659 if (shsurf->type != SHELL_SURFACE_NONE) {
4660 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004661 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 shsurf->surface->output = shsurf->output;
4663 shsurf->view->output = shsurf->output;
4664 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004665 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004666
Rafael Antognollied207b42013-12-03 15:35:43 -02004667 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4668 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4669 }
4670
Jason Ekstranda7af7042013-10-12 22:38:11 -05004671 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004672 /* XXX: xwayland's using the same fields for transient type */
4673 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004674 if (shsurf->transient.flags ==
4675 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4676 break;
4677 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004678 if (shsurf->state.relative &&
4679 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4680 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004681 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004682 break;
4683 wl_list_for_each(seat, &compositor->seat_list, link)
4684 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004685 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004686 case SHELL_SURFACE_POPUP:
4687 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004688 default:
4689 break;
4690 }
4691
Rafael Antognolli03b16592013-12-03 15:35:42 -02004692 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4693 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004694 {
4695 switch (shell->win_animation_type) {
4696 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004697 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004698 break;
4699 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004700 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004701 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004702 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004703 default:
4704 break;
4705 }
4706 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004707}
4708
4709static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004710configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004711 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004712{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004713 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004714 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004715 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004716
Pekka Paalanen77346a62011-11-30 16:26:35 +02004717 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004718
Rafael Antognolli03b16592013-12-03 15:35:42 -02004719 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004720 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004721 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004722 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004723 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4724 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004725 mx = shsurf->output->x - surf_x;
4726 my = shsurf->output->y +
4727 get_output_panel_height(shell,shsurf->output) - surf_y;
4728 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004729 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004730 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004731 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004732
Alex Wu4539b082012-03-01 12:57:46 +08004733 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004734 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 wl_list_for_each(view, &surface->views, surface_link)
4736 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004737
Rafael Antognolli03b16592013-12-03 15:35:42 -02004738 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004739 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004740 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004741}
4742
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004743static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004744shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004745{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004746 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004747 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004748
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004749 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004750
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004751 if (!weston_surface_is_mapped(es) &&
4752 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004753 remove_popup_grab(shsurf);
4754 }
4755
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004756 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004757 return;
4758
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004759 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004760 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004761 type_changed = 1;
4762 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004763
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004764 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004766 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004767 shsurf->last_width != es->width ||
4768 shsurf->last_height != es->height) {
4769 shsurf->last_width = es->width;
4770 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004771 float from_x, from_y;
4772 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004773
Jason Ekstranda7af7042013-10-12 22:38:11 -05004774 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4775 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004776 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004778 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004779 }
4780}
4781
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004782static void
4783shell_surface_output_destroyed(struct weston_surface *es)
4784{
4785 struct shell_surface *shsurf = get_shell_surface(es);
4786
4787 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004788 shsurf->next_state.maximized = false;
4789 shsurf->next_state.fullscreen = false;
4790 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004791}
4792
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004793static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004794
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004795static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004796desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004797{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004798 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004799 struct desktop_shell *shell =
4800 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004801
4802 shell->child.process.pid = 0;
4803 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004804
4805 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4806 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004807 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004808 shell->child.deathstamp = time;
4809 shell->child.deathcount = 0;
4810 }
4811
4812 shell->child.deathcount++;
4813 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004814 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004815 return;
4816 }
4817
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004818 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004819 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004820 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004821}
4822
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004823static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004824desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4825{
4826 struct desktop_shell *shell;
4827
4828 shell = container_of(listener, struct desktop_shell,
4829 child.client_destroy_listener);
4830
4831 shell->child.client = NULL;
4832}
4833
4834static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004835launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004836{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004837 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004838
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004839 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004840 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004841 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004842 desktop_shell_sigchld);
4843
4844 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004845 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004846
4847 shell->child.client_destroy_listener.notify =
4848 desktop_shell_client_destroy;
4849 wl_client_add_destroy_listener(shell->child.client,
4850 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004851}
4852
4853static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004854bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4855{
Tiago Vignattibe143262012-04-16 17:31:41 +03004856 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004857 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004858
Jason Ekstranda85118c2013-06-27 20:17:02 -05004859 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4860 if (resource)
4861 wl_resource_set_implementation(resource, &shell_implementation,
4862 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004863}
4864
Kristian Høgsberg75840622011-09-06 13:48:16 -04004865static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004866bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4867{
4868 struct desktop_shell *shell = data;
4869 struct wl_resource *resource;
4870
4871 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4872 if (resource)
4873 wl_resource_set_dispatcher(resource,
4874 xdg_shell_unversioned_dispatch,
4875 NULL, shell, NULL);
4876}
4877
4878static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004879unbind_desktop_shell(struct wl_resource *resource)
4880{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004881 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004882
4883 if (shell->locked)
4884 resume_desktop(shell);
4885
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004886 shell->child.desktop_shell = NULL;
4887 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004888}
4889
4890static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004891bind_desktop_shell(struct wl_client *client,
4892 void *data, uint32_t version, uint32_t id)
4893{
Tiago Vignattibe143262012-04-16 17:31:41 +03004894 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004895 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004896
Jason Ekstranda85118c2013-06-27 20:17:02 -05004897 resource = wl_resource_create(client, &desktop_shell_interface,
4898 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004899
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004900 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004901 wl_resource_set_implementation(resource,
4902 &desktop_shell_implementation,
4903 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004904 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004905
4906 if (version < 2)
4907 shell_fade_startup(shell);
4908
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004909 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004910 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004911
4912 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4913 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004914 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004915}
4916
Pekka Paalanen6e168112011-11-24 11:34:05 +02004917static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004918screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004919{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004920 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004922
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004923 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004924 return;
4925
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004926 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004927 if (!shell->locked)
4928 return;
4929
Jason Ekstranda7af7042013-10-12 22:38:11 -05004930 view = container_of(surface->views.next, struct weston_view, surface_link);
4931 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004932
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 if (wl_list_empty(&view->layer_link)) {
4934 wl_list_insert(shell->lock_layer.view_list.prev,
4935 &view->layer_link);
4936 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004937 wl_event_source_timer_update(shell->screensaver.timer,
4938 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004939 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004940 }
4941}
4942
4943static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004944screensaver_set_surface(struct wl_client *client,
4945 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004946 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004947 struct wl_resource *output_resource)
4948{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004949 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004950 struct weston_surface *surface =
4951 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004952 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004953 struct weston_view *view, *next;
4954
4955 /* Make sure we only have one view */
4956 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4957 weston_view_destroy(view);
4958 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004959
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004960 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004961 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004962 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004963}
4964
4965static const struct screensaver_interface screensaver_implementation = {
4966 screensaver_set_surface
4967};
4968
4969static void
4970unbind_screensaver(struct wl_resource *resource)
4971{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004972 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004973
Pekka Paalanen77346a62011-11-30 16:26:35 +02004974 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004975}
4976
4977static void
4978bind_screensaver(struct wl_client *client,
4979 void *data, uint32_t version, uint32_t id)
4980{
Tiago Vignattibe143262012-04-16 17:31:41 +03004981 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004982 struct wl_resource *resource;
4983
Jason Ekstranda85118c2013-06-27 20:17:02 -05004984 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004985
Pekka Paalanen77346a62011-11-30 16:26:35 +02004986 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004987 wl_resource_set_implementation(resource,
4988 &screensaver_implementation,
4989 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004990 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004991 return;
4992 }
4993
4994 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4995 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004996 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004997}
4998
Kristian Høgsberg07045392012-02-19 18:52:44 -05004999struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005000 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005001 struct weston_surface *current;
5002 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005003 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005004};
5005
5006static void
5007switcher_next(struct switcher *switcher)
5008{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005010 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005011 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005012 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005013
Jason Ekstranda7af7042013-10-12 22:38:11 -05005014 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005015 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005016 if (shsurf &&
5017 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5018 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005019 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005020 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005021 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 next = view->surface;
5023 prev = view->surface;
5024 view->alpha = 0.25;
5025 weston_view_geometry_dirty(view);
5026 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005027 }
Alex Wu1659daa2012-04-01 20:13:09 +08005028
Jason Ekstranda7af7042013-10-12 22:38:11 -05005029 if (is_black_surface(view->surface, NULL)) {
5030 view->alpha = 0.25;
5031 weston_view_geometry_dirty(view);
5032 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005033 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005034 }
5035
5036 if (next == NULL)
5037 next = first;
5038
Alex Wu07b26062012-03-12 16:06:01 +08005039 if (next == NULL)
5040 return;
5041
Kristian Høgsberg07045392012-02-19 18:52:44 -05005042 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005043 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005044
5045 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005046 wl_list_for_each(view, &next->views, surface_link)
5047 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005048
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005049 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005050 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005052}
5053
5054static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005055switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005056{
5057 struct switcher *switcher =
5058 container_of(listener, struct switcher, listener);
5059
5060 switcher_next(switcher);
5061}
5062
5063static void
Daniel Stone351eb612012-05-31 15:27:47 -04005064switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005065{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005066 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005067 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005068 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005069
Jason Ekstranda7af7042013-10-12 22:38:11 -05005070 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005071 if (is_focus_view(view))
5072 continue;
5073
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 view->alpha = 1.0;
5075 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076 }
5077
Alex Wu07b26062012-03-12 16:06:01 +08005078 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005079 activate(switcher->shell, switcher->current,
5080 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005082 weston_keyboard_end_grab(keyboard);
5083 if (keyboard->input_method_resource)
5084 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005085 free(switcher);
5086}
5087
5088static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005089switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005090 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005091{
5092 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005093 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005094
Daniel Stonec9785ea2012-05-30 16:31:52 +01005095 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005096 switcher_next(switcher);
5097}
5098
5099static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005100switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005101 uint32_t mods_depressed, uint32_t mods_latched,
5102 uint32_t mods_locked, uint32_t group)
5103{
5104 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005105 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005106
Daniel Stone351eb612012-05-31 15:27:47 -04005107 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5108 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005109}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005110
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005111static void
5112switcher_cancel(struct weston_keyboard_grab *grab)
5113{
5114 struct switcher *switcher = container_of(grab, struct switcher, grab);
5115
5116 switcher_destroy(switcher);
5117}
5118
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005119static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005120 switcher_key,
5121 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005122 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005123};
5124
5125static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005126switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005127 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005128{
Tiago Vignattibe143262012-04-16 17:31:41 +03005129 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005130 struct switcher *switcher;
5131
5132 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005133 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005134 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005135 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005136 wl_list_init(&switcher->listener.link);
5137
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005138 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005139 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005140 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005141 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5142 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005143 switcher_next(switcher);
5144}
5145
Pekka Paalanen3c647232011-12-22 13:43:43 +02005146static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005147backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005148 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005149{
5150 struct weston_compositor *compositor = data;
5151 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005152 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005153
5154 /* TODO: we're limiting to simple use cases, where we assume just
5155 * control on the primary display. We'd have to extend later if we
5156 * ever get support for setting backlights on random desktop LCD
5157 * panels though */
5158 output = get_default_output(compositor);
5159 if (!output)
5160 return;
5161
5162 if (!output->set_backlight)
5163 return;
5164
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005165 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5166 backlight_new = output->backlight_current - 25;
5167 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5168 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005169
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005170 if (backlight_new < 5)
5171 backlight_new = 5;
5172 if (backlight_new > 255)
5173 backlight_new = 255;
5174
5175 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005176 output->set_backlight(output, output->backlight_current);
5177}
5178
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005179struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005180 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005181 struct weston_seat *seat;
5182 uint32_t key[2];
5183 int key_released[2];
5184};
5185
5186static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005187debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005188 uint32_t key, uint32_t state)
5189{
5190 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005191 struct weston_compositor *ec = db->seat->compositor;
5192 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005193 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005194 uint32_t serial;
5195 int send = 0, terminate = 0;
5196 int check_binding = 1;
5197 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005198 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005199
5200 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5201 /* Do not run bindings on key releases */
5202 check_binding = 0;
5203
5204 for (i = 0; i < 2; i++)
5205 if (key == db->key[i])
5206 db->key_released[i] = 1;
5207
5208 if (db->key_released[0] && db->key_released[1]) {
5209 /* All key releases been swalled so end the grab */
5210 terminate = 1;
5211 } else if (key != db->key[0] && key != db->key[1]) {
5212 /* Should not swallow release of other keys */
5213 send = 1;
5214 }
5215 } else if (key == db->key[0] && !db->key_released[0]) {
5216 /* Do not check bindings for the first press of the binding
5217 * key. This allows it to be used as a debug shortcut.
5218 * We still need to swallow this event. */
5219 check_binding = 0;
5220 } else if (db->key[1]) {
5221 /* If we already ran a binding don't process another one since
5222 * we can't keep track of all the binding keys that were
5223 * pressed in order to swallow the release events. */
5224 send = 1;
5225 check_binding = 0;
5226 }
5227
5228 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005229 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5230 key, state)) {
5231 /* We ran a binding so swallow the press and keep the
5232 * grab to swallow the released too. */
5233 send = 0;
5234 terminate = 0;
5235 db->key[1] = key;
5236 } else {
5237 /* Terminate the grab since the key pressed is not a
5238 * debug binding key. */
5239 send = 1;
5240 terminate = 1;
5241 }
5242 }
5243
5244 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005245 serial = wl_display_next_serial(display);
5246 resource_list = &grab->keyboard->focus_resource_list;
5247 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005248 wl_keyboard_send_key(resource, serial, time, key, state);
5249 }
5250 }
5251
5252 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005253 weston_keyboard_end_grab(grab->keyboard);
5254 if (grab->keyboard->input_method_resource)
5255 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005256 free(db);
5257 }
5258}
5259
5260static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005261debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005262 uint32_t mods_depressed, uint32_t mods_latched,
5263 uint32_t mods_locked, uint32_t group)
5264{
5265 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005266 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005267
Neil Roberts96d790e2013-09-19 17:32:00 +01005268 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005269
Neil Roberts96d790e2013-09-19 17:32:00 +01005270 wl_resource_for_each(resource, resource_list) {
5271 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5272 mods_latched, mods_locked, group);
5273 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005274}
5275
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005276static void
5277debug_binding_cancel(struct weston_keyboard_grab *grab)
5278{
5279 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5280
5281 weston_keyboard_end_grab(grab->keyboard);
5282 free(db);
5283}
5284
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005285struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005286 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005287 debug_binding_modifiers,
5288 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005289};
5290
5291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005292debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005293{
5294 struct debug_binding_grab *grab;
5295
5296 grab = calloc(1, sizeof *grab);
5297 if (!grab)
5298 return;
5299
5300 grab->seat = (struct weston_seat *) seat;
5301 grab->key[0] = key;
5302 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005303 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005304}
5305
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005306static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005307force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005308 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005309{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005310 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005311 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005312 struct desktop_shell *shell = data;
5313 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005314 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005315
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005316 focus_surface = seat->keyboard->focus;
5317 if (!focus_surface)
5318 return;
5319
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005320 wl_signal_emit(&compositor->kill_signal, focus_surface);
5321
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005322 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005323 wl_client_get_credentials(client, &pid, NULL, NULL);
5324
5325 /* Skip clients that we launched ourselves (the credentials of
5326 * the socketpair is ours) */
5327 if (pid == getpid())
5328 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005329
Daniel Stone325fc2d2012-05-30 16:31:58 +01005330 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005331}
5332
5333static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005334workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005335 uint32_t key, void *data)
5336{
5337 struct desktop_shell *shell = data;
5338 unsigned int new_index = shell->workspaces.current;
5339
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005340 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005341 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005342 if (new_index != 0)
5343 new_index--;
5344
5345 change_workspace(shell, new_index);
5346}
5347
5348static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005349workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005350 uint32_t key, void *data)
5351{
5352 struct desktop_shell *shell = data;
5353 unsigned int new_index = shell->workspaces.current;
5354
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005355 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005356 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005357 if (new_index < shell->workspaces.num - 1)
5358 new_index++;
5359
5360 change_workspace(shell, new_index);
5361}
5362
5363static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005364workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005365 uint32_t key, void *data)
5366{
5367 struct desktop_shell *shell = data;
5368 unsigned int new_index;
5369
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005370 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005371 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005372 new_index = key - KEY_F1;
5373 if (new_index >= shell->workspaces.num)
5374 new_index = shell->workspaces.num - 1;
5375
5376 change_workspace(shell, new_index);
5377}
5378
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005379static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005380workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005381 uint32_t key, void *data)
5382{
5383 struct desktop_shell *shell = data;
5384 unsigned int new_index = shell->workspaces.current;
5385
5386 if (shell->locked)
5387 return;
5388
5389 if (new_index != 0)
5390 new_index--;
5391
5392 take_surface_to_workspace_by_seat(shell, seat, new_index);
5393}
5394
5395static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005396workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005397 uint32_t key, void *data)
5398{
5399 struct desktop_shell *shell = data;
5400 unsigned int new_index = shell->workspaces.current;
5401
5402 if (shell->locked)
5403 return;
5404
5405 if (new_index < shell->workspaces.num - 1)
5406 new_index++;
5407
5408 take_surface_to_workspace_by_seat(shell, seat, new_index);
5409}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005410
5411static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005412handle_output_destroy(struct wl_listener *listener, void *data)
5413{
5414 struct shell_output *output_listener =
5415 container_of(listener, struct shell_output, destroy_listener);
5416
5417 wl_list_remove(&output_listener->destroy_listener.link);
5418 wl_list_remove(&output_listener->link);
5419 free(output_listener);
5420}
5421
5422static void
5423create_shell_output(struct desktop_shell *shell,
5424 struct weston_output *output)
5425{
5426 struct shell_output *shell_output;
5427
5428 shell_output = zalloc(sizeof *shell_output);
5429 if (shell_output == NULL)
5430 return;
5431
5432 shell_output->output = output;
5433 shell_output->shell = shell;
5434 shell_output->destroy_listener.notify = handle_output_destroy;
5435 wl_signal_add(&output->destroy_signal,
5436 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005437 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005438}
5439
5440static void
5441handle_output_create(struct wl_listener *listener, void *data)
5442{
5443 struct desktop_shell *shell =
5444 container_of(listener, struct desktop_shell, output_create_listener);
5445 struct weston_output *output = (struct weston_output *)data;
5446
5447 create_shell_output(shell, output);
5448}
5449
5450static void
5451setup_output_destroy_handler(struct weston_compositor *ec,
5452 struct desktop_shell *shell)
5453{
5454 struct weston_output *output;
5455
5456 wl_list_init(&shell->output_list);
5457 wl_list_for_each(output, &ec->output_list, link)
5458 create_shell_output(shell, output);
5459
5460 shell->output_create_listener.notify = handle_output_create;
5461 wl_signal_add(&ec->output_created_signal,
5462 &shell->output_create_listener);
5463}
5464
5465static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005466shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005467{
Tiago Vignattibe143262012-04-16 17:31:41 +03005468 struct desktop_shell *shell =
5469 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005470 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005471 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005472
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005473 /* Force state to unlocked so we don't try to fade */
5474 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005475 if (shell->child.client)
5476 wl_client_destroy(shell->child.client);
5477
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005478 wl_list_remove(&shell->idle_listener.link);
5479 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005480
5481 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005482
Xiong Zhang6b481422013-10-23 13:58:32 +08005483 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5484 wl_list_remove(&shell_output->destroy_listener.link);
5485 wl_list_remove(&shell_output->link);
5486 free(shell_output);
5487 }
5488
5489 wl_list_remove(&shell->output_create_listener.link);
5490
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005491 wl_array_for_each(ws, &shell->workspaces.array)
5492 workspace_destroy(*ws);
5493 wl_array_release(&shell->workspaces.array);
5494
Pekka Paalanen3c647232011-12-22 13:43:43 +02005495 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005496 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005497 free(shell);
5498}
5499
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005500static void
5501shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5502{
5503 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005504 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005505
5506 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005507 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5508 MODIFIER_CTRL | MODIFIER_ALT,
5509 terminate_binding, ec);
5510 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5511 click_to_activate_binding,
5512 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005513 weston_compositor_add_touch_binding(ec, 0,
5514 touch_to_activate_binding,
5515 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005516 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5517 MODIFIER_SUPER | MODIFIER_ALT,
5518 surface_opacity_binding, NULL);
5519 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5520 MODIFIER_SUPER, zoom_axis_binding,
5521 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005522
5523 /* configurable bindings */
5524 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005525 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5526 zoom_key_binding, NULL);
5527 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5528 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005529 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5530 maximize_binding, NULL);
5531 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5532 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005533 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5534 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005535 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005536 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5537 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005538
5539 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5540 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5541 rotate_binding, NULL);
5542
Daniel Stone325fc2d2012-05-30 16:31:58 +01005543 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5544 shell);
5545 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5546 ec);
5547 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5548 backlight_binding, ec);
5549 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5550 ec);
5551 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5552 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005553 weston_compositor_add_key_binding(ec, KEY_K, mod,
5554 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005555 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5556 workspace_up_binding, shell);
5557 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5558 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005559 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5560 workspace_move_surface_up_binding,
5561 shell);
5562 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5563 workspace_move_surface_down_binding,
5564 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005565
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005566 if (shell->exposay_modifier)
5567 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5568 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005569
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005570 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5571 if (shell->workspaces.num > 1) {
5572 num_workspace_bindings = shell->workspaces.num;
5573 if (num_workspace_bindings > 6)
5574 num_workspace_bindings = 6;
5575 for (i = 0; i < num_workspace_bindings; i++)
5576 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5577 workspace_f_binding,
5578 shell);
5579 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005580
5581 /* Debug bindings */
5582 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5583 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005584}
5585
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005586WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005587module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005588 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005589{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005590 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005591 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005592 struct workspace **pws;
5593 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005594 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005595
Peter Huttererf3d62272013-08-08 11:57:05 +10005596 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005597 if (shell == NULL)
5598 return -1;
5599
Kristian Høgsberg75840622011-09-06 13:48:16 -04005600 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005601
5602 shell->destroy_listener.notify = shell_destroy;
5603 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005604 shell->idle_listener.notify = idle_handler;
5605 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5606 shell->wake_listener.notify = wake_handler;
5607 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005608
Scott Moreauff1db4a2012-04-17 19:06:18 -06005609 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005610 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005611 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005612 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005613 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005614 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005615 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005616 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005617 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005618 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005619 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005620
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005621 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5622 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005623 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5624 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005625 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005626
5627 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005628 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005629
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005630 if (input_panel_setup(shell) < 0)
5631 return -1;
5632
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005633 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005634
Daniel Stonedf8133b2013-11-19 11:37:14 +01005635 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5636 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5637
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005638 for (i = 0; i < shell->workspaces.num; i++) {
5639 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5640 if (pws == NULL)
5641 return -1;
5642
5643 *pws = workspace_create();
5644 if (*pws == NULL)
5645 return -1;
5646 }
5647 activate_workspace(shell, 0);
5648
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005649 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005650 wl_list_init(&shell->workspaces.animation.link);
5651 shell->workspaces.animation.frame = animate_workspace_change_frame;
5652
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005653 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005654 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005655 return -1;
5656
Rafael Antognollie2a34552013-12-03 15:35:45 -02005657 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5658 shell, bind_xdg_shell) == NULL)
5659 return -1;
5660
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005661 if (wl_global_create(ec->wl_display,
5662 &desktop_shell_interface, 2,
5663 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005664 return -1;
5665
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005666 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5667 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005668 return -1;
5669
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005670 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5671 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005672 return -1;
5673
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005674 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005675
Xiong Zhang6b481422013-10-23 13:58:32 +08005676 setup_output_destroy_handler(ec, shell);
5677
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005678 loop = wl_display_get_event_loop(ec->wl_display);
5679 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005680
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005681 shell->screensaver.timer =
5682 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5683
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005684 wl_list_for_each(seat, &ec->seat_list, link)
5685 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005686
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005687 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005688
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005689 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005690
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005691 return 0;
5692}